View Javadoc
1   /*-
2    * #%L
3    * io.earcam.instrumental.lade
4    * %%
5    * Copyright (C) 2018 earcam
6    * %%
7    * SPDX-License-Identifier: (BSD-3-Clause OR EPL-1.0 OR Apache-2.0 OR MIT)
8    * 
9    * You <b>must</b> choose to accept, in full - any individual or combination of 
10   * the following licenses:
11   * <ul>
12   * 	<li><a href="https://opensource.org/licenses/BSD-3-Clause">BSD-3-Clause</a></li>
13   * 	<li><a href="https://www.eclipse.org/legal/epl-v10.html">EPL-1.0</a></li>
14   * 	<li><a href="https://www.apache.org/licenses/LICENSE-2.0">Apache-2.0</a></li>
15   * 	<li><a href="https://opensource.org/licenses/MIT">MIT</a></li>
16   * </ul>
17   * #L%
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  	// EARCAM_SNIPPET_BEGIN: selffirst
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  	// EARCAM_SNIPPET_END: selffirst
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 }