Mon, 27 Jul 2020 11:56:41 +0200
adds SudokuTextFieldTest
1.1 --- a/pom.xml Mon Jul 27 10:56:17 2020 +0200 1.2 +++ b/pom.xml Mon Jul 27 11:56:41 2020 +0200 1.3 @@ -5,17 +5,30 @@ 1.4 <artifactId>sudoku</artifactId> 1.5 <version>1.0</version> 1.6 1.7 + <properties> 1.8 + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 1.9 + <maven.compiler.source>11</maven.compiler.source> 1.10 + <maven.compiler.target>11</maven.compiler.target> 1.11 + </properties> 1.12 + 1.13 + <dependencies> 1.14 + <dependency> 1.15 + <groupId>org.junit.jupiter</groupId> 1.16 + <artifactId>junit-jupiter</artifactId> 1.17 + <version>5.6.2</version> 1.18 + <scope>test</scope> 1.19 + </dependency> 1.20 + <dependency> 1.21 + <groupId>org.junit.jupiter</groupId> 1.22 + <artifactId>junit-jupiter-params</artifactId> 1.23 + <version>5.6.2</version> 1.24 + <scope>test</scope> 1.25 + </dependency> 1.26 + </dependencies> 1.27 + 1.28 <build> 1.29 <plugins> 1.30 <plugin> 1.31 - <artifactId>maven-compiler-plugin</artifactId> 1.32 - <version>3.8.1</version> 1.33 - <configuration> 1.34 - <source>1.8</source> 1.35 - <target>1.8</target> 1.36 - </configuration> 1.37 - </plugin> 1.38 - <plugin> 1.39 <groupId>org.apache.maven.plugins</groupId> 1.40 <artifactId>maven-jar-plugin</artifactId> 1.41 <version>3.0.2</version>
2.1 --- a/src/main/java/de/uapcore/sudoku/SudokuTextField.java Mon Jul 27 10:56:17 2020 +0200 2.2 +++ b/src/main/java/de/uapcore/sudoku/SudokuTextField.java Mon Jul 27 11:56:41 2020 +0200 2.3 @@ -127,7 +127,7 @@ 2.4 public void setValue(int v) { 2.5 if (v == 0) { 2.6 setText(""); 2.7 - } else if (v < 10) { 2.8 + } else if (v > 0 && v < 10) { 2.9 setText(String.valueOf(v)); 2.10 } else { 2.11 throw new IllegalArgumentException(
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/src/test/java/de/uapcore/sudoku/SudokuTextFieldTest.java Mon Jul 27 11:56:41 2020 +0200 3.3 @@ -0,0 +1,199 @@ 3.4 +package de.uapcore.sudoku; 3.5 + 3.6 + 3.7 +import org.junit.jupiter.api.AfterEach; 3.8 +import org.junit.jupiter.api.BeforeEach; 3.9 +import org.junit.jupiter.api.Test; 3.10 + 3.11 +import javax.swing.*; 3.12 +import java.awt.event.KeyEvent; 3.13 +import java.lang.reflect.InvocationTargetException; 3.14 + 3.15 +import static org.junit.jupiter.api.Assertions.*; 3.16 + 3.17 +class SudokuTextFieldTest { 3.18 + 3.19 + private void dispatch(SudokuTextField tf, KeyEvent pressed) { 3.20 + final var released = new KeyEvent(tf, KeyEvent.KEY_RELEASED, pressed.getWhen()+1, 3.21 + pressed.getModifiersEx(), pressed.getKeyCode(), pressed.getKeyChar()); 3.22 + final KeyEvent typed; 3.23 + if (pressed.getKeyChar() != KeyEvent.CHAR_UNDEFINED) { 3.24 + typed = new KeyEvent(tf, KeyEvent.KEY_TYPED, pressed.getWhen(), 3.25 + pressed.getModifiersEx(), KeyEvent.VK_UNDEFINED, pressed.getKeyChar()); 3.26 + } else { 3.27 + typed = null; 3.28 + } 3.29 + tf.requestFocusInWindow(); 3.30 + tf.dispatchEvent(pressed); 3.31 + if (typed != null) { 3.32 + tf.dispatchEvent(typed); 3.33 + } 3.34 + tf.dispatchEvent(released); 3.35 + } 3.36 + 3.37 + private JFrame testFrame; 3.38 + 3.39 + @BeforeEach 3.40 + void createTestFrame() { 3.41 + // we have to use a visible frame for the events to be dispatched 3.42 + testFrame = new JFrame(); 3.43 + testFrame.setVisible(true); 3.44 + } 3.45 + 3.46 + @AfterEach 3.47 + void disposeTestFrame() { 3.48 + testFrame.dispose(); 3.49 + } 3.50 + 3.51 + private SudokuTextField createTestSubject() { 3.52 + final var tf = new SudokuTextField(); 3.53 + testFrame.add(tf); 3.54 + return tf; 3.55 + } 3.56 + 3.57 + @Test 3.58 + void getValue() { 3.59 + 3.60 + } 3.61 + 3.62 + @Test 3.63 + void setValue() { 3.64 + // given 3.65 + final var tf = createTestSubject(); 3.66 + for (int i = 0 ; i <= 9 ; i++) { 3.67 + // when 3.68 + tf.setValue(i); 3.69 + // then 3.70 + assertEquals(i, tf.getValue()); 3.71 + assertEquals(i == 0 ? "" : String.valueOf(i), tf.getText()); 3.72 + assertFalse(tf.isModified()); 3.73 + } 3.74 + } 3.75 + 3.76 + @Test 3.77 + void setInvalidValue() { 3.78 + // given 3.79 + final var tf = createTestSubject(); 3.80 + // when / then 3.81 + assertThrows(IllegalArgumentException.class, () -> tf.setValue(10)); 3.82 + assertThrows(IllegalArgumentException.class, () -> tf.setValue(-1)); 3.83 + assertFalse(tf.isModified()); 3.84 + } 3.85 + 3.86 + @Test 3.87 + void testModifiedByCall() { 3.88 + // given 3.89 + final var tf = createTestSubject(); 3.90 + // then initially 3.91 + assertFalse(tf.isModified()); 3.92 + // when 3.93 + tf.setModified(true); 3.94 + // then 3.95 + assertTrue(tf.isModified()); 3.96 + // when 3.97 + tf.setModified(false); 3.98 + // then 3.99 + assertFalse(tf.isModified()); 3.100 + } 3.101 + 3.102 + @Test 3.103 + void testModifiedByKeystroke() throws InvocationTargetException, InterruptedException { 3.104 + // given 3.105 + final var tf = createTestSubject(); 3.106 + assertFalse(tf.isModified()); 3.107 + final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 3.108 + 0, KeyEvent.VK_4, '4'); 3.109 + SwingUtilities.invokeAndWait(() -> { 3.110 + // when 3.111 + dispatch(tf, event); 3.112 + // then 3.113 + assertTrue(tf.isModified()); 3.114 + }); 3.115 + } 3.116 + 3.117 + @Test 3.118 + void testTextChangeByKeystore() throws InvocationTargetException, InterruptedException { 3.119 + // given 3.120 + final var tf = createTestSubject(); 3.121 + assertEquals("", tf.getText()); 3.122 + final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 3.123 + 0, KeyEvent.VK_4, '4'); 3.124 + SwingUtilities.invokeAndWait(() -> { 3.125 + // when 3.126 + dispatch(tf, event); 3.127 + // then 3.128 + assertEquals("4", tf.getText()); 3.129 + }); 3.130 + } 3.131 + 3.132 + @Test 3.133 + void testTextOverwriteByKeystore() throws InvocationTargetException, InterruptedException { 3.134 + // given 3.135 + final var tf = createTestSubject(); 3.136 + tf.setText("6"); 3.137 + tf.selectAll(); 3.138 + final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 3.139 + 0, KeyEvent.VK_4, '4'); 3.140 + SwingUtilities.invokeAndWait(() -> { 3.141 + // when 3.142 + dispatch(tf, event); 3.143 + // then 3.144 + assertEquals("4", tf.getText()); 3.145 + }); 3.146 + } 3.147 + 3.148 + @Test 3.149 + void testNumbersOnly() throws InvocationTargetException, InterruptedException { 3.150 + // given 3.151 + final var tf = createTestSubject(); 3.152 + tf.setText("6"); 3.153 + tf.selectAll(); 3.154 + final var event = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 3.155 + 0, KeyEvent.VK_A, 'a'); 3.156 + SwingUtilities.invokeAndWait(() -> { 3.157 + // when 3.158 + dispatch(tf, event); 3.159 + // then 3.160 + assertEquals("6", tf.getText()); 3.161 + }); 3.162 + } 3.163 + 3.164 + @Test 3.165 + void testSingleDigit() throws InvocationTargetException, InterruptedException { 3.166 + // given 3.167 + final var tf = createTestSubject(); 3.168 + assertEquals("", tf.getText()); 3.169 + final var firstEvent = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 3.170 + 0, KeyEvent.VK_A, '4'); 3.171 + final var secondEvent = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 3.172 + 0, KeyEvent.VK_A, '6'); 3.173 + SwingUtilities.invokeAndWait(() -> { 3.174 + // when 3.175 + dispatch(tf, firstEvent); 3.176 + dispatch(tf, secondEvent); 3.177 + // then 3.178 + assertEquals("4", tf.getText()); 3.179 + }); 3.180 + } 3.181 + 3.182 + @Test 3.183 + void testBackspace() throws InvocationTargetException, InterruptedException { 3.184 + // given 3.185 + final var tf = createTestSubject(); 3.186 + assertEquals("", tf.getText()); 3.187 + final var typeFour = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 3.188 + 0, KeyEvent.VK_A, '4'); 3.189 + final var typeBackspace = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 3.190 + 0, KeyEvent.VK_BACK_SPACE, KeyEvent.CHAR_UNDEFINED); 3.191 + final var typeSix = new KeyEvent(tf, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 3.192 + 0, KeyEvent.VK_A, '6'); 3.193 + SwingUtilities.invokeAndWait(() -> { 3.194 + // when 3.195 + dispatch(tf, typeFour); 3.196 + dispatch(tf, typeBackspace); 3.197 + dispatch(tf, typeSix); 3.198 + // then 3.199 + assertEquals("6", tf.getText()); 3.200 + }); 3.201 + } 3.202 +} 3.203 \ No newline at end of file