/
ByteArrayClassLoaderChildFirstTest.java
225 lines (191 loc) · 8.97 KB
/
ByteArrayClassLoaderChildFirstTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package net.bytebuddy.dynamic.loading;
import net.bytebuddy.ClassFileVersion;
import net.bytebuddy.asm.AsmVisitorWrapper;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.test.utility.ClassFileExtraction;
import net.bytebuddy.test.utility.MockitoRule;
import org.hamcrest.CoreMatchers;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.mockito.Mock;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.commons.ClassRemapper;
import org.objectweb.asm.commons.SimpleRemapper;
import java.io.InputStream;
import java.net.URL;
import java.security.ProtectionDomain;
import java.util.*;
import static junit.framework.TestCase.assertEquals;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
@RunWith(Parameterized.class)
public class ByteArrayClassLoaderChildFirstTest {
private static final String BAR = "bar", CLASS_FILE = ".class";
private static final ProtectionDomain DEFAULT_PROTECTION_DOMAIN = null;
private final ByteArrayClassLoader.PersistenceHandler persistenceHandler;
private final boolean expectedResourceLookup;
@Rule
public TestRule mockitoRule = new MockitoRule(this);
private ClassLoader classLoader;
@Mock
private PackageDefinitionStrategy packageDefinitionStrategy;
public ByteArrayClassLoaderChildFirstTest(ByteArrayClassLoader.PersistenceHandler persistenceHandler, boolean expectedResourceLookup) {
this.persistenceHandler = persistenceHandler;
this.expectedResourceLookup = expectedResourceLookup;
}
@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{ByteArrayClassLoader.PersistenceHandler.LATENT, false},
{ByteArrayClassLoader.PersistenceHandler.MANIFEST, true}
});
}
@Before
public void setUp() throws Exception {
Map<String, byte[]> values = Collections.singletonMap(Foo.class.getName(),
ClassFileExtraction.extract(Bar.class, new RenamingWrapper(Bar.class.getName().replace('.', '/'),
Foo.class.getName().replace('.', '/'))));
classLoader = new ByteArrayClassLoader.ChildFirst(getClass().getClassLoader(),
values,
DEFAULT_PROTECTION_DOMAIN,
persistenceHandler,
PackageDefinitionStrategy.NoOp.INSTANCE);
}
@Test
public void testLoading() throws Exception {
Class<?> type = classLoader.loadClass(Foo.class.getName());
assertThat(type.getClassLoader(), is(classLoader));
assertEquals(classLoader.loadClass(Foo.class.getName()), type);
assertThat(type, not(CoreMatchers.<Class<?>>is(Foo.class)));
assertThat(type.getPackage(), notNullValue(Package.class));
// Due to change in API in Java 9 where package identity is no longer bound by hierarchy.
assertThat(type.getPackage(), ClassFileVersion.forThisVm().isAtLeast(ClassFileVersion.JAVA_V9)
? not(is(Foo.class.getPackage()))
: is(Foo.class.getPackage()));
}
@Test
public void testResourceStreamLookupBeforeLoading() throws Exception {
InputStream inputStream = classLoader.getResourceAsStream(Foo.class.getName().replace('.', '/') + CLASS_FILE);
try {
assertThat(inputStream, expectedResourceLookup ? notNullValue(InputStream.class) : nullValue(InputStream.class));
} finally {
if (inputStream != null) {
inputStream.close();
}
}
}
@Test
public void testResourceStreamLookupAfterLoading() throws Exception {
assertThat(classLoader.loadClass(Foo.class.getName()).getClassLoader(), is(classLoader));
InputStream inputStream = classLoader.getResourceAsStream(Foo.class.getName().replace('.', '/') + CLASS_FILE);
try {
assertThat(inputStream, expectedResourceLookup ? notNullValue(InputStream.class) : nullValue(InputStream.class));
} finally {
if (inputStream != null) {
inputStream.close();
}
}
}
@Test
public void testResourceLookupBeforeLoading() throws Exception {
assertThat(classLoader.getResource(Foo.class.getName().replace('.', '/') + CLASS_FILE), expectedResourceLookup
? notNullValue(URL.class)
: nullValue(URL.class));
}
@Test
public void testResourceLookupAfterLoading() throws Exception {
assertThat(classLoader.loadClass(Foo.class.getName()).getClassLoader(), is(classLoader));
assertThat(classLoader.getResource(Foo.class.getName().replace('.', '/') + CLASS_FILE), expectedResourceLookup
? notNullValue(URL.class)
: nullValue(URL.class));
}
@Test
public void testResourcesLookupBeforeLoading() throws Exception {
Enumeration<URL> enumeration = classLoader.getResources(Foo.class.getName().replace('.', '/') + CLASS_FILE);
assertThat(enumeration.hasMoreElements(), is(true));
assertThat(enumeration.nextElement(), notNullValue(URL.class));
assertThat(enumeration.hasMoreElements(), is(expectedResourceLookup));
if (expectedResourceLookup) {
assertThat(enumeration.nextElement(), notNullValue(URL.class));
assertThat(enumeration.hasMoreElements(), is(false));
}
}
@Test
public void testResourcesLookupAfterLoading() throws Exception {
assertThat(classLoader.loadClass(Foo.class.getName()).getClassLoader(), is(classLoader));
Enumeration<URL> enumeration = classLoader.getResources(Foo.class.getName().replace('.', '/') + CLASS_FILE);
assertThat(enumeration.hasMoreElements(), is(true));
assertThat(enumeration.nextElement(), notNullValue(URL.class));
assertThat(enumeration.hasMoreElements(), is(expectedResourceLookup));
if (expectedResourceLookup) {
assertThat(enumeration.nextElement(), notNullValue(URL.class));
assertThat(enumeration.hasMoreElements(), is(false));
}
}
@Test
public void testResourceLookupWithPrefixBeforeLoading() throws Exception {
assertThat(classLoader.getResource("/" + Foo.class.getName().replace('.', '/') + CLASS_FILE), expectedResourceLookup
? notNullValue(URL.class)
: nullValue(URL.class));
}
@Test
public void testResourceLookupWithPrefixAfterLoading() throws Exception {
assertThat(classLoader.loadClass(Foo.class.getName()).getClassLoader(), is(classLoader));
assertThat(classLoader.getResource("/" + Foo.class.getName().replace('.', '/') + CLASS_FILE), expectedResourceLookup
? notNullValue(URL.class)
: nullValue(URL.class));
}
@Test
public void testResourcesLookupWithPrefixBeforeLoading() throws Exception {
Enumeration<URL> enumeration = classLoader.getResources("/" + Foo.class.getName().replace('.', '/') + CLASS_FILE);
assertThat(enumeration.hasMoreElements(), is(expectedResourceLookup));
if (expectedResourceLookup) {
assertThat(enumeration.nextElement(), notNullValue(URL.class));
assertThat(enumeration.hasMoreElements(), is(false));
}
}
@Test
public void testResourcesLookupWithPrefixAfterLoading() throws Exception {
assertThat(classLoader.loadClass(Foo.class.getName()).getClassLoader(), is(classLoader));
Enumeration<URL> enumeration = classLoader.getResources("/" + Foo.class.getName().replace('.', '/') + CLASS_FILE);
assertThat(enumeration.hasMoreElements(), is(expectedResourceLookup));
if (expectedResourceLookup) {
assertThat(enumeration.nextElement(), notNullValue(URL.class));
assertThat(enumeration.hasMoreElements(), is(false));
}
}
@Test(expected = ClassNotFoundException.class)
public void testNotFoundException() throws Exception {
// Note: Will throw a class format error instead targeting not found exception targeting loader attempts.
classLoader.loadClass(BAR);
}
public static class Foo {
/* empty */
}
public static class Bar {
/* empty */
}
private static class RenamingWrapper implements AsmVisitorWrapper {
private final String oldName, newName;
private RenamingWrapper(String oldName, String newName) {
this.oldName = oldName;
this.newName = newName;
}
@Override
public int mergeWriter(int flags) {
return flags;
}
@Override
public int mergeReader(int flags) {
return flags;
}
@Override
public ClassVisitor wrap(TypeDescription instrumentedType, ClassVisitor classVisitor, int writerFlags, int readerFlags) {
return new ClassRemapper(classVisitor, new SimpleRemapper(oldName, newName));
}
}
}