1 package com.atlassian.plugin.classloader;
2
3 import org.junit.Test;
4
5 import static org.junit.Assert.assertNotNull;
6
7
8
9
10 public class TestDelegationClassLoader {
11
12 @Test
13 public void testLoadSystemClassWithOutDelegateSet() throws ClassNotFoundException {
14 DelegationClassLoader classLoader = new DelegationClassLoader();
15 Class c = classLoader.loadClass("java.lang.String");
16 assertNotNull(c);
17 }
18
19 @Test
20 public void testLoadSystemClassWithDelegateSet() throws ClassNotFoundException {
21 ClassLoader parentClassLoader = TestDelegationClassLoader.class.getClassLoader();
22 DelegationClassLoader classLoader = new DelegationClassLoader();
23 classLoader.setDelegateClassLoader(parentClassLoader);
24 Class c = classLoader.loadClass("java.lang.String");
25 assertNotNull(c);
26 }
27
28 @Test(expected = ClassNotFoundException.class)
29 public void testCantLoadUnknownClassWithOutDelegateSet() throws ClassNotFoundException {
30 DelegationClassLoader classLoader = new DelegationClassLoader();
31 classLoader.loadClass("not.a.real.class.path.NotARealClass");
32 }
33
34 @Test(expected = ClassNotFoundException.class)
35 public void testCantLoadUnknownClassWithDelegateSet() throws ClassNotFoundException {
36 ClassLoader parentClassLoader = TestDelegationClassLoader.class.getClassLoader();
37 DelegationClassLoader classLoader = new DelegationClassLoader();
38 classLoader.setDelegateClassLoader(parentClassLoader);
39 classLoader.loadClass("not.a.real.class.path.NotARealClass");
40 }
41
42 @Test(expected = NullPointerException.class)
43 public void testCantSetNullDelegate() {
44 DelegationClassLoader dcl = new DelegationClassLoader();
45 dcl.setDelegateClassLoader(null);
46 }
47
48 @Test
49 public void testCanSetDelegate() {
50 DelegationClassLoader dcl = new DelegationClassLoader();
51 dcl.setDelegateClassLoader(getClass().getClassLoader());
52 }
53 }