Debian Codegraph @

src/test/java/org/jboss/modules/util/ModulesTestBase.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.util;
20
21 import org.jboss.modules.DependencySpec;
22 import org.jboss.modules.Module;
23 import org.jboss.modules.ModuleClassLoader;
24 import org.jboss.modules.ModuleLoadException;
25 import org.jboss.modules.ModuleLoader;
26 import org.jboss.modules.ModuleSpec;
27 import org.jboss.modules.filter.PathFilter;
28 import org.jboss.modules.filter.PathFilters;
29 import org.junit.Before;
30
31 import java.net.URL;
32 import java.util.Collections;
33 import java.util.HashMap;
34 import java.util.HashSet;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Set;
38
39 import static org.junit.Assert.assertEquals;
40 import static org.junit.Assert.assertNotNull;
41 import static org.junit.Assert.fail;
42
43 /**
44 * Test low level modules use cases.
45 *
46 * @author [email protected]
47 * @since 15-Sep-2010
48 */
49 public abstract class ModulesTestBase {
50
51 private ModuleLoaderSupport moduleLoader;
52
53 @Before
54 public void setUp() throws Exception {
55 moduleLoader = new ModuleLoaderSupport("default");
56 }
57
58 protected ModuleLoaderSupport getModuleLoader() {
59 return moduleLoader;
60 }
61
62 protected void addModuleSpec(ModuleSpec moduleSpec) {
63 moduleLoader.addModuleSpec(moduleSpec);
64 }
65
66 protected Module loadModule(String identifier) throws ModuleLoadException {
67 return moduleLoader.loadModule(identifier);
68 }
69
70 protected PathFilter getPathFilter(Class<?>... classes) {
71 Set<String> paths = getFilterPaths(classes);
72 return PathFilters.in(paths);
73 }
74
75 protected Set<String> getFilterPaths(Class<?>... classes) {
76 Set<String> paths = new HashSet<String>();
77 for (Class<?> clazz : classes) {
78 paths.add(getPathForClassName(clazz.getName()));
79 }
80 return Collections.unmodifiableSet(paths);
81 }
82
83 protected String getPathForClassName(String className) {
84 className = className.substring(0, className.lastIndexOf('.'));
85 className = className.replace('.', '/');
86 return className;
87 }
88
89 protected void assertLoadClass(String identifier, String className) throws Exception {
90 Class<?> clazz = loadClass(identifier, className);
91 assertNotNull(clazz);
92 }
93
94 protected void assertLoadClass(String identifier, String className, String exporterId) throws Exception {
95 Class<?> clazz = loadClass(identifier, className);
96 ClassLoader wasClassLoader = clazz.getClassLoader();
97 if (exporterId == null && wasClassLoader == null) {
98 return;
99 }
100 ModuleClassLoader expClassLoader = loadModule(exporterId).getClassLoader();
101 assertEquals(expClassLoader, wasClassLoader);
102 }
103
104 protected void assertLoadClassFail(String identifier, String className) throws Exception {
105 try {
106 Class<?> clazz = loadClass(identifier, className);
107 assertNotNull("ClassNotFoundException expected for [" + className + "], but was: " + clazz, clazz);
108 fail("ClassNotFoundException expected for [" + className + "], but was loaded from: " + clazz.getClassLoader());
109 } catch (ClassNotFoundException ex) {
110 // expected
111 } catch (NoClassDefFoundError ex) {
112 // expected
113 }
114 }
115
116 protected Class<?> loadClass(String identifier, String className) throws Exception {
117 // ClassLoader#resolveClass() only links the class; it doesn't necessarily force it to be initialized.
118 // To initialize the class you can do Class.forName(name, true, classLoader)
119 ModuleClassLoader classLoader = loadModule(identifier).getClassLoader();
120 Class<?> clazz = Class.forName(className, true, classLoader);
121 return clazz;
122 }
123
124 protected URL getResource(String identifier, String resourcePath) throws Exception {
125 ModuleClassLoader classLoader = loadModule(identifier).getClassLoader();
126 return classLoader.getResource(resourcePath);
127 }
128
129 static class ModuleLoaderSupport extends ModuleLoader {
130
131 private String loaderName;
132 private Map<String, ModuleSpec> modules = new HashMap<>();
133
134 ModuleLoaderSupport(String loaderName) {
135 this.loaderName = loaderName;
136 }
137
138 void addModuleSpec(ModuleSpec moduleSpec) {
139 modules.put(moduleSpec.getName(), moduleSpec);
140 }
141
142 @Override
143 protected ModuleSpec findModule(String name) throws ModuleLoadException {
144 ModuleSpec moduleSpec = modules.get(name);
145 return moduleSpec;
146 }
147
148 @Override
149 protected void setAndRelinkDependencies(Module module, List<DependencySpec> dependencies) throws ModuleLoadException {
150 super.setAndRelinkDependencies(module, dependencies);
151 }
152
153 @Override
154 public String toString() {
155 return "ModuleLoaderSupport[" + loaderName + "]";
156 }
157 }
158 }