Tue, 28 Jul 2020 14:45:06 +0200
adds target/ to .hgignore
20 | 1 | package de.uapcore.sudoku; |
2 | ||
3 | import org.junit.jupiter.api.Test; | |
4 | ||
5 | import java.io.File; | |
6 | import java.io.FileOutputStream; | |
7 | import java.io.IOException; | |
8 | import java.nio.file.Files; | |
9 | ||
10 | import static org.junit.jupiter.api.Assertions.*; | |
11 | ||
12 | class DocumentHandlerTest { | |
13 | ||
14 | @Test | |
15 | void testFilenameManagement() { | |
16 | final var dh = new DocumentHandler(); | |
17 | assertFalse(dh.isFilenameSet()); | |
18 | dh.setFilename("foo"); | |
19 | assertTrue(dh.isFilenameSet()); | |
20 | dh.clearFilename(); | |
21 | assertFalse(dh.isFilenameSet()); | |
22 | } | |
23 | ||
24 | private File extractFile(String name) throws IOException { | |
25 | final var f = File.createTempFile("sudoku-test", ".txt"); | |
26 | try (var in = DocumentHandlerTest.class.getClassLoader().getResourceAsStream(name); | |
27 | var out = new FileOutputStream(f)) { | |
28 | in.transferTo(out); | |
29 | } | |
30 | return f; | |
31 | } | |
32 | ||
33 | private int[] createTestdata() { | |
34 | return new int[]{0,1,0,9,0,0,8,0,0, | |
35 | 0,0,0,0,0,8,0,0,4, | |
36 | 6,0,5,0,0,0,7,0,0, | |
37 | 0,9,0,0,6,0,0,0,8, | |
38 | 0,0,0,2,0,7,0,0,0, | |
39 | 8,0,0,0,3,0,0,6,0, | |
40 | 0,0,2,0,0,0,5,0,3, | |
41 | 1,0,0,4,0,0,0,0,0, | |
42 | 0,0,6,0,0,2,0,1,0}; | |
43 | } | |
44 | ||
45 | @Test | |
46 | void load() throws IOException { | |
47 | final var f = extractFile("test-sudoku"); | |
48 | final var dh = new DocumentHandler(); | |
49 | final var field = new Field(); | |
50 | dh.setFilename(f.getAbsolutePath()); | |
51 | dh.load(field); | |
52 | ||
53 | final var expected = createTestdata(); | |
54 | for (int x = 0 ; x < 9 ; x++) { | |
55 | for (int y = 0 ; y < 9 ; y++) { | |
56 | assertEquals(expected[x+9*y], field.getCellValue(x, y)); | |
57 | } | |
58 | } | |
59 | } | |
60 | ||
61 | @Test | |
62 | void save() throws IOException { | |
63 | final var f = File.createTempFile("sudoku-test", ".txt"); | |
64 | final var dh = new DocumentHandler(); | |
65 | ||
66 | final var field = new Field(); | |
21
11cb2d9413d4
DocumentHandlerTest tests if modified state is reset
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
67 | field.setAllCellsModified(true); |
11cb2d9413d4
DocumentHandlerTest tests if modified state is reset
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
68 | assertTrue(field.isAnyCellModified()); |
11cb2d9413d4
DocumentHandlerTest tests if modified state is reset
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
69 | |
20 | 70 | final var testdata = createTestdata(); |
71 | for (int x = 0 ; x < 9 ; x++) { | |
72 | for (int y = 0 ; y < 9 ; y++) { | |
73 | field.setCellValue(x, y, testdata[x+9*y]); | |
74 | } | |
75 | } | |
76 | ||
77 | dh.setFilename(f.getAbsolutePath()); | |
78 | dh.save(field); | |
79 | ||
80 | final String[] expected = { | |
81 | "_ 1 _ 9 _ _ 8 _ _", | |
82 | "_ _ _ _ _ 8 _ _ 4", | |
83 | "6 _ 5 _ _ _ 7 _ _", | |
84 | "_ 9 _ _ 6 _ _ _ 8", | |
85 | "_ _ _ 2 _ 7 _ _ _", | |
86 | "8 _ _ _ 3 _ _ 6 _", | |
87 | "_ _ 2 _ _ _ 5 _ 3", | |
88 | "1 _ _ 4 _ _ _ _ _", | |
89 | "_ _ 6 _ _ 2 _ 1 _" | |
90 | }; | |
91 | ||
92 | final var actual = Files.readAllLines(f.toPath()); | |
93 | ||
94 | assertEquals(expected.length, actual.size()); | |
95 | for (int i = 0 ; i < expected.length ; i++) { | |
96 | assertEquals(expected[i], actual.get(i)); | |
97 | } | |
21
11cb2d9413d4
DocumentHandlerTest tests if modified state is reset
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
98 | |
11cb2d9413d4
DocumentHandlerTest tests if modified state is reset
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
99 | assertFalse(field.isAnyCellModified()); |
20 | 100 | } |
101 | } |