_ fix test pt.2

pull/1/head
io42630 3 years ago
parent f01cf5ec84
commit 8c6f296272

1
.gitignore vendored

@ -5,3 +5,4 @@
*.xlsx# *.xlsx#
testfile.txt testfile.txt
*.log* *.log*
/src/test/temp/*

@ -4,6 +4,7 @@ import com.olexyn.ensync.artifacts.DataRoot;
import com.olexyn.ensync.artifacts.StateFile; import com.olexyn.ensync.artifacts.StateFile;
import com.olexyn.ensync.artifacts.SyncDirectory; import com.olexyn.ensync.artifacts.SyncDirectory;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -12,14 +13,21 @@ public class Flow implements Runnable {
private static final Logger LOGGER = LogUtil.get(Flow.class); private static final Logger LOGGER = LogUtil.get(Flow.class);
public static final long POLLING_PAUSE = 400; public static final long POLLING_PAUSE = 400;
private final AtomicBoolean running = new AtomicBoolean(false);
public void start() {
Thread worker = new Thread(this);
worker.start();
}
public void stop() {
running.set(false);
}
/**
*
*/
@Override @Override
public void run() { public void run() {
running.set(true);
while (true) { while (running.get()) {
synchronized(DataRoot.getSyncBundles()) { synchronized(DataRoot.getSyncBundles()) {
@ -34,9 +42,11 @@ public class Flow implements Runnable {
} }
try { try {
System.out.println("Pausing... for " + POLLING_PAUSE + "ms."); LOGGER.info("Pausing... for " + POLLING_PAUSE + "ms.");
Thread.sleep(POLLING_PAUSE); Thread.sleep(POLLING_PAUSE);
} catch (InterruptedException ignored) { } } catch (InterruptedException ignored) {
LOGGER.info("Thread interrupted.");
}
} }
} }
@ -44,7 +54,7 @@ public class Flow implements Runnable {
* *
*/ */
private void doSyncDirectory(SyncDirectory sd) { private void doSyncDirectory(SyncDirectory sd) {
LOGGER.info("READ"); LOGGER.info("DO SYNC DIRECTORY");
sd.readFileSystem(); sd.readFileSystem();
@ -69,7 +79,7 @@ public class Flow implements Runnable {
for (var sd : syncBundle.syncDirectories.values()) { for (var sd : syncBundle.syncDirectories.values()) {
var stateFile = new StateFile(sd.directoryPath); var stateFile = new StateFile(sd.directoryPath);
if (stateFile.exists()) { if (stateFile.exists()) {
LOGGER.info("READ-STATE-FILE-" + sd.readStateFile()); LOGGER.info("READ-STATE-FILE");
} else { } else {
sd.writeStateFile(new StateFile(sd.directoryPath)); sd.writeStateFile(new StateFile(sd.directoryPath));
} }

@ -13,7 +13,7 @@ import java.util.logging.SimpleFormatter;
public class LogUtil { public class LogUtil {
private static final String format = "[%1$tF %1$tT] [%4$-7s] %5$-120s [%2$s]\n"; private static final String format = "[%1$tF %1$tT] [%4$-7s] %5$-100s [%2$s]\n";
public static Logger get(Class<?> c) { public static Logger get(Class<?> c) {
return get(c, Level.INFO); return get(c, Level.INFO);

@ -0,0 +1,256 @@
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 org.junit.After;
import org.junit.Assert;
import org.junit.Before;
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.List;
import java.util.logging.Logger;
/**
* Perform the 15 test cases in TestCases.xlsx.
* Simple means with a static test-config.json, thus no SyncMaps are added at runtime.
*/
public class FifteenTests {
private static final Logger LOGGER = LogUtil.get(FifteenTests.class);
final public static Flow FLOW = new Flow();
final private static Tools tools = new Tools();
private final static long FILE_OPS_PAUSE = 800;
private final static long WAIT_BEFORE_ASSERT = 4000;
private final static Execute x = new Execute();
private static final Path TEMP_DIR = Path.of(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 Path A_DIR = TEMP_DIR.resolve("a");
private static final Path B_DIR = TEMP_DIR.resolve("b");
private final TestFile aFile = new TestFile(A_DIR + "/testfile.txt");
private final TestFile bFile = 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(FILE_OPS_PAUSE);
} catch (InterruptedException e) {
System.out.println("");
}
return stringList;
}
private List<String> updateFile(File file) {
List<String> stringList = new ArrayList<>();
try {
stringList.addAll(tools.fileToLines(file));
stringList.add(LocalDateTime.now().format(dateTimeFormatter) + " UPDATED");
tools.writeStringListToFile(file.getAbsolutePath(), stringList);
Thread.sleep(FILE_OPS_PAUSE);
} catch (InterruptedException e) {
System.out.println("");
}
return stringList;
}
private static void deleteFile(File file) {
try {
Files.delete(file.toPath());
Thread.sleep(FILE_OPS_PAUSE);
} catch (IOException | InterruptedException e) {
LOGGER.severe("Could not delete file.");
}
}
private static void deleteRec(Path path) {
var file = path.toFile();
if (!file.exists()) {
return;
}
if (file.isDirectory()) {
try {
Files.walk(path)
.filter(subPath -> !subPath.equals(path))
.forEach(FifteenTests::deleteRec);
} catch (IOException e) {
LOGGER.severe("Could not walk path.");
}
}
try {
Files.delete(path);
} catch (IOException e) {
LOGGER.severe("Could not delete file.");
}
}
private void cleanDirs(Path... dirs) {
for (var dir : dirs) {
deleteRec(dir);
try {
Files.createDirectory(dir);
} catch (IOException e) {
LOGGER.severe("Could not clear dirs.");
}
}
}
SyncBundle syncBundle;
public static void waitBeforeAssert() {
try {
Thread.sleep(WAIT_BEFORE_ASSERT);
} catch (InterruptedException ignored) {}
}
@Before
public void prepare() {
syncBundle = new SyncBundle("testSyncBundle");
syncBundle.addDirectory(A_DIR);
syncBundle.addDirectory(B_DIR);
cleanDirs(A_DIR, B_DIR);
DataRoot.get().put(syncBundle.name, syncBundle);
FLOW.start();
}
@After
public void reset() {
FLOW.stop();
cleanDirs(A_DIR, B_DIR);
}
@Test
public void one() {
createFile(aFile);
deleteFile(aFile);
waitBeforeAssert();
Assert.assertFalse(aFile.exists());
Assert.assertFalse(bFile.exists());
}
@Test
public void three() {
createFile(aFile);
createFile(bFile);
deleteFile(aFile);
deleteFile(bFile);
waitBeforeAssert();
Assert.assertFalse(aFile.exists());
Assert.assertFalse(bFile.exists());
}
@Test
public void four() {
createFile(aFile);
deleteFile(aFile);
var bContent = createFile(bFile);
waitBeforeAssert();
Assert.assertEquals(bContent, aFile.readContent());
}
@Test
public void five() {
createFile(aFile);
createFile(bFile);
deleteFile(aFile);
var bContent = updateFile(bFile);
waitBeforeAssert();
Assert.assertEquals(bContent, aFile.readContent());
}
@Test
public void six() {
var aContent = createFile(aFile);
waitBeforeAssert();
Assert.assertEquals(aContent, bFile.readContent());
}
@Test
public void eight() {
createFile(aFile);
createFile(bFile);
deleteFile(bFile);
waitBeforeAssert();
Assert.assertFalse(aFile.exists());
Assert.assertFalse(bFile.exists());
}
@Test
public void nine() {
createFile(aFile);
var bContent = createFile(bFile);
waitBeforeAssert();
Assert.assertEquals(bContent, aFile.readContent());
}
@Test
public void ten() {
createFile(bFile);
createFile(aFile);
var bContent = updateFile(bFile);
waitBeforeAssert();
Assert.assertEquals(bContent, aFile.readContent());
}
@Test
public void eleven() {
createFile(aFile);
var aContent = updateFile(aFile);
waitBeforeAssert();
Assert.assertEquals(aContent, bFile.readContent());
}
@Test
public void thirteen() {
createFile(aFile);
createFile(bFile);
updateFile(aFile);
deleteFile(bFile);
waitBeforeAssert();
Assert.assertFalse(aFile.exists());
Assert.assertFalse(bFile.exists());
}
@Test
public void fourteen() {
createFile(aFile);
updateFile(aFile);
var bContent = createFile(bFile);
waitBeforeAssert();
Assert.assertEquals(bContent, aFile.readContent());
}
@Test
public void fifteen() {
createFile(aFile);
createFile(bFile);
updateFile(aFile);
var bContent = updateFile(bFile);
waitBeforeAssert();
Assert.assertEquals(bContent, aFile.readContent());
}
}

@ -1,260 +0,0 @@
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.SyncBundle;
import com.olexyn.ensync.artifacts.SyncDirectory;
import org.json.JSONException;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
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.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();
private final static long FILE_OPS_PAUSE = 800;
private final static long WAIT_BEFORE_ASSERT = 4000;
private final static Execute x = new Execute();
private static final Path TEMP_DIR = Path.of(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 Path A_DIR = TEMP_DIR.resolve("/a");
private static final Path B_DIR = TEMP_DIR.resolve("/b");
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(FILE_OPS_PAUSE);
} catch (InterruptedException e) {
System.out.println("");
}
return stringList;
}
private List<String> updateFile(File file) {
List<String> stringList = new ArrayList<>();
try {
stringList.addAll(tools.fileToLines(file));
stringList.add(LocalDateTime.now().format(dateTimeFormatter) + " UPDATED");
tools.writeStringListToFile(file.getAbsolutePath(), stringList);
Thread.sleep(FILE_OPS_PAUSE);
} catch (InterruptedException e) {
System.out.println("");
}
return stringList;
}
private static void deleteFile(File file) {
try {
Files.delete(file.toPath());
Thread.sleep(FILE_OPS_PAUSE);
} catch (IOException | InterruptedException e) {
LOGGER.severe("Could not delete file.");
}
}
private void cleanDirs(Path... dirs) {
for (var dir : dirs) {
try {
Files.delete(dir);
Files.createDirectory(dir);
} catch (IOException e) {
Assert.fail();
}
}
}
SyncBundle syncMap;
List<String> sideloadContentA;
List<String> sideloadContentB;
@Before
public void prepare() {
syncMap = new SyncBundle("testSyncBundle");
syncMap.addDirectory(A_DIR);
syncMap.addDirectory(B_DIR);
}
@After
public void reset() {
}
/**
* 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 doSimpleFileTests() throws JSONException {
FLOW_THREAD.start();
List<String> sideloadContentA;
List<String> sideloadContentB;
cleanDirs();
// 1
createFile(aTestFile);
deleteFile(aTestFile);
try {
Thread.sleep(WAIT_BEFORE_ASSERT);
} catch (InterruptedException ignored) {}
Assert.assertFalse(aTestFile.exists());
Assert.assertFalse(bTestFile.exists());
cleanDirs();
// 2
createFile(bTestFile);
createFile(aTestFile);
deleteFile(aTestFile);
try {
Thread.sleep(WAIT_BEFORE_ASSERT);
} catch (InterruptedException ignored) {}
Assert.assertFalse(aTestFile.exists());
Assert.assertFalse(bTestFile.exists());
cleanDirs();
// 3
createFile(aTestFile);
createFile(bTestFile);
deleteFile(aTestFile);
deleteFile(bTestFile);
try {
Thread.sleep(WAIT_BEFORE_ASSERT);
} catch (InterruptedException ignored) {}
Assert.assertFalse(aTestFile.exists());
Assert.assertFalse(bTestFile.exists());
cleanDirs();
// 4
createFile(aTestFile);
deleteFile(aTestFile);
sideloadContentB = createFile(bTestFile);
try {
Thread.sleep(WAIT_BEFORE_ASSERT);
} catch (InterruptedException ignored) {}
Assert.assertEquals(sideloadContentB, aTestFile.updateContent().getContent());
cleanDirs();
// 5
createFile(aTestFile);
createFile(bTestFile);
deleteFile(aTestFile);
sideloadContentB = updateFile(bTestFile);
try {
Thread.sleep(WAIT_BEFORE_ASSERT);
} catch (InterruptedException ignored) {}
Assert.assertEquals(sideloadContentB, aTestFile.updateContent().getContent());
cleanDirs();
// 6
sideloadContentA = createFile(aTestFile);
try {
Thread.sleep(WAIT_BEFORE_ASSERT);
} catch (InterruptedException ignored) {}
Assert.assertEquals(sideloadContentA, bTestFile.updateContent().getContent());
// 7
createFile(bTestFile);
createFile(aTestFile);
try {
Thread.sleep(WAIT_BEFORE_ASSERT);
} catch (InterruptedException ignored) {}
Assert.assertEquals(sideloadContentA, bTestFile.updateContent().getContent());
// 8
createFile(aTestFile);
createFile(bTestFile);
deleteFile(bTestFile);
try {
Thread.sleep(WAIT_BEFORE_ASSERT);
} catch (InterruptedException ignored) {}
Assert.assertFalse(aTestFile.exists());
Assert.assertFalse(bTestFile.exists());
cleanDirs();
//9
createFile(aTestFile);
sideloadContentB = createFile(bTestFile);
try {
Thread.sleep(WAIT_BEFORE_ASSERT);
} catch (InterruptedException ignored) {}
Assert.assertEquals(sideloadContentB, aTestFile.updateContent().getContent());
cleanDirs();
// 10
createFile(bTestFile);
createFile(aTestFile);
sideloadContentB = updateFile(bTestFile);
try {
Thread.sleep(WAIT_BEFORE_ASSERT);
} catch (InterruptedException ignored) {}
Assert.assertEquals(sideloadContentB, aTestFile.updateContent().getContent());
// 11
createFile(aTestFile);
sideloadContentA = updateFile(aTestFile);
try {
Thread.sleep(WAIT_BEFORE_ASSERT);
} catch (InterruptedException ignored) {}
Assert.assertEquals(sideloadContentA, bTestFile.updateContent().getContent());
// 12
createFile(aTestFile);
createFile(bTestFile);
sideloadContentA = updateFile(aTestFile);
try {
Thread.sleep(WAIT_BEFORE_ASSERT);
} catch (InterruptedException ignored) {}
Assert.assertEquals(sideloadContentA, bTestFile.updateContent().getContent());
// 13
createFile(aTestFile);
createFile(bTestFile);
updateFile(aTestFile);
deleteFile(bTestFile);
try {
Thread.sleep(WAIT_BEFORE_ASSERT);
} catch (InterruptedException ignored) {}
Assert.assertFalse(aTestFile.exists());
Assert.assertFalse(bTestFile.exists());
cleanDirs();
// 14
createFile(aTestFile);
updateFile(aTestFile);
sideloadContentB = createFile(bTestFile);
try {
Thread.sleep(WAIT_BEFORE_ASSERT);
} catch (InterruptedException ignored) {}
Assert.assertEquals(sideloadContentB, aTestFile.updateContent().getContent());
cleanDirs();
// 15
createFile(aTestFile);
createFile(bTestFile);
updateFile(aTestFile);
sideloadContentB = updateFile(bTestFile);
try {
Thread.sleep(WAIT_BEFORE_ASSERT);
} catch (InterruptedException ignored) {}
Assert.assertEquals(sideloadContentB, aTestFile.updateContent().getContent());
cleanDirs();
}
}

@ -23,6 +23,11 @@ public class TestFile extends File {
this.content = content; this.content = content;
} }
public List<String> readContent() {
this.content = tools.fileToLines(this);
return content;
}
public List<String> getContent() { public List<String> getContent() {
return content; return content;
} }

Loading…
Cancel
Save