Skip to content

Commit c3df782

Browse files
jonahgrahamakurtakov
authored andcommitted
Convert tests to JUnit5
Because most all the tests remaining tests are in huge class hierarchy under Test_org_eclipse_swt_widgets_Widget they all need changing at once. The tests that are not under Test_org_eclipse_swt_widgets_Widget are generally pretty small, so included here to fully remove JUnit4 dependency. This means that junit-vintage-engine and org.junit removed from Require-Bundle of the manifest. Messages have been removed from assertions that didn't add value. The order of parameters to assertEquals and others changed, and for most of the calls I removed the message based on previous guidance. I tried to leave in messages that were more useful for documenting or diagnosing errors. But I removed messages that more or less repeated as a string what the item being tested is. A lot of things work quite differently in JUnit5. Here are some highlights that have affected or benefited this change: There is a better way of integrating screenshots into failed tests as tests can be intercepted between the test completion and the `@AfterEach` methods. Therefore we can take a screenshot prior to disposal, and then let the tear down message cleanup in a less convoluted way. See Test_org_eclipse_swt_widgets_Widget.afterTestExecutionCallback and tearDown methods to see this in action. The way to get a test name is quite different. Instead of a TestName `@Rule` a `TestInfo` can be obtained. The name returned is not exactly the same format, but where the name needed a specific format, I adjusted the code appropriately. See Test_org_eclipse_swt_browser_Browser.testInfo and Test_org_eclipse_swt_widgets_Widget.testInfo for a couple of use cases. Whole class parameterized tests don't really exist in a simple way in JUnit5. As there was very little use of this, rather than having complicated setup, I used sub-classes to provide the parameterization. See for example Test_org_eclipse_swt_widgets_DateTime and its new subclasses.
1 parent 12ac449 commit c3df782

File tree

84 files changed

+2290
-2334
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+2290
-2334
lines changed

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/graphics/ImageDataTestHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
package org.eclipse.swt.tests.graphics;
1616

17-
import static org.junit.Assert.assertArrayEquals;
17+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
1818

1919
import java.lang.reflect.InvocationTargetException;
2020
import java.lang.reflect.Method;

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/AllBrowserTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
@Suite
2121
@SelectClasses({
2222
Test_org_eclipse_swt_browser_Browser.class,
23+
Test_org_eclipse_swt_browser_Browser_IE.class
2324
})
2425
public class AllBrowserTests {
2526
}

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/AllWidgetTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@
4848
Test_org_eclipse_swt_widgets_Composite.class, //
4949
Test_org_eclipse_swt_widgets_CoolBar.class, //
5050
// Failing test: Test_org_eclipse_swt_widgets_CoolItem.class, //
51-
Test_org_eclipse_swt_widgets_DateTime.class, //
51+
Test_org_eclipse_swt_widgets_DateTime_Style_CALENDAR.class, //
52+
Test_org_eclipse_swt_widgets_DateTime_Style_DATE.class, //
53+
Test_org_eclipse_swt_widgets_DateTime_Style_TIME.class, //
5254
Test_org_eclipse_swt_widgets_DirectoryDialog.class, //
5355
Test_org_eclipse_swt_widgets_Event.class, //
5456
Test_org_eclipse_swt_widgets_ExpandBar.class, //

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/ImageTestUtil.java

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

16-
import static org.junit.Assert.assertEquals;
17-
import static org.junit.Assert.assertNotEquals;
18-
import static org.junit.Assert.assertNotNull;
19-
import static org.junit.Assert.fail;
16+
import static org.junit.jupiter.api.Assertions.assertEquals;
17+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
18+
import static org.junit.jupiter.api.Assertions.assertNotNull;
19+
import static org.junit.jupiter.api.Assertions.fail;
2020

2121
import java.util.function.BiFunction;
2222

@@ -64,12 +64,12 @@ public static void assertImagesNotEqual(ImageData[] expected, ImageData[] actual
6464
public static void assertImagesEqual(ImageData[] expected, ImageData[] actual) {
6565
assertNotNull(expected);
6666
assertNotNull(actual);
67-
assertEquals("Different number of frames.", expected.length, actual.length);
67+
assertEquals(expected.length, actual.length, "Different number of frames.");
6868
BiFunction<String, Integer, String> formatMsg = (msg, i) -> expected.length == 1 ? msg + "."
6969
: msg + " in frame " + i + ".";
7070
for (int i = 0; i < expected.length; i++) {
71-
assertEquals(formatMsg.apply("Different width", i), expected[i].width, actual[i].width);
72-
assertEquals(formatMsg.apply("Different height", i), expected[i].height, actual[i].height);
71+
assertEquals(expected[i].width, actual[i].width, formatMsg.apply("Different width", i));
72+
assertEquals(expected[i].height, actual[i].height, formatMsg.apply("Different height", i));
7373
// improve performance in case the frame has a global fixed alpha value
7474
int expectedFixAlpha = getEffectiveAlpha(expected[i], -1, -1);
7575
int actualFixAlpha = getEffectiveAlpha(actual[i], -1, -1);
@@ -79,15 +79,15 @@ public static void assertImagesEqual(ImageData[] expected, ImageData[] actual) {
7979
expected[i].getPixels(0, y, expected[i].width, expectedLine, 0);
8080
actual[i].getPixels(0, y, actual[i].width, actualLine, 0);
8181
for (int x = 0; x < expected[i].width; x++) {
82-
assertEquals(formatMsg.apply("Different color at x=" + x + ", y=" + y, i),
83-
expected[i].palette.getRGB(expectedLine[x]), actual[i].palette.getRGB(actualLine[x]));
82+
assertEquals(expected[i].palette.getRGB(expectedLine[x]), actual[i].palette.getRGB(actualLine[x]),
83+
formatMsg.apply("Different color at x=" + x + ", y=" + y, i));
8484
int expectedAlpha = expectedFixAlpha < 0 ? getEffectiveAlpha(expected[i], x, y) : expectedFixAlpha;
8585
int actualAlpha = actualFixAlpha < 0 ? getEffectiveAlpha(actual[i], x, y) : actualFixAlpha;
8686
if (expectedAlpha != actualAlpha) {
87-
assertEquals(formatMsg.apply("Different alpha at x=" + x + ", y=" + y, i), expectedAlpha,
88-
actualAlpha);
87+
assertEquals(expectedAlpha, actualAlpha,
88+
formatMsg.apply("Different alpha at x=" + x + ", y=" + y, i));
8989
}
90-
assertNotEquals(formatMsg.apply("Invalid alpha at x=" + x + ", y=" + y, i), -1, actualAlpha);
90+
assertNotEquals(-1, actualAlpha, formatMsg.apply("Invalid alpha at x=" + x + ", y=" + y, i));
9191
}
9292
}
9393
}

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/JSVGRasterizerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
package org.eclipse.swt.tests.junit;
1515

1616
import static org.eclipse.swt.tests.junit.SwtTestUtil.assertSWTProblem;
17-
import static org.junit.Assert.assertThrows;
1817
import static org.junit.jupiter.api.Assertions.assertEquals;
18+
import static org.junit.jupiter.api.Assertions.assertThrows;
1919

2020
import java.io.ByteArrayInputStream;
2121
import java.nio.charset.StandardCharsets;

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/SwtTestUtil.java

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

1616

17-
import static org.hamcrest.CoreMatchers.is;
18-
import static org.hamcrest.MatcherAssert.assertThat;
19-
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.assertTrue;
21-
import static org.junit.Assert.fail;
17+
import static org.junit.jupiter.api.Assertions.assertEquals;
2218
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;
2321

2422
import java.io.IOException;
2523
import java.io.InputStream;
@@ -49,7 +47,6 @@
4947
import org.eclipse.swt.widgets.Listener;
5048
import org.eclipse.swt.widgets.Shell;
5149
import org.eclipse.test.Screenshots;
52-
import org.junit.rules.TemporaryFolder;
5350

5451
public class SwtTestUtil {
5552
/**
@@ -130,19 +127,19 @@ public class SwtTestUtil {
130127
public static void assertSWTProblem(String message, int expectedCode, Throwable actualThrowable) {
131128
if (actualThrowable instanceof SWTError) {
132129
SWTError error = (SWTError) actualThrowable;
133-
assertEquals(message, expectedCode, error.code);
130+
assertEquals(expectedCode, error.code, message);
134131
} else if (actualThrowable instanceof SWTException) {
135132
SWTException exception = (SWTException) actualThrowable;
136-
assertEquals(message, expectedCode, exception.code);
133+
assertEquals(expectedCode, exception.code, message);
137134
} else {
138135
try {
139136
SWT.error(expectedCode);
140137
} catch (Throwable expectedThrowable) {
141138
if (actualThrowable.getMessage().length() > expectedThrowable.getMessage().length()) {
142-
assertTrue(message, actualThrowable.getMessage().startsWith(expectedThrowable.getMessage()));
139+
assertTrue(actualThrowable.getMessage().startsWith(expectedThrowable.getMessage()), message);
143140
}
144141
else {
145-
assertEquals(message, expectedThrowable.getMessage(), actualThrowable.getMessage());
142+
assertEquals(expectedThrowable.getMessage(), actualThrowable.getMessage(), message);
146143
}
147144
}
148145
}
@@ -369,7 +366,7 @@ public static void assertSimilarBrightness(String message, int expected, int act
369366
// 2) and ensure brightness is within 12.5% of the range.
370367
double expectedIntensity = getBrightness(expected);
371368
double actualIntensity = getBrightness(actual);
372-
assertEquals(message, expectedIntensity, actualIntensity, 255f / 8);
369+
assertEquals(expectedIntensity, actualIntensity, 255f / 8, message);
373370
}
374371
}
375372

@@ -478,7 +475,7 @@ public static void waitShellActivate(Runnable trigger, Shell shell) {
478475
// Something went wrong? Get more info to diagnose
479476
Screenshots.takeScreenshot(SwtTestUtil.class, "waitShellActivate-" + System.currentTimeMillis());
480477
dumpShellState(System.out);
481-
assertThat("Shell did not activate", shell.getDisplay().getActiveShell(), is(shell));
478+
assertEquals(shell.getDisplay().getActiveShell(), shell, "Shell did not activate");
482479
fail("SWT.Activate was not received but Shell is (incorrectly?) reported active");
483480
}
484481

@@ -586,13 +583,12 @@ public static boolean hasPixelNotMatching(Image image, Color nonMatchingColor, R
586583
return false;
587584
}
588585

589-
public static Path getPath(String fileName, TemporaryFolder tempFolder) {
590-
Path path = tempFolder.getRoot().toPath();
591-
Path filePath = path.resolve("image-resources").resolve(Path.of(fileName));
592-
return getPath(fileName, filePath);
586+
public static Path getPath(String fileName, Path tempFolder) {
587+
Path filePath = tempFolder.resolve("image-resources").resolve(Path.of(fileName));
588+
return copyFile(fileName, filePath);
593589
}
594590

595-
public static Path getPath(String sourceFilename, Path destinationPath) {
591+
public static Path copyFile(String sourceFilename, Path destinationPath) {
596592
if (!Files.isRegularFile(destinationPath)) {
597593
// Extract resource on the classpath to a temporary file to ensure it's
598594
// available as plain file, even if this bundle is packed as jar

0 commit comments

Comments
 (0)