View Javadoc
1   package org.codehaus.mojo.jaxb2.shared.classloader;
2   
3   import org.codehaus.mojo.jaxb2.BufferingLog;
4   import org.codehaus.mojo.jaxb2.shared.FileSystemUtilities;
5   import org.codehaus.mojo.jaxb2.shared.environment.classloading.ThreadContextClassLoaderBuilder;
6   import org.codehaus.mojo.jaxb2.shared.environment.classloading.ThreadContextClassLoaderHolder;
7   import org.junit.After;
8   import org.junit.Assert;
9   import org.junit.Before;
10  import org.junit.Test;
11  
12  import java.io.File;
13  import java.net.URL;
14  import java.util.Collections;
15  import java.util.List;
16  
17  /**
18   * @author <a href="mailto:lj@jguru.se">Lennart J&ouml;relid</a>, jGuru Europe AB
19   */
20  public class ThreadContextClassLoaderBuilderTest {
21  
22      // Shared state
23      private ThreadContextClassLoaderHolder holder;
24      private URL extraClassLoaderDirURL;
25      private File extraClassLoaderDirFile;
26      private BufferingLog log;
27      private ClassLoader originalClassLoader;
28  
29      @Before
30      public void setupSharedState() {
31  
32          log = new BufferingLog(BufferingLog.LogLevel.DEBUG);
33  
34          final String extraPath = "testdata/shared/classloader";
35          extraClassLoaderDirURL = getClass().getClassLoader().getResource(extraPath);
36          Assert.assertNotNull(extraClassLoaderDirURL);
37  
38          extraClassLoaderDirFile = new File(extraClassLoaderDirURL.getPath());
39          Assert.assertTrue(extraClassLoaderDirFile.exists() && extraClassLoaderDirFile.isDirectory());
40  
41          // Stash the original ClassLoader
42          originalClassLoader = Thread.currentThread().getContextClassLoader();
43      }
44  
45      @After
46      public void teardownSharedState() {
47          if (holder != null) {
48              holder.restoreClassLoaderAndReleaseThread();
49          } else {
50              Thread.currentThread().setContextClassLoader(originalClassLoader);
51          }
52      }
53  
54      @Test
55      public void validateAddingURLsToThreadContextClassLoader() throws Exception {
56  
57          // Assemble
58          holder = ThreadContextClassLoaderBuilder
59                  .createFor(originalClassLoader, log)
60                  .addURL(extraClassLoaderDirURL)
61                  .buildAndSet();
62  
63          // Act
64          final ClassLoader ctxClassLoader = Thread.currentThread().getContextClassLoader();
65          final List<URL> resources = Collections.list(ctxClassLoader.getResources(""));
66  
67          // Assert
68          Assert.assertEquals(3, resources.size());
69          validateContains(resources, "target/classes");
70          validateContains(resources, "target/test-classes");
71          validateContains(resources, "target/test-classes/testdata/shared/classloader");
72  
73          for (URL current : resources) {
74              final File aFile = new File(current.getPath());
75              Assert.assertTrue(aFile.exists() && aFile.isDirectory());
76              System.out.println("[" + current.getPath() + "]: " + aFile.getPath());
77          }
78      }
79  
80      @Test
81      public void validateResourceAccessInAugmentedClassLoader() {
82  
83          // Assemble
84          holder = ThreadContextClassLoaderBuilder
85                  .createFor(originalClassLoader, log)
86                  .addURL(extraClassLoaderDirURL)
87                  .buildAndSet();
88  
89          // Act
90          final ClassLoader ctxClassLoader = Thread.currentThread().getContextClassLoader();
91  
92          final URL immediateResource = ctxClassLoader.getResource("ImmediateTestResource.txt");
93          final URL subResource = ctxClassLoader.getResource("subdirectory/SubdirectoryTestResource.txt");
94  
95          // Assert
96          Assert.assertNotNull(immediateResource);
97          Assert.assertNotNull(subResource);
98  
99          final File immediateFile = new File(immediateResource.getPath());
100         final File subFile = new File(subResource.getPath());
101 
102         Assert.assertTrue(immediateFile.exists() && immediateFile.isFile());
103         Assert.assertTrue(subFile.exists() && subFile.isFile());
104     }
105 
106     @Test
107     public void validateLoadingResourcesInJars() {
108 
109         // Assemble
110         final File theJar = new File(extraClassLoaderDirFile, "jarSubDirectory/aJarWithResources.jar");
111 
112         holder = ThreadContextClassLoaderBuilder
113                 .createFor(originalClassLoader, log)
114                 .addPath(FileSystemUtilities.getCanonicalPath(theJar))
115                 .buildAndSet();
116 
117         // Act
118         final ClassLoader ctxClassLoader = Thread.currentThread().getContextClassLoader();
119 
120         final URL containedTopLevelResource = ctxClassLoader.getResource("ContainedFileResource.txt");
121         final URL containedSubLevelResource = ctxClassLoader.getResource("internalSubDir/SubContainedResource.txt");
122 
123         // Assert
124         Assert.assertNotNull(containedTopLevelResource);
125         Assert.assertNotNull(containedSubLevelResource);
126     }
127 
128     //
129     // Private helpers
130     //
131 
132     private void validateContains(final List<URL> resources, final String snippet) {
133 
134         for (URL current : resources) {
135             if (current.toString().contains(snippet)) {
136                 return;
137             }
138         }
139 
140         Assert.fail("Snippet [" + snippet + "] was not found within URL resources.");
141     }
142 }