diff --git a/src/main/java/com/jetbrains/jacobfs/JacobFS.java b/src/main/java/com/jetbrains/jacobfs/JacobFS.java index 27eeda1..e3d69d9 100644 --- a/src/main/java/com/jetbrains/jacobfs/JacobFS.java +++ b/src/main/java/com/jetbrains/jacobfs/JacobFS.java @@ -21,12 +21,8 @@ public void executeCommand(VoidCommand command) throws IOException { command.execute(treeLocator); } - public void createContainer(String path) throws IOException { - treeLocator = TreeLocator.init(Path.of(path)); - } - public void openContainer(String path) throws IOException { - treeLocator = TreeLocator.load(Path.of(path)); + treeLocator = new TreeLocator(Path.of(path)); } public void closeContainer() { diff --git a/src/main/java/com/jetbrains/jacobfs/tree/TreeLocator.java b/src/main/java/com/jetbrains/jacobfs/tree/TreeLocator.java index 545a14b..f408ab4 100644 --- a/src/main/java/com/jetbrains/jacobfs/tree/TreeLocator.java +++ b/src/main/java/com/jetbrains/jacobfs/tree/TreeLocator.java @@ -12,27 +12,33 @@ public class TreeLocator { private RootNode rootNode; private TreeLocatorMetadata treeLocatorMetadata; - private final TreeLocatorDecoder treeLocatorDecoder = new TreeLocatorDecoder(); - private final TreeLocatorEncoder treeLocatorEncoder = new TreeLocatorEncoder(); + private final TreeLocatorDecoder treeLocatorDecoder; + private final TreeLocatorEncoder treeLocatorEncoder; - private TreeLocator() { + public TreeLocator(Path path) throws IOException { + this(path, new TreeLocatorEncoder(), new TreeLocatorDecoder()); } - public static TreeLocator init(Path path) throws IOException { - TreeLocator treeLocator = new TreeLocator(); - treeLocator.treeLocatorMetadata = TreeLocatorMetadata.init(path); - treeLocator.rootNode = new RootNode(); - treeLocator.rootNode.setPayloadSpaceEndOffset(treeLocator.treeLocatorMetadata.getPayloadSpaceOffset()); - treeLocator.saveState(); - return treeLocator; + public TreeLocator(Path path, TreeLocatorEncoder treeLocatorEncoder, TreeLocatorDecoder treeLocatorDecoder) throws IOException { + this.treeLocatorEncoder = treeLocatorEncoder; + this.treeLocatorDecoder = treeLocatorDecoder; + if (path.toFile().exists()) { + loadContainer(path); + } else { + createNewContainer(path); + } + } + + private void createNewContainer(Path path) throws IOException { + treeLocatorMetadata = TreeLocatorMetadata.init(path); + rootNode = new RootNode(); + rootNode.setPayloadSpaceEndOffset(treeLocatorMetadata.getPayloadSpaceOffset()); + saveState(); } - public static TreeLocator load(Path path) throws IOException { - TreeLocator treeLocator = new TreeLocator(); - treeLocator.treeLocatorMetadata = TreeLocatorMetadata.load(path); - treeLocator.rootNode = treeLocator.getTreeLocatorDecoder() - .loadTreeLocatorFromFile(treeLocator.treeLocatorMetadata); - return treeLocator; + public void loadContainer(Path path) throws IOException { + treeLocatorMetadata = TreeLocatorMetadata.load(path); + rootNode = treeLocatorDecoder.loadTreeLocatorFromFile(treeLocatorMetadata); } /** @@ -102,18 +108,10 @@ public RootNode getRootNode() { } public void saveState() throws IOException { - getTreeLocatorEncoder().saveTreeLocatorToFile(rootNode, treeLocatorMetadata); + treeLocatorEncoder.saveTreeLocatorToFile(rootNode, treeLocatorMetadata); } public File getFile() { return treeLocatorMetadata.getFile(); } - - public TreeLocatorDecoder getTreeLocatorDecoder() { - return treeLocatorDecoder; - } - - public TreeLocatorEncoder getTreeLocatorEncoder() { - return treeLocatorEncoder; - } } diff --git a/src/test/java/com/jetbrains/jacobfs/AbstractJacobFSTest.java b/src/test/java/com/jetbrains/jacobfs/AbstractJacobFSTest.java index 222084a..4d7dab7 100644 --- a/src/test/java/com/jetbrains/jacobfs/AbstractJacobFSTest.java +++ b/src/test/java/com/jetbrains/jacobfs/AbstractJacobFSTest.java @@ -24,7 +24,7 @@ public static Command getPayloadSpaceEndOffsetCommand() { @BeforeEach protected void setUp() throws IOException { jacobFS.deleteContainer(TEST_CNT_PATH); - jacobFS.createContainer(TEST_CNT_PATH); + jacobFS.openContainer(TEST_CNT_PATH); } protected void reopenTestContainer() throws IOException { diff --git a/src/test/java/com/jetbrains/jacobfs/functionaltests/AbstractFunctionalTest.java b/src/test/java/com/jetbrains/jacobfs/functionaltests/AbstractFunctionalTest.java new file mode 100644 index 0000000..2784f05 --- /dev/null +++ b/src/test/java/com/jetbrains/jacobfs/functionaltests/AbstractFunctionalTest.java @@ -0,0 +1,37 @@ +package com.jetbrains.jacobfs.functionaltests; + +import com.jetbrains.jacobfs.AbstractJacobFSTest; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class AbstractFunctionalTest extends AbstractJacobFSTest { + public Map getProjectFiles() throws IOException { + try (Stream walk = Files.walk(Path.of("."))) { + return walk.map(Path::normalize) + .filter(path -> !path.startsWith("target")) + .filter(path -> !path.startsWith(".idea")) + .filter(path -> !path.startsWith(".git")) + .filter(path -> !path.startsWith(".github")) + .filter(path -> !path.endsWith(TEST_CNT_PATH.replaceAll("\\./", ""))) + .filter(path -> path.toFile().isFile()) + .collect(Collectors.toMap(AbstractFunctionalTest::convertWindowsPath, AbstractFunctionalTest::readAllBytesSilently)); + } + } + + public static byte[] readAllBytesSilently(Path path) { + try { + return Files.readAllBytes(path); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public static String convertWindowsPath(Path path) { + return "/" + path.toString().replaceAll("\\\\", "/"); + } +} diff --git a/src/test/java/com/jetbrains/jacobfs/functionaltests/ImportExportProjectFunctionalTest.java b/src/test/java/com/jetbrains/jacobfs/functionaltests/ImportExportProjectFunctionalTest.java index 9976a29..425b2cc 100644 --- a/src/test/java/com/jetbrains/jacobfs/functionaltests/ImportExportProjectFunctionalTest.java +++ b/src/test/java/com/jetbrains/jacobfs/functionaltests/ImportExportProjectFunctionalTest.java @@ -1,21 +1,17 @@ package com.jetbrains.jacobfs.functionaltests; -import com.jetbrains.jacobfs.AbstractJacobFSTest; import com.jetbrains.jacobfs.command.FetchAllFiles; import com.jetbrains.jacobfs.command.WriteFile; import org.junit.jupiter.api.Test; import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; import java.util.Arrays; import java.util.Map; -import java.util.stream.Collectors; -import java.util.stream.Stream; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; -class ImportExportProjectFunctionalTest extends AbstractJacobFSTest { +class ImportExportProjectFunctionalTest extends AbstractFunctionalTest { @Test void shouldImportExportProjectFiles() throws IOException { @@ -28,26 +24,4 @@ void shouldImportExportProjectFiles() throws IOException { assertTrue(projectFiles.entrySet().stream() .allMatch(entry -> Arrays.equals(entry.getValue(), containerFiles.get(entry.getKey())))); } - - private Map getProjectFiles() throws IOException { - try (Stream walk = Files.walk(Path.of("."))) { - return walk.map(Path::normalize) - .filter(path -> !path.startsWith("target")) - .filter(path -> path.toFile().isFile()) - .collect(Collectors.toMap(ImportExportProjectFunctionalTest::convertWindowsPath, ImportExportProjectFunctionalTest::readAllBytesSilently)); - } - } - - private static byte[] readAllBytesSilently(Path path) { - try { - return Files.readAllBytes(path); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - private static String convertWindowsPath(Path path) { - return "/" + path.toString().replaceAll("\\\\", "/"); - } - } \ No newline at end of file diff --git a/src/test/java/com/jetbrains/jacobfs/functionaltests/JBTaskFunctionalTest.java b/src/test/java/com/jetbrains/jacobfs/functionaltests/JBTaskFunctionalTest.java new file mode 100644 index 0000000..f76f04c --- /dev/null +++ b/src/test/java/com/jetbrains/jacobfs/functionaltests/JBTaskFunctionalTest.java @@ -0,0 +1,55 @@ +package com.jetbrains.jacobfs.functionaltests; + +import com.jetbrains.jacobfs.command.DeleteFile; +import com.jetbrains.jacobfs.command.FetchAllFiles; +import com.jetbrains.jacobfs.command.WriteFile; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; + +class JBTaskFunctionalTest extends AbstractFunctionalTest { + + @Test + void shouldPerformJBTestFromTask() throws IOException { + //add all project files + Map projectFiles = getProjectFiles(); + for (Map.Entry entry : projectFiles.entrySet()) { + jacobFS.executeCommand(new WriteFile(entry.getKey(), entry.getValue())); + } + + //remove everything but com.jetbrains.jacobfs.command sources + String filter70percent = "/src/main/java/com/jetbrains/jacobfs/command"; + for (String toDelete : projectFiles.keySet()) { + if (!toDelete.startsWith(filter70percent)) { + jacobFS.executeCommand(new DeleteFile(toDelete)); + } + } + + //add all project files again under clone/ dir + for (Map.Entry entry : projectFiles.entrySet()) { + jacobFS.executeCommand(new WriteFile("/clone" + entry.getKey(), entry.getValue())); + } + + //reopen container + reopenTestContainer(); + + //verify + List commandFiles = projectFiles.keySet().stream() + .filter(key -> key.startsWith(filter70percent)).toList(); + Map containerFiles = jacobFS.executeCommand(new FetchAllFiles()); + assertEquals(projectFiles.size() + commandFiles.size(), containerFiles.size()); + + for (String commandFileKey : commandFiles) { + assertArrayEquals(projectFiles.get(commandFileKey), containerFiles.get(commandFileKey)); + } + + for (Map.Entry entry : projectFiles.entrySet()) { + assertArrayEquals(entry.getValue(), containerFiles.get("/clone" + entry.getKey())); + } + } +} \ No newline at end of file diff --git a/src/test/java/com/jetbrains/jacobfs/tree/PathValidatorTest.java b/src/test/java/com/jetbrains/jacobfs/tree/PathValidatorTest.java index c2a597f..3561af5 100644 --- a/src/test/java/com/jetbrains/jacobfs/tree/PathValidatorTest.java +++ b/src/test/java/com/jetbrains/jacobfs/tree/PathValidatorTest.java @@ -10,7 +10,10 @@ class PathValidatorTest { @ValueSource(strings = { "", "/", + "//", "d", + "f", + "\\\\", "/./no", "/some/../file.txt" })