pull/1/head
io42630 3 years ago
parent 63b08df477
commit f01cf5ec84

@ -7,6 +7,7 @@ import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.File; import java.io.File;
import java.nio.file.Path;
public class MainApp { public class MainApp {
@ -23,7 +24,7 @@ public class MainApp {
SyncBundle syncBundle = new SyncBundle(bundleKey); SyncBundle syncBundle = new SyncBundle(bundleKey);
dataRoot.getJSONArray(bundleKey).toList() dataRoot.getJSONArray(bundleKey).toList()
.forEach( .forEach(
directoryPath -> syncBundle.addDirectory(directoryPath.toString()) directoryPath -> syncBundle.addDirectory(Path.of(directoryPath.toString()))
); );
DataRoot.get().put(bundleKey, syncBundle); DataRoot.get().put(bundleKey, syncBundle);

@ -1,15 +1,16 @@
package com.olexyn.ensync.artifacts package com.olexyn.ensync.artifacts
import java.io.File import java.io.File
import java.nio.file.Path
class StateFile(val targetPath: String) { class StateFile(val targetPath: Path) {
fun getPath(): String { fun getPath(): Path {
return targetPath + "/" + Constants.STATE_FILE_NAME return targetPath.resolve(Constants.STATE_FILE_NAME)
} }
private fun getFile(): File { private fun getFile(): File {
return File(getPath()) return getPath().toFile();
} }
fun exists(): Boolean { fun exists(): Boolean {

@ -4,6 +4,7 @@ package com.olexyn.ensync.artifacts;
import com.olexyn.ensync.Tools; import com.olexyn.ensync.Tools;
import java.io.File; import java.io.File;
import java.nio.file.Path;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -15,7 +16,7 @@ import java.util.Map;
public class SyncBundle { public class SyncBundle {
public String name; public String name;
public Map<String, SyncDirectory> syncDirectories = new HashMap<>(); public Map<Path, SyncDirectory> syncDirectories = new HashMap<>();
Tools tools = new Tools(); Tools tools = new Tools();
@ -37,8 +38,8 @@ public class SyncBundle {
* @param path the path from which the SyncDirectory is created. * @param path the path from which the SyncDirectory is created.
* @see SyncDirectory * @see SyncDirectory
*/ */
public void addDirectory(String path) { public void addDirectory(Path path) {
if (new File(path).isDirectory()) { if (path.toFile().isDirectory()) {
syncDirectories.put(path, new SyncDirectory(path, this)); syncDirectories.put(path, new SyncDirectory(path, this));
} }
} }

@ -26,7 +26,7 @@ public class SyncDirectory {
private static final Logger LOGGER = LogUtil.get(SyncDirectory.class); private static final Logger LOGGER = LogUtil.get(SyncDirectory.class);
private final SyncBundle syncMap; private final SyncBundle syncMap;
public String directoryPath; public Path directoryPath;
public final Map<String, SyncFile> listCreated = new HashMap<>(); public final Map<String, SyncFile> listCreated = new HashMap<>();
public final Map<String, SyncFile> listDeleted = new HashMap<>(); public final Map<String, SyncFile> listDeleted = new HashMap<>();
@ -41,7 +41,7 @@ public class SyncDirectory {
* *
* @see SyncBundle * @see SyncBundle
*/ */
public SyncDirectory(String directoryPath, SyncBundle syncMap) { public SyncDirectory(Path directoryPath, SyncBundle syncMap) {
this.directoryPath = directoryPath; this.directoryPath = directoryPath;
this.syncMap = syncMap; this.syncMap = syncMap;
@ -69,7 +69,7 @@ public class SyncDirectory {
public Map<String, SyncFile> readStateFile() { public Map<String, SyncFile> readStateFile() {
Map<String, SyncFile> filemap = new HashMap<>(); Map<String, SyncFile> filemap = new HashMap<>();
var stateFile = new StateFile(directoryPath); var stateFile = new StateFile(directoryPath);
List<String> lines = tools.fileToLines(new File(stateFile.getPath())); List<String> lines = tools.fileToLines(stateFile.getPath().toFile());
for (String line : lines) { for (String line : lines) {
// this is a predefined format: "modification-time path" // this is a predefined format: "modification-time path"
@ -155,15 +155,15 @@ public class SyncDirectory {
getFiles().forEach( getFiles().forEach(
file -> { file -> {
String relativePath = file.getAbsolutePath() String relativePath = file.getAbsolutePath()
.replace(stateFile.getTargetPath(), ""); .replace(stateFile.getTargetPath().toString(), "");
outputList.add("" + file.lastModified() + " " + relativePath); outputList.add("" + file.lastModified() + " " + relativePath);
}); });
tools.writeStringListToFile(stateFile.getPath(), outputList); tools.writeStringListToFile(stateFile.getPath().toString(), outputList);
} }
private Stream<File> getFiles() { private Stream<File> getFiles() {
try { try {
return Files.walk(Paths.get(directoryPath)) return Files.walk(directoryPath)
.filter(Files::isRegularFile) .filter(Files::isRegularFile)
.map(Path::toFile) .map(Path::toFile)
.filter(file -> !file.getName().equals(Constants.STATE_FILE_NAME)); .filter(file -> !file.getName().equals(Constants.STATE_FILE_NAME));

@ -12,14 +12,14 @@ public class SyncFile extends File {
// Very IMPORTANT field. Allows to store lastModified as it is stored in the StateFile. // Very IMPORTANT field. Allows to store lastModified as it is stored in the StateFile.
public long timeModifiedFromStateFile = 0; public long timeModifiedFromStateFile = 0;
private String relativePath; private final String relativePath;
private final SyncDirectory sd; private final SyncDirectory sd;
public SyncFile(SyncDirectory sd, String absolutePath) { public SyncFile(SyncDirectory sd, String absolutePath) {
super(absolutePath); super(absolutePath);
this.sd = sd; this.sd = sd;
relativePath = this.getPath().replace(sd.directoryPath, ""); relativePath = this.getPath().replace(sd.directoryPath.toString(), "");
} }
public String getRelativePath() { public String getRelativePath() {

@ -7,7 +7,9 @@ import com.olexyn.ensync.Tools;
import com.olexyn.ensync.artifacts.SyncBundle; import com.olexyn.ensync.artifacts.SyncBundle;
import com.olexyn.ensync.artifacts.SyncDirectory; import com.olexyn.ensync.artifacts.SyncDirectory;
import org.json.JSONException; import org.json.JSONException;
import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.io.File; import java.io.File;
@ -35,11 +37,11 @@ public class FileTest {
private final static Execute x = new Execute(); private final static Execute x = new Execute();
private static final String TEMP_DIR = System.getProperty("user.dir") + "/src/test/temp"; 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"; private static final String RESOURCES_DIR = System.getProperty("user.dir") + "/src/test/resources";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
private static final String A_DIR = TEMP_DIR + "/a"; private static final Path A_DIR = TEMP_DIR.resolve("/a");
private static final String B_DIR = TEMP_DIR + "/b"; private static final Path B_DIR = TEMP_DIR.resolve("/b");
private final TestFile aTestFile = new TestFile(A_DIR + "/testfile.txt"); private final TestFile aTestFile = new TestFile(A_DIR + "/testfile.txt");
private final TestFile bTestFile = new TestFile(B_DIR + "/testfile.txt"); private final TestFile bTestFile = new TestFile(B_DIR + "/testfile.txt");
@ -90,15 +92,28 @@ public class FileTest {
} }
} }
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. * Perform the 15 test cases in TestCases.xlsx.
* Simple means with a static test-config.json, thus no SyncMaps are added at runtime. * Simple means with a static test-config.json, thus no SyncMaps are added at runtime.
*/ */
@Test @Test
public void doSimpleFileTests() throws JSONException { public void doSimpleFileTests() throws JSONException {
SyncBundle syncMap = new SyncBundle("testSyncBundle");
syncMap.addDirectory(A_DIR);
syncMap.addDirectory(B_DIR);
FLOW_THREAD.start(); FLOW_THREAD.start();

Loading…
Cancel
Save