Debian Codegraph @

src/test/java/org/jboss/modules/LayeredModulePathTest.java

raw
1 /*
2 * JBoss, Home of Professional Open Source.
3 * Copyright 2014 Red Hat, Inc., and individual contributors
4 * as indicated by the @author tags.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package org.jboss.modules;
20
21 import java.io.File;
22 import java.io.FileOutputStream;
23 import java.io.FileWriter;
24 import java.io.IOException;
25 import java.io.OutputStream;
26 import java.io.PrintWriter;
27 import java.util.Arrays;
28 import java.util.Collections;
29 import java.util.HashSet;
30 import java.util.List;
31 import java.util.Set;
32
33 import org.junit.After;
34 import org.junit.Assert;
35 import org.junit.Before;
36 import org.junit.Test;
37
38 /**
39 * Tests of {@link LocalModuleLoader} when "layers" and "add-ons" are configured.
40 *
41 * @author Brian Stansberry (c) 2012 Red Hat Inc.
42 */
43 public class LayeredModulePathTest extends AbstractModuleTestCase {
44
45 private static final String PATH = "test/layeredmodulepath/";
46
47 private static final String SHARED = "test.shared";
48
49 private String originalModulePath;
50
51 private File reposRoot;
52 private File repoA;
53 private File repoB;
54
55 @Before
56 public void setUp() throws Exception {
57
58 originalModulePath = System.getProperty("module.path");
59
60 reposRoot = new File(getResource(PATH), "repos");
61 if (!reposRoot.mkdirs() && !reposRoot.isDirectory()) {
62 throw new IllegalStateException("Cannot create reposRoot");
63 }
64 repoA = new File(reposRoot, "root-a");
65 if (!repoA.mkdirs() && !repoA.isDirectory()) {
66 throw new IllegalStateException("Cannot create reposA");
67 }
68 repoB = new File(reposRoot, "root-b");
69 if (!repoB.mkdirs() && !repoB.isDirectory()) {
70 throw new IllegalStateException("Cannot create reposB");
71 }
72 }
73
74 @After
75 public void tearDown() throws Exception {
76 if (reposRoot != null) {
77 cleanFile(reposRoot);
78 }
79
80 if (originalModulePath != null) {
81 System.setProperty("module.path", originalModulePath);
82 } else {
83 System.clearProperty("module.path");
84 }
85 }
86
87 private void cleanFile(File file) {
88 File[] children = file.listFiles();
89 if (children != null) {
90 for (File child : children) {
91 cleanFile(child);
92 }
93 }
94 if (!file.delete() && file.exists()) {
95 file.deleteOnExit();
96 }
97 }
98
99 @Test
100 public void testBaseLayer() throws Exception {
101 createRepo("root-a", false, false, Collections.singletonList("base"));
102
103 File[] standardPath = { repoA };
104 File[] modulePath = LayeredModulePathFactory.resolveLayeredModulePath(standardPath);
105 validateModulePath(modulePath, repoA, 0, -1, false, "base");
106
107 validateModuleLoading(standardPath, false, false, false, "base");
108 }
109
110 @Test
111 public void testSpecifiedBaseLayer() throws Exception {
112 // This setup puts "base" in layers.conf
113 createRepo("root-a", false, false, "base");
114
115 File[] standardPath = { repoA };
116 File[] modulePath = LayeredModulePathFactory.resolveLayeredModulePath(standardPath);
117 validateModulePath(modulePath, repoA, 0, -1, false, "base");
118
119 validateModuleLoading(standardPath, false, false, false, "base");
120 }
121
122 @Test
123 public void testSimpleOverlay() throws Exception {
124 createRepo("root-a", false, false, Collections.singletonList("base"), "top");
125
126 File[] standardPath = { repoA };
127 File[] modulePath = LayeredModulePathFactory.resolveLayeredModulePath(standardPath);
128 validateModulePath(modulePath, repoA, 0, -1, false, "top", "base");
129
130 validateModuleLoading(standardPath, false, false, false, "top", "base");
131 }
132
133 @Test
134 public void testSpecifiedBaseLayerWithSimpleOverlay() throws Exception {
135 // This setup puts "base" in layers.conf
136 createRepo("root-a", false, false, "top", "base");
137
138 File[] standardPath = { repoA };
139 File[] modulePath = LayeredModulePathFactory.resolveLayeredModulePath(standardPath);
140 validateModulePath(modulePath, repoA, 0, -1, false, "top", "base");
141
142 validateModuleLoading(standardPath, false, false, false, "top", "base");
143 }
144
145 @Test
146 public void testMultipleOverlays() throws Exception {
147 createRepo("root-a", false, false, Collections.singletonList("base"), "top", "mid");
148
149 File[] standardPath = { repoA };
150 File[] modulePath = LayeredModulePathFactory.resolveLayeredModulePath(standardPath);
151 validateModulePath(modulePath, repoA, 0, -1, false, "top", "mid", "base");
152
153 validateModuleLoading(standardPath, false, false, false, "top", "mid", "base");
154 }
155
156 @Test
157 public void testSpecifiedBaseLayerWithMultipleOverlays() throws Exception {
158 // This setup puts "base" in layers.conf
159 createRepo("root-a", false, false, "top", "mid", "base");
160
161 File[] standardPath = { repoA };
162 File[] modulePath = LayeredModulePathFactory.resolveLayeredModulePath(standardPath);
163 validateModulePath(modulePath, repoA, 0, -1, false, "top", "mid", "base");
164
165 validateModuleLoading(standardPath, false, false, false, "top", "mid", "base");
166 }
167
168 @Test
169 public void testBasePlusAddOns() throws Exception {
170 createRepo("root-a", true, false, Collections.singletonList("base"));
171
172 File[] standardPath = { repoA };
173 File[] modulePath = LayeredModulePathFactory.resolveLayeredModulePath(standardPath);
174 validateModulePath(modulePath, repoA, 0, -1, true, "base");
175
176 validateModuleLoading(standardPath, true, false, false, "base");
177 }
178
179 @Test
180 public void testSpecifiedBasePlusAddOns() throws Exception {
181 // This setup puts "base" in layers.conf
182 createRepo("root-a", true, false, "base");
183
184 File[] standardPath = { repoA };
185 File[] modulePath = LayeredModulePathFactory.resolveLayeredModulePath(standardPath);
186 validateModulePath(modulePath, repoA, 0, -1, true, "base");
187
188 validateModuleLoading(standardPath, true, false, false, "base");
189 }
190
191 @Test
192 public void testLayersAndAddOns() throws Exception {
193 createRepo("root-a", true, false, Collections.singletonList("base"), "top", "mid");
194
195 File[] standardPath = { repoA };
196 File[] modulePath = LayeredModulePathFactory.resolveLayeredModulePath(standardPath);
197 validateModulePath(modulePath, repoA, 0, -1, true, "top", "mid", "base");
198
199 validateModuleLoading(standardPath, true, false, false, "top", "mid", "base");
200 }
201
202 @Test
203 public void testSpecifiedBaseLayersAndAddOns() throws Exception {
204 // This setup puts "base" in layers.conf
205 createRepo("root-a", true, false, "top", "mid", "base");
206
207 File[] standardPath = { repoA };
208 File[] modulePath = LayeredModulePathFactory.resolveLayeredModulePath(standardPath);
209 validateModulePath(modulePath, repoA, 0, -1, true, "top", "mid", "base");
210
211 validateModuleLoading(standardPath, true, false, false, "top", "mid", "base");
212 }
213
214 @Test
215 public void testBaseLayerAndUser() throws Exception {
216 createRepo("root-a", false, false, Collections.singletonList("base"));
217
218 File[] standardPath = { repoA };
219 File[] modulePath = LayeredModulePathFactory.resolveLayeredModulePath(standardPath);
220 validateModulePath(modulePath, repoA, 0, -1, false, "base");
221
222 validateModuleLoading(standardPath, false, false, false, "base");
223 }
224
225 @Test
226 public void testSpecifiedBaseLayerAndUser() throws Exception {
227 // This setup puts "base" in layers.conf
228 createRepo("root-a", false, true, "base");
229
230 File[] standardPath = { repoA };
231 File[] modulePath = LayeredModulePathFactory.resolveLayeredModulePath(standardPath);
232 validateModulePath(modulePath, repoA, 0, -1, false, "base");
233
234 validateModuleLoading(standardPath, false, true, true, "base");
235 }
236
237 @Test
238 public void testSingleRootComplete() throws Exception {
239 createRepo("root-a", true, false, Collections.singletonList("base"), "top", "mid");
240
241 File[] standardPath = { repoA };
242 File[] modulePath = LayeredModulePathFactory.resolveLayeredModulePath(standardPath);
243 validateModulePath(modulePath, repoA, 0, -1, true, "top", "mid", "base");
244
245 validateModuleLoading(standardPath, true, false, false, "top", "mid", "base");
246 }
247
248 @Test
249 public void testSpecifiedBaseSingleRootComplete() throws Exception {
250 // This setup puts "base" in layers.conf
251 createRepo("root-a", true, true, "top", "mid", "base");
252
253 File[] standardPath = { repoA };
254 File[] modulePath = LayeredModulePathFactory.resolveLayeredModulePath(standardPath);
255 validateModulePath(modulePath, repoA, 0, -1, true, "top", "mid", "base");
256
257 validateModuleLoading(standardPath, true, true, true, "top", "mid", "base");
258 }
259
260 @Test
261 public void testSecondRepoHigherPrecedence() throws Exception {
262 createRepo("root-a", false, false, Collections.singletonList("base"));
263 createRepo("root-b", false, true);
264
265 File[] standardPath = { repoB, repoA };
266 File[] modulePath = LayeredModulePathFactory.resolveLayeredModulePath(standardPath);
267 validateModulePath(modulePath, repoA, 1, 0, false, "base");
268
269 validateModuleLoading(standardPath, false, true, true, "base");
270 }
271
272 @Test
273 public void testSecondRepoLowerPrecedence() throws Exception {
274 createRepo("root-a", false, false, Collections.singletonList("base"));
275 createRepo("root-b", false, true);
276
277 File[] standardPath = { repoA, repoB };
278 File[] modulePath = LayeredModulePathFactory.resolveLayeredModulePath(standardPath);
279 validateModulePath(modulePath, repoA, 0, 2, false, "base");
280
281 validateModuleLoading(standardPath, false, true, false, "base");
282 }
283
284 @Test
285 public void testExtraneousOverlay() throws Exception {
286 createRepo("root-a", false, false, Arrays.asList("base", "mid"), "top");
287
288 File[] standardPath = { repoA };
289 File[] modulePath = LayeredModulePathFactory.resolveLayeredModulePath(standardPath);
290 validateModulePath(modulePath, repoA, 0, -1, false, "top", "base");
291
292 validateModuleLoading(standardPath, false, false, false, "top", "base");
293 }
294
295 @Test
296 public void testSpecifiedBaseLayerWithExtraneousOverlay() throws Exception {
297 // This setup puts "base" in layers.conf
298 createRepo("root-a", false, false, Arrays.asList("mid"), "top", "base");
299
300 File[] standardPath = { repoA };
301 File[] modulePath = LayeredModulePathFactory.resolveLayeredModulePath(standardPath);
302 validateModulePath(modulePath, repoA, 0, -1, false, "top", "base");
303
304 validateModuleLoading(standardPath, false, false, false, "top", "base");
305 }
306
307 /** Tests that setting up add-ons has no effect without the layers structure */
308 @Test
309 public void testLayersRequiredForAddOns() throws Exception {
310 createRepo("root-a", true, false);
311
312 File[] standardPath = { repoA };
313 File[] modulePath = LayeredModulePathFactory.resolveLayeredModulePath(standardPath);
314 validateModulePath(modulePath, repoA, 0, -1, false);
315
316 validateModuleLoading(standardPath, false, false, false);
317
318 // Now add the layers/base dir
319 new File(repoA, "system/layers/base").mkdirs();
320
321 modulePath = LayeredModulePathFactory.resolveLayeredModulePath(standardPath);
322 validateModulePath(modulePath, repoA, 0, -1, true, "base");
323
324 validateModuleLoading(standardPath, true, false, false);
325 }
326
327 @Test
328 public void testRejectConfWithNoStructure() throws Exception {
329 createRepo("root-a", false, false);
330 writeLayersConf("root-a", "top");
331
332 File[] standardPath = { repoA };
333 try {
334 LayeredModulePathFactory.resolveLayeredModulePath(standardPath);
335 Assert.fail("layers.conf with no layers should fail");
336 } catch (Exception good) {
337 // good
338 }
339 }
340
341 @Test
342 public void testRejectConfWithMissingLayer() throws Exception {
343 createRepo("root-a", false, false, Arrays.asList("top", "base"));
344 writeLayersConf("root-a", "top", "mid");
345
346 File[] standardPath = { repoA };
347 try {
348 LayeredModulePathFactory.resolveLayeredModulePath(standardPath);
349 Assert.fail("layers.conf with no layers should fail");
350 } catch (Exception good) {
351 // good
352 }
353 }
354
355 @Test
356 public void testEmptyLayersConf() throws Exception {
357 createRepo("root-a", false, false, Collections.singletonList("base"));
358 writeLayersConf("root-a");
359
360 File[] standardPath = { repoA };
361 File[] modulePath = LayeredModulePathFactory.resolveLayeredModulePath(standardPath);
362 validateModulePath(modulePath, repoA, 0, -1, false, "base");
363
364 validateModuleLoading(standardPath, false, false, false, "base");
365 }
366
367 @Test
368 public void testLayersOverlayModulePath() throws Exception {
369 createRepo("root-a", false, false, Arrays.asList("top", "base"));
370 writeLayersConf("root-a", "top", "base");
371
372 createOverlays(repoA, "top", false, "top1", "top2");
373 createOverlays(repoA, "base", false, "base1", "base2");
374
375 File[] standardPath = { repoA };
376 File[] modulePath = LayeredModulePathFactory.resolveLayeredModulePath(standardPath);
377
378 Assert.assertEquals(7, modulePath.length);
379 Assert.assertEquals(repoA, modulePath[0]);
380 Assert.assertEquals(new File(repoA, "system/layers/top/.overlays/top1"), modulePath[1]);
381 Assert.assertEquals(new File(repoA, "system/layers/top/.overlays/top2"), modulePath[2]);
382 Assert.assertEquals(new File(repoA, "system/layers/top"), modulePath[3]);
383 Assert.assertEquals(new File(repoA, "system/layers/base/.overlays/base1"), modulePath[4]);
384 Assert.assertEquals(new File(repoA, "system/layers/base/.overlays/base2"), modulePath[5]);
385 Assert.assertEquals(new File(repoA, "system/layers/base"), modulePath[6]);
386
387 }
388
389 @Test
390 public void testAddOnsOverlayModulePath() throws Exception {
391 createRepo("root-a", true, false, Arrays.asList("base"));
392 writeLayersConf("root-a", "base");
393
394 createOverlays(repoA, "a", true, "a");
395 createOverlays(repoA, "b", true, "b");
396
397 File[] standardPath = { repoA };
398 File[] modulePath = LayeredModulePathFactory.resolveLayeredModulePath(standardPath);
399
400 Assert.assertEquals(6, modulePath.length);
401 Assert.assertEquals(repoA, modulePath[0]);
402 Assert.assertEquals(new File(repoA, "system/layers/base"), modulePath[1]);
403 // The order of the add-ons is non deterministic
404 Assert.assertEquals(".overlays", modulePath[2].getParentFile().getName());
405 final String firstOverlay = modulePath[2].getName();
406 Assert.assertEquals(new File(repoA, "system/add-ons/" + firstOverlay), modulePath[3]);
407 Assert.assertEquals(".overlays", modulePath[4].getParentFile().getName());
408 final String secondOverlays = modulePath[4].getName();
409 Assert.assertEquals(new File(repoA, "system/add-ons/" + secondOverlays), modulePath[5]);
410
411 }
412
413 private void writeLayersConf(String rootName, String... layers) throws IOException {
414 if (layers != null && layers.length > 0) {
415
416 StringBuilder sb = new StringBuilder("layers=");
417 for (int i = 0; i < layers.length; i++) {
418 if (i > 0) {
419 sb.append(',');
420 }
421 sb.append(layers[i]);
422 }
423
424 File repo = "root-a".equals(rootName) ? repoA : repoB;
425 File layersConf = new File(repo, "layers.conf");
426 layersConf.createNewFile();
427 FileWriter fw = new FileWriter(layersConf);
428 try {
429 PrintWriter pw = new PrintWriter(fw);
430 pw.println(sb.toString());
431 pw.close();
432 } finally {
433 try {
434 fw.close();
435 } catch (Exception e) {
436 // meh
437 }
438 }
439 }
440 }
441
442 private void createRepo(String rootName, boolean includeAddons, boolean includeUser, String... layers) throws Exception {
443 List<String> empty = Collections.emptyList();
444 createRepo(rootName, includeAddons, includeUser, empty, layers);
445 }
446
447 private void createRepo(String rootName, boolean includeAddons, boolean includeUser, List<String> extraLayers, String... layers) throws Exception {
448 if (layers != null && layers.length > 0) {
449 writeLayersConf(rootName, layers);
450 for (String layer : layers) {
451 createLayer(rootName, layer);
452 }
453 }
454 if (extraLayers != null) {
455 for (String extraLayer : extraLayers) {
456 createLayer(rootName, extraLayer);
457 }
458 }
459
460 if (includeAddons) {
461 createAddOn(rootName, "a");
462 createAddOn(rootName, "b");
463 }
464
465 if (includeUser) {
466 createUserModules(rootName);
467 }
468 }
469
470 private void createLayer(String rootName, String layerName) throws Exception {
471 createModules("layers/" + layerName, rootName + "/system/layers/" + layerName, layerName);
472 }
473
474 private void createAddOn(String rootName, String addOnName) throws Exception {
475 createModules("add-ons/" + addOnName, rootName + "/system/add-ons/" + addOnName, addOnName);
476 }
477
478 private void createUserModules(String rootName) throws Exception {
479 createModules("user", rootName, "user");
480 }
481
482 private void createModules(String sourcePath, String relativeRepoPath, String uniqueName) throws Exception {
483 copyResource(PATH + sourcePath + "/shared/module.xml", PATH, "repos/" + relativeRepoPath + "/test/shared/main");
484 copyResource(PATH + sourcePath + "/unique/module.xml", PATH, "repos/" + relativeRepoPath + "/test/" + uniqueName + "/main");
485 }
486
487 private void validateModulePath(File[] modulePath, File repoRoot, int expectedStartPos,
488 int expectedOtherRootPos, boolean expectAddons, String... layers) {
489 int expectedLength = 1 + layers.length + (expectAddons ? 2 : 0);
490
491 // Validate positional parameters -- check for bad test writers ;)
492 if (expectedOtherRootPos < 0) {
493 Assert.assertEquals(0, expectedStartPos); //
494 } else if (expectedStartPos == 0) {
495 Assert.assertEquals(expectedLength, expectedOtherRootPos);
496 }
497
498 if (expectedOtherRootPos < 1) {
499 Assert.assertEquals("Correct module path length", expectedStartPos + expectedLength, modulePath.length);
500 } else {
501 Assert.assertTrue("Correct module path length", modulePath.length > expectedStartPos + expectedLength);
502 }
503
504 Assert.assertEquals(repoRoot, modulePath[expectedStartPos]);
505 for (int i = 0; i < layers.length; i++) {
506 File layer = new File(repoRoot, "system/layers/" + layers[i]);
507 Assert.assertEquals(layer, modulePath[expectedStartPos + i + 1]);
508 }
509 if (expectAddons) {
510 File addOnBase = new File(repoRoot, "system/add-ons");
511 Set<String> valid = new HashSet<String>(Arrays.asList("a", "b"));
512 for (int i = 0; i < 2; i++) {
513 File addOn = modulePath[expectedStartPos + layers.length + i + 1];
514 Assert.assertEquals(addOnBase, addOn.getParentFile());
515 String addOnName = addOn.getName();
516 Assert.assertTrue(addOnName, valid.remove(addOnName));
517 }
518
519 }
520
521 if (expectedOtherRootPos == 0) {
522 for (int i = 0; i < expectedStartPos; i++) {
523 validateNotChild(modulePath[i], repoRoot);
524 }
525 } else if (expectedOtherRootPos > 0) {
526 for (int i = expectedOtherRootPos; i < modulePath.length; i++) {
527 validateNotChild(modulePath[i], repoRoot);
528 }
529 }
530
531 }
532
533 private void validateNotChild(File file, File repoRoot) {
534 File stop = repoRoot.getParentFile();
535 File testee = file;
536 while (testee != null && !testee.equals(stop)) {
537 Assert.assertFalse(testee.equals(repoRoot));
538 testee = testee.getParentFile();
539 }
540 }
541
542 private void validateModuleLoading(File[] standardPath, boolean expectAddOns, boolean expectUser,
543 boolean expectUserPrecedence, String... layers) throws ModuleLoadException {
544 // This is nasty, but the alternative is exposing the layers config stuff in the LocalModuleLoader API
545 setUpModulePathProperty(standardPath);
546 ModuleLoader moduleLoader = new LocalModuleLoader();
547
548 // Validate the expected path produced the shared module
549 if (expectUser || layers.length > 0 || expectAddOns) {
550 Module shared = moduleLoader.loadModule(SHARED);
551 String sharedProp = shared.getProperty("test.prop");
552 if (expectUserPrecedence) {
553 Assert.assertEquals("user", sharedProp);
554 } else if (layers.length > 0) {
555 Assert.assertEquals(layers[0], sharedProp);
556 } else if (expectAddOns) {
557 Assert.assertTrue("a".equals(sharedProp) || "b".equals(sharedProp));
558 }
559 }
560
561 // Validate the expected unique modules are present
562 Set<String> layersSet = new HashSet<String>(Arrays.asList(layers));
563 loadModule(moduleLoader, "user", expectUser);
564 loadModule(moduleLoader, "top", layersSet.contains("top"));
565 loadModule(moduleLoader, "mid", layersSet.contains("mid"));
566 loadModule(moduleLoader, "base", layersSet.contains("base"));
567 loadModule(moduleLoader, "a", expectAddOns);
568 loadModule(moduleLoader, "b", expectAddOns);
569 }
570
571 private void setUpModulePathProperty(File[] standardPath) {
572 StringBuilder sb = new StringBuilder();
573 for (int i = 0; i < standardPath.length; i++) {
574 if (i > 0) {
575 sb.append(File.pathSeparatorChar);
576 }
577 sb.append(standardPath[i].getAbsolutePath());
578 }
579 System.setProperty("module.path", sb.toString());
580 }
581
582 private void loadModule(ModuleLoader moduleLoader, String moduleName, boolean expectAvailable) {
583 try {
584 Module module = moduleLoader.loadModule("test." + moduleName);
585 if (!expectAvailable) {
586 Assert.fail("test." + moduleName + " should not be loadable");
587 }
588 String prop = module.getProperty("test.prop");
589 Assert.assertEquals(moduleName, prop);
590 } catch (ModuleLoadException e) {
591 if (expectAvailable) {
592 Assert.fail(e.getMessage());
593 }
594 }
595 }
596
597 private void createOverlays(final File root, final String name, boolean addOn, String... overlays) throws IOException {
598 final File system = new File(root, "system");
599 final File layers = addOn ? new File(system, "add-ons") : new File(system, "layers");
600 final File repo = new File(layers, name);
601 final File overlaysRoot = new File(repo, ".overlays");
602 overlaysRoot.mkdir();
603 final File overlaysConfig = new File(overlaysRoot, ".overlays");
604 final OutputStream os = new FileOutputStream(overlaysConfig);
605 try {
606 for (final String overlay : overlays) {
607 os.write(overlay.getBytes());
608 os.write('\n');
609 new File(overlaysRoot, overlay).mkdirs();
610 }
611 } finally {
612 if (os != null) try {
613 os.close();
614 } catch (IOException e) {
615 e.printStackTrace();
616 }
617 }
618 }
619 }