-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTreeTester.java
More file actions
172 lines (155 loc) · 6.03 KB
/
TreeTester.java
File metadata and controls
172 lines (155 loc) · 6.03 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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 t = new Tree ();
t.add("blob : f187ae69bdfb0f1510f108534b241abaa5a4ca72 : file1.txt");
assertTrue(t.getBlobEntries().containsKey("f187ae69bdfb0f1510f108534b241abaa5a4ca72"));
assertEquals("file1.txt", t.getBlobEntries().get("f187ae69bdfb0f1510f108534b241abaa5a4ca72"));
}
// Adds data to tree and checks if it's correct
@Test
void testAddTree() throws IOException {
Tree t = new Tree ();
t.add("tree : fa87e7501e7a09957de75317910a08559d45878f");
// assertTrue(Tree.t.contains("fa87e7501e7a09957de75317910a08559d45878f"));
}
// Removes data from tree and checks if correct
@Test
void testRemoveBlob() throws IOException {
Tree t = new Tree();
t.getBlobEntries().put("fa87e7501e7a09957de75317910a08559d45878f", "file2.txt");
t.remove("fa87e7501e7a09957de75317910a08559d45878f");
assertFalse(t.getBlobEntries().containsKey("fa87e7501e7a09957de75317910a08559d45878f"));
}
// Removes data from tree and checks if correct
@Test
void testRemoveTree() throws IOException {
Tree t = new Tree ();
t.add("e914baad1d20942535ed0ac3857b43208e3d6bab");
t.remove("e914baad1d20942535ed0ac3857b43208e3d6bab");
// assertFalse(t.contains("e914baad1d20942535ed0ac3857b43208e3d6bab"));
}
// Gets contents of file and checks if it matches actual contents
@Test
void testGetContents() throws IOException {
// Add data to Tree
Tree t = new Tree ();
t.add("blob : f187ae69bdfb0f1510f108534b241abaa5a4ca72 : file1.txt");
t.add("tree : fa87e7501e7a09957de75317910a08559d45878f");
t.getContents();
// Compares expected and receieved
String expectedOutput = "blob : f187ae69bdfb0f1510f108534b241abaa5a4ca72 : file1.txt\ntree : fa87e7501e7a09957de75317910a08559d45878f";
assertEquals(expectedOutput, t.printSB());
// Clean up SB
// Tree.sb = new StringBuilder();
}
// Copies over the contents and name to the objects folder
@Test
void testWriteToObjects() throws IOException {
Tree t = new Tree ();
t.add("blob : f187ae69bdfb0f1510f108534b241abaa5a4ca72 : file1.txt");
t.add("tree : fa87e7501e7a09957de75317910a08559d45878f");
t.getContents();
t.writeToObjects();
File objectsFile = new File("objects/" + Blob.Sha1(t.printSB()));
assertTrue(objectsFile.exists());
// t.sb = new StringBuilder();
// objectsFile.delete();
}
@Test
void testaddDirectoryBasic() throws IOException
{
//testcase 1
File dir1 = new File ("dir1");
dir1.mkdir();
File a = new File ("dir1/a.txt");
a.createNewFile();
PrintWriter pw = new PrintWriter (a);
pw.write("a");
pw.close();
File b = new File ("dir1/b.txt");
b.createNewFile();
PrintWriter p = new PrintWriter (b);
p.write("b");
p.close();
File c = new File ("dir1/c.txt");
c.createNewFile();
PrintWriter l = new PrintWriter (c);
l.write("c");
l.close();
Tree t = new Tree ();
t.addDirectory("dir1");
t.getContents();
t.writeToObjects();
assertTrue(t.printSB().contains("blob : 86f7e437faa5a7fce15d1ddcb9eaeaea377667b8 : /dir1/a.txt"));
assertTrue(t.printSB().contains("blob : e9d71f5ee7c92d6dc9e92ffdad17b8bd49418f98 : /dir1/b.txt"));
assertTrue(t.printSB().contains("blob : 84a516841ba77a5b4648de2cd0dfcb30ea46dbb4 : /dir1/c.txt"));
}
@Test
void testaddDirectoryAdvanced() throws IOException
{
//testcase 1
File dir1 = new File ("dir1");
dir1.mkdir();
File a = new File ("dir1/a.txt");
a.createNewFile();
PrintWriter pw = new PrintWriter (a);
pw.write("a");
pw.close();
File b = new File ("dir1/b.txt");
b.createNewFile();
PrintWriter p = new PrintWriter (b);
p.write("b");
p.close();
File c = new File ("dir1/c.txt");
c.createNewFile();
PrintWriter l = new PrintWriter (c);
l.write("c");
l.close();
File d = new File ("dir1/d");
d.mkdir();
File e = new File ("dir1/e");
e.mkdir();
File f = new File ("dir1/d/f.txt");
f.createNewFile();
PrintWriter bruh = new PrintWriter (f);
bruh.write("f");
bruh.close();
Tree t = new Tree ();
t.addDirectory("dir1");
t.getContents();
t.writeToObjects();
assertTrue(t.printSB().contains("blob : 86f7e437faa5a7fce15d1ddcb9eaeaea377667b8 : /dir1/a.txt"));
assertTrue(t.printSB().contains("blob : e9d71f5ee7c92d6dc9e92ffdad17b8bd49418f98 : /dir1/b.txt"));
assertTrue(t.printSB().contains("blob : 84a516841ba77a5b4648de2cd0dfcb30ea46dbb4 : /dir1/c.txt"));
assertTrue(t.printSB().contains("tree : da39a3ee5e6b4b0d3255bfef95601890afd80709"));
// assertTrue(t.printSB().contains("blob : 4a0a19218e082a343a1b17e5333409af9d98f0f5 : /dir1/d/f.txt"));
}
}