run first test - fail.

pull/1/head
Ivan Olexyn 4 years ago
parent 4a99d7a11a
commit cd5f7be740

1
.gitignore vendored

@ -3,3 +3,4 @@
/target/ /target/
/ensync.iml /ensync.iml
*.xlsx# *.xlsx#
testfile.txt

@ -1,18 +1,21 @@
package com.olexyn.ensync; package com.olexyn.ensync;
import com.olexyn.ensync.artifacts.MapOfSyncMaps;
import com.olexyn.ensync.artifacts.SyncDirectory; import com.olexyn.ensync.artifacts.SyncDirectory;
import com.olexyn.ensync.artifacts.SyncMap; import com.olexyn.ensync.artifacts.SyncMap;
import java.io.File; import java.io.File;
import java.util.Map.Entry; import java.util.Map.Entry;
import static com.olexyn.ensync.Main.MAP_OF_SYNCMAPS;
public class Flow implements Runnable { public class Flow implements Runnable {
Tools tools = new Tools(); Tools tools = new Tools();
public long pollingPause = 200;
private String state; private String state;
@ -21,11 +24,11 @@ public class Flow implements Runnable {
while (true) { while (true) {
synchronized (MAP_OF_SYNCMAPS) { synchronized (MapOfSyncMaps.get()) {
readOrMakeStateFile(); readOrMakeStateFile();
for (Entry<String, SyncMap> syncMapEntry : MAP_OF_SYNCMAPS.entrySet()) { for (Entry<String, SyncMap> syncMapEntry : MapOfSyncMaps.get().entrySet()) {
for (Entry<String, SyncDirectory> SDEntry : syncMapEntry.getValue().syncDirectories.entrySet()) { for (Entry<String, SyncDirectory> SDEntry : syncMapEntry.getValue().syncDirectories.entrySet()) {
@ -34,9 +37,8 @@ public class Flow implements Runnable {
} }
} }
try { try {
long pause = 2000; System.out.println("Pausing... for " + pollingPause + "ms.");
System.out.println("Pausing... for " + pause + "ms."); Thread.sleep(pollingPause);
Thread.sleep(pause);
} catch (InterruptedException ignored) {} } catch (InterruptedException ignored) {}
} }
} }
@ -68,7 +70,7 @@ public class Flow implements Runnable {
* If the StateFile is missing, then create a StateFile. * If the StateFile is missing, then create a StateFile.
*/ */
private void readOrMakeStateFile() { private void readOrMakeStateFile() {
for (var syncMapEntry : MAP_OF_SYNCMAPS.entrySet()) { for (var syncMapEntry : MapOfSyncMaps.get().entrySet()) {
SyncMap syncMap = syncMapEntry.getValue(); SyncMap syncMap = syncMapEntry.getValue();
state = syncMap.toString(); state = syncMap.toString();

@ -1,5 +1,6 @@
package com.olexyn.ensync; package com.olexyn.ensync;
import com.olexyn.ensync.artifacts.MapOfSyncMaps;
import com.olexyn.ensync.artifacts.SyncMap; import com.olexyn.ensync.artifacts.SyncMap;
import com.olexyn.ensync.ui.UI; import com.olexyn.ensync.ui.UI;
@ -17,7 +18,6 @@ public class Main{
final public static Thread UI_THREAD = new Thread(new UI(), "ui"); 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 Thread FLOW_THREAD = new Thread(new Flow(), "flow");
final public static HashMap<String, SyncMap> MAP_OF_SYNCMAPS = new HashMap<>();
final private static Tools tools = new Tools(); final private static Tools tools = new Tools();
public static void main(String[] args) { public static void main(String[] args) {
@ -37,7 +37,7 @@ public class Main{
for (Object jsonSyncDirPath : jsonMapOfSyncMaps.getJSONArray(key).toList()) { for (Object jsonSyncDirPath : jsonMapOfSyncMaps.getJSONArray(key).toList()) {
syncMap.addDirectory(jsonSyncDirPath.toString()); syncMap.addDirectory(jsonSyncDirPath.toString());
} }
MAP_OF_SYNCMAPS.put(key, syncMap); MapOfSyncMaps.get().put(key, syncMap);
} }
break; break;
default: default:

@ -0,0 +1,19 @@
package com.olexyn.ensync.artifacts;
import java.util.HashMap;
public class MapOfSyncMaps {
private static HashMap<String, SyncMap> mapOfSyncMaps;
private MapOfSyncMaps() {}
public static HashMap<String, SyncMap> get() {
if (mapOfSyncMaps == null) {
mapOfSyncMaps = new HashMap<>();
}
return mapOfSyncMaps;
}
}

@ -1,12 +1,13 @@
package com.olexyn.ensync.ui; package com.olexyn.ensync.ui;
import com.olexyn.ensync.artifacts.MapOfSyncMaps;
import com.olexyn.ensync.artifacts.SyncMap; import com.olexyn.ensync.artifacts.SyncMap;
import java.io.File; import java.io.File;
import static com.olexyn.ensync.Main.MAP_OF_SYNCMAPS;
/** /**
* Connect the Controller and the Flow * Connect the Controller and the Flow
@ -16,22 +17,22 @@ public class Bridge {
void newCollection(String collectionName) { void newCollection(String collectionName) {
synchronized (MAP_OF_SYNCMAPS) { synchronized (MapOfSyncMaps.get()) {
MAP_OF_SYNCMAPS.put(collectionName, new SyncMap(collectionName)); MapOfSyncMaps.get().put(collectionName, new SyncMap(collectionName));
} }
} }
void removeCollection(String collectionName) { void removeCollection(String collectionName) {
synchronized (MAP_OF_SYNCMAPS) { synchronized (MapOfSyncMaps.get()) {
MAP_OF_SYNCMAPS.remove(collectionName); MapOfSyncMaps.get().remove(collectionName);
} }
} }
void addDirectory(String collectionName, File diretory) { void addDirectory(String collectionName, File diretory) {
synchronized (MAP_OF_SYNCMAPS) { synchronized (MapOfSyncMaps.get()) {
MAP_OF_SYNCMAPS.get(collectionName).addDirectory(diretory.getAbsolutePath()); MapOfSyncMaps.get().get(collectionName).addDirectory(diretory.getAbsolutePath());
} }
//TODO pause syning when adding //TODO pause syning when adding
} }
@ -43,8 +44,8 @@ public class Bridge {
*/ */
void removeDirectory(String directoryAbsolutePath) { void removeDirectory(String directoryAbsolutePath) {
//TODO fix ConcurrentModificationException. This will possibly resolve further errors. //TODO fix ConcurrentModificationException. This will possibly resolve further errors.
synchronized (MAP_OF_SYNCMAPS) { synchronized (MapOfSyncMaps.get()) {
for (var syncMap : MAP_OF_SYNCMAPS.entrySet()) { for (var syncMap : MapOfSyncMaps.get().entrySet()) {
syncMap.getValue().removeDirectory(directoryAbsolutePath); syncMap.getValue().removeDirectory(directoryAbsolutePath);
} }
} }

@ -1,49 +1,75 @@
package com.olexyn.ensync.files; package com.olexyn.ensync.files;
import com.olexyn.ensync.Execute; import com.olexyn.ensync.Execute;
import com.olexyn.ensync.Flow;
import com.olexyn.ensync.OperationMode;
import com.olexyn.ensync.Tools; 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.Assert;
import org.junit.Test; import org.junit.Test;
import java.io.File; import java.io.File;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
public class FileTest { public class FileTest {
final public static Thread FLOW_THREAD = new Thread(new Flow(), "flow");
final private static Tools tools = new Tools();
final private HashMap<String, SyncMap> mapOfSyncMaps = MapOfSyncMaps.get();
public long fileOpsPause = 800;
public long assertPause = 4000;
Execute x = new Execute(); 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 TEST_RESOURCES = System.getProperty("user.dir") + "/src/test/resources";
private static final String fileBPath = "asff"; DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
private final TestFile a = new TestFile(fileAPath); private final TestFile a = new TestFile(TEST_RESOURCES + "/a/testfile.txt");
private final TestFile b = new TestFile(fileBPath); private final TestFile b = new TestFile(TEST_RESOURCES + "/b/testfile.txt");
private List<String> createFile(File file) { private List<String> createFile(File file) {
List<String> stringList = new ArrayList<>();
try { try {
Thread.sleep(10); stringList.add(LocalDateTime.now().format(dateTimeFormatter) + " CREATED");
tools.writeStringListToFile(file.getAbsolutePath(), stringList);
Thread.sleep(fileOpsPause);
} catch (InterruptedException e) { } catch (InterruptedException e) {
System.out.println(""); System.out.println("");
} }
return new ArrayList<String>(); return stringList;
} }
private List<String> updateFile(File file) { private List<String> updateFile(File file) {
List<String> stringList = new ArrayList<>();
try { 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) { } catch (InterruptedException e) {
System.out.println(""); System.out.println("");
} }
return new ArrayList<String>(); return stringList;
} }
private void deleteFile(File file) { private void deleteFile(File file) {
try { try {
Thread.sleep(10); List<String> cmd = List.of("rm", "-r", file.getAbsolutePath());
x.execute(cmd);
Thread.sleep(fileOpsPause);
} catch (InterruptedException e) { } catch (InterruptedException e) {
System.out.println(""); System.out.println("");
} }
@ -56,16 +82,36 @@ public class FileTest {
/** /**
* Perform the 15 test cases in TestCases.xlsx. * Perform the 15 test cases in TestCases.xlsx.
* Simple means with a static test-config.json, thus no SyncMaps are added at runtime.
*/ */
@Test @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<String> sideloadContentA; List<String> sideloadContentA;
List<String> sideloadContentB; List<String> sideloadContentB;
cleanDirs();
// 1 // 1
createFile(a); createFile(a);
deleteFile(a); deleteFile(a);
try {
Thread.sleep(assertPause);
} catch (InterruptedException ignored) {}
Assert.assertFalse(a.exists()); Assert.assertFalse(a.exists());
Assert.assertFalse(b.exists()); Assert.assertFalse(b.exists());
cleanDirs(); cleanDirs();
@ -73,6 +119,9 @@ public class FileTest {
createFile(b); createFile(b);
createFile(a); createFile(a);
deleteFile(a); deleteFile(a);
try {
Thread.sleep(assertPause);
} catch (InterruptedException ignored) {}
Assert.assertFalse(a.exists()); Assert.assertFalse(a.exists());
Assert.assertFalse(b.exists()); Assert.assertFalse(b.exists());
cleanDirs(); cleanDirs();
@ -81,6 +130,9 @@ public class FileTest {
createFile(b); createFile(b);
deleteFile(a); deleteFile(a);
deleteFile(b); deleteFile(b);
try {
Thread.sleep(assertPause);
} catch (InterruptedException ignored) {}
Assert.assertFalse(a.exists()); Assert.assertFalse(a.exists());
Assert.assertFalse(b.exists()); Assert.assertFalse(b.exists());
cleanDirs(); cleanDirs();
@ -88,6 +140,9 @@ public class FileTest {
createFile(a); createFile(a);
deleteFile(a); deleteFile(a);
sideloadContentB = createFile(b); sideloadContentB = createFile(b);
try {
Thread.sleep(assertPause);
} catch (InterruptedException ignored) {}
Assert.assertEquals(sideloadContentB, a.updateContent().getContent()); Assert.assertEquals(sideloadContentB, a.updateContent().getContent());
cleanDirs(); cleanDirs();
// 5 // 5
@ -95,46 +150,73 @@ public class FileTest {
createFile(b); createFile(b);
deleteFile(a); deleteFile(a);
sideloadContentB = updateFile(b); sideloadContentB = updateFile(b);
try {
Thread.sleep(assertPause);
} catch (InterruptedException ignored) {}
Assert.assertEquals(sideloadContentB, a.updateContent().getContent()); Assert.assertEquals(sideloadContentB, a.updateContent().getContent());
cleanDirs(); cleanDirs();
// 6 // 6
sideloadContentA = createFile(a); sideloadContentA = createFile(a);
try {
Thread.sleep(assertPause);
} catch (InterruptedException ignored) {}
Assert.assertEquals(sideloadContentA, b.updateContent().getContent()); Assert.assertEquals(sideloadContentA, b.updateContent().getContent());
// 7 // 7
createFile(b); createFile(b);
createFile(a); createFile(a);
try {
Thread.sleep(assertPause);
} catch (InterruptedException ignored) {}
Assert.assertEquals(sideloadContentA, b.updateContent().getContent()); Assert.assertEquals(sideloadContentA, b.updateContent().getContent());
// 8 // 8
createFile(a); createFile(a);
createFile(b); createFile(b);
deleteFile(b); deleteFile(b);
try {
Thread.sleep(assertPause);
} catch (InterruptedException ignored) {}
Assert.assertFalse(a.exists()); Assert.assertFalse(a.exists());
Assert.assertFalse(b.exists()); Assert.assertFalse(b.exists());
cleanDirs(); cleanDirs();
//9 //9
createFile(a); createFile(a);
sideloadContentB = createFile(b); sideloadContentB = createFile(b);
try {
Thread.sleep(assertPause);
} catch (InterruptedException ignored) {}
Assert.assertEquals(sideloadContentB, a.updateContent().getContent()); Assert.assertEquals(sideloadContentB, a.updateContent().getContent());
cleanDirs(); cleanDirs();
// 10 // 10
createFile(b); createFile(b);
createFile(a); createFile(a);
sideloadContentB = updateFile(b); sideloadContentB = updateFile(b);
try {
Thread.sleep(assertPause);
} catch (InterruptedException ignored) {}
Assert.assertEquals(sideloadContentB, a.updateContent().getContent()); Assert.assertEquals(sideloadContentB, a.updateContent().getContent());
// 11 // 11
createFile(a); createFile(a);
sideloadContentA = updateFile(a); sideloadContentA = updateFile(a);
try {
Thread.sleep(assertPause);
} catch (InterruptedException ignored) {}
Assert.assertEquals(sideloadContentA, b.updateContent().getContent()); Assert.assertEquals(sideloadContentA, b.updateContent().getContent());
// 12 // 12
createFile(a); createFile(a);
createFile(b); createFile(b);
sideloadContentA = updateFile(a); sideloadContentA = updateFile(a);
try {
Thread.sleep(assertPause);
} catch (InterruptedException ignored) {}
Assert.assertEquals(sideloadContentA, b.updateContent().getContent()); Assert.assertEquals(sideloadContentA, b.updateContent().getContent());
// 13 // 13
createFile(a); createFile(a);
createFile(b); createFile(b);
updateFile(a); updateFile(a);
deleteFile(b); deleteFile(b);
try {
Thread.sleep(assertPause);
} catch (InterruptedException ignored) {}
Assert.assertFalse(a.exists()); Assert.assertFalse(a.exists());
Assert.assertFalse(b.exists()); Assert.assertFalse(b.exists());
cleanDirs(); cleanDirs();
@ -142,6 +224,9 @@ public class FileTest {
createFile(a); createFile(a);
updateFile(a); updateFile(a);
sideloadContentB = createFile(b); sideloadContentB = createFile(b);
try {
Thread.sleep(assertPause);
} catch (InterruptedException ignored) {}
Assert.assertEquals(sideloadContentB, a.updateContent().getContent()); Assert.assertEquals(sideloadContentB, a.updateContent().getContent());
cleanDirs(); cleanDirs();
// 15 // 15
@ -149,6 +234,9 @@ public class FileTest {
createFile(b); createFile(b);
updateFile(a); updateFile(a);
sideloadContentB = updateFile(b); sideloadContentB = updateFile(b);
try {
Thread.sleep(assertPause);
} catch (InterruptedException ignored) {}
Assert.assertEquals(sideloadContentB, a.updateContent().getContent()); Assert.assertEquals(sideloadContentB, a.updateContent().getContent());
cleanDirs(); cleanDirs();
} }

@ -0,0 +1,2 @@
TODO
* `test-config.json` should be generated.

@ -0,0 +1,8 @@
{
"jsonMapOfSyncMaps": {
"syncMap1": [
"/home/user/ws/idea/ensync/src/test/resources/a",
"/home/user/ws/idea/ensync/src/test/resources/b"
]
}
}
Loading…
Cancel
Save