-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIndexTester.java
More file actions
161 lines (146 loc) · 4.79 KB
/
IndexTester.java
File metadata and controls
161 lines (146 loc) · 4.79 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
import java.io.File;
import java.io.FileWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
public class IndexTester {
public static void main(String[] args) {
try {
boolean prepOk = prepareSamples();
if (prepOk == false) {
System.out.println("Prepare samples: FAIL");
return;
} else {
System.out.println("Prepare samples: PASS");
}
Index idx = new Index();
idx.add("s1.txt");
idx.add("s2.txt");
idx.add("s3.txt");
List<String> lines = readIndexLines();
boolean check1 = verifyEntry(lines, "s1.txt");
boolean check2 = verifyEntry(lines, "s2.txt");
boolean check3 = verifyEntry(lines, "s3.txt");
if (check1 == true) {
System.out.println("s1.txt indexed: PASS");
} else {
System.out.println("s1.txt indexed: FAIL");
}
if (check2 == true) {
System.out.println("s2.txt indexed: PASS");
} else {
System.out.println("s2.txt indexed: FAIL");
}
if (check3 == true) {
System.out.println("s3.txt indexed: PASS");
} else {
System.out.println("s3.txt indexed: FAIL");
}
boolean all = false;
if (check1 == true && check2 == true && check3 == true) {
all = true;
} else {
all = false;
}
if (all == true) {
System.out.println("All index checks: PASS");
} else {
System.out.println("All index checks: FAIL");
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static boolean prepareSamples() {
try {
boolean ok = makeFile("s1.txt", "alpha\nline2\n");
if (ok == false) {
return false;
}
ok = makeFile("s2.txt", "beta\nline2\nline3\n");
if (ok == false) {
return false;
}
ok = makeFile("s3.txt", "gamma\n");
if (ok == false) {
return false;
}
return true;
} catch (Exception e) {
return false;
}
}
private static boolean makeFile(String name, String content) {
try {
File f = new File(name);
FileWriter w = new FileWriter(f, false);
w.write(content);
w.flush();
w.close();
return true;
} catch (Exception e) {
return false;
}
}
private static List<String> readIndexLines() {
try {
File idx = new File("git" + File.separator + "index");
if (idx.exists()) {
return Files.readAllLines(idx.toPath(), StandardCharsets.UTF_8);
} else {
return new ArrayList<String>();
}
} catch (Exception e) {
return new ArrayList<String>();
}
}
private static boolean verifyEntry(List<String> lines, String fileName) {
try {
String hashFromIndex = findHashFor(lines, fileName);
if (hashFromIndex == null) {
return false;
}
File src = new File(fileName);
String recomputed = Blob.sha1FromFile(src);
if (recomputed.equals(hashFromIndex)) {
File obj = new File("git" + File.separator + "objects" + File.separator + hashFromIndex);
if (obj.exists()) {
if (obj.isFile()) {
return true;
} else {
return false;
}
} else {
return false;
}
} else {
return false;
}
} catch (Exception e) {
return false;
}
}
private static String findHashFor(List<String> lines, String fileName) {
int i = 0;
while (i < lines.size()) {
String line = lines.get(i);
if (line != null) {
int space = line.indexOf(' ');
if (space != -1) {
String left = line.substring(0, space);
String right = line.substring(space + 1);
if (right.equals(fileName)) {
return left;
}
} else {
if (line.equals(fileName)) {
return "";
}
}
}
i = i + 1;
}
return null;
}
}