-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTreeTest.java
More file actions
265 lines (200 loc) · 8.99 KB
/
TreeTest.java
File metadata and controls
265 lines (200 loc) · 8.99 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import static org.junit.Assert.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Path;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import Utilities.FileUtils;
public class TreeTest {
@BeforeAll
static void setUpBeforeClass() throws Exception {
FileUtils.createFile("file1");
FileUtils.writeFile("file1", "derpderpderp");
//initializes an index file + adds blobs to it?
Index ind = new Index();
ind.initialize();
ind.addBlob("file1");
}
@AfterAll
static void tearDownAfterClass() throws Exception
{
File tree = new File("./tree");
tree.delete();
FileUtils.deleteFile("file1");
FileUtils.deleteDirectory("./objects");
}
// @Test
// void addSomeText() throws Exception
// {
// Tree test = new Tree();
// String cont = "";
// String ryanzc = "Not to have an empty file";
// Utils.writeToFile("/Users/ryancheng/p/Honors Topics/programminggit/tree", ryanzc );
// BufferedReader br = new BufferedReader(new FileReader("/Users/ryancheng/p/Honors Topics/programminggit/tree"));
// while(br.ready())
// {
// cont += "" + br.readLine();
// }
// br.close();
// assertTrue(cont.contains(ryanzc));
// }
@Test
void testAddTree() throws Exception
{
Tree test = new Tree();
String input = "blob : 732d12f7e4f2e629e2954acbb720c32c0be985d1 : file1";
test.addToTree(input);
//check if the above string is in test file
File file = new File("./tree");
//Path path = Paths.get("objects");
//TEST CONTENTS: test if the "blob : sha1 : fileName" is in there
//ACTUALLY just test if all contents are the same? how?? IDK!!!!!!
String contents = "";
BufferedReader br = new BufferedReader(new FileReader(file));
while(br.ready()) {
contents += "" + br.readLine();
}
br.close();
//File contents of Tree contain input
//assertTrue(contents.contains(input));
System.out.println("contents" +contents.trim());
System.out.println("input: " + input);
//System.out.println(input);
assertTrue(contents.trim().contains(input.trim()));
}
@Test
void testAddTreeIfAlreadyTree() throws Exception {
Tree test = new Tree();
String input = "blob : 732d12f7e4f2e629e2954acbb720c32c0be985d1 : file1";
test.addToTree(input);
test.addToTree(input);
File file = new File("./tree");
int inputCounter = 0;
BufferedReader br = new BufferedReader(new FileReader(file));
while(br.ready()) {
if(input.equals(br.readLine())) {
inputCounter++;
}
}
br.close();
assertTrue(inputCounter<=1);
}
@Test
void testAddToObjects() throws Exception {
File tree = new File("./tree");
tree.delete();
Tree test = new Tree();
String input = "blob : 732d12f7e4f2e629e2954acbb720c32c0be985d1 : file1";
test.addToTree(input);
test.saveToObjects();
File file = new File ("./objects/327426a7b35dc90762e335b17ea59ecfaec45e16");
assertTrue(file.exists());
}
@Test
void testAddDirectory () throws Exception
{
//first create some files that we can use
File subDir = new File("./test1");
subDir.mkdirs();
FileWriter fw = new FileWriter("./test1/examplefile1.txt");
fw.write("the sha of this is ... ?");
fw.close();
fw = new FileWriter("./test1/examplefile2.txt");
fw.write("zomg wut are u doing. LAWL");
fw.close();
fw = new FileWriter("./test1/examplefile3.txt");
fw.write("LOL please dont read this. Good job being thorough tho!");
fw.close();
Tree t = new Tree();
t.addDirectory("./test1");
t.saveToObjects();
BufferedReader bufferedReader = new BufferedReader (new FileReader ("./objects/" + t.getEncryption()));
StringBuilder stringBuilder = new StringBuilder();
while (bufferedReader.ready())
{
stringBuilder.append((char) bufferedReader.read());
}
bufferedReader.close();
String contents = stringBuilder.toString();
assertTrue("tree is missing expected files", contents.contains("./test1/examplefile1.txt"));
assertTrue("tree did not hash correctly", contents.contains("6cecd98f685b1c9bfce96f2bbf3f8f381bcc717e"));
}
// @Test
// public void testCommitFileDeletionsAndEdits()
// {
// Index index = new Index();
// // Simulate file edits and deletions
// // index.editBlob("file1.txt", "newfile1sha1");
// index.editBlob("file1.txt");
// index.deleteBlob("file2.txt");
// // Create a commit with the updated index
// Tree tree = new Tree(index);
// //Commit commit = new Commit("commit message", "author", tree);
// Commit commit = new Commit("commit message", "author", "newfile1sha1");
// // Assertions to validate the Tree of the commit
// assertNull(commit.getMyTree().getFileSHA1("file2.txt")); // File2 should not exist
// assertEquals("newfile1sha1", commit.getMyTree().getFileSHA1("file1.txt")); // File1 should have the new SHA1
// // Continue with further assertions and more commit tests...
// }
// @Test
// public void testCommitWithFileDeletionsAndEdits() throws Exception {
// // Instantiate the Index class (assuming you have methods to handle edits and deletions)
// Index index = new Index();
// index.editBlob("file1.txt", "newfile1sha1");
// index.deleteBlob("file2.txt");
// // Create a tree using the updated index
// Tree tree = new Tree(index);
// // Simulate creating a commit by providing author, commit message, and parent commit hash
// Commit commit = new Commit("Author Name", "Commit message", "parentSHA1Hash", tree.getEncryption());
// // Write the commit (which should also write the tree)
// commit.saveCommit();
// // Now, we retrieve the tree SHA1 from the commit to check if the changes are reflected
// String treeSHA1 = commit.getParentSHA1();
// // Load the saved tree using its SHA1 (assuming you have such a constructor or method)
// Tree savedTree = new Tree(); // Replace with actual constructor/method that takes SHA1 to load tree
// savedTree.loadFromSHA1(treeSHA1);
// // Assertions
// // Assuming you have a method 'getFileSHA1' in the Tree class to get file's SHA1
// assertNull(savedTree.getFileSHA1("file2.txt")); // file2.txt should have been deleted
// assertEquals("newfile1sha1", savedTree.getFileSHA1("file1.txt")); // file1.txt should have the new SHA1
// // Check if the parent SHA1 is set correctly in the commit
// assertEquals("parentSHA1Hash", commit.getParentSHA1());
// // ... and continue with more tests for different commit scenarios.
// }
@Test
public void testCommitFileDeletionsAndEdits() throws Exception
{
// Assuming Index and Tree classes exist
Index index = new Index();
// index.editBlob("file1.txt", "newfile1sha1");
index.editBlob("./file1.txt");
index.deleteBlob("./file2.txt");
// Assuming that the Tree class has a constructor that accepts an Index object
Tree tree = new Tree(index);
Commit commit = new Commit("Author Name", "Commit message");
// Write the commit (which should also write the tree)
commit.setMyTree(tree); // Let's assume we need to associate the Tree with the Commit
commit.writeToFile();
// we read the tree from the commit to ensure that the changes are there
// this would require a method in Commit to get the associated Tree object based on SHA1
Tree savedTree = new Tree(); // This would need to be the Tree loaded from SHA1
savedTree.loadFromSHA1("./objects/" + commit.getTreeSHA1FileLocation());
// Assertions would depend on how Tree is implemented to check for file existence
assertFalse(savedTree.hasFile("./file2.txt")); // File2 should not exist after deletion
assertTrue(savedTree.hasFile("./file1.txt")); // File1 should exist
}
@Test
public void testTreeTraverse() throws Exception{
File tree = new File("./tree");
tree.delete();
Tree test = new Tree();
String input = "blob : 732d12f7e4f2e629e2954acbb720c32c0be985d1 : file1";
test.addToTree(input);
test.saveToObjects();
assertEquals(Tree.traverseForFile("file1", test.getEncryption()).size(), 0);
}
}