adds more tests

Tue, 28 Jul 2020 13:48:59 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 28 Jul 2020 13:48:59 +0200
changeset 20
a95423d37588
parent 19
627e1c99bcf1
child 21
11cb2d9413d4

adds more tests

src/test/java/de/uapcore/sudoku/ButtonPanelTest.java file | annotate | diff | comparison | revisions
src/test/java/de/uapcore/sudoku/DocumentHandlerTest.java file | annotate | diff | comparison | revisions
src/test/java/de/uapcore/sudoku/MainMenuTest.java file | annotate | diff | comparison | revisions
src/test/java/de/uapcore/sudoku/SudokuTest.java file | annotate | diff | comparison | revisions
src/test/resources/test-sudoku file | annotate | diff | comparison | revisions
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/test/java/de/uapcore/sudoku/ButtonPanelTest.java	Tue Jul 28 13:48:59 2020 +0200
@@ -0,0 +1,42 @@
+package de.uapcore.sudoku;
+
+import org.junit.jupiter.api.Test;
+
+import javax.swing.*;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class ButtonPanelTest {
+
+    @Test
+    void testButtonsPresent() {
+        final var panel = new ButtonPanel(new ActionHandler(new Field()));
+        assertEquals(3, panel.getComponentCount());
+
+        assertTrue(panel.getComponent(0) instanceof JButton);
+        assertEquals("Speichern", ((JButton)panel.getComponent(0)).getText());
+        assertTrue(panel.getComponent(1) instanceof JButton);
+        assertEquals("Prüfen", ((JButton)panel.getComponent(1)).getText());
+        assertTrue(panel.getComponent(2) instanceof JButton);
+        assertEquals("Lösen", ((JButton)panel.getComponent(2)).getText());
+    }
+
+    @Test
+    void testActionListener() {
+        final var ah = new ActionHandler(new Field());
+        final var panel = new ButtonPanel(ah);
+        assertEquals(3, panel.getComponentCount());
+
+        final var save = (JButton)panel.getComponent(0);
+        final var check = (JButton)panel.getComponent(1);
+        final var solve = (JButton)panel.getComponent(2);
+
+        assertEquals(1, save.getActionListeners().length);
+        assertEquals(ah, save.getActionListeners()[0]);
+        assertEquals(1, check.getActionListeners().length);
+        assertEquals(ah, check.getActionListeners()[0]);
+        assertEquals(1, solve.getActionListeners().length);
+        assertEquals(ah, solve.getActionListeners()[0]);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/test/java/de/uapcore/sudoku/DocumentHandlerTest.java	Tue Jul 28 13:48:59 2020 +0200
@@ -0,0 +1,96 @@
+package de.uapcore.sudoku;
+
+import org.junit.jupiter.api.Test;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.nio.file.Files;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+class DocumentHandlerTest {
+
+    @Test
+    void testFilenameManagement() {
+        final var dh = new DocumentHandler();
+        assertFalse(dh.isFilenameSet());
+        dh.setFilename("foo");
+        assertTrue(dh.isFilenameSet());
+        dh.clearFilename();
+        assertFalse(dh.isFilenameSet());
+    }
+
+    private File extractFile(String name) throws IOException {
+        final var f = File.createTempFile("sudoku-test", ".txt");
+        try (var in = DocumentHandlerTest.class.getClassLoader().getResourceAsStream(name);
+             var out = new FileOutputStream(f)) {
+            in.transferTo(out);
+        }
+        return f;
+    }
+
+    private int[] createTestdata() {
+        return new int[]{0,1,0,9,0,0,8,0,0,
+                0,0,0,0,0,8,0,0,4,
+                6,0,5,0,0,0,7,0,0,
+                0,9,0,0,6,0,0,0,8,
+                0,0,0,2,0,7,0,0,0,
+                8,0,0,0,3,0,0,6,0,
+                0,0,2,0,0,0,5,0,3,
+                1,0,0,4,0,0,0,0,0,
+                0,0,6,0,0,2,0,1,0};
+    }
+
+    @Test
+    void load() throws IOException {
+        final var f = extractFile("test-sudoku");
+        final var dh = new DocumentHandler();
+        final var field = new Field();
+        dh.setFilename(f.getAbsolutePath());
+        dh.load(field);
+
+        final var expected = createTestdata();
+        for (int x = 0 ; x < 9 ; x++) {
+            for (int y = 0 ; y < 9 ; y++) {
+                assertEquals(expected[x+9*y], field.getCellValue(x, y));
+            }
+        }
+    }
+
+    @Test
+    void save() throws IOException {
+        final var f = File.createTempFile("sudoku-test", ".txt");
+        final var dh = new DocumentHandler();
+
+        final var field = new Field();
+        final var testdata = createTestdata();
+        for (int x = 0 ; x < 9 ; x++) {
+            for (int y = 0 ; y < 9 ; y++) {
+                field.setCellValue(x, y, testdata[x+9*y]);
+            }
+        }
+
+        dh.setFilename(f.getAbsolutePath());
+        dh.save(field);
+
+        final String[] expected = {
+                "_ 1 _ 9 _ _ 8 _ _",
+                "_ _ _ _ _ 8 _ _ 4",
+                "6 _ 5 _ _ _ 7 _ _",
+                "_ 9 _ _ 6 _ _ _ 8",
+                "_ _ _ 2 _ 7 _ _ _",
+                "8 _ _ _ 3 _ _ 6 _",
+                "_ _ 2 _ _ _ 5 _ 3",
+                "1 _ _ 4 _ _ _ _ _",
+                "_ _ 6 _ _ 2 _ 1 _"
+        };
+
+        final var actual = Files.readAllLines(f.toPath());
+
+        assertEquals(expected.length, actual.size());
+        for (int i = 0 ; i < expected.length ; i++) {
+            assertEquals(expected[i], actual.get(i));
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/test/java/de/uapcore/sudoku/MainMenuTest.java	Tue Jul 28 13:48:59 2020 +0200
@@ -0,0 +1,97 @@
+package de.uapcore.sudoku;
+
+import org.junit.jupiter.api.Test;
+
+import javax.swing.*;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+class MainMenuTest {
+
+    private final ActionHandler actionHandler = new ActionHandler(new Field());
+
+    private void testListeners(JMenuItem m) {
+        final var listeners = m.getActionListeners();
+        assertEquals(1, listeners.length);
+        assertEquals(actionHandler, listeners[0]);
+    }
+
+    @Test
+    void testStructure() {
+        final var menu = new MainMenu(actionHandler);
+
+        assertEquals(2, menu.getMenuCount());
+
+        final var datei = menu.getMenu(0);
+        assertEquals('D', datei.getMnemonic());
+        assertEquals(9, datei.getItemCount());
+        {
+            final var item = datei.getItem(0);
+            assertEquals("Neu", item.getText());
+            assertEquals('N', item.getMnemonic());
+            assertEquals(1, item.getRegisteredKeyStrokes().length);
+            assertEquals(KeyStroke.getKeyStroke("control N"), item.getRegisteredKeyStrokes()[0]);
+            testListeners(item);
+        }
+        {
+            final var item = datei.getItem(1);
+            assertEquals("Öffnen", item.getText());
+            assertEquals('F', item.getMnemonic());
+            assertEquals(1, item.getRegisteredKeyStrokes().length);
+            assertEquals(KeyStroke.getKeyStroke("control O"), item.getRegisteredKeyStrokes()[0]);
+            testListeners(item);
+        }
+        {
+            final var item = datei.getItem(2);
+            assertEquals("Speichern", item.getText());
+            assertEquals('S', item.getMnemonic());
+            assertEquals(1, item.getRegisteredKeyStrokes().length);
+            assertEquals(KeyStroke.getKeyStroke("control S"), item.getRegisteredKeyStrokes()[0]);
+            testListeners(item);
+        }
+        {
+            final var item = datei.getItem(3);
+            assertEquals("Speichern als...", item.getText());
+            assertEquals('A', item.getMnemonic());
+            testListeners(item);
+        }
+
+        assertNull(datei.getItem(4));
+        {
+            final var item = datei.getItem(5);
+            assertEquals("Prüfen", item.getText());
+            assertEquals('P', item.getMnemonic());
+            assertEquals(1, item.getRegisteredKeyStrokes().length);
+            assertEquals(KeyStroke.getKeyStroke("control P"), item.getRegisteredKeyStrokes()[0]);
+            testListeners(item);
+        }
+        {
+            final var item = datei.getItem(6);
+            assertEquals("Lösen", item.getText());
+            assertEquals('L', item.getMnemonic());
+            assertEquals(1, item.getRegisteredKeyStrokes().length);
+            assertEquals(KeyStroke.getKeyStroke("control L"), item.getRegisteredKeyStrokes()[0]);
+            testListeners(item);
+        }
+        assertNull(datei.getItem(7));
+        {
+            final var item = datei.getItem(8);
+            assertEquals("Beenden", item.getText());
+            assertEquals('E', item.getMnemonic());
+            testListeners(item);
+        }
+
+        final var info = menu.getMenu(1);
+        assertEquals("Info", info.getText());
+        assertEquals('I', info.getMnemonic());
+
+        assertEquals(1, info.getItemCount());
+        final var about = info.getItem(0);
+        assertEquals("Über...", about.getText());
+        assertEquals('B', about.getMnemonic());
+        assertEquals(1, about.getRegisteredKeyStrokes().length);
+        assertEquals(KeyStroke.getKeyStroke("F1"), about.getRegisteredKeyStrokes()[0]);
+        testListeners(about);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/test/java/de/uapcore/sudoku/SudokuTest.java	Tue Jul 28 13:48:59 2020 +0200
@@ -0,0 +1,41 @@
+package de.uapcore.sudoku;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import javax.swing.*;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class SudokuTest {
+
+    private Sudoku subject;
+
+    @BeforeEach
+    void init() {
+        subject = new Sudoku();
+    }
+
+    @AfterEach
+    void destroy() {
+        subject.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
+        subject.dispose();
+    }
+
+    @Test
+    void testDefaultCloseOperation() {
+        assertEquals(WindowConstants.EXIT_ON_CLOSE, subject.getDefaultCloseOperation());
+    }
+
+    @Test
+    void testMinimumSize() {
+        assertEquals(subject.getSize(), subject.getMinimumSize());
+    }
+
+    @Test
+    void testLocationByPlattform() {
+        assertTrue(subject.isLocationByPlatform());
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/test/resources/test-sudoku	Tue Jul 28 13:48:59 2020 +0200
@@ -0,0 +1,9 @@
+_ 1 _ 9 _ _ 8 _ _
+_ _ _ _ _ 8 _ _ 4
+6 _ 5 _ _ _ 7 _ _
+_ 9 _ _ 6 _ _ _ 8
+_ _ _ 2 _ 7 _ _ _
+8 _ _ _ 3 _ _ 6 _
+_ _ 2 _ _ _ 5 _ 3
+1 _ _ 4 _ _ _ _ _
+_ _ 6 _ _ 2 _ 1 _

mercurial