|
20 | 20 | import static org.junit.Assert.assertTrue;
|
21 | 21 | import static org.junit.Assert.fail;
|
22 | 22 | import static org.junit.jupiter.api.Assertions.assertNotNull;
|
23 |
| -import static org.junit.jupiter.api.Assertions.assertTrue; |
24 | 23 |
|
| 24 | +import java.io.IOException; |
| 25 | +import java.io.InputStream; |
25 | 26 | import java.io.PrintStream;
|
26 |
| -import java.net.URI; |
27 |
| -import java.net.URISyntaxException; |
28 |
| -import java.net.URL; |
29 | 27 | import java.nio.file.Files;
|
30 | 28 | import java.nio.file.Path;
|
31 | 29 | import java.util.concurrent.atomic.AtomicBoolean;
|
|
50 | 48 | import org.eclipse.swt.widgets.Listener;
|
51 | 49 | import org.eclipse.swt.widgets.Shell;
|
52 | 50 | import org.eclipse.test.Screenshots;
|
| 51 | +import org.junit.rules.TemporaryFolder; |
53 | 52 |
|
54 | 53 | public class SwtTestUtil {
|
55 | 54 | /**
|
@@ -582,26 +581,19 @@ public static boolean hasPixelNotMatching(Image image, Color nonMatchingColor, R
|
582 | 581 | return false;
|
583 | 582 | }
|
584 | 583 |
|
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) { |
594 | 594 | throw new IllegalArgumentException(e);
|
595 | 595 | }
|
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(); |
603 | 596 | }
|
604 |
| - assertTrue(Files.exists(path), "file not found: " + uri); |
605 |
| - return path.toString(); |
| 597 | + return filePath.toString(); |
606 | 598 | }
|
607 | 599 | }
|
0 commit comments