-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCommitTest.java
More file actions
469 lines (436 loc) · 27.2 KB
/
CommitTest.java
File metadata and controls
469 lines (436 loc) · 27.2 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
//import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Paths;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
public class CommitTest {
@AfterAll
static void cleanup() throws IOException
{
File cleanObj = new File("./objects");
cleanObj.mkdir();
for (File f : cleanObj.listFiles())
{
f.delete();
}
File f1 = new File("file1");
File f2 = new File("file2");
File f3 = new File("file3");
File f4 = new File("file4");
File f5 = new File("file5");
File f6 = new File("file6");
File f7 = new File("file7");
File f8 = new File("file8");
f1.delete();
f2.delete();
f3.delete();
f4.delete();
f5.delete();
f6.delete();
f7.delete();
f8.delete();
}
@Test
void testConstructor() throws Exception {
Commit testCom = new Commit("", "Bob", "this is a test");
String sha = testCom.getSha();
String dirName = "./objects/";
File dir = new File (dirName);
File check = new File(dir,sha);
//how do i check if time keeps changing?
assertTrue(check.exists());
}
@Test
void testConstructor2() throws Exception {
Index ind = new Index();
Commit parentCom = new Commit("Bob", "this is a test");
String shaOfParent = parentCom.getSha();
String parentCommitContents = ind.fileToString("./objects/" + shaOfParent);
int i = parentCommitContents.lastIndexOf("\n");
int j = Commit.ordinalIndexOf(parentCommitContents, "\n", 4);
Commit commitWithPrevCommit = new Commit(shaOfParent, "Wyatt", "this is not a test");
// Testing if the childSha is updated for the Parent
// Read thu parent commit
// Get third line
// Verify theres actually a SHA there
parentCommitContents = ind.fileToString("./objects/" + shaOfParent);
//System.out.println(sha);
String dirName = "./objects/";
File dir = new File (dirName);
File check = new File(dir,shaOfParent);
//how do i check if time keeps changing?
//File f = new File(testCom.getSha());
assertTrue(check.exists());
//assertTrue("d5af8be8c2a1c1163e51f44e8631247217b0e4ca".equals(parentCom.encryptPassword(parentCom.getContents((f)))));
}
private static void createFile(String directoryPath, String fileContents) throws IOException {
File file = new File(directoryPath);
file.createNewFile();
FileWriter fw = new FileWriter(directoryPath);
PrintWriter pw = new PrintWriter(fw);
pw.print(fileContents);
fw.close();
pw.close();
}
@Test
void testCreatingCommit1() throws Exception {
File file1 = new File("file1");
createFile("file1", "fileContents1");
File file2 = new File("file2");
createFile("file2", "fileContents2");
Index index = new Index();
index.init();
index.addBlob("file1");
index.addBlob("file2");
Commit c0 = new Commit("Wyatt", "this is a test");
String shaOfFile1 = c0.encryptPassword(index.fileToString("file1"));
String shaOfFile2 = c0.encryptPassword(index.fileToString("file2"));
String contentsOfTree = "blob : " + shaOfFile2 + " : file2" + "\n" + "blob : " + shaOfFile1 + " : file1";
String shaOfTree = c0.encryptPassword(contentsOfTree);
Date date = new Date(java.lang.System.currentTimeMillis());
String commitContents = shaOfTree + "\n" + "\n" + "\n" + "Wyatt" + "\n" + date + "\n" + "this is a test";
String shaOfCommit = c0.encryptPassword(commitContents);
File fileForTree = new File("./objects/" + shaOfTree);
File fileForCommit = new File("./objects/" + shaOfCommit);
assertTrue(fileForTree.exists());
assertEquals(index.fileToString("./objects/" + shaOfTree),contentsOfTree);
assertTrue(fileForCommit.exists());
assertEquals(index.fileToString("./objects/" + shaOfCommit),commitContents);
}
@Test
void testCreatingCommitWithFilesAndFolder() throws Exception {
File file1 = new File("file1");
createFile("file1", "file1Contents");//got this from addDirectoryTest.java
File file2 = new File("file2");
createFile("file2", "file2Contents");
Index ind = new Index();
ind.init();
Date date = new Date(java.lang.System.currentTimeMillis());
ind.addBlob("file1");
ind.addBlob("file2");
Commit c0 = new Commit("Wyatt", "oh wow it works maybe????");
File file3 = new File("file3");
createFile("file3", "file3Contents");
File file4 = new File("file4");
createFile("file4", "file4Contents");
File dir = new File("dir");
dir.mkdir();
Index ind2 = new Index();
ind2.init();
ind2.addBlob("file3");
ind2.addBlob("file4");
ind2.addBlob("dir");
Commit c1 = new Commit(c0.getSha(), "Wonka", "Willy chocolate");
String file1Sha = c0.encryptPassword(ind.fileToString("file1"));
String file2Sha = c0.encryptPassword(ind.fileToString("file2"));
String treeContents = "blob : " + file2Sha + " : file2" + "\n" + "blob : " + file1Sha + " : file1";
String treeSha = c0.encryptPassword(treeContents);
String commitContents = treeSha + "\n" + "\n" + "\n" + "Wyatt" + "\n" + date + "\n" + "oh wow it works maybe????";
String newCommitContents = treeSha + "\n" + "\n" + c1.getSha() + "\n" + "Wyatt" + "\n" + date + "\n" + "oh wow it works maybe????";
String commitSha = c0.encryptPassword(commitContents);
File treeFile = new File("./objects/" + treeSha);
File commitFile = new File("./objects/" + commitSha);
String file3Sha = c1.encryptPassword(ind.fileToString("file3"));
String file4Sha = c1.encryptPassword(ind.fileToString("file4"));
String treeContents1 = "tree : " + treeSha + "\n" + "blob : " + file4Sha + " : file4" + "\n" + "blob : " + file3Sha + " : file3" + "\n" + "tree : da39a3ee5e6b4b0d3255bfef95601890afd80709 : dir";
String treeSha1 = c1.encryptPassword(treeContents1);
String newCommitContents1 = treeSha1 + "\n" + c0.getSha() + "\n" + "\n" + "Wonka" + "\n" + date+ "\n" + "Willy chocolate";
String newCommitSha1 = c0.encryptPassword(newCommitContents1);
File treeFile2 = new File("./objects/" + treeSha1);
File commitFile2 = new File("./objects/" + newCommitSha1);
assertTrue("tree doesn't exist", treeFile.exists());
assertEquals("contents of tree wrong", ind.fileToString("./objects/" + treeSha), treeContents);
assertTrue("commit1 doesn't exist", commitFile.exists());
assertEquals("contents of this commit wrong", ind.fileToString("./objects/" + commitSha), newCommitContents);
assertTrue("tree doesn't exist", treeFile2.exists());
assertEquals("contents of tree wrong", ind.fileToString("./objects/" + treeSha1), treeContents1);
assertTrue("commit2 doesn't exist", commitFile2.exists());
assertEquals("contents of this commit wrong", ind.fileToString("./objects/" + newCommitSha1), newCommitContents1);
}
@Test
void testCreatingFourCommits() throws Exception {
Date date = new Date(java.lang.System.currentTimeMillis());
File file1 = new File("file1");
createFile("file1", "fileContents1");
File file2 = new File("file2");
createFile("file2", "fileContents2");
Index ind = new Index();
ind.init();
ind.addBlob("file1");
ind.addBlob("file2");
Commit firstCommit = new Commit("Wyatt", "first commit");
File file3 = new File("file3");
createFile("file3", "fileContents3");
File file4 = new File("file4");
createFile("file4", "fileContents4");
File dir = new File("dir");
dir.mkdir();
Index ind2 = new Index();
ind2.init();
ind2.addBlob("file3");
ind2.addBlob("file4");
ind2.addBlob("dir");
Commit commit1 = new Commit(firstCommit.getSha(), "Wyatt", "second commit");
File f1 = new File("index");
f1.delete();
f1.createNewFile();
//ind.removeBlob("dir");
System.out.println(ind.fileToString("index"));
File file5 = new File("file5");
createFile("file5", "fileContents5");
File file6 = new File("file6");
createFile("file6", "fileContents6");
new FileWriter("./index", false).close();
Index ind3 = new Index();
ind3.init();
ind3.addBlob("file5");
ind3.addBlob("file6");
Commit commit2 = new Commit(commit1.getSha(), "Wyatt", "third commit");
File f2 = new File("index");
f2.delete();
f2.createNewFile();
File file7 = new File("file7");
createFile("file7", "fileContents7");
File file8 = new File("file8");
createFile("file8", "fileContents8");
File dir2 = new File("dir2");
dir2.mkdir();
Index ind4 = new Index();
ind4.init();
ind4.addBlob("file7");
ind4.addBlob("file8");
ind4.addBlob("dir2");
Commit commit3 = new Commit(commit2.getSha(), "Wyatt", "fourth commit");
File f3 = new File("index");
f3.delete();
f3.createNewFile();
//
/*String shaOfParent = parentCom.getSha();
String parentCommitContents = ind.fileToString("./objects/" + shaOfParent);
int i = parentCommitContents.lastIndexOf("\n");
int j = Commit.ordinalIndexOf(parentCommitContents, "\n", 4);
Commit commitWithPrevCommit = new Commit(shaOfParent, "Wyatt", "this is not a test");
String shaOfChild = commitWithPrevCommit.getSha();
Commit thirdCommit = new Commit(shaOfChild, "Willy", "explosions");
// Testing if the childSha is updated for the Parent
// Read thu parent commit
// Get third line
// Verify theres actually a SHA there
parentCommitContents = ind.fileToString("./objects/" + shaOfParent);
*/
String shaOfFirstFile = firstCommit.encryptPassword(ind.fileToString("file1"));
String shaOfSecondFile = firstCommit.encryptPassword(ind2.fileToString("file2"));
String contentsOfTreeForFirstCommit = "blob : " + shaOfSecondFile + " : file2" + "\n" + "blob : " + shaOfFirstFile + " : file1";
String shaOfTreeForFirstCommit = firstCommit.encryptPassword(contentsOfTreeForFirstCommit);
String contentsForFirstCommit = shaOfTreeForFirstCommit + "\n" + "\n" + "\n" + "Wyatt" + "\n" + date + "\n" + "first commit";
String contentsOfFirstCommitAfterSecondCommit = shaOfTreeForFirstCommit + "\n" + "\n" + commit1.getSha() + "\n" + "Wyatt" + "\n" + date + "\n" + "first commit";
String shaOfFirstCommit = firstCommit.encryptPassword(contentsForFirstCommit);
File fileOfTreeForFirstCommit = new File("./objects/" + shaOfTreeForFirstCommit);
File fileOfFirstCommit = new File("./objects/" + shaOfFirstCommit);
String shaOfThirdFile = firstCommit.encryptPassword(ind.fileToString("file3"));
String shaOfFourthFile = firstCommit.encryptPassword(ind.fileToString("file4"));
String contentsOfTreeForSecondCommit = "tree : " + shaOfTreeForFirstCommit + "\n" + "blob : " + shaOfFourthFile + " : file4\nblob : " + shaOfThirdFile + " : file3\ntree : da39a3ee5e6b4b0d3255bfef95601890afd80709 : dir";
String shaOfTreeForSecondCommit = firstCommit.encryptPassword(contentsOfTreeForSecondCommit);
String contentsForSecondCommit = shaOfTreeForSecondCommit + "\n" + firstCommit.getSha() + "\n" + "\n" + "Wyatt" + "\n" + date + "\n" + "second commit";
String contentsOfSecondCommitAfterThirdCommit = shaOfTreeForSecondCommit + "\n" + firstCommit.getSha() + "\n" + commit2.getSha() + "\n" + "Wyatt" + "\n" + date + "\n" + "second commit";
String shaOfSecondCommit = firstCommit.encryptPassword(contentsForSecondCommit);
File fileOfTreeForSecondCommit = new File("./objects/" + shaOfTreeForSecondCommit);
File fileOfSecondCommit = new File("./objects/" + shaOfSecondCommit);
String shaOfFifthFile = firstCommit.encryptPassword(ind.fileToString("file5"));
String shaOfSixthFile = firstCommit.encryptPassword(ind.fileToString("file6"));
String contentsOfTreeForThirdCommit = "tree : " + shaOfTreeForSecondCommit + "\n" + "blob : " + shaOfSixthFile + " : file6" + "\n" + "blob : " + shaOfFifthFile + " : file5";
String shaOfTreeForThirdCommit = firstCommit.encryptPassword(contentsOfTreeForThirdCommit);
//Tree.deleteFile(shaOfTreeForThirdCommit, "dir");
String contentsOfThirdCommit = shaOfTreeForThirdCommit + "\n" + commit1.getSha() + "\n" + "\n" + "Wyatt" + "\n" + date + "\n" + "third commit";
String contentsOfThirdCommitAfterFourthCommit = shaOfTreeForThirdCommit + "\n" + commit1.getSha() + "\n" + commit3.getSha() + "\n" + "Wyatt" + "\n" + date + "\n" + "third commit";
String shaOfThirdCommit = firstCommit.encryptPassword(contentsOfThirdCommit);
File fileOfTreeForThirdCommit = new File("./objects/" + shaOfTreeForThirdCommit);
File fileOfThirdCommit = new File("./objects/" + shaOfThirdCommit);
String shaOfSeventhFile = firstCommit.encryptPassword(ind.fileToString("file7"));
String shaOfEighthFile = firstCommit.encryptPassword(ind.fileToString("file8"));
String contentsOfTreeForFourthCommit = "tree : " + shaOfTreeForThirdCommit + "\n" + "blob : " + shaOfEighthFile + " : file8\nblob : " + shaOfSeventhFile + " : file7" + "\n" + "tree : da39a3ee5e6b4b0d3255bfef95601890afd80709 : dir2";
String shaOfTreeForFourthCommit = firstCommit.encryptPassword(contentsOfTreeForFourthCommit);
String contentsOfFourthCommit = shaOfTreeForFourthCommit + "\n" + commit2.getSha() + "\n" + "\n" + "Wyatt" + "\n" + date + "\n" + "fourth commit";
String shaOfFourthCommit = firstCommit.encryptPassword(contentsOfFourthCommit);
File fileOfTreeForFourthCommit = new File("./objects/" + shaOfTreeForFourthCommit);
File fileOfFourthCommit = new File("./objects/" + shaOfFourthCommit);
assertEquals(ind.fileToString("./objects/" + shaOfTreeForFirstCommit), contentsOfTreeForFirstCommit);
assertEquals(ind.fileToString("./objects/" + shaOfFirstCommit), contentsOfFirstCommitAfterSecondCommit);
assertEquals(ind.fileToString("./objects/" + shaOfTreeForSecondCommit), contentsOfTreeForSecondCommit);
assertEquals(ind.fileToString("./objects/" + shaOfSecondCommit), contentsOfSecondCommitAfterThirdCommit);
assertEquals((ind.fileToString("./objects/" + shaOfTreeForThirdCommit)), contentsOfTreeForThirdCommit);
assertEquals(ind.fileToString("./objects/" + shaOfThirdCommit), contentsOfThirdCommitAfterFourthCommit);
assertEquals(ind.fileToString("./objects/" + shaOfTreeForFourthCommit), contentsOfTreeForFourthCommit);
assertEquals(ind.fileToString("./objects/" + shaOfFourthCommit), contentsOfFourthCommit);
}
@Test
void testWithFiveCommitsDelete() throws Exception {
Date date = new Date(java.lang.System.currentTimeMillis());
File file1 = new File("file1");
createFile("file1", "fileContents1");
File file2 = new File("file2");
createFile("file2", "fileContents2");
Index ind = new Index();
ind.init();
ind.addBlob("file1");
ind.addBlob("file2");
Commit firstCommit = new Commit("Wyatt", "first commit");
File file3 = new File("file3");
createFile("file3", "fileContents3");
File file4 = new File("file4");
createFile("file4", "fileContents4");
File dir = new File("dir");
dir.mkdir();
Index ind2 = new Index();
ind2.init();
ind2.addBlob("file3");
ind2.addBlob("file4");
ind2.addBlob("dir");
Commit commit1 = new Commit(firstCommit.getSha(), "Wyatt", "second commit");
File f1 = new File("index");
f1.delete();
f1.createNewFile();
ind.removeBlob("dir");
System.out.println(ind.fileToString("index"));
File file5 = new File("file5");
createFile("file5", "fileContents5");
File file6 = new File("file6");
createFile("file6", "fileContents6");
new FileWriter("./index", false).close();
Index ind3 = new Index();
ind3.init();
ind3.addBlob("file5");
ind3.addBlob("file6");
ind3.removeBlob("file2");
Commit commit2 = new Commit(commit1.getSha(), "Wyatt", "third commit");
File f2 = new File("index");
f2.delete();
f2.createNewFile();
File file7 = new File("file7");
createFile("file7", "fileContents7");
File file8 = new File("file8");
createFile("file8", "fileContents8");
File dir2 = new File("dir2");
dir2.mkdir();
Index ind4 = new Index();
ind4.init();
ind4.addBlob("file7");
ind4.addBlob("file8");
ind4.addBlob("dir2");
Commit commit3 = new Commit(commit2.getSha(), "Wyatt", "fourth commit");
File f3 = new File("index");
f3.delete();
f3.createNewFile();
//
/*String shaOfParent = parentCom.getSha();
String parentCommitContents = ind.fileToString("./objects/" + shaOfParent);
int i = parentCommitContents.lastIndexOf("\n");
int j = Commit.ordinalIndexOf(parentCommitContents, "\n", 4);
Commit commitWithPrevCommit = new Commit(shaOfParent, "Wyatt", "this is not a test");
String shaOfChild = commitWithPrevCommit.getSha();
Commit thirdCommit = new Commit(shaOfChild, "Willy", "explosions");
// Testing if the childSha is updated for the Parent
// Read thu parent commit
// Get third line
// Verify theres actually a SHA there
parentCommitContents = ind.fileToString("./objects/" + shaOfParent);
*/
String shaOfFirstFile = firstCommit.encryptPassword(ind.fileToString("file1"));
String shaOfSecondFile = firstCommit.encryptPassword(ind2.fileToString("file2"));
String contentsOfTreeForFirstCommit = "blob : " + shaOfSecondFile + " : file2" + "\n" + "blob : " + shaOfFirstFile + " : file1";
String shaOfTreeForFirstCommit = firstCommit.encryptPassword(contentsOfTreeForFirstCommit);
String contentsForFirstCommit = shaOfTreeForFirstCommit + "\n" + "\n" + "\n" + "Wyatt" + "\n" + date + "\n" + "first commit";
String contentsOfFirstCommitAfterSecondCommit = shaOfTreeForFirstCommit + "\n" + "\n" + commit1.getSha() + "\n" + "Wyatt" + "\n" + date + "\n" + "first commit";
String shaOfFirstCommit = firstCommit.encryptPassword(contentsForFirstCommit);
File fileOfTreeForFirstCommit = new File("./objects/" + shaOfTreeForFirstCommit);
File fileOfFirstCommit = new File("./objects/" + shaOfFirstCommit);
String shaOfThirdFile = firstCommit.encryptPassword(ind.fileToString("file3"));
String shaOfFourthFile = firstCommit.encryptPassword(ind.fileToString("file4"));
String contentsOfTreeForSecondCommit = "tree : " + shaOfTreeForFirstCommit + "\n" + "blob : " + shaOfFourthFile + " : file4\nblob : " + shaOfThirdFile + " : file3\ntree : da39a3ee5e6b4b0d3255bfef95601890afd80709 : dir";
String shaOfTreeForSecondCommit = firstCommit.encryptPassword(contentsOfTreeForSecondCommit);
String contentsForSecondCommit = shaOfTreeForSecondCommit + "\n" + firstCommit.getSha() + "\n" + "\n" + "Wyatt" + "\n" + date + "\n" + "second commit";
String contentsOfSecondCommitAfterThirdCommit = shaOfTreeForSecondCommit + "\n" + firstCommit.getSha() + "\n" + commit2.getSha() + "\n" + "Wyatt" + "\n" + date + "\n" + "second commit";
String shaOfSecondCommit = firstCommit.encryptPassword(contentsForSecondCommit);
File fileOfTreeForSecondCommit = new File("./objects/" + shaOfTreeForSecondCommit);
File fileOfSecondCommit = new File("./objects/" + shaOfSecondCommit);
String shaOfFifthFile = firstCommit.encryptPassword(ind.fileToString("file5"));
String shaOfSixthFile = firstCommit.encryptPassword(ind.fileToString("file6"));
String contentsOfTreeForThirdCommit = "tree : " + shaOfTreeForSecondCommit + "\n" + "blob : " + shaOfSixthFile + " : file6" + "\n" + "blob : " + shaOfFifthFile + " : file5";
String shaOfTreeForThirdCommit = firstCommit.encryptPassword(contentsOfTreeForThirdCommit);
String contentsOfThirdCommit = shaOfTreeForThirdCommit + "\n" + commit1.getSha() + "\n" + "\n" + "Wyatt" + "\n" + date + "\n" + "third commit";
String contentsOfThirdCommitAfterFourthCommit = shaOfTreeForThirdCommit + "\n" + commit1.getSha() + "\n" + commit3.getSha() + "\n" + "Wyatt" + "\n" + date + "\n" + "third commit";
String shaOfThirdCommit = firstCommit.encryptPassword(contentsOfThirdCommit);
File fileOfTreeForThirdCommit = new File("./objects/" + shaOfTreeForThirdCommit);
File fileOfThirdCommit = new File("./objects/" + shaOfThirdCommit);
String shaOfSeventhFile = firstCommit.encryptPassword(ind.fileToString("file7"));
String shaOfEighthFile = firstCommit.encryptPassword(ind.fileToString("file8"));
String contentsOfTreeForFourthCommit = "tree : " + shaOfTreeForThirdCommit + "\n" + "blob : " + shaOfEighthFile + " : file8\nblob : " + shaOfSeventhFile + " : file7" + "\n" + "tree : da39a3ee5e6b4b0d3255bfef95601890afd80709 : dir2";
String shaOfTreeForFourthCommit = firstCommit.encryptPassword(contentsOfTreeForFourthCommit);
String contentsOfFourthCommit = shaOfTreeForFourthCommit + "\n" + commit2.getSha() + "\n" + "\n" + "Wyatt" + "\n" + date + "\n" + "fourth commit";
String shaOfFourthCommit = firstCommit.encryptPassword(contentsOfFourthCommit);
File fileOfTreeForFourthCommit = new File("./objects/" + shaOfTreeForFourthCommit);
File fileOfFourthCommit = new File("./objects/" + shaOfFourthCommit);
assertEquals(ind.fileToString("./objects/" + shaOfTreeForFirstCommit), contentsOfTreeForFirstCommit);
assertEquals(ind.fileToString("./objects/" + shaOfFirstCommit), contentsOfFirstCommitAfterSecondCommit);
assertEquals(ind.fileToString("./objects/" + shaOfTreeForSecondCommit), contentsOfTreeForSecondCommit);
assertEquals(ind.fileToString("./objects/" + shaOfSecondCommit), contentsOfSecondCommitAfterThirdCommit);
assertEquals((ind.fileToString("./objects/" + shaOfTreeForThirdCommit)), contentsOfTreeForThirdCommit);
}
public static int ordinalIndexOf(String str, String substr, int n) {
int pos = str.indexOf(substr);
while (--n > 0 && pos != -1)
pos = str.indexOf(substr, pos + 1);
return pos;
}
/*
@Test
public void testConstructorThreeCommits() throws Exception
{
Index ind = new Index();
Commit parentCom = new Commit("Bob", "this is a test");
String shaOfParent = parentCom.getSha();
String parentCommitContents = ind.fileToString("./objects/" + shaOfParent);
int i = parentCommitContents.lastIndexOf("\n");
int j = Commit.ordinalIndexOf(parentCommitContents, "\n", 4);
Commit commitWithPrevCommit = new Commit(shaOfParent, "Wyatt", "this is not a test");
String shaOfChild = commitWithPrevCommit.getSha();
Commit thirdCommit = new Commit(shaOfChild, "Willy", "explosions");
// Testing if the childSha is updated for the Parent
// Read thu parent commit
// Get third line
// Verify theres actually a SHA there
parentCommitContents = ind.fileToString("./objects/" + shaOfParent);
//System.out.println(sha);
String dirName = "./objects/";
File dir = new File (dirName);
File check = new File(dir,shaOfChild);
//how do i check if time keeps changing?
//File f = new File(testCom.getSha());
assertTrue(check.exists());
//assertTrue("d5af8be8c2a1c1163e51f44e8631247217b0e4ca".equals(parentCom.encryptPassword(parentCom.getContents((f)))));
}
*/
@Test
void testCreateTree() throws IOException {
Tree tree = new Tree();
String sha = tree.getSha();
String expectedSha = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
assertTrue(sha.equals(expectedSha));
}
/*@Test
void testGetDate() {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
//um how do i test if the time keeps changing
assertTrue(timeStamp.equals("20230921_195130"));
}*/
}