|
|
|
@ -2,48 +2,54 @@ package com.olexyn.ensync.files;
|
|
|
|
|
|
|
|
|
|
import com.olexyn.ensync.Execute;
|
|
|
|
|
import com.olexyn.ensync.Flow;
|
|
|
|
|
import com.olexyn.ensync.LogUtil;
|
|
|
|
|
import com.olexyn.ensync.Tools;
|
|
|
|
|
import com.olexyn.ensync.artifacts.DataRoot;
|
|
|
|
|
import com.olexyn.ensync.artifacts.SyncBundle;
|
|
|
|
|
import com.olexyn.ensync.artifacts.SyncDirectory;
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
import org.junit.Assert;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.nio.file.Files;
|
|
|
|
|
import java.nio.file.Path;
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class FileTest {
|
|
|
|
|
|
|
|
|
|
private static final Logger LOGGER = LogUtil.get(FileTest.class);
|
|
|
|
|
|
|
|
|
|
final public static Thread FLOW_THREAD = new Thread(new Flow(), "flow");
|
|
|
|
|
final private static Tools tools = new Tools();
|
|
|
|
|
final private HashMap<String, SyncBundle> mapOfSyncMaps = DataRoot.get();
|
|
|
|
|
|
|
|
|
|
public long fileOpsPause = 800;
|
|
|
|
|
public long assertPause = 4000;
|
|
|
|
|
|
|
|
|
|
private final static long FILE_OPS_PAUSE = 800;
|
|
|
|
|
private final static long WAIT_BEFORE_ASSERT = 4000;
|
|
|
|
|
|
|
|
|
|
Execute x = new Execute();
|
|
|
|
|
|
|
|
|
|
private final static Execute x = new Execute();
|
|
|
|
|
|
|
|
|
|
private static final String TEST_RESOURCES = System.getProperty("user.dir") + "/src/test/resources";
|
|
|
|
|
private static final String TEMP_DIR = System.getProperty("user.dir") + "/src/test/temp";
|
|
|
|
|
private static final String RESOURCES_DIR = System.getProperty("user.dir") + "/src/test/resources";
|
|
|
|
|
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
|
|
|
|
|
private static final String A_DIR = TEMP_DIR + "/a";
|
|
|
|
|
private static final String B_DIR = TEMP_DIR + "/b";
|
|
|
|
|
|
|
|
|
|
private final TestFile a = new TestFile(TEST_RESOURCES + "/a/testfile.txt");
|
|
|
|
|
private final TestFile b = new TestFile(TEST_RESOURCES + "/b/testfile.txt");
|
|
|
|
|
private final TestFile aTestFile = new TestFile(A_DIR + "/testfile.txt");
|
|
|
|
|
private final TestFile bTestFile = new TestFile(B_DIR + "/testfile.txt");
|
|
|
|
|
|
|
|
|
|
private List<String> createFile(File file) {
|
|
|
|
|
List<String> stringList = new ArrayList<>();
|
|
|
|
|
try {
|
|
|
|
|
stringList.add(LocalDateTime.now().format(dateTimeFormatter) + " CREATED");
|
|
|
|
|
tools.writeStringListToFile(file.getAbsolutePath(), stringList);
|
|
|
|
|
Thread.sleep(fileOpsPause);
|
|
|
|
|
Thread.sleep(FILE_OPS_PAUSE);
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
System.out.println("");
|
|
|
|
|
}
|
|
|
|
@ -56,7 +62,7 @@ public class FileTest {
|
|
|
|
|
stringList.addAll(tools.fileToLines(file));
|
|
|
|
|
stringList.add(LocalDateTime.now().format(dateTimeFormatter) + " UPDATED");
|
|
|
|
|
tools.writeStringListToFile(file.getAbsolutePath(), stringList);
|
|
|
|
|
Thread.sleep(fileOpsPause);
|
|
|
|
|
Thread.sleep(FILE_OPS_PAUSE);
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
System.out.println("");
|
|
|
|
|
}
|
|
|
|
@ -64,19 +70,24 @@ public class FileTest {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void deleteFile(File file) {
|
|
|
|
|
private static void deleteFile(File file) {
|
|
|
|
|
try {
|
|
|
|
|
List<String> cmd = List.of("rm", "-r", file.getAbsolutePath());
|
|
|
|
|
x.execute(cmd);
|
|
|
|
|
Thread.sleep(fileOpsPause);
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
System.out.println("");
|
|
|
|
|
Files.delete(file.toPath());
|
|
|
|
|
Thread.sleep(FILE_OPS_PAUSE);
|
|
|
|
|
} catch (IOException | InterruptedException e) {
|
|
|
|
|
LOGGER.severe("Could not delete file.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void cleanDirs() {
|
|
|
|
|
deleteFile(a);
|
|
|
|
|
deleteFile(b);
|
|
|
|
|
private void cleanDirs(Path... dirs) {
|
|
|
|
|
for (var dir : dirs) {
|
|
|
|
|
try {
|
|
|
|
|
Files.delete(dir);
|
|
|
|
|
Files.createDirectory(dir);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
Assert.fail();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -85,18 +96,9 @@ public class FileTest {
|
|
|
|
|
*/
|
|
|
|
|
@Test
|
|
|
|
|
public void doSimpleFileTests() throws JSONException {
|
|
|
|
|
|
|
|
|
|
String configPath = System.getProperty("user.dir") + "/src/test/resources/test-config.json";
|
|
|
|
|
String configString = tools.fileToString(new File(configPath));
|
|
|
|
|
JSONObject jsonMapOfSyncMaps = new JSONObject(configString).getJSONObject("jsonMapOfSyncMaps");
|
|
|
|
|
// for (String key : jsonMapOfSyncMaps.keySet()) {
|
|
|
|
|
// SyncMap syncMap = new SyncMap(key);
|
|
|
|
|
// for (Object jsonSyncDirPath : jsonMapOfSyncMaps.getJSONArray(key).toList()) {
|
|
|
|
|
// syncMap.addDirectory(jsonSyncDirPath.toString());
|
|
|
|
|
// }
|
|
|
|
|
// MapOfSyncMaps.get().put(key, syncMap);
|
|
|
|
|
//
|
|
|
|
|
// }
|
|
|
|
|
SyncBundle syncMap = new SyncBundle("testSyncBundle");
|
|
|
|
|
syncMap.addDirectory(A_DIR);
|
|
|
|
|
syncMap.addDirectory(B_DIR);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FLOW_THREAD.start();
|
|
|
|
@ -106,137 +108,137 @@ public class FileTest {
|
|
|
|
|
cleanDirs();
|
|
|
|
|
|
|
|
|
|
// 1
|
|
|
|
|
createFile(a);
|
|
|
|
|
deleteFile(a);
|
|
|
|
|
createFile(aTestFile);
|
|
|
|
|
deleteFile(aTestFile);
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(assertPause);
|
|
|
|
|
Thread.sleep(WAIT_BEFORE_ASSERT);
|
|
|
|
|
} catch (InterruptedException ignored) {}
|
|
|
|
|
Assert.assertFalse(a.exists());
|
|
|
|
|
Assert.assertFalse(b.exists());
|
|
|
|
|
Assert.assertFalse(aTestFile.exists());
|
|
|
|
|
Assert.assertFalse(bTestFile.exists());
|
|
|
|
|
cleanDirs();
|
|
|
|
|
// 2
|
|
|
|
|
createFile(b);
|
|
|
|
|
createFile(a);
|
|
|
|
|
deleteFile(a);
|
|
|
|
|
createFile(bTestFile);
|
|
|
|
|
createFile(aTestFile);
|
|
|
|
|
deleteFile(aTestFile);
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(assertPause);
|
|
|
|
|
Thread.sleep(WAIT_BEFORE_ASSERT);
|
|
|
|
|
} catch (InterruptedException ignored) {}
|
|
|
|
|
Assert.assertFalse(a.exists());
|
|
|
|
|
Assert.assertFalse(b.exists());
|
|
|
|
|
Assert.assertFalse(aTestFile.exists());
|
|
|
|
|
Assert.assertFalse(bTestFile.exists());
|
|
|
|
|
cleanDirs();
|
|
|
|
|
// 3
|
|
|
|
|
createFile(a);
|
|
|
|
|
createFile(b);
|
|
|
|
|
deleteFile(a);
|
|
|
|
|
deleteFile(b);
|
|
|
|
|
createFile(aTestFile);
|
|
|
|
|
createFile(bTestFile);
|
|
|
|
|
deleteFile(aTestFile);
|
|
|
|
|
deleteFile(bTestFile);
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(assertPause);
|
|
|
|
|
Thread.sleep(WAIT_BEFORE_ASSERT);
|
|
|
|
|
} catch (InterruptedException ignored) {}
|
|
|
|
|
Assert.assertFalse(a.exists());
|
|
|
|
|
Assert.assertFalse(b.exists());
|
|
|
|
|
Assert.assertFalse(aTestFile.exists());
|
|
|
|
|
Assert.assertFalse(bTestFile.exists());
|
|
|
|
|
cleanDirs();
|
|
|
|
|
// 4
|
|
|
|
|
createFile(a);
|
|
|
|
|
deleteFile(a);
|
|
|
|
|
sideloadContentB = createFile(b);
|
|
|
|
|
createFile(aTestFile);
|
|
|
|
|
deleteFile(aTestFile);
|
|
|
|
|
sideloadContentB = createFile(bTestFile);
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(assertPause);
|
|
|
|
|
Thread.sleep(WAIT_BEFORE_ASSERT);
|
|
|
|
|
} catch (InterruptedException ignored) {}
|
|
|
|
|
Assert.assertEquals(sideloadContentB, a.updateContent().getContent());
|
|
|
|
|
Assert.assertEquals(sideloadContentB, aTestFile.updateContent().getContent());
|
|
|
|
|
cleanDirs();
|
|
|
|
|
// 5
|
|
|
|
|
createFile(a);
|
|
|
|
|
createFile(b);
|
|
|
|
|
deleteFile(a);
|
|
|
|
|
sideloadContentB = updateFile(b);
|
|
|
|
|
createFile(aTestFile);
|
|
|
|
|
createFile(bTestFile);
|
|
|
|
|
deleteFile(aTestFile);
|
|
|
|
|
sideloadContentB = updateFile(bTestFile);
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(assertPause);
|
|
|
|
|
Thread.sleep(WAIT_BEFORE_ASSERT);
|
|
|
|
|
} catch (InterruptedException ignored) {}
|
|
|
|
|
Assert.assertEquals(sideloadContentB, a.updateContent().getContent());
|
|
|
|
|
Assert.assertEquals(sideloadContentB, aTestFile.updateContent().getContent());
|
|
|
|
|
cleanDirs();
|
|
|
|
|
// 6
|
|
|
|
|
sideloadContentA = createFile(a);
|
|
|
|
|
sideloadContentA = createFile(aTestFile);
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(assertPause);
|
|
|
|
|
Thread.sleep(WAIT_BEFORE_ASSERT);
|
|
|
|
|
} catch (InterruptedException ignored) {}
|
|
|
|
|
Assert.assertEquals(sideloadContentA, b.updateContent().getContent());
|
|
|
|
|
Assert.assertEquals(sideloadContentA, bTestFile.updateContent().getContent());
|
|
|
|
|
// 7
|
|
|
|
|
createFile(b);
|
|
|
|
|
createFile(a);
|
|
|
|
|
createFile(bTestFile);
|
|
|
|
|
createFile(aTestFile);
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(assertPause);
|
|
|
|
|
Thread.sleep(WAIT_BEFORE_ASSERT);
|
|
|
|
|
} catch (InterruptedException ignored) {}
|
|
|
|
|
Assert.assertEquals(sideloadContentA, b.updateContent().getContent());
|
|
|
|
|
Assert.assertEquals(sideloadContentA, bTestFile.updateContent().getContent());
|
|
|
|
|
// 8
|
|
|
|
|
createFile(a);
|
|
|
|
|
createFile(b);
|
|
|
|
|
deleteFile(b);
|
|
|
|
|
createFile(aTestFile);
|
|
|
|
|
createFile(bTestFile);
|
|
|
|
|
deleteFile(bTestFile);
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(assertPause);
|
|
|
|
|
Thread.sleep(WAIT_BEFORE_ASSERT);
|
|
|
|
|
} catch (InterruptedException ignored) {}
|
|
|
|
|
Assert.assertFalse(a.exists());
|
|
|
|
|
Assert.assertFalse(b.exists());
|
|
|
|
|
Assert.assertFalse(aTestFile.exists());
|
|
|
|
|
Assert.assertFalse(bTestFile.exists());
|
|
|
|
|
cleanDirs();
|
|
|
|
|
//9
|
|
|
|
|
createFile(a);
|
|
|
|
|
sideloadContentB = createFile(b);
|
|
|
|
|
createFile(aTestFile);
|
|
|
|
|
sideloadContentB = createFile(bTestFile);
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(assertPause);
|
|
|
|
|
Thread.sleep(WAIT_BEFORE_ASSERT);
|
|
|
|
|
} catch (InterruptedException ignored) {}
|
|
|
|
|
Assert.assertEquals(sideloadContentB, a.updateContent().getContent());
|
|
|
|
|
Assert.assertEquals(sideloadContentB, aTestFile.updateContent().getContent());
|
|
|
|
|
cleanDirs();
|
|
|
|
|
// 10
|
|
|
|
|
createFile(b);
|
|
|
|
|
createFile(a);
|
|
|
|
|
sideloadContentB = updateFile(b);
|
|
|
|
|
createFile(bTestFile);
|
|
|
|
|
createFile(aTestFile);
|
|
|
|
|
sideloadContentB = updateFile(bTestFile);
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(assertPause);
|
|
|
|
|
Thread.sleep(WAIT_BEFORE_ASSERT);
|
|
|
|
|
} catch (InterruptedException ignored) {}
|
|
|
|
|
Assert.assertEquals(sideloadContentB, a.updateContent().getContent());
|
|
|
|
|
Assert.assertEquals(sideloadContentB, aTestFile.updateContent().getContent());
|
|
|
|
|
// 11
|
|
|
|
|
createFile(a);
|
|
|
|
|
sideloadContentA = updateFile(a);
|
|
|
|
|
createFile(aTestFile);
|
|
|
|
|
sideloadContentA = updateFile(aTestFile);
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(assertPause);
|
|
|
|
|
Thread.sleep(WAIT_BEFORE_ASSERT);
|
|
|
|
|
} catch (InterruptedException ignored) {}
|
|
|
|
|
Assert.assertEquals(sideloadContentA, b.updateContent().getContent());
|
|
|
|
|
Assert.assertEquals(sideloadContentA, bTestFile.updateContent().getContent());
|
|
|
|
|
// 12
|
|
|
|
|
createFile(a);
|
|
|
|
|
createFile(b);
|
|
|
|
|
sideloadContentA = updateFile(a);
|
|
|
|
|
createFile(aTestFile);
|
|
|
|
|
createFile(bTestFile);
|
|
|
|
|
sideloadContentA = updateFile(aTestFile);
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(assertPause);
|
|
|
|
|
Thread.sleep(WAIT_BEFORE_ASSERT);
|
|
|
|
|
} catch (InterruptedException ignored) {}
|
|
|
|
|
Assert.assertEquals(sideloadContentA, b.updateContent().getContent());
|
|
|
|
|
Assert.assertEquals(sideloadContentA, bTestFile.updateContent().getContent());
|
|
|
|
|
// 13
|
|
|
|
|
createFile(a);
|
|
|
|
|
createFile(b);
|
|
|
|
|
updateFile(a);
|
|
|
|
|
deleteFile(b);
|
|
|
|
|
createFile(aTestFile);
|
|
|
|
|
createFile(bTestFile);
|
|
|
|
|
updateFile(aTestFile);
|
|
|
|
|
deleteFile(bTestFile);
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(assertPause);
|
|
|
|
|
Thread.sleep(WAIT_BEFORE_ASSERT);
|
|
|
|
|
} catch (InterruptedException ignored) {}
|
|
|
|
|
Assert.assertFalse(a.exists());
|
|
|
|
|
Assert.assertFalse(b.exists());
|
|
|
|
|
Assert.assertFalse(aTestFile.exists());
|
|
|
|
|
Assert.assertFalse(bTestFile.exists());
|
|
|
|
|
cleanDirs();
|
|
|
|
|
// 14
|
|
|
|
|
createFile(a);
|
|
|
|
|
updateFile(a);
|
|
|
|
|
sideloadContentB = createFile(b);
|
|
|
|
|
createFile(aTestFile);
|
|
|
|
|
updateFile(aTestFile);
|
|
|
|
|
sideloadContentB = createFile(bTestFile);
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(assertPause);
|
|
|
|
|
Thread.sleep(WAIT_BEFORE_ASSERT);
|
|
|
|
|
} catch (InterruptedException ignored) {}
|
|
|
|
|
Assert.assertEquals(sideloadContentB, a.updateContent().getContent());
|
|
|
|
|
Assert.assertEquals(sideloadContentB, aTestFile.updateContent().getContent());
|
|
|
|
|
cleanDirs();
|
|
|
|
|
// 15
|
|
|
|
|
createFile(a);
|
|
|
|
|
createFile(b);
|
|
|
|
|
updateFile(a);
|
|
|
|
|
sideloadContentB = updateFile(b);
|
|
|
|
|
createFile(aTestFile);
|
|
|
|
|
createFile(bTestFile);
|
|
|
|
|
updateFile(aTestFile);
|
|
|
|
|
sideloadContentB = updateFile(bTestFile);
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(assertPause);
|
|
|
|
|
Thread.sleep(WAIT_BEFORE_ASSERT);
|
|
|
|
|
} catch (InterruptedException ignored) {}
|
|
|
|
|
Assert.assertEquals(sideloadContentB, a.updateContent().getContent());
|
|
|
|
|
Assert.assertEquals(sideloadContentB, aTestFile.updateContent().getContent());
|
|
|
|
|
cleanDirs();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|