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 ccdcc03 commit 63446fa
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
1 change: 0 additions & 1 deletion src/main/java/com/jetbrains/jacobfs/tree/TreeLocator.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public class TreeLocator {

private TreeLocator() {
}
//TODO test for too big locator

public static TreeLocator init(Path path) throws IOException {
TreeLocator treeLocator = new TreeLocator();
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/com/jetbrains/jacobfs/tree/TreeLocatorEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@ public final class TreeLocatorEncoder {
public static final String END_LINE_TAG = "\n";

public void saveTreeLocatorToFile(RootNode rootNode, TreeLocatorMetadata treeLocatorMetadata) throws IOException {
String string = encode(rootNode);
String encoded = encode(rootNode);
if (encoded.length() > treeLocatorMetadata.getTreeLocatorReservedSpace()) {
throw new RuntimeException("TreeLocator is too big to be saved! Last command has not been persisted");
}
try (RandomAccessFile randomAccessFile = new RandomAccessFile(treeLocatorMetadata.getFile(), "rw")) {
randomAccessFile.seek(treeLocatorMetadata.getTreeLocatorOffset());
randomAccessFile.writeBytes(string);
randomAccessFile.writeBytes(encoded);
}
treeLocatorMetadata.setTreeLocatorLength(string.length());
treeLocatorMetadata.setTreeLocatorLength(encoded.length());
treeLocatorMetadata.saveState();
}

public String encode(DirNode rootNode) {
public String encode(RootNode rootNode) {
StringBuilder sb = new StringBuilder();
appendToStringBuilder(rootNode, sb);
return sb.toString();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.jetbrains.jacobfs.tree;

import org.junit.jupiter.api.Test;

import java.nio.file.FileAlreadyExistsException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;

class TreeLocatorEncoderTest {

@Test
void shouldThrowIfTreeLocatorTooBig() {
TreeLocatorMetadata mockedTLM = mock(TreeLocatorMetadata.class);
int reservedSpace = 8;
when(mockedTLM.getTreeLocatorReservedSpace()).thenReturn(reservedSpace);

TreeLocatorEncoder treeLocatorEncoder = spy(new TreeLocatorEncoder());
doReturn(new String(new byte[reservedSpace + 1])).when(treeLocatorEncoder).encode(any());
assertThrows(RuntimeException.class,
() -> treeLocatorEncoder.saveTreeLocatorToFile(new RootNode(), mockedTLM));
}

@Test
void shouldEncodeDecode() throws FileAlreadyExistsException {
DirNode lvl31 = new DirNode("lvl3");
lvl31.addFileNode(new FileNode("31.txt", 0L, 8));
lvl31.addFileNode(new FileNode("32.txt", 8L, 16));

DirNode lvl32 = new DirNode("lvl3");
lvl32.addFileNode(new FileNode("33.txt", 16L, 24));

DirNode lvl2 = new DirNode("lvl2");
lvl2.addDirNode(lvl31);
lvl2.addDirNode(lvl32);
lvl2.addFileNode(new FileNode("34.dll", 24L, 32));

DirNode lvl1 = new DirNode("lvl1");
lvl1.addDirNode(lvl2);

RootNode rootNode = new RootNode();
rootNode.addDirNode(lvl1);
rootNode.addFileNode(new FileNode("35.lock", 32L, 40));

TreeLocatorEncoder treeLocatorEncoder = new TreeLocatorEncoder();
TreeLocatorDecoder treeLocatorDecoder = new TreeLocatorDecoder();
String encoded1 = treeLocatorEncoder.encode(rootNode);
RootNode decoded1 = treeLocatorDecoder.decode(encoded1);
String encoded2 = treeLocatorEncoder.encode(decoded1);
assertEquals(encoded1, encoded2);
}
}

0 comments on commit 63446fa

Please sign in to comment.