diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/SwtTestUtil.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/SwtTestUtil.java index f952c86adcc..0faa8b09a7e 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/SwtTestUtil.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/SwtTestUtil.java @@ -20,12 +20,10 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; +import java.io.IOException; +import java.io.InputStream; import java.io.PrintStream; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; import java.util.concurrent.atomic.AtomicBoolean; @@ -50,6 +48,7 @@ import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; import org.eclipse.test.Screenshots; +import org.junit.rules.TemporaryFolder; public class SwtTestUtil { /** @@ -582,26 +581,19 @@ public static boolean hasPixelNotMatching(Image image, Color nonMatchingColor, R return false; } -public static String getPath(String fileName) { - URI uri; - String pluginPath = System.getProperty("PLUGIN_PATH"); - if (pluginPath == null) { - URL url = SwtTestUtil.class.getResource(fileName); - assertNotNull(url, "URL == null for file " + fileName); - try { - uri = url.toURI(); - } catch (URISyntaxException e) { +public static String getPath(String fileName, TemporaryFolder tempFolder) { + Path filePath = tempFolder.getRoot().toPath().resolve("image-resources").resolve(Path.of(fileName)); + if (!Files.isRegularFile(filePath)) { + // Extract resource on the classpath to a temporary file to ensure it's + // available as plain file, even if this bundle is packed as jar + try (InputStream inStream = SwtTestUtil.class.getResourceAsStream(fileName)) { + assertNotNull(inStream, "InputStream == null for file " + fileName); + Files.createDirectories(filePath.getParent()); + Files.copy(inStream, filePath); + } catch (IOException e) { throw new IllegalArgumentException(e); } - } else { - uri = URI.create(pluginPath + "/data/" + fileName); - } - // Fallback when test is locally executed as plug-in test - Path path = Path.of(uri); - if (!Files.exists(path)) { - path = Path.of("data/" + fileName).toAbsolutePath(); } - assertTrue(Files.exists(path), "file not found: " + uri); - return path.toString(); + return filePath.toString(); } } diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Image.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Image.java index 64d4b4e9db8..aaad21801c6 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Image.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Image.java @@ -16,7 +16,6 @@ import static org.eclipse.swt.tests.junit.SwtTestUtil.assertSWTProblem; -import static org.eclipse.swt.tests.junit.SwtTestUtil.getPath; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -49,7 +48,9 @@ import org.eclipse.swt.internal.DPIUtil; import org.eclipse.swt.widgets.Display; import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; /** * Automated Test Suite for class org.eclipse.swt.graphics.Image @@ -58,6 +59,14 @@ */ @SuppressWarnings("restriction") public class Test_org_eclipse_swt_graphics_Image { + + @ClassRule + public static TemporaryFolder tempFolder = new TemporaryFolder(); + + private static String getPath(String fileName) { + return SwtTestUtil.getPath(fileName, tempFolder); + } + ImageFileNameProvider imageFileNameProvider = zoom -> { String fileName = switch (zoom) { case 100 -> "collapseall.png"; @@ -319,11 +328,11 @@ public void test_ConstructorLorg_eclipse_swt_graphics_DeviceLjava_lang_String() @Test public void test_ConstructorLorg_eclipse_swt_graphics_Device_ImageFileNameProvider() { - Exception e; + Exception e; // Null provider ImageFileNameProvider provider1 = null; - e = assertThrows(IllegalArgumentException.class, ()->new Image(display, provider1)); + e = assertThrows(IllegalArgumentException.class, ()->new Image(display, provider1)); assertSWTProblem("Incorrect exception thrown for provider == null", SWT.ERROR_NULL_ARGUMENT, e); // Invalid provider diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_internal_SVGRasterizer.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_internal_SVGRasterizer.java index 1b6cf117b66..6eb35f14295 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_internal_SVGRasterizer.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_internal_SVGRasterizer.java @@ -13,7 +13,6 @@ package org.eclipse.swt.tests.junit; import static org.eclipse.swt.tests.junit.SwtTestUtil.assertSWTProblem; -import static org.eclipse.swt.tests.junit.SwtTestUtil.getPath; import static org.junit.jupiter.api.Assertions.assertThrows; import org.eclipse.swt.SWT; @@ -23,7 +22,9 @@ import org.eclipse.swt.graphics.ImageDataProvider; import org.eclipse.swt.graphics.ImageFileNameProvider; import org.eclipse.swt.widgets.Display; +import org.junit.ClassRule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; /** * When executed locally (outside Tycho build), this tests needs to be run as @@ -32,6 +33,13 @@ */ public class Test_org_eclipse_swt_internal_SVGRasterizer { + @ClassRule + public static TemporaryFolder tempFolder = new TemporaryFolder(); + + private static String getPath(String fileName) { + return SwtTestUtil.getPath(fileName, tempFolder); + } + @Test public void test_ConstructorLorg_eclipse_swt_graphics_Device_ImageFileNameProvider() { ImageFileNameProvider validImageFileNameProvider = zoom -> getPath("collapseall.svg");