Skip to content

Commit

Permalink
refactoring, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bonepl committed Feb 5, 2025
1 parent 63446fa commit 957ba2f
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 60 deletions.
6 changes: 1 addition & 5 deletions src/main/java/com/jetbrains/jacobfs/JacobFS.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
48 changes: 23 additions & 25 deletions src/main/java/com/jetbrains/jacobfs/tree/TreeLocator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static Command<Long> 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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, byte[]> getProjectFiles() throws IOException {
try (Stream<Path> 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("\\\\", "/");
}
}
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -28,26 +24,4 @@ void shouldImportExportProjectFiles() throws IOException {
assertTrue(projectFiles.entrySet().stream()
.allMatch(entry -> Arrays.equals(entry.getValue(), containerFiles.get(entry.getKey()))));
}

private Map<String, byte[]> getProjectFiles() throws IOException {
try (Stream<Path> 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("\\\\", "/");
}

}
Original file line number Diff line number Diff line change
@@ -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<String, byte[]> projectFiles = getProjectFiles();
for (Map.Entry<String, byte[]> 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<String, byte[]> entry : projectFiles.entrySet()) {
jacobFS.executeCommand(new WriteFile("/clone" + entry.getKey(), entry.getValue()));
}

//reopen container
reopenTestContainer();

//verify
List<String> commandFiles = projectFiles.keySet().stream()
.filter(key -> key.startsWith(filter70percent)).toList();
Map<String, byte[]> 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<String, byte[]> entry : projectFiles.entrySet()) {
assertArrayEquals(entry.getValue(), containerFiles.get("/clone" + entry.getKey()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ class PathValidatorTest {
@ValueSource(strings = {
"",
"/",
"//",
"d",
"f",
"\\\\",
"/./no",
"/some/../file.txt"
})
Expand Down

0 comments on commit 957ba2f

Please sign in to comment.