22 * or visit www.oracle.com if you need additional information or have any |
22 * or visit www.oracle.com if you need additional information or have any |
23 * questions. |
23 * questions. |
24 */ |
24 */ |
25 package jdk.jpackage.internal; |
25 package jdk.jpackage.internal; |
26 |
26 |
|
27 import java.io.IOException; |
|
28 import java.nio.file.Files; |
27 import java.nio.file.Path; |
29 import java.nio.file.Path; |
28 import java.util.Collections; |
30 import java.util.*; |
29 import java.util.List; |
31 import java.util.function.Consumer; |
30 import java.util.Map; |
32 import java.util.function.Predicate; |
|
33 import java.util.function.UnaryOperator; |
|
34 import java.util.stream.Collectors; |
|
35 import java.util.stream.Stream; |
31 import static org.hamcrest.CoreMatchers.equalTo; |
36 import static org.hamcrest.CoreMatchers.equalTo; |
32 import static org.hamcrest.CoreMatchers.not; |
37 import static org.hamcrest.CoreMatchers.not; |
33 import static org.junit.Assert.*; |
38 import static org.junit.Assert.*; |
|
39 import org.junit.Rule; |
34 import org.junit.Test; |
40 import org.junit.Test; |
|
41 import org.junit.rules.TemporaryFolder; |
35 |
42 |
36 |
43 |
37 public class PathGroupTest { |
44 public class PathGroupTest { |
38 public PathGroupTest() { |
45 |
39 } |
46 @Rule |
|
47 public final TemporaryFolder tempFolder = new TemporaryFolder(); |
40 |
48 |
41 @Test(expected = NullPointerException.class) |
49 @Test(expected = NullPointerException.class) |
42 public void testNullId() { |
50 public void testNullId() { |
43 new PathGroup(Map.of()).getPath(null); |
51 new PathGroup(Map.of()).getPath(null); |
44 } |
52 } |
123 assertTrue(paths.contains(aPath.resolve(PATH_FOO))); |
131 assertTrue(paths.contains(aPath.resolve(PATH_FOO))); |
124 assertTrue(paths.contains(aPath.resolve(PATH_BAR))); |
132 assertTrue(paths.contains(aPath.resolve(PATH_BAR))); |
125 assertEquals(aPath, pg2.roots().get(0)); |
133 assertEquals(aPath, pg2.roots().get(0)); |
126 } |
134 } |
127 |
135 |
|
136 @Test |
|
137 public void testTransform() throws IOException { |
|
138 for (var transform : TransformType.values()) { |
|
139 testTransform(false, transform); |
|
140 } |
|
141 } |
|
142 |
|
143 @Test |
|
144 public void testTransformWithExcludes() throws IOException { |
|
145 for (var transform : TransformType.values()) { |
|
146 testTransform(true, transform); |
|
147 } |
|
148 } |
|
149 |
|
150 enum TransformType { Copy, Move, Handler }; |
|
151 |
|
152 private void testTransform(boolean withExcludes, TransformType transform) |
|
153 throws IOException { |
|
154 final PathGroup pg = new PathGroup(Map.of(0, PATH_FOO, 1, PATH_BAR, 2, |
|
155 PATH_EMPTY, 3, PATH_BAZ)); |
|
156 |
|
157 final Path srcDir = tempFolder.newFolder().toPath(); |
|
158 final Path dstDir = tempFolder.newFolder().toPath(); |
|
159 |
|
160 Files.createDirectories(srcDir.resolve(PATH_FOO).resolve("a/b/c/d")); |
|
161 Files.createFile(srcDir.resolve(PATH_FOO).resolve("a/b/c/file1")); |
|
162 Files.createFile(srcDir.resolve(PATH_FOO).resolve("a/b/file2")); |
|
163 Files.createFile(srcDir.resolve(PATH_FOO).resolve("a/b/file3")); |
|
164 Files.createFile(srcDir.resolve(PATH_BAR)); |
|
165 Files.createFile(srcDir.resolve(PATH_EMPTY).resolve("file4")); |
|
166 Files.createDirectories(srcDir.resolve(PATH_BAZ).resolve("1/2/3")); |
|
167 |
|
168 var dst = pg.resolveAt(dstDir); |
|
169 var src = pg.resolveAt(srcDir); |
|
170 if (withExcludes) { |
|
171 // Exclude from transformation. |
|
172 src.setPath(new Object(), srcDir.resolve(PATH_FOO).resolve("a/b/c")); |
|
173 src.setPath(new Object(), srcDir.resolve(PATH_EMPTY).resolve("file4")); |
|
174 } |
|
175 |
|
176 var srcFilesBeforeTransform = walkFiles(srcDir); |
|
177 |
|
178 if (transform == TransformType.Handler) { |
|
179 List<Map.Entry<Path, Path>> copyFile = new ArrayList<>(); |
|
180 List<Path> createDirectory = new ArrayList<>(); |
|
181 src.transform(dst, new PathGroup.TransformHandler() { |
|
182 @Override |
|
183 public void copyFile(Path src, Path dst) throws IOException { |
|
184 copyFile.add(Map.entry(src, dst)); |
|
185 } |
|
186 |
|
187 @Override |
|
188 public void createDirectory(Path dir) throws IOException { |
|
189 createDirectory.add(dir); |
|
190 } |
|
191 }); |
|
192 |
|
193 Consumer<Path> assertFile = path -> { |
|
194 var entry = Map.entry(srcDir.resolve(path), dstDir.resolve(path)); |
|
195 assertTrue(copyFile.contains(entry)); |
|
196 }; |
|
197 |
|
198 Consumer<Path> assertDir = path -> { |
|
199 assertTrue(createDirectory.contains(dstDir.resolve(path))); |
|
200 }; |
|
201 |
|
202 assertEquals(withExcludes ? 3 : 5, copyFile.size()); |
|
203 assertEquals(withExcludes ? 8 : 10, createDirectory.size()); |
|
204 |
|
205 assertFile.accept(PATH_FOO.resolve("a/b/file2")); |
|
206 assertFile.accept(PATH_FOO.resolve("a/b/file3")); |
|
207 assertFile.accept(PATH_BAR); |
|
208 assertDir.accept(PATH_FOO.resolve("a/b")); |
|
209 assertDir.accept(PATH_FOO.resolve("a")); |
|
210 assertDir.accept(PATH_FOO); |
|
211 assertDir.accept(PATH_BAZ); |
|
212 assertDir.accept(PATH_BAZ.resolve("1")); |
|
213 assertDir.accept(PATH_BAZ.resolve("1/2")); |
|
214 assertDir.accept(PATH_BAZ.resolve("1/2/3")); |
|
215 assertDir.accept(PATH_EMPTY); |
|
216 |
|
217 if (!withExcludes) { |
|
218 assertFile.accept(PATH_FOO.resolve("a/b/c/file1")); |
|
219 assertFile.accept(PATH_EMPTY.resolve("file4")); |
|
220 assertDir.accept(PATH_FOO.resolve("a/b/c/d")); |
|
221 assertDir.accept(PATH_FOO.resolve("a/b/c")); |
|
222 } |
|
223 |
|
224 assertArrayEquals(new Path[] { Path.of("") }, walkFiles(dstDir)); |
|
225 return; |
|
226 } |
|
227 |
|
228 if (transform == TransformType.Copy) { |
|
229 src.copy(dst); |
|
230 } else if (transform == TransformType.Move) { |
|
231 src.move(dst); |
|
232 } |
|
233 |
|
234 final List<Path> excludedPaths; |
|
235 if (withExcludes) { |
|
236 excludedPaths = List.of( |
|
237 PATH_EMPTY.resolve("file4"), |
|
238 PATH_FOO.resolve("a/b/c") |
|
239 ); |
|
240 } else { |
|
241 excludedPaths = Collections.emptyList(); |
|
242 } |
|
243 UnaryOperator<Path[]> removeExcludes = paths -> { |
|
244 return Stream.of(paths) |
|
245 .filter(path -> !excludedPaths.stream().anyMatch( |
|
246 path::startsWith)) |
|
247 .collect(Collectors.toList()).toArray(Path[]::new); |
|
248 }; |
|
249 |
|
250 var dstFiles = walkFiles(dstDir); |
|
251 assertArrayEquals(removeExcludes.apply(srcFilesBeforeTransform), dstFiles); |
|
252 |
|
253 if (transform == TransformType.Copy) { |
|
254 assertArrayEquals(dstFiles, removeExcludes.apply(walkFiles(srcDir))); |
|
255 } else if (transform == TransformType.Move) { |
|
256 assertFalse(Files.exists(srcDir)); |
|
257 } |
|
258 } |
|
259 |
|
260 private static Path[] walkFiles(Path root) throws IOException { |
|
261 try (var files = Files.walk(root)) { |
|
262 return files.map(root::relativize).sorted().collect( |
|
263 Collectors.toList()).toArray(Path[]::new); |
|
264 } |
|
265 } |
|
266 |
128 private final static Path PATH_FOO = Path.of("foo"); |
267 private final static Path PATH_FOO = Path.of("foo"); |
129 private final static Path PATH_BAR = Path.of("bar"); |
268 private final static Path PATH_BAR = Path.of("bar"); |
|
269 private final static Path PATH_BAZ = Path.of("baz"); |
130 private final static Path PATH_EMPTY = Path.of(""); |
270 private final static Path PATH_EMPTY = Path.of(""); |
131 } |
271 } |