bugfix: modified state is reset even when saving fails + more tests

Tue, 28 Jul 2020 14:27:14 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 28 Jul 2020 14:27:14 +0200
changeset 22
06170a0be62a
parent 21
11cb2d9413d4
child 23
07b9adaed78e

bugfix: modified state is reset even when saving fails + more tests

src/main/java/de/uapcore/sudoku/ActionHandler.java file | annotate | diff | comparison | revisions
src/main/java/de/uapcore/sudoku/DocumentHandler.java file | annotate | diff | comparison | revisions
src/main/java/de/uapcore/sudoku/Field.java file | annotate | diff | comparison | revisions
src/test/java/de/uapcore/sudoku/FieldTest.java file | annotate | diff | comparison | revisions
src/test/java/de/uapcore/sudoku/SudokuTextFieldTest.java file | annotate | diff | comparison | revisions
--- a/src/main/java/de/uapcore/sudoku/ActionHandler.java	Tue Jul 28 14:05:04 2020 +0200
+++ b/src/main/java/de/uapcore/sudoku/ActionHandler.java	Tue Jul 28 14:27:14 2020 +0200
@@ -129,7 +129,6 @@
             }
         }
         if (solver.check(field)) {
-            field.setAllCellsModified(false);
             try {
                 doc.save(field);
             } catch (IOException e) {
--- a/src/main/java/de/uapcore/sudoku/DocumentHandler.java	Tue Jul 28 14:05:04 2020 +0200
+++ b/src/main/java/de/uapcore/sudoku/DocumentHandler.java	Tue Jul 28 14:27:14 2020 +0200
@@ -81,6 +81,7 @@
 
     /**
      * Saves the specified field to a file.
+     * On success, the modified state of all cells is set to false.
      *
      * @param field the field to save
      * @throws IOException if saving fails or the file name has not been set before
@@ -101,6 +102,7 @@
                 }
             }
         }
+        field.setAllCellsModified(false);
     }
 
     /**
--- a/src/main/java/de/uapcore/sudoku/Field.java	Tue Jul 28 14:05:04 2020 +0200
+++ b/src/main/java/de/uapcore/sudoku/Field.java	Tue Jul 28 14:27:14 2020 +0200
@@ -152,6 +152,17 @@
     }
 
     /**
+     * Checks the modified state of a specific cell.
+     *
+     * @param x horizontal position
+     * @param y vertical position
+     * @return the modified state
+     */
+    public boolean isCellModified(int x, int y) {
+        return cells[x][y].isModified();
+    }
+
+    /**
      * Sets the modified state of all cells.
      *
      * @param modified the modified state
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/test/java/de/uapcore/sudoku/FieldTest.java	Tue Jul 28 14:27:14 2020 +0200
@@ -0,0 +1,154 @@
+package de.uapcore.sudoku;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+class FieldTest {
+
+    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};
+    }
+
+    private Field createTestField() {
+        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]);
+            }
+        }
+        return field;
+    }
+
+    @Test
+    void getAndSetValues() {
+        final var f = new Field();
+        assertEquals(0, f.getCellValue(3, 4));
+        assertTrue(f.isCellEmpty(3, 4));
+        f.setCellValue(3, 4, 6);
+        assertEquals(6, 3, 4);
+        assertFalse(f.isCellEmpty(3, 4));
+    }
+
+    @Test
+    void setAllCellsModified() {
+        final var f = new Field();
+
+        assertFalse(f.isAnyCellModified());
+        f.setAllCellsModified(true);
+        for (int x = 0 ; x < 9 ; x++) {
+            for (int y = 0 ; y < 9 ; y++) {
+                assertTrue(f.isCellModified(x, y));
+            }
+        }
+
+        f.setAllCellsModified(false);
+        assertFalse(f.isAnyCellModified());
+    }
+
+    @Test
+    void isAnyCellModified() {
+        final var f = new Field();
+
+        assertFalse(f.isAnyCellModified());
+        f.setCellValue(3, 4, 6);
+        assertFalse(f.isAnyCellModified());
+        f.setCellModified(3, 4, true);
+        assertTrue(f.isAnyCellModified());
+    }
+
+    @Test
+    void clear() {
+        final var f = new Field();
+        f.setCellValue(3, 4, 6);
+        f.setCellValue(2, 5, 9);
+        f.clear();
+        for (int x = 0 ; x < 9 ; x++) {
+            for (int y = 0 ; y < 9 ; y++) {
+                assertTrue(f.isCellEmpty(x, y));
+            }
+        }
+    }
+
+    @Test
+    void getSquare() {
+        final var field = createTestField();
+
+        final var square01 = new int[][]{
+                new int[]{0,0,8},
+                new int[]{9,0,0},
+                new int[]{0,0,0}};
+
+        final var square01actual = field.getSquare(0, 1);
+        assertEquals(3, square01actual.length);
+        for (int i = 0 ; i < 3 ; i++) {
+            assertArrayEquals(square01[i], square01actual[i]);
+        }
+
+        final var square20 = new int[][]{
+                new int[]{8,0,7},
+                new int[]{0,0,0},
+                new int[]{0,4,0}};
+
+        final var square20actual = field.getSquare(2, 0);
+        assertEquals(3, square20actual.length);
+        for (int i = 0 ; i < 3 ; i++) {
+            assertArrayEquals(square20[i], square20actual[i]);
+        }
+    }
+
+    @Test
+    void getRow() {
+        final var field = createTestField();
+
+        final var row3 = new int[]{0,9,0,0,6,0,0,0,8};
+        final var row4 = new int[]{0,0,0,2,0,7,0,0,0};
+
+        assertArrayEquals(row3, field.getRow(3));
+        assertArrayEquals(row4, field.getRow(4));
+    }
+
+    @Test
+    void getColumn() {
+        final var field = createTestField();
+
+        final var col2 = new int[]{0,0,5,0,0,0,2,0,6};
+        final var col6 = new int[]{8,0,7,0,0,0,5,0,0};
+
+        assertArrayEquals(col2, field.getColumn(2));
+        assertArrayEquals(col6, field.getColumn(6));
+    }
+
+    @Test
+    void getSquareIAE() {
+        final var field = createTestField();
+        assertThrows(IllegalArgumentException.class, () -> field.getSquare(3, 2));
+        assertThrows(IllegalArgumentException.class, () -> field.getSquare(2, 3));
+        assertThrows(IllegalArgumentException.class, () -> field.getSquare(2, -1));
+        assertThrows(IllegalArgumentException.class, () -> field.getSquare(-1, 2));
+    }
+
+    @Test
+    void getRowIAE() {
+        final var field = createTestField();
+        assertThrows(IllegalArgumentException.class, () -> field.getRow(9));
+        assertThrows(IllegalArgumentException.class, () -> field.getRow(-1));
+    }
+
+    @Test
+    void getColumnIAE() {
+        final var field = createTestField();
+        assertThrows(IllegalArgumentException.class, () -> field.getColumn(9));
+        assertThrows(IllegalArgumentException.class, () -> field.getColumn(-1));
+    }
+}
--- a/src/test/java/de/uapcore/sudoku/SudokuTextFieldTest.java	Tue Jul 28 14:05:04 2020 +0200
+++ b/src/test/java/de/uapcore/sudoku/SudokuTextFieldTest.java	Tue Jul 28 14:27:14 2020 +0200
@@ -1,6 +1,5 @@
 package de.uapcore.sudoku;
 
-
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
@@ -244,4 +243,37 @@
         // then
         SwingUtilities.invokeAndWait(() -> assertNull(tf.getSelectedText()));
     }
-}
\ No newline at end of file
+
+    @Test
+    void testFont() {
+        final var tf = new SudokuTextField();
+        assertEquals(18, tf.getFont().getSize());
+    }
+
+    @Test
+    void testAlignment() {
+        final var tf = new SudokuTextField();
+        assertEquals(JTextField.CENTER, tf.getHorizontalAlignment());
+    }
+
+    @Test
+    void testBackground() {
+        final var tf = new SudokuTextField();
+        assertEquals(Color.WHITE, tf.getBackground());
+    }
+
+    @Test
+    void testSize() {
+        final var tf = new SudokuTextField();
+        final var expected = new Dimension(40, 40);
+        assertEquals(expected, tf.getMinimumSize());
+        assertEquals(expected, tf.getPreferredSize());
+        assertEquals(expected, tf.getMaximumSize());
+    }
+
+    @Test
+    void testBorder() {
+        final var tf = new SudokuTextField();
+        assertNull(tf.getBorder());
+    }
+}

mercurial