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

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

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

@ -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<String, SyncDirectory> syncDirectories = new HashMap<>();
public Map<Path, SyncDirectory> 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));
}
}

@ -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<String, SyncFile> listCreated = new HashMap<>();
public final Map<String, SyncFile> 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<String, SyncFile> readStateFile() {
Map<String, SyncFile> filemap = new HashMap<>();
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) {
// 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<File> 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));

@ -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() {

@ -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<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 {
SyncBundle syncMap = new SyncBundle("testSyncBundle");
syncMap.addDirectory(A_DIR);
syncMap.addDirectory(B_DIR);
FLOW_THREAD.start();

Loading…
Cancel
Save