adds SudokuTextFieldTest

Mon, 27 Jul 2020 11:56:41 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 27 Jul 2020 11:56:41 +0200
changeset 13
5e69b1bb707f
parent 12
1c62c6009161
child 14
959e13642554

adds SudokuTextFieldTest

pom.xml file | annotate | diff | comparison | revisions
src/main/java/de/uapcore/sudoku/SudokuTextField.java file | annotate | diff | comparison | revisions
src/test/java/de/uapcore/sudoku/SudokuTextFieldTest.java file | annotate | diff | comparison | revisions
--- a/pom.xml	Mon Jul 27 10:56:17 2020 +0200
+++ b/pom.xml	Mon Jul 27 11:56:41 2020 +0200
@@ -5,17 +5,30 @@
     <artifactId>sudoku</artifactId>
     <version>1.0</version>
 
+    <properties>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        <maven.compiler.source>11</maven.compiler.source>
+        <maven.compiler.target>11</maven.compiler.target>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.junit.jupiter</groupId>
+            <artifactId>junit-jupiter</artifactId>
+            <version>5.6.2</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.junit.jupiter</groupId>
+            <artifactId>junit-jupiter-params</artifactId>
+            <version>5.6.2</version>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+
     <build>
         <plugins>
             <plugin>
-                <artifactId>maven-compiler-plugin</artifactId>
-                <version>3.8.1</version>
-                <configuration>
-                    <source>1.8</source>
-                    <target>1.8</target>
-                </configuration>
-            </plugin>
-            <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-jar-plugin</artifactId>
                 <version>3.0.2</version>
--- a/src/main/java/de/uapcore/sudoku/SudokuTextField.java	Mon Jul 27 10:56:17 2020 +0200
+++ b/src/main/java/de/uapcore/sudoku/SudokuTextField.java	Mon Jul 27 11:56:41 2020 +0200
@@ -127,7 +127,7 @@
     public void setValue(int v) {
         if (v == 0) {
             setText("");
-        } else if (v < 10) {
+        } else if (v > 0 && v < 10) {
             setText(String.valueOf(v));
         } else {
             throw new IllegalArgumentException(
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/test/java/de/uapcore/sudoku/SudokuTextFieldTest.java	Mon Jul 27 11:56:41 2020 +0200
@@ -0,0 +1,199 @@
+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 java.awt.event.KeyEvent;
+import java.lang.reflect.InvocationTargetException;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+class SudokuTextFieldTest {
+
+    private void dispatch(SudokuTextField tf, KeyEvent pressed) {
+        final var released = new KeyEvent(tf, KeyEvent.KEY_RELEASED, pressed.getWhen()+1,
+                pressed.getModifiersEx(), pressed.getKeyCode(), pressed.getKeyChar());
+        final KeyEvent typed;
+        if (pressed.getKeyChar() != KeyEvent.CHAR_UNDEFINED) {
+            typed = new KeyEvent(tf, KeyEvent.KEY_TYPED, pressed.getWhen(),
+                    pressed.getModifiersEx(), KeyEvent.VK_UNDEFINED, pressed.getKeyChar());
+        } else {
+            typed = null;
+        }
+        tf.requestFocusInWindow();
+        tf.dispatchEvent(pressed);
+        if (typed != null) {
+            tf.dispatchEvent(typed);
+        }
+        tf.dispatchEvent(released);
+    }
+
+    private JFrame testFrame;
+
+    @BeforeEach
+    void createTestFrame() {
+        // we have to use a visible frame for the events to be dispatched
+        testFrame = new JFrame();
+        testFrame.setVisible(true);
+    }
+
+    @AfterEach
+    void disposeTestFrame() {
+        testFrame.dispose();
+    }
+
+    private SudokuTextField createTestSubject() {
+        final var tf = new SudokuTextField();
+        testFrame.add(tf);
+        return tf;
+    }
+
+    @Test
+    void getValue() {
+
+    }
+
+    @Test
+    void setValue() {
+        // given
+        final var tf = createTestSubject();
+        for (int i = 0 ; i <= 9 ; i++) {
+            // when
+            tf.setValue(i);
+            // then
+            assertEquals(i, tf.getValue());
+            assertEquals(i == 0 ? "" : String.valueOf(i), tf.getText());
+            assertFalse(tf.isModified());
+        }
+    }
+
+    @Test
+    void setInvalidValue() {
+        // given
+        final var tf = createTestSubject();
+        // when / then
+        assertThrows(IllegalArgumentException.class, () -> tf.setValue(10));
+        assertThrows(IllegalArgumentException.class, () -> tf.setValue(-1));
+        assertFalse(tf.isModified());
+    }
+
+    @Test
+    void testModifiedByCall() {
+        // given
+        final var tf = createTestSubject();
+        // then initially
+        assertFalse(tf.isModified());
+        // when
+        tf.setModified(true);
+        // then
+        assertTrue(tf.isModified());
+        // when
+        tf.setModified(false);
+        // then
+        assertFalse(tf.isModified());
+    }
+
+    @Test
+    void testModifiedByKeystroke() throws InvocationTargetException, InterruptedException {
+        // given
+        final var tf = createTestSubject();
+        assertFalse(tf.isModified());
+        final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
+                0, KeyEvent.VK_4, '4');
+        SwingUtilities.invokeAndWait(() -> {
+            // when
+            dispatch(tf, event);
+            // then
+            assertTrue(tf.isModified());
+        });
+    }
+
+    @Test
+    void testTextChangeByKeystore() throws InvocationTargetException, InterruptedException {
+        // given
+        final var tf = createTestSubject();
+        assertEquals("", tf.getText());
+        final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
+                0, KeyEvent.VK_4, '4');
+        SwingUtilities.invokeAndWait(() -> {
+            // when
+            dispatch(tf, event);
+            // then
+            assertEquals("4", tf.getText());
+        });
+    }
+
+    @Test
+    void testTextOverwriteByKeystore() throws InvocationTargetException, InterruptedException {
+        // given
+        final var tf = createTestSubject();
+        tf.setText("6");
+        tf.selectAll();
+        final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
+                0, KeyEvent.VK_4, '4');
+        SwingUtilities.invokeAndWait(() -> {
+            // when
+            dispatch(tf, event);
+            // then
+            assertEquals("4", tf.getText());
+        });
+    }
+
+    @Test
+    void testNumbersOnly() throws InvocationTargetException, InterruptedException {
+        // given
+        final var tf = createTestSubject();
+        tf.setText("6");
+        tf.selectAll();
+        final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
+                0, KeyEvent.VK_A, 'a');
+        SwingUtilities.invokeAndWait(() -> {
+            // when
+            dispatch(tf, event);
+            // then
+            assertEquals("6", tf.getText());
+        });
+    }
+
+    @Test
+    void testSingleDigit() throws InvocationTargetException, InterruptedException {
+        // given
+        final var tf = createTestSubject();
+        assertEquals("", tf.getText());
+        final var firstEvent = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
+                0, KeyEvent.VK_A, '4');
+        final var secondEvent = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
+                0, KeyEvent.VK_A, '6');
+        SwingUtilities.invokeAndWait(() -> {
+            // when
+            dispatch(tf, firstEvent);
+            dispatch(tf, secondEvent);
+            // then
+            assertEquals("4", tf.getText());
+        });
+    }
+
+    @Test
+    void testBackspace() throws InvocationTargetException, InterruptedException {
+        // given
+        final var tf = createTestSubject();
+        assertEquals("", tf.getText());
+        final var typeFour = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
+                0, KeyEvent.VK_A, '4');
+        final var typeBackspace = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
+                0, KeyEvent.VK_BACK_SPACE, KeyEvent.CHAR_UNDEFINED);
+        final var typeSix = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(),
+                0, KeyEvent.VK_A, '6');
+        SwingUtilities.invokeAndWait(() -> {
+            // when
+            dispatch(tf, typeFour);
+            dispatch(tf, typeBackspace);
+            dispatch(tf, typeSix);
+            // then
+            assertEquals("6", tf.getText());
+        });
+    }
+}
\ No newline at end of file

mercurial