-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyFileWriterTester.java
More file actions
143 lines (137 loc) · 5.54 KB
/
MyFileWriterTester.java
File metadata and controls
143 lines (137 loc) · 5.54 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
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
public class MyFileWriterTester {
public static void main(String[] args) {
//MyFileWriter.generateHiddenFile("hi", ".myFileFun");
//MyFileWriter.generateRegularFile();
// try {
// String answer = MyFileWriter.stringify("regularFile");
// System.out.println(answer);
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//originalTestHash();
//testHashFileEmptyFiles();
//testHashFileSpecialChars();
//testHashFileDNE();
testHashFilesLarge();
}
//returned the same hash, passed for a normal file
public static void originalTestHash(){
try{
File tempFile = File.createTempFile("tempSpecial", ".txt");
String specialString = "hi!";
Files.write(tempFile.toPath(), specialString.getBytes(StandardCharsets.UTF_8));
String hash = MyFileWriter.hashFile(tempFile.getAbsolutePath());
String expectedHash = "C0DDD62C7717180E7FFB8A15BB9674D3EC92592E0B7AC7D1D5289836B4553BE2";
System.out.println("Regular File Test");
System.out.println("Expected: " + expectedHash);
System.out.println("Actual: " + hash);
if(hash.equals(expectedHash)){
System.out.println("Passed!");
}
else{
System.out.println("Failed!");
}
tempFile.delete();
}
catch (IOException e) {
System.err.println("something happened..." + e.getMessage());
}
}
//worked as expected, returned true for empty files!
public static void testHashFileEmptyFiles(){
try{
File tempFile = File.createTempFile("temporary", ".txt");
String hash = MyFileWriter.hashFile(tempFile.getAbsolutePath());
String expectedHash = "E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855";
System.out.println("Empty File Test");
System.out.println("Expected: " + expectedHash);
System.out.println("Actual: " + hash);
if(hash.equals(expectedHash)){
System.out.println("Passed!");
}
else{
System.out.println("Failed!");
}
tempFile.delete();
}
catch (Exception e) {
System.err.println("something happened..." + e.getMessage());
}
}
//returned the same hash and work completely, this is for special characters
public static void testHashFileSpecialChars(){
try{
File tempFile = File.createTempFile("tempSpecial", ".txt");
String specialString = "⋆。‧˚ʚ🍓ɞ˚‧。⋆";
Files.write(tempFile.toPath(), specialString.getBytes(StandardCharsets.UTF_8));
String hash = MyFileWriter.hashFile(tempFile.getAbsolutePath());
String expectedHash = "DDDCE16B1228DCF37574C7523D8E7121B8DDFDA8237D97F28727A13006F5FB10";
System.out.println("Special File Test");
System.out.println("Expected: " + expectedHash);
System.out.println("Actual: " + hash);
if(hash.equals(expectedHash)){
System.out.println("Passed!");
}
else{
System.out.println("Failed!");
}
tempFile.delete();
}
catch (IOException e) {
System.err.println("something happened..." + e.getMessage());
}
}
//returned the same hash and work completely, this is for large files
public static void testHashFilesLarge(){
try{
File tempFile = new File("tempSpecial.txt");
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(tempFile))) {
for (int i = 0; i < 10000; i++) {
bufferedWriter.write("hello my name is ellika, have fun making a big file. \n");
}
}
String hash = MyFileWriter.hashFile(tempFile.getAbsolutePath());
String expectedHash = "F45D4857A44ED2CE6D603EF79DA42AB1F7A815BEFA438DDFF74B92F2AAFEBCB6";
System.out.println("Large File Test");
System.out.println("Expected: " + expectedHash);
System.out.println("Actual: " + hash);
if(hash.equalsIgnoreCase(expectedHash)){
System.out.println("Passed!");
}
else{
System.out.println("Failed!");
}
tempFile.delete();
}
catch (Exception e) {
System.err.println("something happened..." + e.getMessage());
}
}
//this was on a file that doesnt exist and it threw the right error message
public static void testHashFileDNE(){
try{
String fake = "dne.txt";
String hash = MyFileWriter.hashFile(fake);
String expectedHash = null;
System.out.println("DNE File Test");
System.out.println("Expected: " + expectedHash);
System.out.println("Actual: " + hash);
if(hash.equals(expectedHash)){
System.out.println("Passed!");
}
else{
System.out.println("Failed!");
}
}
catch (Exception e) {
System.err.println("something happened..." + e.getMessage());
}
}
}