|
1 |
| -package org.codehaus.plexus.classworlds.realm; |
2 |
| - |
3 | 1 | /*
|
4 | 2 | * Licensed to the Apache Software Foundation (ASF) under one
|
5 | 3 | * or more contributor license agreements. See the NOTICE file
|
|
18 | 16 | * specific language governing permissions and limitations
|
19 | 17 | * under the License.
|
20 | 18 | */
|
| 19 | +package org.codehaus.plexus.classworlds.realm; |
21 | 20 |
|
| 21 | +import java.io.IOException; |
| 22 | +import java.util.Collections; |
22 | 23 | import java.util.HashSet;
|
23 | 24 | import java.util.Set;
|
24 | 25 |
|
|
36 | 37 | public class FilteredClassRealmTest extends AbstractClassWorldsTestCase
|
37 | 38 | {
|
38 | 39 | private ClassWorld world;
|
| 40 | + private ClassRealm realmA; |
39 | 41 |
|
40 | 42 | @Before
|
41 |
| - public void setUp() |
| 43 | + public void setUp() throws DuplicateRealmException |
42 | 44 | {
|
43 | 45 | this.world = new ClassWorld();
|
| 46 | + // only allow loading resources whose names start with "a." |
| 47 | + Set<String> allowedResourcePrefixes = new HashSet<>(); |
| 48 | + allowedResourcePrefixes.add( "a." ); |
| 49 | + allowedResourcePrefixes.add( "a/Aa" ); |
| 50 | + realmA = this.world.newRealm( "realmA", allowedResourcePrefixes ); |
44 | 51 | }
|
45 | 52 |
|
46 | 53 | @Test
|
47 |
| - public void testLoadClassAndResourcesFiltered() |
| 54 | + public void testLoadResources() |
48 | 55 | throws Exception
|
49 | 56 | {
|
50 |
| - // only allow loading resources whose names start with "a." |
51 |
| - Set<String> allowedResourcePrefixes = new HashSet<>(); |
52 |
| - allowedResourcePrefixes.add( "a." ); |
53 |
| - allowedResourcePrefixes.add( "a/Aa" ); |
54 |
| - ClassRealm realmA = this.world.newRealm( "realmA", allowedResourcePrefixes ); |
| 57 | + realmA.addURL( getJarUrl( "a.jar" ) ); |
| 58 | + assertNull( realmA.getResource( "common.properties" ) ); |
| 59 | + assertFalse( realmA.getResources( "common.properties" ).hasMoreElements() ); |
| 60 | + |
| 61 | + assertNotNull( realmA.getResource( "a.properties" ) ); |
| 62 | + assertTrue( realmA.getResources( "a.properties" ).hasMoreElements() ); |
| 63 | + } |
55 | 64 |
|
| 65 | + @Test |
| 66 | + public void testLoadClass() throws ClassNotFoundException |
| 67 | + { |
56 | 68 | assertThrows( ClassNotFoundException.class, () -> realmA.loadClass( "a.Aa" ) );
|
57 | 69 | realmA.addURL( getJarUrl( "a.jar" ) );
|
58 | 70 |
|
59 | 71 | assertNotNull( realmA.loadClass( "a.Aa" ) );
|
60 | 72 | assertThrows( ClassNotFoundException.class, () -> realmA.loadClass( "a.A" ) );
|
61 | 73 |
|
62 |
| - assertNull( realmA.getResource( "common.properties" ) ); |
63 |
| - assertFalse( realmA.getResources( "common.properties" ).hasMoreElements() ); |
64 |
| - |
65 |
| - assertNotNull( realmA.getResource( "a.properties" ) ); |
66 |
| - assertTrue( realmA.getResources( "a.properties" ).hasMoreElements() ); |
| 74 | + assertNotNull( realmA.loadClass( "a.Aa" ) ); |
| 75 | + assertThrows( ClassNotFoundException.class, () -> realmA.loadClass( "a.A" ) ); |
67 | 76 | }
|
68 | 77 |
|
| 78 | + @Test |
| 79 | + public void testLoadClassWithModule() throws IOException |
| 80 | + { |
| 81 | + try (ExtendedFilteredClassRealm realmA = new ExtendedFilteredClassRealm( world, Collections.singleton( "a/Aa" ) )) { |
| 82 | + realmA.addURL( getJarUrl( "a.jar" ) ); |
| 83 | + assertNotNull( realmA.simulateLoadClassFromModule( "a.Aa" ) ); |
| 84 | + assertNull( realmA.simulateLoadClassFromModule( "a.A" ) ); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Simulates new {@code java.lang.ClassLoader#findClass(String,String)} introduced with Java 9. |
| 90 | + * It is reversed in terms of inheritance but enables to simulate the same behavior in these tests. |
| 91 | + * @see <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/ClassLoader.html#findClass(java.lang.String,java.lang.String)">ClassLoader#findClass(String,String)</a> |
| 92 | + * @see ClassRealmImplTest.ExtendedClassRealm |
| 93 | + */ |
| 94 | + static class ExtendedFilteredClassRealm extends FilteredClassRealm |
| 95 | + { |
| 96 | + |
| 97 | + public ExtendedFilteredClassRealm( final ClassWorld world, Set<String> allowedResourcePrefixes ) |
| 98 | + { |
| 99 | + super( allowedResourcePrefixes, world, "java9", Thread.currentThread().getContextClassLoader() ); |
| 100 | + } |
| 101 | + |
| 102 | + public Class<?> simulateLoadClassFromModule(final String name) |
| 103 | + { |
| 104 | + synchronized (getClassLoadingLock(name)) |
| 105 | + { |
| 106 | + Class<?> c = findLoadedClass(name); |
| 107 | + if (c == null) { |
| 108 | + c = findClass(null, name); |
| 109 | + } |
| 110 | + return c; |
| 111 | + } |
| 112 | + } |
| 113 | + } |
69 | 114 | }
|
0 commit comments