| 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.lang.reflect.Method; |
| 22 |
import org.junit.BeforeClass; |
| 23 |
import org.junit.Test; |
| 24 |
|
| 25 |
import java.io.File; |
| 26 |
import java.net.URL; |
| 27 |
import java.nio.file.Paths; |
| 28 |
import java.util.ArrayList; |
| 29 |
import java.util.Enumeration; |
| 30 |
|
| 31 |
import static org.junit.Assert.assertEquals; |
| 32 |
import static org.junit.Assert.assertNotNull; |
| 33 |
|
| 34 |
/** |
| 35 |
* Test to verify the functionality of the ClassPathModuleLoader. |
| 36 |
* |
| 37 |
* @author @author <a href="mailto:[email protected]">James R. Perkins</a> |
| 38 |
* @author Scott Stark ([email protected]) |
| 39 |
*/ |
| 40 |
public class ClassPathModuleLoaderTest extends AbstractModuleTestCase { |
| 41 |
|
| 42 |
@BeforeClass |
| 43 |
public static void beforeClass() throws Exception { |
| 44 |
final Method method = ModuleLoader.class.getDeclaredMethod("installMBeanServer"); |
| 45 |
method.setAccessible(true); |
| 46 |
method.invoke(null); |
| 47 |
} |
| 48 |
|
| 49 |
@Test |
| 50 |
public void testLoader() throws Exception { |
| 51 |
final File repoRoot = getResource("test/repo"); |
| 52 |
final String item = Paths.get("").toAbsolutePath().resolve("./target/test-classes/test/repo").normalize().toString(); |
| 53 |
final String[] classPath = { item }; |
| 54 |
final String deps = "test.test,test.with-deps"; |
| 55 |
final String mainClass = "org.jboss.modules.test.TestClass"; |
| 56 |
final ModuleLoader moduleLoader = new DelegatingModuleLoader(Module.getSystemModuleLoader(), new ClassPathModuleFinder(new LocalModuleLoader(new File[] { repoRoot }), classPath, deps, mainClass)); |
| 57 |
final Module module = moduleLoader.loadModule(item); |
| 58 |
module.getClassLoader(); |
| 59 |
assertNotNull(module); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* I need to be able to load EJBContainerProvider from a dependency. |
| 64 |
* |
| 65 |
* @author <a href="mailto:[email protected]">Carlo de Wolf</a> |
| 66 |
*/ |
| 67 |
@Test |
| 68 |
public void testService() throws Exception { |
| 69 |
final File repoRoot = getResource("test/repo"); |
| 70 |
final String item = Paths.get("").toAbsolutePath().resolve("./target/test-classes/test/repo").normalize().toString(); |
| 71 |
final String[] classPath = { item }; |
| 72 |
final String deps = "test.service"; |
| 73 |
final String mainClass = null; |
| 74 |
final ModuleLoader moduleLoader = new DelegatingModuleLoader(Module.getSystemModuleLoader(), new ClassPathModuleFinder(new LocalModuleLoader(new File[] { repoRoot }), classPath, deps, mainClass)); |
| 75 |
final Module module = moduleLoader.loadModule(item); |
| 76 |
final ClassLoader classLoader = module.getClassLoader(); |
| 77 |
final URL url = classLoader.getResource("META-INF/services/dummy"); |
| 78 |
assertNotNull(url); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Validate that dependent module META-INF/services/* content is seen |
| 83 |
* @throws Exception |
| 84 |
*/ |
| 85 |
@Test |
| 86 |
public void testMultipleServices() throws Exception { |
| 87 |
final File repoRoot = getResource("test/repo"); |
| 88 |
final String item = Paths.get("").toAbsolutePath().resolve("./target/test-classes/test/repo").normalize().toString(); |
| 89 |
final String[] classPath = { item }; |
| 90 |
final String deps = "test.jaxrs"; |
| 91 |
final String mainClass = null; |
| 92 |
final ModuleLoader moduleLoader = new DelegatingModuleLoader(Module.getSystemModuleLoader(), new ClassPathModuleFinder(new LocalModuleLoader(new File[] { repoRoot }), classPath, deps, mainClass)); |
| 93 |
final Module module = moduleLoader.loadModule(item); |
| 94 |
final ClassLoader classLoader = module.getClassLoader(); |
| 95 |
final Enumeration<URL> services = classLoader.getResources("META-INF/services/javax.ws.rs.ext.Providers"); |
| 96 |
assertNotNull(services); |
| 97 |
ArrayList<URL> found = new ArrayList<URL>(); |
| 98 |
while(services.hasMoreElements()) { |
| 99 |
found.add(services.nextElement()); |
| 100 |
} |
| 101 |
assertEquals("Found 2 services of type javax.ws.rs.ext.Providers", 2, found.size()); |
| 102 |
} |
| 103 |
} |