1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.junit;
11
12 import java.net.MalformedURLException;
13 import java.net.URL;
14 import java.net.URLClassLoader;
15 import java.nio.file.Paths;
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 String pathSeparator = System.getProperty("path.separator");
44 String[] classPathEntries = System.getProperty("java.class.path")
45 .split(pathSeparator);
46 URL[] urls = new URL[classPathEntries.length];
47 for (int i = 0; i < classPathEntries.length; i++) {
48 urls[i] = Paths.get(classPathEntries[i]).toUri().toURL();
49 }
50 ClassLoader testClassLoader = new URLClassLoader(urls) {
51
52 @Override
53 public Class<?> loadClass(String name)
54 throws ClassNotFoundException {
55 if (name.startsWith("org.eclipse.jgit.")) {
56 return super.findClass(name);
57 }
58
59 return super.loadClass(name);
60 }
61 };
62 return Class.forName(klass.getName(), true, testClassLoader);
63 } catch (ClassNotFoundException | MalformedURLException e) {
64 throw new InitializationError(e);
65 }
66 }
67 }