Debian Codegraph @

src/test/java/org/jboss/modules/ClassDefinerTest.java

raw
1 /*
2 * JBoss, Home of Professional Open Source.
3 * Copyright 2018 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.io.ByteArrayOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.net.URL;
25 import java.security.CodeSigner;
26 import java.security.CodeSource;
27 import java.security.ProtectionDomain;
28 import java.util.Collection;
29 import java.util.Collections;
30 import org.jboss.modules.util.TestModuleLoader;
31 import org.junit.Before;
32 import org.junit.Test;
33
34 import static org.junit.Assert.*;
35
36 /**
37 */
38 public class ClassDefinerTest extends AbstractModuleTestCase {
39
40 private TestModuleLoader moduleLoader;
41
42 @Before
43 public void setupModuleLoader() {
44 moduleLoader = new TestModuleLoader();
45 ModuleSpec.Builder builder = ModuleSpec.build("org.module.foo");
46 builder.addDependency(ModuleDependencySpec.JAVA_BASE);
47 builder.addDependency(DependencySpec.OWN_DEPENDENCY);
48 builder.addResourceRoot(ResourceLoaderSpec.createResourceLoaderSpec(new ResourceLoader() {
49 public ClassSpec getClassSpec(final String fileName) {
50 return null;
51 }
52
53 public PackageSpec getPackageSpec(final String name) {
54 return null;
55 }
56
57 public Resource getResource(final String name) {
58 return null;
59 }
60
61 public String getLibrary(final String name) {
62 return null;
63 }
64
65 public Collection<String> getPaths() {
66 return Collections.singletonList("org/module/foo");
67 }
68 }));
69 moduleLoader.addModuleSpec(builder.create());
70 }
71
72 @Test
73 public void testDefineClass() throws ModuleLoadException, IOException, ClassNotFoundException {
74 final ClassDefiner classDefiner = ClassDefiner.getInstance();
75 final Module module = moduleLoader.loadModule("org.module.foo");
76 final URL resource = ClassDefinerTest.class.getClassLoader().getResource("test/GeneratedClass.class");
77 assertNotNull(resource);
78 final byte[] classBytes;
79 try (final InputStream stream = resource.openConnection().getInputStream()) {
80 try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) {
81 byte[] buf = new byte[256];
82 int res;
83 while ((res = stream.read(buf)) != -1) {
84 os.write(buf, 0, res);
85 }
86 classBytes = os.toByteArray();
87 }
88 }
89 Class<?> c1 = classDefiner.defineClass(module, "org.module.foo.GeneratedClass", new ProtectionDomain(
90 new CodeSource(
91 resource, (CodeSigner[]) null
92 ),
93 ModulesPolicy.DEFAULT_PERMISSION_COLLECTION
94 ), classBytes);
95 Class<?> c2 = module.getClassLoaderPrivate().loadClass("org.module.foo.GeneratedClass");
96 assertEquals(c1, c2);
97 assertEquals(module.getClassLoaderPrivate(), c1.getClassLoader());
98 }
99 }