Debian Codegraph @

src/test/java/org/jboss/modules/util/TestResourceLoader.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.AbstractResourceLoader;
22 import org.jboss.modules.ClassSpec;
23 import org.jboss.modules.PackageSpec;
24 import org.jboss.modules.Resource;
25
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.net.MalformedURLException;
31 import java.net.URL;
32 import java.util.Collection;
33 import java.util.HashMap;
34 import java.util.HashSet;
35 import java.util.Map;
36 import java.util.Set;
37 import java.util.jar.Attributes;
38 import java.util.jar.Manifest;
39
40 import static org.junit.Assert.assertTrue;
41 import static org.jboss.modules.util.Util.getClassBytes;
42
43 /**
44 * A test resource loader that simple retrieves resources frm maps. This allows tests to build
45 * arbitrary modules with arbitrary content without having to have the module on disk.
46 *
47 * @author John E. Bailey
48 */
49 public class TestResourceLoader extends AbstractResourceLoader {
50 private final Map<String, ClassSpec> classSpecs = new HashMap<String, ClassSpec>();
51 private final Map<String, Resource> resources = new HashMap<String, Resource>();
52 private final Set<String> paths = new HashSet<String>();
53 private Manifest manifest;
54
55 public String getRootName() {
56 return "test";
57 }
58
59 @Override
60 public ClassSpec getClassSpec(final String fileName) throws IOException {
61 final Map<String, ClassSpec> classSpecs = this.classSpecs;
62 return classSpecs.get(fileName);
63 }
64
65 void addClassSpec(final String name, final ClassSpec classSpec) {
66 final Map<String, ClassSpec> classSpecs = this.classSpecs;
67 classSpecs.put(name.replace('.', '/') + ".class", classSpec);
68 addPaths(getPathFromClassName(name));
69 }
70
71 @Override
72 public PackageSpec getPackageSpec(final String name) throws IOException {
73 return getPackageSpec(name, getManifest(), null);
74 }
75
76 private Manifest getManifest() throws IOException {
77 if(manifest != null)
78 return manifest;
79
80 final Resource manifestResource = getResource("META-INF/MANIFEST.MF");
81 if(manifestResource == null)
82 return null;
83 final InputStream manifestStream = manifestResource.openStream();
84 try {
85 manifest = new Manifest(manifestStream);
86 } finally {
87 if(manifestStream != null) manifestStream.close();
88 }
89 return manifest;
90 }
91
92 private static String getDefinedAttribute(Attributes.Name name, Attributes entryAttribute, Attributes mainAttribute) {
93 final String value = entryAttribute == null ? null : entryAttribute.getValue(name);
94 return value == null ? mainAttribute == null ? null : mainAttribute.getValue(name) : value;
95 }
96
97 @Override
98 public Resource getResource(final String name) {
99 String resourceName = name;
100 if (resourceName.startsWith("/"))
101 resourceName = resourceName.substring(1);
102 final Map<String, Resource> resources = this.resources;
103 return resources.get(resourceName);
104 }
105
106 void addResource(final String name, final Resource resource) {
107 final Map<String, Resource> resources = this.resources;
108 resources.put(name, resource);
109 addPaths(getPathFromResourceName(name));
110 }
111
112 private void addPaths(String path) {
113 final String[] parts = path.split("/");
114 String current = "";
115 for(String part : parts) {
116 current += part;
117 paths.add(current);
118 current += "/";
119 }
120 }
121
122 @Override
123 public String getLibrary(String name) {
124 return null;
125 }
126
127 @Override
128 public Collection<String> getPaths() {
129 return paths;
130 }
131
132 private String getPathFromResourceName(final String resourcePath) {
133 int idx = resourcePath.lastIndexOf('/');
134 final String path = idx > -1 ? resourcePath.substring(0, idx) : "";
135 return path;
136 }
137
138 private String getPathFromClassName(final String className) {
139 int idx = className.lastIndexOf('.');
140 return idx > -1 ? className.substring(0, idx).replace('.', '/') : "";
141 }
142
143 public static TestResourceLoaderBuilder build() {
144 return new TestResourceLoaderBuilder();
145 }
146
147 public static class TestResourceLoaderBuilder {
148 private final TestResourceLoader resourceLoader = new TestResourceLoader();
149
150 public TestResourceLoader create() {
151 return resourceLoader;
152 }
153
154 public TestResourceLoaderBuilder addResource(final String name, final URL resourceUrl) {
155 addResource(name, new Resource() {
156 @Override
157 public String getName() {
158 return name;
159 }
160
161 @Override
162 public URL getURL() {
163 return resourceUrl;
164 }
165
166 @Override
167 public InputStream openStream() throws IOException {
168 return resourceUrl.openStream();
169 }
170
171 @Override
172 public long getSize() {
173 return 0L;
174 }
175 });
176 return this;
177 }
178
179 public TestResourceLoaderBuilder addResource(final String name, final File resource) throws MalformedURLException {
180 final URL url = resource.toURI().toURL();
181 addResource(name, new Resource() {
182 @Override
183 public String getName() {
184 return name;
185 }
186
187 @Override
188 public URL getURL() {
189 return url;
190 }
191
192 @Override
193 public InputStream openStream() throws IOException {
194 return new FileInputStream(resource);
195 }
196
197 @Override
198 public long getSize() {
199 return resource.length();
200 }
201 });
202 return this;
203 }
204
205 public TestResourceLoaderBuilder addResource(final String name, final Resource resource) {
206 final TestResourceLoader resourceLoader = this.resourceLoader;
207 resourceLoader.addResource(name, resource);
208 return this;
209 }
210
211 public TestResourceLoaderBuilder addResources(final File base) throws Exception {
212 addResources("", base);
213 return this;
214 }
215
216 private void addResources(final String pathBase, final File base) throws Exception {
217 assertTrue(base.isDirectory());
218 final File[] children = base.listFiles();
219 for (File child : children) {
220 final String childPath = pathBase + child.getName();
221 if (child.isDirectory()) {
222 addResources(childPath + "/", child);
223 } else {
224 addResource(childPath, child);
225 }
226 }
227 }
228
229 public TestResourceLoaderBuilder addClass(final Class<?> aClass) throws Exception {
230 final ClassSpec classSpec = new ClassSpec();
231 classSpec.setCodeSource(aClass.getProtectionDomain().getCodeSource());
232 final byte[] classBytes = getClassBytes(aClass);
233 classSpec.setBytes(classBytes);
234 addClassSpec(aClass.getName(), classSpec);
235 return this;
236 }
237
238 public TestResourceLoaderBuilder addClasses(final Class<?>... classes) throws Exception {
239 for(Class<?> aClass : classes) {
240 addClass(aClass);
241 }
242 return this;
243 }
244
245 public TestResourceLoaderBuilder addClassSpec(final String name, final ClassSpec classSpec) {
246 final TestResourceLoader resourceLoader = this.resourceLoader;
247 resourceLoader.addClassSpec(name, classSpec);
248 return this;
249 }
250 }
251 }