forked from m4rkyma/Git
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeTester.java
More file actions
94 lines (80 loc) · 3.27 KB
/
TreeTester.java
File metadata and controls
94 lines (80 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import org.junit.jupiter.api.*;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import static org.junit.jupiter.api.Assertions.*;
public class TreeTester {
private static final String TEST_FILE_PATH = "tree_test";
static void setup() throws IOException {
File testFile = new File(TEST_FILE_PATH);
testFile.createNewFile();
}
static void cleanup() {
File testFile = new File(TEST_FILE_PATH);
testFile.delete();
}
@Test
@DisplayName("[8] Test if initialize and objects are created correctly")
void testInitialize() throws Exception {
Git git = new Git();
git.initialize();
File file = new File("index");
Path path = Paths.get("objects");
assertTrue(file.exists());
assertTrue(Files.exists(path));
}
// Adds data to Tree and checks if it's correct
@Test
void testAddBlob() throws IOException {
Tree.add("blob : f187ae69bdfb0f1510f108534b241abaa5a4ca72 : file1.txt");
assertTrue(Tree.hMap.containsKey("f187ae69bdfb0f1510f108534b241abaa5a4ca72"));
assertEquals("file1.txt", Tree.hMap.get("f187ae69bdfb0f1510f108534b241abaa5a4ca72"));
}
// Adds data to tree and checks if it's correct
@Test
void testAddTree() throws IOException {
Tree.add("tree : fa87e7501e7a09957de75317910a08559d45878f");
assertTrue(Tree.tree.contains("fa87e7501e7a09957de75317910a08559d45878f"));
}
// Removes data from tree and checks if correct
@Test
void testRemoveBlob() {
Tree.hMap.put("fa87e7501e7a09957de75317910a08559d45878f", "file2.txt");
Tree.remove("fa87e7501e7a09957de75317910a08559d45878f");
assertFalse(Tree.hMap.containsKey("fa87e7501e7a09957de75317910a08559d45878f"));
}
// Removes data from tree and checks if correct
@Test
void testRemoveTree() {
Tree.tree.add("e914baad1d20942535ed0ac3857b43208e3d6bab");
Tree.remove("e914baad1d20942535ed0ac3857b43208e3d6bab");
assertFalse(Tree.tree.contains("e914baad1d20942535ed0ac3857b43208e3d6bab"));
}
// Gets contents of file and checks if it matches actual contents
@Test
void testGetContents() throws IOException {
// Add data to Tree
Tree.add("blob : f187ae69bdfb0f1510f108534b241abaa5a4ca72 : file1.txt");
Tree.add("tree : fa87e7501e7a09957de75317910a08559d45878f");
Tree.getContents();
// Compares expected and receieved
String expectedOutput = "blob : f187ae69bdfb0f1510f108534b241abaa5a4ca72 : file1.txt\ntree : fa87e7501e7a09957de75317910a08559d45878f";
assertEquals(expectedOutput, Tree.printSB());
// Clean up SB
Tree.sb = new StringBuilder();
}
// Copies over the contents and name to the objects folder
@Test
void testWriteToObjects() throws IOException {
Tree.add("blob : f187ae69bdfb0f1510f108534b241abaa5a4ca72 : file1.txt");
Tree.add("tree : fa87e7501e7a09957de75317910a08559d45878f");
Tree.getContents();
Tree.writeToObjects();
File objectsFile = new File("objects/" + Blob.Sha1(Tree.printSB()));
assertTrue(objectsFile.exists());
Tree.sb = new StringBuilder();
objectsFile.delete();
}
}