-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyFileWriterTest.java
More file actions
64 lines (57 loc) · 2.28 KB
/
MyFileWriterTest.java
File metadata and controls
64 lines (57 loc) · 2.28 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
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
public class MyFileWriterTest {
public static void testHashFileKnown() throws IOException {
Path known = Files.createTempFile("known", ".txt");
Files.write(known, "abc".getBytes(StandardCharsets.UTF_8));
String hash = MyFileWriter.hashFile(known.toString());
System.out.println("Known test (abc): " + hash);
}
public static void testHashFileEmptyFiles() throws IOException {
Path empty = Files.createTempFile("empty", ".txt");
String hash = MyFileWriter.hashFile(empty.toString());
System.out.println("Empty file: " + hash);
}
public static void testHashFileLargeFiles() throws IOException {
Path large = Files.createTempFile("large", ".bin");
byte[] block = new byte[1024];
for (int i = 0; i < 1024; i++) {
block[i] = (byte) (i & 0xff);
}
for (int i = 0; i < 1000; i++) {
Files.write(large, block, java.nio.file.StandardOpenOption.APPEND);
}
String hash = MyFileWriter.hashFile(large.toString());
System.out.println("Large file: " + hash);
}
public static void testHashFileSpecialChars() throws IOException {
Path special = Files.createTempFile("special", ".txt");
String content = "漢字かなカナ";
Files.write(special, content.getBytes(StandardCharsets.UTF_8));
String hash = MyFileWriter.hashFile(special.toString());
System.out.println("Special chars: " + hash);
}
public static void testHashFileNonExistent() {
try {
MyFileWriter.hashFile("this_file_does_not_exist.txt");
} catch (FileNotFoundException e) {
System.out.println("Non-existent file: exception thrown as expected");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
testHashFileKnown();
testHashFileEmptyFiles();
testHashFileLargeFiles();
testHashFileSpecialChars();
testHashFileNonExistent();
} catch (IOException e) {
e.printStackTrace();
}
}
}