From f01cf5ec84ff3c5dd7f1afcb49718d7e6288c32b Mon Sep 17 00:00:00 2001 From: io42630 Date: Sun, 3 Apr 2022 16:46:46 +0200 Subject: [PATCH] _ use Path --- src/main/java/com/olexyn/ensync/MainApp.java | 3 ++- .../com/olexyn/ensync/artifacts/StateFile.kt | 9 ++++--- .../olexyn/ensync/artifacts/SyncBundle.java | 7 ++--- .../ensync/artifacts/SyncDirectory.java | 12 ++++----- .../com/olexyn/ensync/artifacts/SyncFile.java | 4 +-- .../com/olexyn/ensync/files/FileTest.java | 27 ++++++++++++++----- 6 files changed, 40 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/olexyn/ensync/MainApp.java b/src/main/java/com/olexyn/ensync/MainApp.java index 65675ff..54b99e9 100644 --- a/src/main/java/com/olexyn/ensync/MainApp.java +++ b/src/main/java/com/olexyn/ensync/MainApp.java @@ -7,6 +7,7 @@ import org.json.JSONException; import org.json.JSONObject; import java.io.File; +import java.nio.file.Path; public class MainApp { @@ -23,7 +24,7 @@ public class MainApp { SyncBundle syncBundle = new SyncBundle(bundleKey); dataRoot.getJSONArray(bundleKey).toList() .forEach( - directoryPath -> syncBundle.addDirectory(directoryPath.toString()) + directoryPath -> syncBundle.addDirectory(Path.of(directoryPath.toString())) ); DataRoot.get().put(bundleKey, syncBundle); diff --git a/src/main/java/com/olexyn/ensync/artifacts/StateFile.kt b/src/main/java/com/olexyn/ensync/artifacts/StateFile.kt index 25d1b41..ee3ef3d 100644 --- a/src/main/java/com/olexyn/ensync/artifacts/StateFile.kt +++ b/src/main/java/com/olexyn/ensync/artifacts/StateFile.kt @@ -1,15 +1,16 @@ package com.olexyn.ensync.artifacts import java.io.File +import java.nio.file.Path -class StateFile(val targetPath: String) { +class StateFile(val targetPath: Path) { - fun getPath(): String { - return targetPath + "/" + Constants.STATE_FILE_NAME + fun getPath(): Path { + return targetPath.resolve(Constants.STATE_FILE_NAME) } private fun getFile(): File { - return File(getPath()) + return getPath().toFile(); } fun exists(): Boolean { diff --git a/src/main/java/com/olexyn/ensync/artifacts/SyncBundle.java b/src/main/java/com/olexyn/ensync/artifacts/SyncBundle.java index b8d710b..74e92a3 100644 --- a/src/main/java/com/olexyn/ensync/artifacts/SyncBundle.java +++ b/src/main/java/com/olexyn/ensync/artifacts/SyncBundle.java @@ -4,6 +4,7 @@ package com.olexyn.ensync.artifacts; import com.olexyn.ensync.Tools; import java.io.File; +import java.nio.file.Path; import java.util.Collection; import java.util.HashMap; import java.util.Map; @@ -15,7 +16,7 @@ import java.util.Map; public class SyncBundle { public String name; - public Map syncDirectories = new HashMap<>(); + public Map syncDirectories = new HashMap<>(); Tools tools = new Tools(); @@ -37,8 +38,8 @@ public class SyncBundle { * @param path the path from which the SyncDirectory is created. * @see SyncDirectory */ - public void addDirectory(String path) { - if (new File(path).isDirectory()) { + public void addDirectory(Path path) { + if (path.toFile().isDirectory()) { syncDirectories.put(path, new SyncDirectory(path, this)); } } diff --git a/src/main/java/com/olexyn/ensync/artifacts/SyncDirectory.java b/src/main/java/com/olexyn/ensync/artifacts/SyncDirectory.java index 5fcc63e..70ca9b7 100644 --- a/src/main/java/com/olexyn/ensync/artifacts/SyncDirectory.java +++ b/src/main/java/com/olexyn/ensync/artifacts/SyncDirectory.java @@ -26,7 +26,7 @@ public class SyncDirectory { private static final Logger LOGGER = LogUtil.get(SyncDirectory.class); private final SyncBundle syncMap; - public String directoryPath; + public Path directoryPath; public final Map listCreated = new HashMap<>(); public final Map listDeleted = new HashMap<>(); @@ -41,7 +41,7 @@ public class SyncDirectory { * * @see SyncBundle */ - public SyncDirectory(String directoryPath, SyncBundle syncMap) { + public SyncDirectory(Path directoryPath, SyncBundle syncMap) { this.directoryPath = directoryPath; this.syncMap = syncMap; @@ -69,7 +69,7 @@ public class SyncDirectory { public Map readStateFile() { Map filemap = new HashMap<>(); var stateFile = new StateFile(directoryPath); - List lines = tools.fileToLines(new File(stateFile.getPath())); + List lines = tools.fileToLines(stateFile.getPath().toFile()); for (String line : lines) { // this is a predefined format: "modification-time path" @@ -155,15 +155,15 @@ public class SyncDirectory { getFiles().forEach( file -> { String relativePath = file.getAbsolutePath() - .replace(stateFile.getTargetPath(), ""); + .replace(stateFile.getTargetPath().toString(), ""); outputList.add("" + file.lastModified() + " " + relativePath); }); - tools.writeStringListToFile(stateFile.getPath(), outputList); + tools.writeStringListToFile(stateFile.getPath().toString(), outputList); } private Stream getFiles() { try { - return Files.walk(Paths.get(directoryPath)) + return Files.walk(directoryPath) .filter(Files::isRegularFile) .map(Path::toFile) .filter(file -> !file.getName().equals(Constants.STATE_FILE_NAME)); diff --git a/src/main/java/com/olexyn/ensync/artifacts/SyncFile.java b/src/main/java/com/olexyn/ensync/artifacts/SyncFile.java index 6260464..893e96e 100644 --- a/src/main/java/com/olexyn/ensync/artifacts/SyncFile.java +++ b/src/main/java/com/olexyn/ensync/artifacts/SyncFile.java @@ -12,14 +12,14 @@ public class SyncFile extends File { // Very IMPORTANT field. Allows to store lastModified as it is stored in the StateFile. public long timeModifiedFromStateFile = 0; - private String relativePath; + private final String relativePath; private final SyncDirectory sd; public SyncFile(SyncDirectory sd, String absolutePath) { super(absolutePath); this.sd = sd; - relativePath = this.getPath().replace(sd.directoryPath, ""); + relativePath = this.getPath().replace(sd.directoryPath.toString(), ""); } public String getRelativePath() { diff --git a/src/test/java/com/olexyn/ensync/files/FileTest.java b/src/test/java/com/olexyn/ensync/files/FileTest.java index 5cf15c4..28119ce 100644 --- a/src/test/java/com/olexyn/ensync/files/FileTest.java +++ b/src/test/java/com/olexyn/ensync/files/FileTest.java @@ -7,7 +7,9 @@ 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; @@ -35,11 +37,11 @@ public class FileTest { 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"; DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; - private static final String A_DIR = TEMP_DIR + "/a"; - private static final String B_DIR = TEMP_DIR + "/b"; + 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"); @@ -90,15 +92,28 @@ public class FileTest { } } + SyncBundle syncMap; + List sideloadContentA; + List 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 { - SyncBundle syncMap = new SyncBundle("testSyncBundle"); - syncMap.addDirectory(A_DIR); - syncMap.addDirectory(B_DIR); FLOW_THREAD.start();