From cd5f7be740a0318c8cfe109b6b30c7bbace15314 Mon Sep 17 00:00:00 2001 From: Ivan Olexyn Date: Wed, 23 Dec 2020 00:07:07 +0100 Subject: [PATCH] run first test - fail. --- .gitignore | 1 + src/main/java/com/olexyn/ensync/Flow.java | 16 +-- src/main/java/com/olexyn/ensync/Main.java | 6 +- .../ensync/artifacts/MapOfSyncMaps.java | 19 +++ .../java/com/olexyn/ensync/ui/Bridge.java | 19 +-- .../com/olexyn/ensync/files/FileTest.java | 112 ++++++++++++++++-- src/test/resources/README.md | 2 + src/test/resources/test-config.json | 8 ++ 8 files changed, 152 insertions(+), 31 deletions(-) create mode 100644 src/main/java/com/olexyn/ensync/artifacts/MapOfSyncMaps.java create mode 100644 src/test/resources/README.md create mode 100644 src/test/resources/test-config.json diff --git a/.gitignore b/.gitignore index 3519122..4f216cd 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ /target/ /ensync.iml *.xlsx# +testfile.txt diff --git a/src/main/java/com/olexyn/ensync/Flow.java b/src/main/java/com/olexyn/ensync/Flow.java index fee39da..170a6e6 100644 --- a/src/main/java/com/olexyn/ensync/Flow.java +++ b/src/main/java/com/olexyn/ensync/Flow.java @@ -1,18 +1,21 @@ package com.olexyn.ensync; +import com.olexyn.ensync.artifacts.MapOfSyncMaps; import com.olexyn.ensync.artifacts.SyncDirectory; import com.olexyn.ensync.artifacts.SyncMap; import java.io.File; import java.util.Map.Entry; -import static com.olexyn.ensync.Main.MAP_OF_SYNCMAPS; + public class Flow implements Runnable { Tools tools = new Tools(); + public long pollingPause = 200; + private String state; @@ -21,11 +24,11 @@ public class Flow implements Runnable { while (true) { - synchronized (MAP_OF_SYNCMAPS) { + synchronized (MapOfSyncMaps.get()) { readOrMakeStateFile(); - for (Entry syncMapEntry : MAP_OF_SYNCMAPS.entrySet()) { + for (Entry syncMapEntry : MapOfSyncMaps.get().entrySet()) { for (Entry SDEntry : syncMapEntry.getValue().syncDirectories.entrySet()) { @@ -34,9 +37,8 @@ public class Flow implements Runnable { } } try { - long pause = 2000; - System.out.println("Pausing... for " + pause + "ms."); - Thread.sleep(pause); + System.out.println("Pausing... for " + pollingPause + "ms."); + Thread.sleep(pollingPause); } catch (InterruptedException ignored) {} } } @@ -68,7 +70,7 @@ public class Flow implements Runnable { * If the StateFile is missing, then create a StateFile. */ private void readOrMakeStateFile() { - for (var syncMapEntry : MAP_OF_SYNCMAPS.entrySet()) { + for (var syncMapEntry : MapOfSyncMaps.get().entrySet()) { SyncMap syncMap = syncMapEntry.getValue(); state = syncMap.toString(); diff --git a/src/main/java/com/olexyn/ensync/Main.java b/src/main/java/com/olexyn/ensync/Main.java index 43c28db..7d1517e 100644 --- a/src/main/java/com/olexyn/ensync/Main.java +++ b/src/main/java/com/olexyn/ensync/Main.java @@ -1,5 +1,6 @@ package com.olexyn.ensync; +import com.olexyn.ensync.artifacts.MapOfSyncMaps; import com.olexyn.ensync.artifacts.SyncMap; import com.olexyn.ensync.ui.UI; @@ -13,11 +14,10 @@ import java.util.List; import java.util.Map; -public class Main{ +public class Main { final public static Thread UI_THREAD = new Thread(new UI(), "ui"); final public static Thread FLOW_THREAD = new Thread(new Flow(), "flow"); - final public static HashMap MAP_OF_SYNCMAPS = new HashMap<>(); final private static Tools tools = new Tools(); public static void main(String[] args) { @@ -37,7 +37,7 @@ public class Main{ for (Object jsonSyncDirPath : jsonMapOfSyncMaps.getJSONArray(key).toList()) { syncMap.addDirectory(jsonSyncDirPath.toString()); } - MAP_OF_SYNCMAPS.put(key, syncMap); + MapOfSyncMaps.get().put(key, syncMap); } break; default: diff --git a/src/main/java/com/olexyn/ensync/artifacts/MapOfSyncMaps.java b/src/main/java/com/olexyn/ensync/artifacts/MapOfSyncMaps.java new file mode 100644 index 0000000..cd8f7ab --- /dev/null +++ b/src/main/java/com/olexyn/ensync/artifacts/MapOfSyncMaps.java @@ -0,0 +1,19 @@ +package com.olexyn.ensync.artifacts; + +import java.util.HashMap; + +public class MapOfSyncMaps { + + private static HashMap mapOfSyncMaps; + + private MapOfSyncMaps() {} + + public static HashMap get() { + + if (mapOfSyncMaps == null) { + mapOfSyncMaps = new HashMap<>(); + } + return mapOfSyncMaps; + + } +} diff --git a/src/main/java/com/olexyn/ensync/ui/Bridge.java b/src/main/java/com/olexyn/ensync/ui/Bridge.java index 9bfc0f9..dcf9960 100644 --- a/src/main/java/com/olexyn/ensync/ui/Bridge.java +++ b/src/main/java/com/olexyn/ensync/ui/Bridge.java @@ -1,12 +1,13 @@ package com.olexyn.ensync.ui; +import com.olexyn.ensync.artifacts.MapOfSyncMaps; import com.olexyn.ensync.artifacts.SyncMap; import java.io.File; - import static com.olexyn.ensync.Main.MAP_OF_SYNCMAPS; + /** * Connect the Controller and the Flow @@ -16,22 +17,22 @@ public class Bridge { void newCollection(String collectionName) { - synchronized (MAP_OF_SYNCMAPS) { - MAP_OF_SYNCMAPS.put(collectionName, new SyncMap(collectionName)); + synchronized (MapOfSyncMaps.get()) { + MapOfSyncMaps.get().put(collectionName, new SyncMap(collectionName)); } } void removeCollection(String collectionName) { - synchronized (MAP_OF_SYNCMAPS) { - MAP_OF_SYNCMAPS.remove(collectionName); + synchronized (MapOfSyncMaps.get()) { + MapOfSyncMaps.get().remove(collectionName); } } void addDirectory(String collectionName, File diretory) { - synchronized (MAP_OF_SYNCMAPS) { - MAP_OF_SYNCMAPS.get(collectionName).addDirectory(diretory.getAbsolutePath()); + synchronized (MapOfSyncMaps.get()) { + MapOfSyncMaps.get().get(collectionName).addDirectory(diretory.getAbsolutePath()); } //TODO pause syning when adding } @@ -43,8 +44,8 @@ public class Bridge { */ void removeDirectory(String directoryAbsolutePath) { //TODO fix ConcurrentModificationException. This will possibly resolve further errors. - synchronized (MAP_OF_SYNCMAPS) { - for (var syncMap : MAP_OF_SYNCMAPS.entrySet()) { + synchronized (MapOfSyncMaps.get()) { + for (var syncMap : MapOfSyncMaps.get().entrySet()) { syncMap.getValue().removeDirectory(directoryAbsolutePath); } } diff --git a/src/test/java/com/olexyn/ensync/files/FileTest.java b/src/test/java/com/olexyn/ensync/files/FileTest.java index 347873e..4412065 100644 --- a/src/test/java/com/olexyn/ensync/files/FileTest.java +++ b/src/test/java/com/olexyn/ensync/files/FileTest.java @@ -1,49 +1,75 @@ package com.olexyn.ensync.files; import com.olexyn.ensync.Execute; +import com.olexyn.ensync.Flow; +import com.olexyn.ensync.OperationMode; import com.olexyn.ensync.Tools; +import com.olexyn.ensync.artifacts.MapOfSyncMaps; +import com.olexyn.ensync.artifacts.SyncMap; +import com.olexyn.ensync.ui.UI; +import org.json.JSONObject; import org.junit.Assert; import org.junit.Test; import java.io.File; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; + + public class FileTest { + final public static Thread FLOW_THREAD = new Thread(new Flow(), "flow"); + final private static Tools tools = new Tools(); + final private HashMap mapOfSyncMaps = MapOfSyncMaps.get(); + + public long fileOpsPause = 800; + public long assertPause = 4000; + + Execute x = new Execute(); - Tools tools = new Tools(); - private static final String PATH = System.getProperty("user.dir"); - private static final String fileAPath = "asdf"; - private static final String fileBPath = "asff"; + private static final String TEST_RESOURCES = System.getProperty("user.dir") + "/src/test/resources"; + DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; - private final TestFile a = new TestFile(fileAPath); - private final TestFile b = new TestFile(fileBPath); + private final TestFile a = new TestFile(TEST_RESOURCES + "/a/testfile.txt"); + private final TestFile b = new TestFile(TEST_RESOURCES + "/b/testfile.txt"); private List createFile(File file) { + List stringList = new ArrayList<>(); try { - Thread.sleep(10); + stringList.add(LocalDateTime.now().format(dateTimeFormatter) + " CREATED"); + tools.writeStringListToFile(file.getAbsolutePath(), stringList); + Thread.sleep(fileOpsPause); } catch (InterruptedException e) { System.out.println(""); } - return new ArrayList(); + return stringList; } private List updateFile(File file) { + List stringList = new ArrayList<>(); try { - Thread.sleep(10); + stringList.addAll(tools.fileToLines(file)); + stringList.add(LocalDateTime.now().format(dateTimeFormatter) + " UPDATED"); + tools.writeStringListToFile(file.getAbsolutePath(), stringList); + Thread.sleep(fileOpsPause); } catch (InterruptedException e) { System.out.println(""); } - return new ArrayList(); + return stringList; } private void deleteFile(File file) { try { - Thread.sleep(10); + List cmd = List.of("rm", "-r", file.getAbsolutePath()); + x.execute(cmd); + Thread.sleep(fileOpsPause); } catch (InterruptedException e) { System.out.println(""); } @@ -56,16 +82,36 @@ public class FileTest { /** * Perform the 15 test cases in TestCases.xlsx. + * Simple means with a static test-config.json, thus no SyncMaps are added at runtime. */ @Test - public void doFileTests() { + public void doSimpleFileTests() { + + 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); + + } + + + FLOW_THREAD.start(); List sideloadContentA; List sideloadContentB; + cleanDirs(); // 1 createFile(a); deleteFile(a); + try { + Thread.sleep(assertPause); + } catch (InterruptedException ignored) {} Assert.assertFalse(a.exists()); Assert.assertFalse(b.exists()); cleanDirs(); @@ -73,6 +119,9 @@ public class FileTest { createFile(b); createFile(a); deleteFile(a); + try { + Thread.sleep(assertPause); + } catch (InterruptedException ignored) {} Assert.assertFalse(a.exists()); Assert.assertFalse(b.exists()); cleanDirs(); @@ -81,6 +130,9 @@ public class FileTest { createFile(b); deleteFile(a); deleteFile(b); + try { + Thread.sleep(assertPause); + } catch (InterruptedException ignored) {} Assert.assertFalse(a.exists()); Assert.assertFalse(b.exists()); cleanDirs(); @@ -88,6 +140,9 @@ public class FileTest { createFile(a); deleteFile(a); sideloadContentB = createFile(b); + try { + Thread.sleep(assertPause); + } catch (InterruptedException ignored) {} Assert.assertEquals(sideloadContentB, a.updateContent().getContent()); cleanDirs(); // 5 @@ -95,46 +150,73 @@ public class FileTest { createFile(b); deleteFile(a); sideloadContentB = updateFile(b); + try { + Thread.sleep(assertPause); + } catch (InterruptedException ignored) {} Assert.assertEquals(sideloadContentB, a.updateContent().getContent()); cleanDirs(); // 6 sideloadContentA = createFile(a); + try { + Thread.sleep(assertPause); + } catch (InterruptedException ignored) {} Assert.assertEquals(sideloadContentA, b.updateContent().getContent()); // 7 createFile(b); createFile(a); + try { + Thread.sleep(assertPause); + } catch (InterruptedException ignored) {} Assert.assertEquals(sideloadContentA, b.updateContent().getContent()); // 8 createFile(a); createFile(b); deleteFile(b); + try { + Thread.sleep(assertPause); + } catch (InterruptedException ignored) {} Assert.assertFalse(a.exists()); Assert.assertFalse(b.exists()); cleanDirs(); //9 createFile(a); sideloadContentB = createFile(b); + try { + Thread.sleep(assertPause); + } catch (InterruptedException ignored) {} Assert.assertEquals(sideloadContentB, a.updateContent().getContent()); cleanDirs(); // 10 createFile(b); createFile(a); sideloadContentB = updateFile(b); + try { + Thread.sleep(assertPause); + } catch (InterruptedException ignored) {} Assert.assertEquals(sideloadContentB, a.updateContent().getContent()); // 11 createFile(a); sideloadContentA = updateFile(a); + try { + Thread.sleep(assertPause); + } catch (InterruptedException ignored) {} Assert.assertEquals(sideloadContentA, b.updateContent().getContent()); // 12 createFile(a); createFile(b); sideloadContentA = updateFile(a); + try { + Thread.sleep(assertPause); + } catch (InterruptedException ignored) {} Assert.assertEquals(sideloadContentA, b.updateContent().getContent()); // 13 createFile(a); createFile(b); updateFile(a); deleteFile(b); + try { + Thread.sleep(assertPause); + } catch (InterruptedException ignored) {} Assert.assertFalse(a.exists()); Assert.assertFalse(b.exists()); cleanDirs(); @@ -142,6 +224,9 @@ public class FileTest { createFile(a); updateFile(a); sideloadContentB = createFile(b); + try { + Thread.sleep(assertPause); + } catch (InterruptedException ignored) {} Assert.assertEquals(sideloadContentB, a.updateContent().getContent()); cleanDirs(); // 15 @@ -149,6 +234,9 @@ public class FileTest { createFile(b); updateFile(a); sideloadContentB = updateFile(b); + try { + Thread.sleep(assertPause); + } catch (InterruptedException ignored) {} Assert.assertEquals(sideloadContentB, a.updateContent().getContent()); cleanDirs(); } diff --git a/src/test/resources/README.md b/src/test/resources/README.md new file mode 100644 index 0000000..b95c86e --- /dev/null +++ b/src/test/resources/README.md @@ -0,0 +1,2 @@ +TODO +* `test-config.json` should be generated. diff --git a/src/test/resources/test-config.json b/src/test/resources/test-config.json new file mode 100644 index 0000000..bdd5c8a --- /dev/null +++ b/src/test/resources/test-config.json @@ -0,0 +1,8 @@ +{ + "jsonMapOfSyncMaps": { + "syncMap1": [ + "/home/user/ws/idea/ensync/src/test/resources/a", + "/home/user/ws/idea/ensync/src/test/resources/b" + ] + } +}