Skip to content

Commit 9e57eae

Browse files
committed
Resolve resources URLs as file-URLs if running in an OSGi environment
Don't consider the 'PLUGIN_PATH' property anymore, which is set in I-build tests and whose handling doesn't work anymore. Fixes #2001
1 parent f468eca commit 9e57eae

File tree

3 files changed

+35
-26
lines changed

3 files changed

+35
-26
lines changed

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

+14-22
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,10 @@
2020
import static org.junit.Assert.assertTrue;
2121
import static org.junit.Assert.fail;
2222
import static org.junit.jupiter.api.Assertions.assertNotNull;
23-
import static org.junit.jupiter.api.Assertions.assertTrue;
2423

24+
import java.io.IOException;
25+
import java.io.InputStream;
2526
import java.io.PrintStream;
26-
import java.net.URI;
27-
import java.net.URISyntaxException;
28-
import java.net.URL;
2927
import java.nio.file.Files;
3028
import java.nio.file.Path;
3129
import java.util.concurrent.atomic.AtomicBoolean;
@@ -50,6 +48,7 @@
5048
import org.eclipse.swt.widgets.Listener;
5149
import org.eclipse.swt.widgets.Shell;
5250
import org.eclipse.test.Screenshots;
51+
import org.junit.rules.TemporaryFolder;
5352

5453
public class SwtTestUtil {
5554
/**
@@ -582,26 +581,19 @@ public static boolean hasPixelNotMatching(Image image, Color nonMatchingColor, R
582581
return false;
583582
}
584583

585-
public static String getPath(String fileName) {
586-
URI uri;
587-
String pluginPath = System.getProperty("PLUGIN_PATH");
588-
if (pluginPath == null) {
589-
URL url = SwtTestUtil.class.getResource(fileName);
590-
assertNotNull(url, "URL == null for file " + fileName);
591-
try {
592-
uri = url.toURI();
593-
} catch (URISyntaxException e) {
584+
public static String getPath(String fileName, TemporaryFolder tempFolder) {
585+
Path filePath = tempFolder.getRoot().toPath().resolve("image-resources").resolve(Path.of(fileName));
586+
if (!Files.isRegularFile(filePath)) {
587+
// Extract resource on the classpath to a temporary file to ensure it's
588+
// available as plain file, even if this bundle is packed as jar
589+
try (InputStream inStream = SwtTestUtil.class.getResourceAsStream(fileName)) {
590+
assertNotNull(inStream, "InputStream == null for file " + fileName);
591+
Files.createDirectories(filePath.getParent());
592+
Files.copy(inStream, filePath);
593+
} catch (IOException e) {
594594
throw new IllegalArgumentException(e);
595595
}
596-
} else {
597-
uri = URI.create(pluginPath + "/data/" + fileName);
598-
}
599-
// Fallback when test is locally executed as plug-in test
600-
Path path = Path.of(uri);
601-
if (!Files.exists(path)) {
602-
path = Path.of("data/" + fileName).toAbsolutePath();
603596
}
604-
assertTrue(Files.exists(path), "file not found: " + uri);
605-
return path.toString();
597+
return filePath.toString();
606598
}
607599
}

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

+12-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717

1818
import static org.eclipse.swt.tests.junit.SwtTestUtil.assertSWTProblem;
19-
import static org.eclipse.swt.tests.junit.SwtTestUtil.getPath;
2019
import static org.junit.Assert.assertEquals;
2120
import static org.junit.Assert.assertFalse;
2221
import static org.junit.Assert.assertNotNull;
@@ -49,7 +48,9 @@
4948
import org.eclipse.swt.internal.DPIUtil;
5049
import org.eclipse.swt.widgets.Display;
5150
import org.junit.Before;
51+
import org.junit.ClassRule;
5252
import org.junit.Test;
53+
import org.junit.rules.TemporaryFolder;
5354

5455
/**
5556
* Automated Test Suite for class org.eclipse.swt.graphics.Image
@@ -58,6 +59,14 @@
5859
*/
5960
@SuppressWarnings("restriction")
6061
public class Test_org_eclipse_swt_graphics_Image {
62+
63+
@ClassRule
64+
public static TemporaryFolder tempFolder = new TemporaryFolder();
65+
66+
private static String getPath(String fileName) {
67+
return SwtTestUtil.getPath(fileName, tempFolder);
68+
}
69+
6170
ImageFileNameProvider imageFileNameProvider = zoom -> {
6271
String fileName = switch (zoom) {
6372
case 100 -> "collapseall.png";
@@ -319,11 +328,11 @@ public void test_ConstructorLorg_eclipse_swt_graphics_DeviceLjava_lang_String()
319328

320329
@Test
321330
public void test_ConstructorLorg_eclipse_swt_graphics_Device_ImageFileNameProvider() {
322-
Exception e;
331+
Exception e;
323332

324333
// Null provider
325334
ImageFileNameProvider provider1 = null;
326-
e = assertThrows(IllegalArgumentException.class, ()->new Image(display, provider1));
335+
e = assertThrows(IllegalArgumentException.class, ()->new Image(display, provider1));
327336
assertSWTProblem("Incorrect exception thrown for provider == null", SWT.ERROR_NULL_ARGUMENT, e);
328337

329338
// Invalid provider

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
package org.eclipse.swt.tests.junit;
1414

1515
import static org.eclipse.swt.tests.junit.SwtTestUtil.assertSWTProblem;
16-
import static org.eclipse.swt.tests.junit.SwtTestUtil.getPath;
1716
import static org.junit.jupiter.api.Assertions.assertThrows;
1817

1918
import org.eclipse.swt.SWT;
@@ -23,7 +22,9 @@
2322
import org.eclipse.swt.graphics.ImageDataProvider;
2423
import org.eclipse.swt.graphics.ImageFileNameProvider;
2524
import org.eclipse.swt.widgets.Display;
25+
import org.junit.ClassRule;
2626
import org.junit.Test;
27+
import org.junit.rules.TemporaryFolder;
2728

2829
/**
2930
* When executed locally (outside Tycho build), this tests needs to be run as
@@ -32,6 +33,13 @@
3233
*/
3334
public class Test_org_eclipse_swt_internal_SVGRasterizer {
3435

36+
@ClassRule
37+
public static TemporaryFolder tempFolder = new TemporaryFolder();
38+
39+
private static String getPath(String fileName) {
40+
return SwtTestUtil.getPath(fileName, tempFolder);
41+
}
42+
3543
@Test
3644
public void test_ConstructorLorg_eclipse_swt_graphics_Device_ImageFileNameProvider() {
3745
ImageFileNameProvider validImageFileNameProvider = zoom -> getPath("collapseall.svg");

0 commit comments

Comments
 (0)