run first test - fail.

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

1
.gitignore vendored

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

@ -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<String, SyncMap> syncMapEntry : MAP_OF_SYNCMAPS.entrySet()) {
for (Entry<String, SyncMap> syncMapEntry : MapOfSyncMaps.get().entrySet()) {
for (Entry<String, SyncDirectory> 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();

@ -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<String, SyncMap> 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:

@ -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;
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);
}
}

@ -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<String, SyncMap> 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<String> createFile(File file) {
List<String> 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<String>();
return stringList;
}
private List<String> updateFile(File file) {
List<String> 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<String>();
return stringList;
}
private void deleteFile(File file) {
try {
Thread.sleep(10);
List<String> 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<String> sideloadContentA;
List<String> 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();
}

@ -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