Skip to content

Commit 641b5a1

Browse files
committed
Migrate SWT Win32 tests to JUnit 5
This migrates the Win32 tests for SWT to JUnit5, including the usage of JUnit 5 test annotations, assertions and test suite.
1 parent 2853d53 commit 641b5a1

File tree

11 files changed

+105
-112
lines changed

11 files changed

+105
-112
lines changed

tests/org.eclipse.swt.tests.win32/JUnit Tests/org/eclipse/swt/graphics/ImageWin32Tests.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
package org.eclipse.swt.graphics;
1515

1616

17-
import static org.junit.Assert.assertEquals;
18-
import static org.junit.Assert.assertNotEquals;
17+
import static org.junit.jupiter.api.Assertions.assertEquals;
18+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
1919

2020
import org.eclipse.swt.internal.DPIUtil;
2121
import org.eclipse.swt.widgets.Display;
22-
import org.junit.Before;
23-
import org.junit.Test;
22+
import org.junit.jupiter.api.BeforeEach;
23+
import org.junit.jupiter.api.Test;
2424

2525
/**
2626
* Automated Tests for class org.eclipse.swt.graphics.Image
@@ -32,7 +32,7 @@
3232
public class ImageWin32Tests {
3333
private Display display;
3434

35-
@Before
35+
@BeforeEach
3636
public void setUp() {
3737
display = Display.getDefault();
3838
}
@@ -45,10 +45,10 @@ public void testImageDataForDifferentFractionalZoomsShouldBeDifferent() {
4545
int zoom2 = 150;
4646
ImageData imageDataAtZoom1 = image.getImageData(zoom1);
4747
ImageData imageDataAtZoom2 = image.getImageData(zoom2);
48-
assertNotEquals("ImageData::height should not be the same for imageData at different zoom levels",
49-
imageDataAtZoom1.height, imageDataAtZoom2.height);
50-
assertNotEquals("ImageData::width should not be the same for imageData at different zoom levels",
51-
imageDataAtZoom1.width, imageDataAtZoom2.width);
48+
assertNotEquals(imageDataAtZoom1.height, imageDataAtZoom2.height,
49+
"ImageData::height should not be the same for imageData at different zoom levels");
50+
assertNotEquals(imageDataAtZoom1.width, imageDataAtZoom2.width,
51+
"ImageData::width should not be the same for imageData at different zoom levels");
5252
}
5353

5454
@Test
@@ -59,11 +59,12 @@ public void testImageShouldHaveDimesionAsPerZoomLevel() {
5959
Image image = new Image(display, noOpGcDrawer, 10, 10);
6060
try {
6161
ImageData baseImageData = image.getImageData(zoom);
62-
assertEquals("Width should equal the initial width on the same zoom", 10, baseImageData.width);
62+
assertEquals(10, baseImageData.width, "Width should equal the initial width on the same zoom");
6363
ImageData scaledImageData = image.getImageData(zoom * scalingFactor);
64-
assertEquals("Width should be doubled on doubled zoom", 10*2, scaledImageData.width);
64+
assertEquals(10*2, scaledImageData.width, "Width should be doubled on doubled zoom");
6565
} finally {
6666
image.dispose();
6767
}
68-
}
69-
}
68+
}
69+
70+
}

tests/org.eclipse.swt.tests.win32/JUnit Tests/org/eclipse/swt/tests/win32/AllWin32Tests.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,16 @@
1717
import org.eclipse.swt.graphics.ImageWin32Tests;
1818
import org.eclipse.swt.tests.win32.widgets.TestTreeColumn;
1919
import org.eclipse.swt.tests.win32.widgets.Test_org_eclipse_swt_widgets_Display;
20-
import org.junit.runner.JUnitCore;
21-
import org.junit.runner.RunWith;
22-
import org.junit.runners.Suite;
23-
24-
@RunWith(Suite.class)
25-
@Suite.SuiteClasses({
26-
ImageWin32Tests.class,
27-
Test_org_eclipse_swt_dnd_DND.class,
28-
Test_org_eclipse_swt_events_KeyEvent.class,
29-
Test_org_eclipse_swt_widgets_Display.class,
30-
TestTreeColumn.class,
31-
Win32DPIUtilTests.class
32-
})
20+
import org.junit.platform.suite.api.SelectClasses;
21+
import org.junit.platform.suite.api.Suite;
3322

23+
@Suite
24+
@SelectClasses({ ImageWin32Tests.class, //
25+
Test_org_eclipse_swt_dnd_DND.class, //
26+
Test_org_eclipse_swt_events_KeyEvent.class, //
27+
Test_org_eclipse_swt_widgets_Display.class, //
28+
TestTreeColumn.class, //
29+
Win32DPIUtilTests.class })
3430
public class AllWin32Tests {
3531

36-
public static void main(String[] args) {
37-
JUnitCore.main(AllWin32Tests.class.getName());
38-
}
39-
4032
}

tests/org.eclipse.swt.tests.win32/JUnit Tests/org/eclipse/swt/tests/win32/KeyboardLayoutTest.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*******************************************************************************/
1414
package org.eclipse.swt.tests.win32;
1515

16-
import static org.junit.Assert.fail;
16+
import static org.junit.jupiter.api.Assertions.fail;
1717

1818
import java.util.ArrayList;
1919

@@ -25,10 +25,9 @@
2525
import org.eclipse.swt.widgets.Event;
2626
import org.eclipse.swt.widgets.Listener;
2727
import org.eclipse.swt.widgets.Shell;
28-
import org.junit.After;
29-
import org.junit.Before;
30-
import org.junit.Rule;
31-
import org.junit.rules.TestName;
28+
import org.junit.jupiter.api.AfterEach;
29+
import org.junit.jupiter.api.BeforeEach;
30+
import org.junit.jupiter.api.TestInfo;
3231

3332
/**
3433
* Supporting code for testing keyboard layouts
@@ -46,8 +45,7 @@ public class KeyboardLayoutTest {
4645
boolean collectKeyErrors;
4746
ArrayList<AssertionError> keyErrors;
4847

49-
@Rule
50-
public TestName testName = new TestName();
48+
private String testName;
5149

5250
// Hardware scan codes, with names according to English US layout
5351
protected enum UsScan {
@@ -222,8 +220,9 @@ public boolean isExtended() {
222220
A___,
223221
};
224222

225-
@Before
226-
public void setUp() {
223+
@BeforeEach
224+
public void setUp(TestInfo testInfo) {
225+
this.testName = testInfo.getDisplayName();
227226
display = new Display();
228227
shell = new Shell();
229228

@@ -253,7 +252,7 @@ public void setUp() {
253252
shell.forceFocus();
254253
}
255254

256-
@After
255+
@AfterEach
257256
public void tearDown() {
258257
if (shell != null) {
259258
shell.dispose();
@@ -496,7 +495,7 @@ protected void onKeyError(AssertionError error) {
496495
if (keyErrors == null)
497496
keyErrors = new ArrayList<>();
498497

499-
System.out.println(testName.getMethodName() + " : " + error.getMessage());
498+
System.out.println(testName + " : " + error.getMessage());
500499
keyErrors.add(error);
501500
}
502501
}

tests/org.eclipse.swt.tests.win32/JUnit Tests/org/eclipse/swt/tests/win32/Test_org_eclipse_swt_dnd_DND.java

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
*******************************************************************************/
1414
package org.eclipse.swt.tests.win32;
1515

16-
import static org.junit.Assert.assertArrayEquals;
17-
import static org.junit.Assert.assertEquals;
18-
import static org.junit.Assert.assertNotNull;
19-
import static org.junit.Assert.assertTrue;
20-
import static org.junit.Assert.fail;
16+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
17+
import static org.junit.jupiter.api.Assertions.assertEquals;
18+
import static org.junit.jupiter.api.Assertions.assertNotNull;
19+
import static org.junit.jupiter.api.Assertions.assertTrue;
20+
import static org.junit.jupiter.api.Assertions.fail;
2121

2222
import java.util.Random;
2323
import java.util.concurrent.atomic.AtomicBoolean;
@@ -45,9 +45,9 @@
4545
import org.eclipse.swt.widgets.Display;
4646
import org.eclipse.swt.widgets.Event;
4747
import org.eclipse.swt.widgets.Shell;
48-
import org.junit.After;
49-
import org.junit.Before;
50-
import org.junit.Test;
48+
import org.junit.jupiter.api.AfterEach;
49+
import org.junit.jupiter.api.BeforeEach;
50+
import org.junit.jupiter.api.Test;
5151

5252
/**
5353
* Some simple tests for drag and drop.
@@ -64,7 +64,7 @@ public class Test_org_eclipse_swt_dnd_DND {
6464

6565
private Shell shell;
6666

67-
@Before
67+
@BeforeEach
6868
public void setUp() {
6969
shell = new Shell();
7070
shell.setLayout(new RowLayout());
@@ -75,10 +75,10 @@ public void setUp() {
7575
} catch (InterruptedException e) {
7676
fail("Initialization interrupted");
7777
}
78-
assertTrue("Shell not visible.", shell.isVisible());
78+
assertTrue(shell.isVisible(), "Shell not visible.");
7979
}
8080

81-
@After
81+
@AfterEach
8282
public void tearDown() {
8383
Display display = shell.getDisplay();
8484
display.dispose();
@@ -108,7 +108,7 @@ protected int[] getTypeIds() {
108108
return new int[] { TEST_ID };
109109
}
110110
}, drag);
111-
assertArrayEquals("Drop received other data as we dragged.", drag, drop);
111+
assertArrayEquals(drag, drop, "Drop received other data as we dragged.");
112112
}
113113

114114
/**
@@ -120,7 +120,7 @@ public void testFileTransfer() throws InterruptedException {
120120
final String[] drop;
121121

122122
drop = testTransferRoundtrip(FileTransfer.getInstance(), drag);
123-
assertArrayEquals("Drop received other data as we dragged.", drag, drop);
123+
assertArrayEquals(drag, drop, "Drop received other data as we dragged.");
124124
}
125125

126126
/**
@@ -132,7 +132,7 @@ public void testHtmlTransfer() throws InterruptedException {
132132
final String drop;
133133

134134
drop = testTransferRoundtrip(HTMLTransfer.getInstance(), drag);
135-
assertEquals("Drop received other data as we dragged.", drag, drop);
135+
assertEquals(drag, drop, "Drop received other data as we dragged.");
136136
}
137137

138138
/**
@@ -208,7 +208,7 @@ public void testRtfTransfer() throws InterruptedException {
208208
final String drop;
209209

210210
drop = testTransferRoundtrip(RTFTransfer.getInstance(), drag);
211-
assertEquals("Drop received other data as we dragged.", drag, drop);
211+
assertEquals(drag, drop, "Drop received other data as we dragged.");
212212
}
213213

214214
/**
@@ -220,7 +220,7 @@ public void testTextTransfer() throws InterruptedException {
220220
final String drop;
221221

222222
drop = testTransferRoundtrip(TextTransfer.getInstance(), drag);
223-
assertEquals("Drop received other data as we dragged.", drag, drop);
223+
assertEquals(drag, drop, "Drop received other data as we dragged.");
224224
}
225225

226226
/**
@@ -232,7 +232,7 @@ public void testUrlTransfer() throws InterruptedException {
232232
final String drop;
233233

234234
drop = testTransferRoundtrip(URLTransfer.getInstance(), drag);
235-
assertEquals("Drop received other data as we dragged.", drag, drop);
235+
assertEquals(drag, drop, "Drop received other data as we dragged.");
236236
}
237237

238238
/**
@@ -267,28 +267,28 @@ private Image createTestImage() {
267267
*/
268268
// This method is necessary because ImageData has no custom equals method and the default one isn't sufficient.
269269
private void assertImageDataEqualsIgoringAlphaInData(final ImageData expected, final ImageData actual) {
270-
assertNotNull("expected data must not be null", expected);
271-
assertNotNull("actual data must not be null", actual);
270+
assertNotNull(expected, "expected data must not be null");
271+
assertNotNull(actual, "actual data must not be null");
272272
if (expected == actual) {
273273
return;
274274
}
275-
assertEquals("height of expected image is different from actual image", expected.height, actual.height);
275+
assertEquals(expected.height, actual.height, "height of expected image is different from actual image");
276276
// Alpha values are taken from alpha data, so ignore whether data depth is 24 or 32 bits
277277
int expectedNormalizedDepth = expected.depth == 32 ? 24 : expected.depth;
278278
int actualNormalizedDepth = expected.depth == 32 ? 24 : expected.depth;
279-
assertEquals("depth of image data to compare must be equal", expectedNormalizedDepth, actualNormalizedDepth);
280-
assertEquals("width of expected image is different from actual image", expected.width, actual.width);
279+
assertEquals(expectedNormalizedDepth, actualNormalizedDepth, "depth of image data to compare must be equal");
280+
assertEquals(expected.width, actual.width, "width of expected image is different from actual image");
281281

282282
for (int y = 0; y < expected.height; y++) {
283283
for (int x = 0; x < expected.width; x++) {
284284
// FIXME win32: dragged ALPHA=FF, dropped ALPHA=00, but other transparencyType
285285
// => alpha stored in ImageData.alphaData
286286
String expectedPixel = String.format("0x%08X", expected.getPixel(x, y) >> (expected.depth == 32 ? 8 : 0));
287287
String actualPixel = String.format("0x%08X", actual.getPixel(x, y) >> (actual.depth == 32 ? 8 : 0));
288-
assertEquals("actual pixel at x=" + x + " y=" + y + " is different from expected pixel", expectedPixel, actualPixel);
288+
assertEquals(expectedPixel, actualPixel, "actual pixel at x=" + x + " y=" + y + " is different from expected pixel");
289289
int expectedAlpha = expected.getAlpha(x, y);
290290
int actualAlpha = actual.getAlpha(x, y);
291-
assertEquals("actual pixel alpha at x=" + x + " y=" + y + " is different from expected pixel", expectedAlpha, actualAlpha);
291+
assertEquals(expectedAlpha, actualAlpha, "actual pixel alpha at x=" + x + " y=" + y + " is different from expected pixel");
292292
}
293293
}
294294
}
@@ -327,8 +327,8 @@ private <T> T testTransferRoundtrip(Transfer transfer, T data) throws Interrupte
327327
SwtWin32TestUtil.processEvents(shell.getDisplay(), 2000, dropped::get);
328328
} while(!dropped.get() && --maxTries > 0);
329329

330-
assertTrue("No drop received.", dropped.get());
331-
assertNotNull("No data was dropped.", droppedData.get());
330+
assertTrue(dropped.get(), "No drop received.");
331+
assertNotNull(droppedData.get(), "No data was dropped.");
332332

333333
return droppedData.get();
334334
}
@@ -341,7 +341,7 @@ private <T> T testTransferRoundtrip(Transfer transfer, T data) throws Interrupte
341341
*/
342342
private void postDragAndDropEvents() {
343343
shell.forceActive();
344-
assertTrue("Test shell requires input focus.", shell.forceFocus());
344+
assertTrue(shell.forceFocus(), "Test shell requires input focus.");
345345
Event event = new Event();
346346
Point pt = shell.toDisplay(50, 50);
347347
event.x = pt.x;

tests/org.eclipse.swt.tests.win32/JUnit Tests/org/eclipse/swt/tests/win32/Test_org_eclipse_swt_events_KeyEvent.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
import org.eclipse.swt.SWT;
1717
import org.eclipse.swt.internal.win32.OS;
1818
import org.eclipse.swt.widgets.Event;
19-
import org.junit.Ignore;
20-
import org.junit.Test;
19+
import org.junit.jupiter.api.Disabled;
20+
import org.junit.jupiter.api.Test;
2121

2222
/**
2323
* Automated Test Suite for class org.eclipse.swt.events.KeyEvent
@@ -166,7 +166,7 @@ private boolean isSystemHotkey(int state, UsScan scanCode) {
166166
/**
167167
* Extensive test for English US layout
168168
*/
169-
@Test
169+
@org.junit.jupiter.api.Test
170170
public void testEnglishUs_stdKeys() {
171171
// This table intentionally provides "naive" expected values. Actual
172172
// expected values have some discrepancies from that, see code below.
@@ -921,7 +921,7 @@ public void testEnglishInternational_deadKey_Shift6() {
921921
* release that isn't paired with key press (due to RegisterHotKey()
922922
* or hook or pressing in other app and releasing in SWT).
923923
*/
924-
@Ignore("Have been broken for ages, maybe not worth fixing")
924+
@Disabled("Have been broken for ages, maybe not worth fixing")
925925
@Test
926926
public void testEnglishUs_unpairedKeyUp() {
927927
runWithLayout(LAYOUT_ENGLISH_US, () -> {
@@ -959,7 +959,7 @@ public void testEnglishUs_unpairedKeyUp() {
959959
* released first. Specifically, it didn't produce SWT.KeyUp for 'C' at all,
960960
* and also produced SWT.KeyUp for 'Ctrl' with incorrect 'character = 0x03'.
961961
*/
962-
@Ignore("Have been broken for ages, maybe not worth fixing")
962+
@Disabled("Have been broken for ages, maybe not worth fixing")
963963
@Test
964964
public void testEnglishUs_unorderedCtrlC() {
965965
runWithLayout(LAYOUT_ENGLISH_US, () -> {
@@ -981,7 +981,7 @@ public void testEnglishUs_unorderedCtrlC() {
981981
* Not yet fixed: SWT doesn't report second SWT.KeyUp when two letters
982982
* were pressed and released out of order
983983
*/
984-
@Ignore("Have been broken for ages, maybe not worth fixing")
984+
@Disabled("Have been broken for ages, maybe not worth fixing")
985985
@Test
986986
public void testEnglishUs_multipleLetters() {
987987
runWithLayout(LAYOUT_ENGLISH_US, () -> {

tests/org.eclipse.swt.tests.win32/JUnit Tests/org/eclipse/swt/tests/win32/Win32DPIUtilTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
import org.eclipse.swt.graphics.Point;
2323
import org.eclipse.swt.graphics.Rectangle;
2424
import org.eclipse.swt.internal.Win32DPIUtils;
25-
import org.junit.Test;
25+
import org.junit.jupiter.api.Test;
26+
2627
/**
2728
* Automated Test Suite for class org.eclipse.swt.internal.Win32DPIUtils
2829
*

tests/org.eclipse.swt.tests.win32/JUnit Tests/org/eclipse/swt/tests/win32/widgets/TestTreeColumn.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*******************************************************************************/
1414
package org.eclipse.swt.tests.win32.widgets;
1515

16-
import static org.junit.Assert.assertEquals;
16+
import static org.junit.jupiter.api.Assertions.assertEquals;
1717

1818
import java.lang.reflect.InvocationTargetException;
1919
import java.lang.reflect.Method;
@@ -24,7 +24,7 @@
2424
import org.eclipse.swt.widgets.Shell;
2525
import org.eclipse.swt.widgets.Tree;
2626
import org.eclipse.swt.widgets.TreeColumn;
27-
import org.junit.Test;
27+
import org.junit.jupiter.api.Test;
2828

2929
public class TestTreeColumn {
3030

0 commit comments

Comments
 (0)