From 105a9d19b692efb40aaefdb60f0bb670bd3531c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Natha=C3=ABl=20NOGU=C3=88S?= Date: Thu, 21 Jul 2016 10:23:27 +0200 Subject: [PATCH] FIX TEST refs #25 : Correcting UploadFile & tests --- .gitignore | 5 +- avek-gui/pom.xml | 12 -- .../axonic/avek/gui/model/json/Jsonifier.java | 13 +- .../gui/model/structure/UploadedFile.java | 31 ++--- .../gui/view/parameters/FileListView.java | 12 +- .../gui/view/model/json/TestJsonifier.java | 3 +- .../model/structure/TestUploadedFile.java | 117 ++++++++++++++---- 7 files changed, 120 insertions(+), 73 deletions(-) diff --git a/.gitignore b/.gitignore index 978d917..0f8dd53 100644 --- a/.gitignore +++ b/.gitignore @@ -24,5 +24,6 @@ target/ # Generated files */logs/ -avek-gui/temp/ -avek-gui/target/ +*/temp/ +*/target/ +/temp/ \ No newline at end of file diff --git a/avek-gui/pom.xml b/avek-gui/pom.xml index 603575d..2eb8ace 100644 --- a/avek-gui/pom.xml +++ b/avek-gui/pom.xml @@ -56,18 +56,6 @@ - - com.zenjava - javafx-maven-plugin - 8.5.0 - - fr.axonic - axonic - AXONIC - PACA - FR - - diff --git a/avek-gui/src/main/java/fr/axonic/avek/gui/model/json/Jsonifier.java b/avek-gui/src/main/java/fr/axonic/avek/gui/model/json/Jsonifier.java index 4e608b4..ce5c99c 100644 --- a/avek-gui/src/main/java/fr/axonic/avek/gui/model/json/Jsonifier.java +++ b/avek-gui/src/main/java/fr/axonic/avek/gui/model/json/Jsonifier.java @@ -6,8 +6,13 @@ /** * Created by Nathaƫl N on 12/07/16. */ -public abstract class Jsonifier { +public class Jsonifier { private static final Logger logger = Logger.getLogger(Jsonifier.class); + private final Class tClass; + + public Jsonifier(Class c) { + this.tClass = c; + } public static String toJson(Object o) { logger.info("Object to JSON:"+ o); @@ -17,7 +22,11 @@ public static String toJson(Object o) { .toJson(o); } - public static T fromJson(String s, Class c) { + public T fromJson(String s) { + return fromJson(s, tClass); + } + + public static TT fromJson(String s, Class c) { logger.info("Creating new "+c.getName()+" from Json"); return new GsonBuilder().create().fromJson(s, c); } diff --git a/avek-gui/src/main/java/fr/axonic/avek/gui/model/structure/UploadedFile.java b/avek-gui/src/main/java/fr/axonic/avek/gui/model/structure/UploadedFile.java index 61e638a..a901926 100644 --- a/avek-gui/src/main/java/fr/axonic/avek/gui/model/structure/UploadedFile.java +++ b/avek-gui/src/main/java/fr/axonic/avek/gui/model/structure/UploadedFile.java @@ -30,31 +30,18 @@ public class UploadedFile { private long uploadedBytes; private Runnable listener; - public UploadedFile(File origin) throws FileNotFoundException, FileAlreadyExistsException { - if(!origin.exists()) - throw new FileNotFoundException(origin.getAbsolutePath()); - + public UploadedFile(File origin) { this.origin = origin; - this.uploadedBytes = -1; - - File uploaded = null; - - // Checking if already uploade - for(File f : uploadedFolder.listFiles()) - if(f.equals(origin)) { - uploaded = f; - break; - } - - this.uploaded = uploaded==null ? - new File(uploadedFolder.getPath() + "/" + origin.getName()) - : uploaded; + this.uploadedBytes = 0; + this.uploaded = new File(uploadedFolder.getPath() + "/" + origin.getName()); + } + public void doUpload() throws FileAlreadyExistsException, FileNotFoundException { + if(!origin.exists()) + throw new FileNotFoundException(origin.getAbsolutePath()); if(this.uploaded.exists()) throw new FileAlreadyExistsException(this.uploaded.getPath()); - } - public void doUpload() { Thread t = new Thread(() -> { long calc = getSize() / 100L + 1L; if (calc > 1024L * 1024L) // 1Mb max @@ -134,7 +121,7 @@ public File getOriginal() { return origin; } - private long size = -1; + private long size = 0; public long getSize() { if(size > 0) return size; @@ -160,8 +147,6 @@ public double getPct() { return (double)uploadedBytes / (double)getSize(); } - - @Override public boolean equals(Object obj) { if(obj instanceof UploadedFile) diff --git a/avek-gui/src/main/java/fr/axonic/avek/gui/view/parameters/FileListView.java b/avek-gui/src/main/java/fr/axonic/avek/gui/view/parameters/FileListView.java index ab68fc5..e1bc491 100644 --- a/avek-gui/src/main/java/fr/axonic/avek/gui/view/parameters/FileListView.java +++ b/avek-gui/src/main/java/fr/axonic/avek/gui/view/parameters/FileListView.java @@ -55,18 +55,18 @@ public FileListCell call(ListView list) { } private void onAddFiles(List list) { - for(final File f : list) + for(final File f : list) { + UploadedFile uf = new UploadedFile(f); try { - UploadedFile uf = new UploadedFile(f); - // Adding to the list on GUI - Platform.runLater(() -> fileList.getItems().add(uf)); uf.doUpload(); + Platform.runLater(() -> fileList.getItems().add(uf)); } catch (FileNotFoundException e) { - logger.error("File not found: "+f, e); + logger.error("File not found: " + f, e); } catch (FileAlreadyExistsException e) { - logger.warn("File already added: "+f.getName(), e); + logger.warn("File already added: " + f.getName(), e); } + } } @FXML diff --git a/avek-gui/src/test/java/fr/axonic/avek/gui/view/model/json/TestJsonifier.java b/avek-gui/src/test/java/fr/axonic/avek/gui/view/model/json/TestJsonifier.java index 50cd5f4..0702abd 100644 --- a/avek-gui/src/test/java/fr/axonic/avek/gui/view/model/json/TestJsonifier.java +++ b/avek-gui/src/test/java/fr/axonic/avek/gui/view/model/json/TestJsonifier.java @@ -64,8 +64,9 @@ private void test(T o, Class tClass) { assertEquals(o, o2); } private void test2(T o, Class tClass) { + Jsonifier js = new Jsonifier<>(tClass); String oJson = Jsonifier.toJson(o); - String o2Json = Jsonifier.toJson(Jsonifier.fromJson(oJson, tClass)); + String o2Json = Jsonifier.toJson(js.fromJson(oJson)); assertEquals(oJson, o2Json); } diff --git a/avek-gui/src/test/java/fr/axonic/avek/gui/view/model/structure/TestUploadedFile.java b/avek-gui/src/test/java/fr/axonic/avek/gui/view/model/structure/TestUploadedFile.java index 3c322b7..22e9ba4 100644 --- a/avek-gui/src/test/java/fr/axonic/avek/gui/view/model/structure/TestUploadedFile.java +++ b/avek-gui/src/test/java/fr/axonic/avek/gui/view/model/structure/TestUploadedFile.java @@ -14,6 +14,7 @@ import java.nio.file.FileAlreadyExistsException; import java.nio.file.Files; import java.util.ArrayList; +import java.util.Calendar; import java.util.Collections; import java.util.List; @@ -32,11 +33,9 @@ public class TestUploadedFile extends ApplicationTest { System.setProperty("java.awt.headless", "true"); } - private Parent root; - @Override public void start(Stage stage) throws Exception { - root = MainController.getRoot(); + Parent root = MainController.getRoot(); Scene scene = new Scene(root, 200, 200); stage.setScene(scene); stage.show(); @@ -69,20 +68,18 @@ public void before() throws IOException { writer.close(); } - UploadedFile uf; @Test - public void uploadedFileTest() throws FileNotFoundException, FileAlreadyExistsException { - uf = new UploadedFile(new File("./temp/yolo.txt")); + public void uploadedFileTest() throws FileNotFoundException, FileAlreadyExistsException, InterruptedException { + UploadedFile uf = new UploadedFile(new File("./temp/yolo.txt")); assertEquals(new File("./temp/yolo.txt"), uf.getOriginal()); - assertEquals(31, uf.getSize(), 1); - uf.setUpdateListener(this::uploadedFileTest2); - uf.doUpload(); - } + assertEquals(31, uf.getSize()); - private void uploadedFileTest2() { - if(uf.getPct() < 1) + if(!waitingForUpload(uf)) { + uf.removeListener(); + assertTrue("Waiting for more than 30s", false); return; + } uf.removeListener(); @@ -92,23 +89,23 @@ private void uploadedFileTest2() { assertEquals(1, fileList.size()); File f = fileList.get(0); assertEquals("yolo.txt", f.getName()); - assertEquals(31, f.length(), 1); + assertEquals(31, f.length()); assertTrue(f.isFile()); } @Test - public void uploadedFolderTest() throws FileNotFoundException, FileAlreadyExistsException { - uf = new UploadedFile(new File("./temp/toto/")); + public void uploadedFolderTest() throws FileNotFoundException, FileAlreadyExistsException, InterruptedException { + UploadedFile uf = new UploadedFile(new File("./temp/toto/")); assertEquals(new File("./temp/toto"), uf.getOriginal()); - assertEquals(25+49, uf.getSize(), 1); - uf.setUpdateListener(this::uploadedFolderTest2); - uf.doUpload(); - } + assertEquals(25 + 49, uf.getSize()); - private void uploadedFolderTest2() { - if(uf.getPct() < 1) + + if(!waitingForUpload(uf)) { + uf.removeListener(); + assertTrue("Waiting for more than 30s", false); return; + } uf.removeListener(); @@ -118,14 +115,13 @@ private void uploadedFolderTest2() { assertEquals(1, fileList.size()); File f = fileList.get(0); assertEquals("toto", f.getName()); - assertEquals(31, f.length(), 1); assertTrue(f.isDirectory()); assertEquals(2, f.listFiles().length); - File titi = f.listFiles()[0]; - File tonton = f.listFiles()[1]; - assertEquals("titi", titi.getName()); - assertEquals("tonton", tonton.getName()); + File titi = f.listFiles()[1]; + File tonton = f.listFiles()[0]; + assertEquals("titi.txt", titi.getName()); + assertEquals("tonton.txt", tonton.getName()); assertTrue(titi.isFile()); assertTrue(tonton.isFile()); @@ -144,14 +140,81 @@ private void uploadedFolderTest2() { } } + @Test + public void alreadyUploadedTest() throws FileNotFoundException, InterruptedException { + UploadedFile uf1 = new UploadedFile(new File("./temp/yolo.txt")); + UploadedFile uf2 = new UploadedFile(new File("./temp/yolo.txt")); + try { + if(!waitingForUpload(uf1)) { + uf1.removeListener(); + assertTrue("Waiting for more than 30s", false); + return; + } + } catch (FileAlreadyExistsException e) { + e.printStackTrace(); + } + + uf1.removeListener(); + + try { + if(!waitingForUpload(uf2)) { + uf2.removeListener(); + assertTrue("Waiting for more than 30s", false); + return; + } + assertTrue("Not thrown: FileAlreadyExistsException", false); + } catch (FileAlreadyExistsException e) { + assertTrue(true); + } + + uf2.removeListener(); + + List fileList = new ArrayList<>(); + Collections.addAll(fileList, UploadedFile.uploadedFolder.listFiles()); + + assertEquals(1, fileList.size()); + File f = fileList.get(0); + assertEquals("yolo.txt", f.getName()); + assertEquals(31, f.length()); + assertTrue(f.isFile()); + } + + private volatile boolean pass; + private boolean waitingForUpload(UploadedFile uf1) throws FileAlreadyExistsException, FileNotFoundException { + pass = false; + + uf1.setUpdateListener(() -> { + if(uf1.getPct() == 1) + pass = true; + }); + uf1.doUpload(); + + long timeStamp = Calendar.getInstance().getTimeInMillis(); + while(!pass) { + if(timeStamp+30_000 < Calendar.getInstance().getTimeInMillis()) + return false; + try { + Thread.sleep(1); + } catch (InterruptedException e) { + e.printStackTrace(); + return false; + } + } + + return true; + } + + @After public void after() throws IOException { File f = new File("./temp/yolo.txt"); delete(f); f = new File("./temp/toto"); delete(f); + for(File ff : UploadedFile.uploadedFolder.listFiles()) + delete(ff); } - void delete(File f) throws IOException { + private static void delete(File f) throws IOException { if (f.isDirectory()) { for (File c : f.listFiles()) delete(c);