1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.junit;
11
12 import static java.lang.ClassLoader.getSystemClassLoader;
13
14 import java.net.URL;
15 import java.net.URLClassLoader;
16
17 import org.junit.runners.BlockJUnit4ClassRunner;
18 import org.junit.runners.model.InitializationError;
19
20
21
22
23
24
25 public class SeparateClassloaderTestRunner extends BlockJUnit4ClassRunner {
26
27
28
29
30
31
32
33
34
35 public SeparateClassloaderTestRunner(Class<?> klass)
36 throws InitializationError {
37 super(loadNewClass(klass));
38 }
39
40 private static Class<?> loadNewClass(Class<?> klass)
41 throws InitializationError {
42 try {
43 URL[] urls = ((URLClassLoader) getSystemClassLoader()).getURLs();
44 ClassLoader testClassLoader = new URLClassLoader(urls) {
45
46 @Override
47 public Class<?> loadClass(String name)
48 throws ClassNotFoundException {
49 if (name.startsWith("org.eclipse.jgit.")) {
50 return super.findClass(name);
51 }
52
53 return super.loadClass(name);
54 }
55 };
56 return Class.forName(klass.getName(), true, testClassLoader);
57 } catch (ClassNotFoundException e) {
58 throw new InitializationError(e);
59 }
60 }
61 }