|
16 | 16 | package org.openrewrite.marketplace; |
17 | 17 |
|
18 | 18 | import org.junit.jupiter.api.Test; |
| 19 | +import org.junit.jupiter.api.io.TempDir; |
19 | 20 | import org.openrewrite.Recipe; |
20 | 21 | import org.openrewrite.config.ClasspathScanningLoader; |
21 | 22 | import org.openrewrite.config.Environment; |
22 | 23 | import org.openrewrite.config.RecipeDescriptor; |
| 24 | +import org.openrewrite.internal.StringUtils; |
23 | 25 |
|
24 | 26 | import java.io.File; |
| 27 | +import java.io.IOException; |
| 28 | +import java.net.URL; |
| 29 | +import java.net.URLClassLoader; |
| 30 | +import java.nio.file.Files; |
25 | 31 | import java.nio.file.Path; |
26 | 32 | import java.nio.file.Paths; |
27 | 33 | import java.util.ArrayList; |
| 34 | +import java.util.Enumeration; |
28 | 35 | import java.util.List; |
29 | 36 | import java.util.Properties; |
30 | 37 |
|
@@ -77,4 +84,83 @@ void loadRecipeWithIsolatedClassLoader() { |
77 | 84 | .as("Recipe should be loaded through RecipeClassLoader for isolation") |
78 | 85 | .isInstanceOf(RecipeClassLoader.class); |
79 | 86 | } |
| 87 | + |
| 88 | + @Test |
| 89 | + void resourcesShouldBeChildLoaded(@TempDir Path tempDir) throws IOException { |
| 90 | + Path lib1 = tempDir.resolve("lib1"); |
| 91 | + Files.createDirectories(lib1); |
| 92 | + Path file1 = lib1.resolve("rewrite.txt"); |
| 93 | + Files.write(file1, "file1".getBytes()); |
| 94 | + Path lib2 = tempDir.resolve("lib2"); |
| 95 | + Files.createDirectories(lib2); |
| 96 | + Path file2 = lib2.resolve("rewrite.txt"); |
| 97 | + Files.write(file2, "file2".getBytes()); |
| 98 | + |
| 99 | + try (URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{lib1.toUri().toURL()})) { |
| 100 | + try (RecipeClassLoader classLoader = new RecipeClassLoader(new URL[]{lib2.toUri().toURL()}, urlClassLoader)) { |
| 101 | + String text = StringUtils.readFully(classLoader.getResourceAsStream("rewrite.txt")); |
| 102 | + assertThat(text).isEqualTo("file2"); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + void resourcesShouldFindFromParentLast(@TempDir Path tempDir) throws IOException { |
| 109 | + Path lib1 = tempDir.resolve("lib1"); |
| 110 | + Files.createDirectories(lib1); |
| 111 | + Path file1 = lib1.resolve("rewrite.txt"); |
| 112 | + Files.write(file1, "file1".getBytes()); |
| 113 | + Path lib2 = tempDir.resolve("lib2"); |
| 114 | + Files.createDirectories(lib2); |
| 115 | + |
| 116 | + try (URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{lib1.toUri().toURL()})) { |
| 117 | + try (RecipeClassLoader classLoader = new RecipeClassLoader(new URL[]{lib2.toUri().toURL()}, urlClassLoader)) { |
| 118 | + String text = StringUtils.readFully(classLoader.getResourceAsStream("rewrite.txt")); |
| 119 | + assertThat(text).isEqualTo("file1"); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + void allResourcesShouldFindFromChildFirst(@TempDir Path tempDir) throws IOException { |
| 126 | + Path lib1 = tempDir.resolve("lib1"); |
| 127 | + Files.createDirectories(lib1); |
| 128 | + Path file1 = lib1.resolve("rewrite.txt"); |
| 129 | + Files.write(file1, "file1".getBytes()); |
| 130 | + Path lib2 = tempDir.resolve("lib2"); |
| 131 | + Files.createDirectories(lib2); |
| 132 | + Path file2 = lib2.resolve("rewrite.txt"); |
| 133 | + Files.write(file2, "file2".getBytes()); |
| 134 | + |
| 135 | + try (URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{lib1.toUri().toURL()})) { |
| 136 | + try (RecipeClassLoader classLoader = new RecipeClassLoader(new URL[]{lib2.toUri().toURL()}, urlClassLoader)) { |
| 137 | + Enumeration<URL> resources = classLoader.getResources("rewrite.txt"); |
| 138 | + assertThat(resources.hasMoreElements()).isTrue(); |
| 139 | + assertThat(resources.nextElement().toString()).contains("lib2/rewrite.txt"); |
| 140 | + assertThat(resources.hasMoreElements()).isTrue(); |
| 141 | + assertThat(resources.nextElement().toString()).contains("lib1/rewrite.txt"); |
| 142 | + assertThat(resources.hasMoreElements()).isFalse(); |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + void allResourcesShouldFindFromParentLast(@TempDir Path tempDir) throws IOException { |
| 149 | + Path lib1 = tempDir.resolve("lib1"); |
| 150 | + Files.createDirectories(lib1); |
| 151 | + Path file1 = lib1.resolve("rewrite.txt"); |
| 152 | + Files.write(file1, "file1".getBytes()); |
| 153 | + Path lib2 = tempDir.resolve("lib2"); |
| 154 | + Files.createDirectories(lib2); |
| 155 | + |
| 156 | + try (URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{lib1.toUri().toURL()})) { |
| 157 | + try (RecipeClassLoader classLoader = new RecipeClassLoader(new URL[]{lib2.toUri().toURL()}, urlClassLoader)) { |
| 158 | + Enumeration<URL> resources = classLoader.getResources("rewrite.txt"); |
| 159 | + assertThat(resources.hasMoreElements()).isTrue(); |
| 160 | + assertThat(resources.nextElement().toString()).contains("lib1/rewrite.txt"); |
| 161 | + assertThat(resources.hasMoreElements()).isFalse(); |
| 162 | + |
| 163 | + } |
| 164 | + } |
| 165 | + } |
80 | 166 | } |
0 commit comments