1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package io.earcam.instrumental.lade;
20
21 import static java.lang.Thread.currentThread;
22 import static org.hamcrest.MatcherAssert.assertThat;
23 import static org.hamcrest.Matchers.*;
24
25 import java.io.IOException;
26 import java.net.URLClassLoader;
27 import java.nio.file.Path;
28 import java.nio.file.Paths;
29
30 import org.junit.Test;
31
32 import io.earcam.instrumental.reflect.Resources;
33
34 public class ClassLoadersTest {
35
36
37 @Test
38 public void selfFirstClassLoader() throws IOException, ClassNotFoundException
39 {
40 Path junitApiJar = Paths.get(Resources.sourceOfResource(Test.class));
41
42 try(URLClassLoader selfFirst = ClassLoaders.selfFirstClassLoader(junitApiJar)) {
43
44 Class<?> loaded = selfFirst.loadClass(Test.class.getCanonicalName());
45 assertThat(loaded, is(not(equalTo(Test.class))));
46 }
47 }
48
49
50
51 @Test
52 public void selfFirstClassLoaderTwiceLoaded() throws IOException, ClassNotFoundException
53 {
54 Path junitApiJar = Paths.get(Resources.sourceOfResource(Test.class));
55
56 try(URLClassLoader selfFirst = ClassLoaders.selfFirstClassLoader(junitApiJar)) {
57
58 Class<?> a = selfFirst.loadClass(Test.class.getCanonicalName());
59 Class<?> b = selfFirst.loadClass(Test.class.getCanonicalName());
60
61 assertThat(a, is(equalTo(b)));
62 }
63 }
64
65
66 @Test
67 public void selfFirstClassLoaderNotFoundInSelf() throws IOException, ClassNotFoundException
68 {
69 Path junitApiJar = Paths.get(Resources.sourceOfResource(Test.class));
70
71 try(URLClassLoader selfFirst = ClassLoaders.selfFirstClassLoader(junitApiJar)) {
72
73 Class<?> loaded = selfFirst.loadClass(String.class.getCanonicalName());
74
75 assertThat(loaded, is(equalTo(String.class)));
76 }
77 }
78
79
80 @Test
81 public void runInContext()
82 {
83 InMemoryClassLoader a = ClassLoaders.inMemoryClassLoader();
84 InMemoryClassLoader b = ClassLoaders.inMemoryClassLoader();
85
86 currentThread().setContextClassLoader(a);
87
88 ClassLoaders.runInContext(b, () -> assertThat(currentThread().getContextClassLoader(), is(sameInstance(b))));
89
90 assertThat(currentThread().getContextClassLoader(), is(sameInstance(a)));
91 }
92
93
94 @Test
95 public void canDefineAClass()
96 {
97 byte[] bytes = Resources.classAsBytes(ClassLoadersTest.class);
98
99 Class<?> loaded = ClassLoaders.load(bytes);
100
101 assertThat(loaded.getCanonicalName(), is(equalTo(ClassLoadersTest.class.getCanonicalName())));
102 assertThat(loaded, is(not(equalTo(ClassLoadersTest.class))));
103 }
104
105
106 @Test
107 public void canDefineAClassFromSubsectionOfArray()
108 {
109 byte[] section = Resources.classAsBytes(ClassLoadersTest.class);
110
111 byte[] bytes = new byte[10 + section.length + 20];
112
113 System.arraycopy(section, 0, bytes, 10, section.length);
114
115 Class<?> loaded = ClassLoaders.load(ClassLoadersTest.class.getCanonicalName(), bytes, 10, section.length);
116
117 assertThat(loaded.getCanonicalName(), is(equalTo(ClassLoadersTest.class.getCanonicalName())));
118 assertThat(loaded, is(not(equalTo(ClassLoadersTest.class))));
119 }
120 }