Skip to content

Commit fd9f122

Browse files
committed
Convert GC test to JUnit 5
Using JUnit 5 Assumptions in JUnit 4 Test led to assumptions being treated as failures rather than skipped tests. Use newer JUnit features like assertThrows and etc.
1 parent 032fa73 commit fd9f122

File tree

1 file changed

+94
-121
lines changed

1 file changed

+94
-121
lines changed

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

Lines changed: 94 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@
1818
import static org.hamcrest.MatcherAssert.assertThat;
1919
import static org.hamcrest.Matchers.is;
2020
import static org.hamcrest.Matchers.not;
21-
import static org.junit.Assert.assertArrayEquals;
22-
import static org.junit.Assert.assertEquals;
23-
import static org.junit.Assert.assertFalse;
24-
import static org.junit.Assert.assertNotEquals;
25-
import static org.junit.Assert.assertNotNull;
26-
import static org.junit.Assert.assertNull;
27-
import static org.junit.Assert.assertTrue;
28-
import static org.junit.Assert.fail;
21+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
22+
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
import static org.junit.jupiter.api.Assertions.assertFalse;
24+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
25+
import static org.junit.jupiter.api.Assertions.assertNotNull;
26+
import static org.junit.jupiter.api.Assertions.assertNull;
27+
import static org.junit.jupiter.api.Assertions.assertThrows;
28+
import static org.junit.jupiter.api.Assertions.assertTrue;
29+
import static org.junit.jupiter.api.Assumptions.assumeFalse;
2930
import static org.junit.jupiter.api.Assumptions.assumeTrue;
3031

3132
import java.util.concurrent.atomic.AtomicReference;
@@ -48,10 +49,9 @@
4849
import org.eclipse.swt.widgets.Canvas;
4950
import org.eclipse.swt.widgets.Display;
5051
import org.eclipse.swt.widgets.Shell;
51-
import org.junit.After;
52-
import org.junit.Assume;
53-
import org.junit.Before;
54-
import org.junit.Test;
52+
import org.junit.jupiter.api.AfterEach;
53+
import org.junit.jupiter.api.BeforeEach;
54+
import org.junit.jupiter.api.Test;
5555

5656
/**
5757
* Automated Test Suite for class org.eclipse.swt.graphics.GC
@@ -61,7 +61,7 @@
6161
@SuppressWarnings("restriction")
6262
public class Test_org_eclipse_swt_graphics_GC {
6363

64-
@Before
64+
@BeforeEach
6565
public void setUp() {
6666
display = Display.getDefault();
6767
shell = new Shell(display);
@@ -70,7 +70,7 @@ public void setUp() {
7070
gc = new GC(image);
7171
}
7272

73-
@After
73+
@AfterEach
7474
public void tearDown() {
7575
gc.dispose();
7676
image.dispose();
@@ -79,54 +79,52 @@ public void tearDown() {
7979

8080
@Test
8181
public void test_ConstructorLorg_eclipse_swt_graphics_Drawable() {
82-
try {
83-
GC gc = new GC(null);
84-
gc.dispose();
85-
fail("No exception thrown for drawable == null");
86-
} catch (IllegalArgumentException e) {
87-
assertSWTProblem("Incorrect exception thrown for drawable == null", SWT.ERROR_NULL_ARGUMENT, e);
88-
}
82+
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> new GC(null),
83+
"No exception thrown for drawable == null");
84+
assertSWTProblem("Incorrect exception thrown for drawable == null", SWT.ERROR_NULL_ARGUMENT, e);
8985

90-
Image image = null;
91-
GC gc1 = null, gc2 = null;
92-
try {
93-
image = new Image(display, 10, 10);
94-
gc1 = new GC(image);
95-
gc2 = new GC(image);
96-
fail("No exception thrown for more than one GC on one image");
97-
} catch (IllegalArgumentException e) {
98-
assertSWTProblem("Incorrect exception thrown for more than one GC on one image", SWT.ERROR_INVALID_ARGUMENT, e);
99-
} finally {
100-
if (image != null) image.dispose();
101-
if (gc1 != null) gc1.dispose();
102-
if (gc2 != null) gc2.dispose();
103-
}
86+
IllegalArgumentException e1 = assertThrows(IllegalArgumentException.class, () -> {
87+
Image image = null;
88+
GC gc1 = null, gc2 = null;
89+
try {
90+
image = new Image(display, 10, 10);
91+
gc1 = new GC(image);
92+
gc2 = new GC(image);
93+
} finally {
94+
if (image != null)
95+
image.dispose();
96+
if (gc1 != null)
97+
gc1.dispose();
98+
if (gc2 != null)
99+
gc2.dispose();
100+
}
101+
}, "No exception thrown for more than one GC on one image");
102+
assertSWTProblem("Incorrect exception thrown for more than one GC on one image", SWT.ERROR_INVALID_ARGUMENT, e1);
104103
}
105104

106105
@Test
107106
public void test_ConstructorLorg_eclipse_swt_graphics_DrawableI() {
108-
try {
109-
GC gc = new GC(null, SWT.LEFT_TO_RIGHT);
110-
gc.dispose();
111-
fail("No exception thrown for drawable == null");
112-
} catch (IllegalArgumentException e) {
113-
assertSWTProblem("Incorrect exception thrown for drawable == null", SWT.ERROR_NULL_ARGUMENT, e);
114-
}
107+
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> new GC(null, SWT.LEFT_TO_RIGHT),
108+
"No exception thrown for drawable == null");
109+
assertSWTProblem("Incorrect exception thrown for drawable == null", SWT.ERROR_NULL_ARGUMENT, e);
115110

116-
Image image = null;
117-
GC gc1 = null, gc2 = null;
118-
try {
119-
image = new Image(display, 10, 10);
120-
gc1 = new GC(image, SWT.RIGHT_TO_LEFT);
121-
gc2 = new GC(image, SWT.LEFT_TO_RIGHT);
122-
fail("No exception thrown for more than one GC on one image");
123-
} catch (IllegalArgumentException e) {
124-
assertSWTProblem("Incorrect exception thrown for more than one GC on one image", SWT.ERROR_INVALID_ARGUMENT, e);
125-
} finally {
126-
if (image != null) image.dispose();
127-
if (gc1 != null) gc1.dispose();
128-
if (gc2 != null) gc2.dispose();
129-
}
111+
IllegalArgumentException e1 = assertThrows(IllegalArgumentException.class, () -> {
112+
Image image = null;
113+
GC gc1 = null, gc2 = null;
114+
try {
115+
image = new Image(display, 10, 10);
116+
new GC(image, SWT.RIGHT_TO_LEFT);
117+
new GC(image, SWT.LEFT_TO_RIGHT);
118+
} finally {
119+
if (image != null)
120+
image.dispose();
121+
if (gc1 != null)
122+
gc1.dispose();
123+
if (gc2 != null)
124+
gc2.dispose();
125+
}
126+
}, "No exception thrown for more than one GC on one image");
127+
assertSWTProblem("Incorrect exception thrown for more than one GC on one image", SWT.ERROR_INVALID_ARGUMENT, e1);
130128

131129
Canvas canvas = new Canvas(shell, SWT.NULL);
132130
GC testGC = new GC(canvas, SWT.RIGHT_TO_LEFT);
@@ -138,6 +136,10 @@ public void test_ConstructorLorg_eclipse_swt_graphics_DrawableI() {
138136

139137
@Test
140138
public void test_copyAreaIIIIII() {
139+
// This test verifies pixel-level color values after a copyArea() operation.
140+
// Such pixel-accurate checks are only reliable at 100% zoom due to fractional scaling.
141+
assumeTrue(DPIUtil.getDeviceZoom() == 100, "Skipping test due to non-100% zoom");
142+
141143
Color white = display.getSystemColor(SWT.COLOR_WHITE);
142144
Color blue = display.getSystemColor(SWT.COLOR_BLUE);
143145
RGB whiteRGB = getRealRGB(white);
@@ -156,22 +158,22 @@ public void test_copyAreaIIIIII() {
156158
ImageData imageData = image.getImageData();
157159
PaletteData palette = imageData.palette;
158160

159-
// This test verifies pixel-level color values after a copyArea() operation.
160-
// Such pixel-accurate checks are only reliable at 100% zoom due to fractional scaling.
161-
assumeTrue(DPIUtil.getDeviceZoom() == 100, "Skipping test due to non-100% zoom");
162-
163161
int pixel = imageData.getPixel(destX + 4, destY);
164-
assertEquals(":a:", whiteRGB, palette.getRGB(pixel));
162+
assertEquals(whiteRGB, palette.getRGB(pixel));
165163
pixel = imageData.getPixel(destX + 6 , destY);
166-
assertEquals(":b:", blueRGB, palette.getRGB(pixel));
164+
assertEquals(blueRGB, palette.getRGB(pixel));
167165
pixel = imageData.getPixel(destX + 10, destY);
168-
assertEquals(":c:", blueRGB, palette.getRGB(pixel));
166+
assertEquals(blueRGB, palette.getRGB(pixel));
169167
pixel = imageData.getPixel(destX + 12, destY);
170-
assertEquals(":d:", whiteRGB, palette.getRGB(pixel));
168+
assertEquals(whiteRGB, palette.getRGB(pixel));
171169
}
172170

173171
@Test
174172
public void test_copyAreaIIIIII_overlapingSourceTarget() {
173+
// This test verifies pixel-level color values after a copyArea() operation.
174+
// Such pixel-accurate checks are only reliable at 100% zoom due to fractional scaling.
175+
assumeTrue(DPIUtil.getDeviceZoom() == 100, "Skipping test due to non-100% zoom");
176+
175177
Color red= display.getSystemColor(SWT.COLOR_RED);
176178
Color blue = display.getSystemColor(SWT.COLOR_BLUE);
177179
RGB redRGB = getRealRGB(red);
@@ -197,10 +199,6 @@ public void test_copyAreaIIIIII_overlapingSourceTarget() {
197199
imageData = image.getImageData();
198200
palette = imageData.palette;
199201

200-
// This test verifies pixel-level color values after a copyArea() operation.
201-
// Such pixel-accurate checks are only reliable at 100% zoom due to fractional scaling.
202-
assumeTrue(DPIUtil.getDeviceZoom() == 100, "Skipping test due to non-100% zoom");
203-
204202
pixel = imageData.getPixel(0, 105);
205203
assertEquals(redRGB, palette.getRGB(pixel));
206204
pixel = imageData.getPixel(0, 145);
@@ -214,6 +212,10 @@ public void test_copyAreaIIIIII_overlapingSourceTarget() {
214212

215213
@Test
216214
public void test_copyAreaLorg_eclipse_swt_graphics_ImageII() {
215+
// This test verifies pixel-level color values after a copyArea() operation.
216+
// Such pixel-accurate checks are only reliable at 100% zoom due to fractional scaling.
217+
assumeTrue(DPIUtil.getDeviceZoom() == 100, "Skipping test due to non-100% zoom");
218+
217219
Color white = display.getSystemColor(SWT.COLOR_WHITE);
218220
Color blue = display.getSystemColor(SWT.COLOR_BLUE);
219221
RGB whiteRGB = getRealRGB(white);
@@ -228,18 +230,14 @@ public void test_copyAreaLorg_eclipse_swt_graphics_ImageII() {
228230
ImageData imageData = image.getImageData();
229231
PaletteData palette = imageData.palette;
230232

231-
// This test verifies pixel-level color values after a copyArea() operation.
232-
// Such pixel-accurate checks are only reliable at 100% zoom due to fractional scaling.
233-
assumeTrue(DPIUtil.getDeviceZoom() == 100, "Skipping test due to non-100% zoom");
234-
235233
int pixel = imageData.getPixel(4, 0);
236-
assertEquals(":a:", whiteRGB, palette.getRGB(pixel));
234+
assertEquals(whiteRGB, palette.getRGB(pixel));
237235
pixel = imageData.getPixel(5, 0);
238-
assertEquals(":b:", blueRGB, palette.getRGB(pixel));
236+
assertEquals(blueRGB, palette.getRGB(pixel));
239237
pixel = imageData.getPixel(10, 0);
240-
assertEquals(":c:", blueRGB, palette.getRGB(pixel));
238+
assertEquals(blueRGB, palette.getRGB(pixel));
241239
pixel = imageData.getPixel(11, 0);
242-
assertEquals(":d:", whiteRGB, palette.getRGB(pixel));
240+
assertEquals(whiteRGB, palette.getRGB(pixel));
243241
image.dispose();
244242
}
245243

@@ -288,12 +286,7 @@ public void test_drawImageLorg_eclipse_swt_graphics_ImageII() {
288286
gc.drawImage(image, 100, 100);
289287
gc.drawImage(imageTransparent, 130, 100);
290288
gc.drawImage(imageAlpha, 160, 100);
291-
try {
292-
gc.drawImage(null, 100, 100);
293-
fail("No exception thrown");
294-
}
295-
catch (IllegalArgumentException e) {
296-
}
289+
assertThrows(IllegalArgumentException.class, () -> gc.drawImage(null, 100, 100));
297290
image.dispose();
298291
imageTransparent.dispose();
299292
imageAlpha.dispose();
@@ -329,12 +322,7 @@ public void test_drawImageLorg_eclipse_swt_graphics_ImageIIIIIIII() {
329322
gc.drawImage(image, 10, 5, 20, 15, 100, 120, 50, 60);
330323
gc.drawImage(imageTransparent, 10, 5, 20, 15, 100, 120, 10, 10);
331324
gc.drawImage(imageAlpha, 10, 5, 20, 15, 100, 120, 20, 15);
332-
try {
333-
gc.drawImage(null, 10, 5, 20, 15, 100, 120, 50, 60);
334-
fail("No exception thrown"); //should never get here
335-
}
336-
catch (IllegalArgumentException e) {
337-
}
325+
assertThrows(IllegalArgumentException.class, () -> gc.drawImage(null, 10, 5, 20, 15, 100, 120, 50, 60));
338326
image.dispose();
339327
imageAlpha.dispose();
340328
imageTransparent.dispose();
@@ -551,18 +539,11 @@ public void test_setBackgroundLorg_eclipse_swt_graphics_Color() {
551539
Color color = new Color(255, 0, 0);
552540
gc.setBackground(color);
553541
assertEquals(color, gc.getBackground());
554-
try {
555-
gc.setBackground(null);
556-
fail("No exception thrown for null color");
557-
} catch (IllegalArgumentException e) {
558-
}
542+
assertThrows(IllegalArgumentException.class, () -> gc.setBackground(null), "No exception thrown for null color");
559543
assertEquals(gc.getBackground(),gc.getBackground());
560544
color.dispose();
561-
try {
562-
gc.setBackground(color);
563-
fail("No exception thrown for color disposed");
564-
} catch (IllegalArgumentException e) {
565-
}
545+
assertThrows(IllegalArgumentException.class, () -> gc.setBackground(color),
546+
"No exception thrown for color disposed");
566547
}
567548

568549
@Test
@@ -618,18 +599,11 @@ public void test_setForegroundLorg_eclipse_swt_graphics_Color() {
618599
Color color = new Color(255, 0, 0);
619600
gc.setForeground(color);
620601
assertEquals(color, gc.getForeground());
621-
try {
622-
gc.setForeground(null);
623-
fail("No exception thrown for null color");
624-
} catch (IllegalArgumentException e) {
625-
}
626-
assertEquals(gc.getForeground(),gc.getForeground());
602+
assertThrows(IllegalArgumentException.class, () -> gc.setForeground(null), "No exception thrown for null color");
603+
assertEquals(gc.getForeground(), gc.getForeground());
627604
color.dispose();
628-
try {
629-
gc.setForeground(color);
630-
fail("No exception thrown for color disposed");
631-
} catch (IllegalArgumentException e) {
632-
}
605+
assertThrows(IllegalArgumentException.class, () -> gc.setForeground(color),
606+
"No exception thrown for color disposed");
633607
}
634608

635609
@Test
@@ -643,13 +617,12 @@ public void test_setForegroundLorg_eclipse_swt_graphics_Color() {
643617
float miterLimit = 2.6f;
644618
LineAttributes passedLineAttributes = new LineAttributes(width, cap, join, style, dashes, dashOffset, miterLimit);
645619
gc.setLineAttributes(passedLineAttributes);
646-
assertEquals("unexpected line width", width, gc.getLineWidth());
647-
assertEquals("unexpected line cap", cap, gc.getLineCap());
648-
assertEquals("unexpected line join", join, gc.getLineJoin());
649-
assertEquals("unexpected line style", style, gc.getLineStyle());
650-
assertEquals("actual line attributes differ from the ones that have been set",
651-
new LineAttributes(width, cap, join, style, dashes, dashOffset, miterLimit), gc.getLineAttributes());
652-
assertEquals("setter call changed line width", width, passedLineAttributes.width, 0.0f);
620+
assertEquals(width, gc.getLineWidth(), "unexpected line width");
621+
assertEquals(cap, gc.getLineCap(), "unexpected line cap");
622+
assertEquals(join, gc.getLineJoin(), "unexpected line join");
623+
assertEquals(style, gc.getLineStyle(), "unexpected line style");
624+
assertEquals(new LineAttributes(width, cap, join, style, dashes, dashOffset, miterLimit), gc.getLineAttributes(), "actual line attributes differ from the ones that have been set");
625+
assertEquals(width, passedLineAttributes.width, 0.0f, "setter call changed line width");
653626

654627
gc.setLineAttributes(new LineAttributes(1));
655628
assertEquals(new LineAttributes(1), gc.getLineAttributes());
@@ -684,12 +657,12 @@ public void test_setLineWidthI() {
684657

685658
@Test
686659
public void test_setLineWidthI_withDeviceScaling() {
687-
executeWithNonDefaultDeviceZoom(() -> test_setLineWidthI());
660+
executeWithNonDefaultDeviceZoom(this::test_setLineWidthI);
688661
}
689662

690663
@Test
691664
public void test_setLineDash$I() {
692-
int[] dashes = new int[] { 5, 1, 3 };
665+
int[] dashes = { 5, 1, 3 };
693666
gc.setLineDash(dashes);
694667
assertArrayEquals(dashes, gc.getLineDash());
695668
gc.setLineDash(null);
@@ -698,7 +671,7 @@ public void test_setLineWidthI_withDeviceScaling() {
698671

699672
@Test
700673
public void test_setLineDash$I_withDeviceScaling() {
701-
executeWithNonDefaultDeviceZoom(() -> test_setLineDash$I());
674+
executeWithNonDefaultDeviceZoom(this::test_setLineDash$I);
702675
}
703676

704677
@Test
@@ -739,7 +712,7 @@ public void test_toString() {
739712

740713
@Test
741714
public void test_bug493455_drawImageAlpha_srcPos() {
742-
Assume.assumeFalse("https://github.com/eclipse-platform/eclipse.platform.swt/issues/40 causes test to fail on Mac", SwtTestUtil.isCocoa);
715+
assumeFalse(SwtTestUtil.isCocoa, "https://github.com/eclipse-platform/eclipse.platform.swt/issues/40 causes test to fail on Mac");
743716
RGB red = new RGB(255, 0, 0);
744717
RGB green = new RGB(0, 255, 0);
745718

@@ -828,7 +801,7 @@ public void test_bug1288_createGCFromImageFromNonDisplayThread() throws Interrup
828801
});
829802
thread.start();
830803
thread.join();
831-
assertNull("Creating a GC from an Image without a device threw an exception", exceptionReference.get());
804+
assertNull(exceptionReference.get(), "Creating a GC from an Image without a device threw an exception");
832805
}
833806

834807
/* custom */

0 commit comments

Comments
 (0)