Skip to content

Commit

Permalink
FIX TEST refs #25 : Correcting UploadFile & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathaël NOGUÈS committed Jul 21, 2016
1 parent d84f76c commit 105a9d1
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 73 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ target/
# Generated files
*/logs/

avek-gui/temp/
avek-gui/target/
*/temp/
*/target/
/temp/
12 changes: 0 additions & 12 deletions avek-gui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,6 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>8.5.0</version>
<configuration>
<certDomain>fr.axonic</certDomain>
<certOrgUnit>axonic</certOrgUnit>
<certOrg>AXONIC</certOrg>
<certState>PACA</certState>
<certCountry>FR</certCountry>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
/**
* Created by Nathaël N on 12/07/16.
*/
public abstract class Jsonifier {
public class Jsonifier <T> {
private static final Logger logger = Logger.getLogger(Jsonifier.class);
private final Class<T> tClass;

public Jsonifier(Class<T> c) {
this.tClass = c;
}

public static String toJson(Object o) {
logger.info("Object to JSON:"+ o);
Expand All @@ -17,7 +22,11 @@ public static String toJson(Object o) {
.toJson(o);
}

public static <T> T fromJson(String s, Class<T> c) {
public T fromJson(String s) {
return fromJson(s, tClass);
}

public static <TT> TT fromJson(String s, Class<TT> c) {
logger.info("Creating new "+c.getName()+" from Json");
return new GsonBuilder().create().fromJson(s, c);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -160,8 +147,6 @@ public double getPct() {
return (double)uploadedBytes / (double)getSize();
}



@Override
public boolean equals(Object obj) {
if(obj instanceof UploadedFile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,18 @@ public FileListCell call(ListView<UploadedFile> list) {
}

private void onAddFiles(List<File> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ private <T> void test(T o, Class<T> tClass) {
assertEquals(o, o2);
}
private <T> void test2(T o, Class<T> tClass) {
Jsonifier<T> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
Expand Down Expand Up @@ -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();

Expand All @@ -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();

Expand All @@ -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());

Expand All @@ -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<File> 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);
Expand Down

0 comments on commit 105a9d1

Please sign in to comment.