src/test/java/de/uapcore/sudoku/DocumentHandlerTest.java

Wed, 26 Aug 2020 19:09:07 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 26 Aug 2020 19:09:07 +0200
changeset 25
569220009c54
parent 21
11cb2d9413d4
permissions
-rw-r--r--

fixes wrong call of assertEquals()

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

mercurial