1 | |
package org.apache.maven.surefire.booter; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.maven.surefire.util.NestedRuntimeException; |
23 | |
|
24 | |
import java.lang.reflect.InvocationTargetException; |
25 | |
import java.lang.reflect.Method; |
26 | |
import java.net.MalformedURLException; |
27 | |
import java.net.URL; |
28 | |
import java.util.Iterator; |
29 | |
import java.util.List; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public class ClasspathConfiguration |
41 | |
{ |
42 | |
private static final String CHILD_DELEGATION = "childDelegation"; |
43 | |
|
44 | |
private static final String ENABLE_ASSERTIONS = "enableAssertions"; |
45 | |
|
46 | |
private static final String CLASSPATH = "classPathUrl."; |
47 | |
|
48 | |
private static final String SUREFIRE_CLASSPATH = "surefireClassPathUrl."; |
49 | |
|
50 | |
private final Classpath classpathUrls; |
51 | |
|
52 | |
private final Classpath surefireClasspathUrls; |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
private final boolean enableAssertions; |
59 | |
|
60 | |
|
61 | |
private final boolean childDelegation; |
62 | |
|
63 | |
public ClasspathConfiguration( boolean enableAssertions, boolean childDelegation ) |
64 | |
{ |
65 | 0 | this( new Classpath(), new Classpath(), enableAssertions, childDelegation ); |
66 | 0 | } |
67 | |
|
68 | |
|
69 | |
ClasspathConfiguration( PropertiesWrapper properties ) |
70 | |
{ |
71 | 0 | this( properties.getClasspath( CLASSPATH ), |
72 | |
properties.getClasspath( SUREFIRE_CLASSPATH ), |
73 | |
properties.getBooleanProperty( ENABLE_ASSERTIONS ), properties.getBooleanProperty( CHILD_DELEGATION ) ); |
74 | 0 | } |
75 | |
|
76 | |
public ClasspathConfiguration( Classpath testClasspath, Classpath surefireClassPathUrls, boolean enableAssertions, |
77 | |
boolean childDelegation ) |
78 | 0 | { |
79 | 0 | this.enableAssertions = enableAssertions; |
80 | 0 | this.childDelegation = childDelegation; |
81 | 0 | this.classpathUrls = testClasspath; |
82 | 0 | this.surefireClasspathUrls = surefireClassPathUrls; |
83 | 0 | } |
84 | |
|
85 | |
public void setForkProperties( PropertiesWrapper properties ) |
86 | |
{ |
87 | 0 | properties.setClasspath( CLASSPATH, classpathUrls ); |
88 | 0 | properties.setClasspath( SUREFIRE_CLASSPATH, surefireClasspathUrls ); |
89 | 0 | properties.setProperty( ENABLE_ASSERTIONS, String.valueOf( enableAssertions ) ); |
90 | 0 | properties.setProperty( CHILD_DELEGATION, String.valueOf( childDelegation ) ); |
91 | 0 | } |
92 | |
|
93 | |
private static Method assertionStatusMethod; |
94 | |
|
95 | |
static |
96 | |
{ |
97 | |
try |
98 | |
{ |
99 | 0 | assertionStatusMethod = |
100 | 0 | ClassLoader.class.getMethod( "setDefaultAssertionStatus", new Class[]{ boolean.class } ); |
101 | |
} |
102 | 0 | catch ( NoSuchMethodException e ) |
103 | |
{ |
104 | 0 | assertionStatusMethod = null; |
105 | 0 | } |
106 | 0 | } |
107 | |
|
108 | |
public ClassLoader createTestClassLoaderConditionallySystem( boolean useSystemClassLoader ) |
109 | |
throws SurefireExecutionException |
110 | |
{ |
111 | 0 | return useSystemClassLoader |
112 | |
? ClassLoader.getSystemClassLoader() |
113 | |
: createTestClassLoader( this.childDelegation ); |
114 | |
} |
115 | |
|
116 | |
public ClassLoader createTestClassLoader( boolean childDelegation ) |
117 | |
throws SurefireExecutionException |
118 | |
{ |
119 | 0 | return createClassLoaderSEE( classpathUrls, null, childDelegation ); |
120 | |
} |
121 | |
|
122 | |
public ClassLoader createTestClassLoader() |
123 | |
throws SurefireExecutionException |
124 | |
{ |
125 | 0 | return createClassLoaderSEE( classpathUrls, null, this.childDelegation ); |
126 | |
} |
127 | |
|
128 | |
public ClassLoader createSurefireClassLoader( ClassLoader parent ) |
129 | |
throws SurefireExecutionException |
130 | |
{ |
131 | 0 | return createClassLoaderSEE( surefireClasspathUrls, parent, false ); |
132 | |
} |
133 | |
|
134 | |
private ClassLoader createClassLoaderSEE( Classpath classPathUrls, ClassLoader parent, boolean childDelegation ) |
135 | |
throws SurefireExecutionException |
136 | |
{ |
137 | |
try |
138 | |
{ |
139 | 0 | return createClassLoader( classPathUrls, parent, childDelegation ); |
140 | |
} |
141 | 0 | catch ( MalformedURLException e ) |
142 | |
{ |
143 | 0 | throw new SurefireExecutionException( "When creating classloader", e ); |
144 | |
} |
145 | |
|
146 | |
} |
147 | |
|
148 | |
private ClassLoader createClassLoader( Classpath classPathUrls, ClassLoader parent, boolean childDelegation ) |
149 | |
throws MalformedURLException |
150 | |
{ |
151 | 0 | List urls = classPathUrls.getAsUrlList(); |
152 | 0 | IsolatedClassLoader classLoader = new IsolatedClassLoader( parent, childDelegation ); |
153 | 0 | if ( assertionStatusMethod != null ) |
154 | |
{ |
155 | |
try |
156 | |
{ |
157 | 0 | Object[] args = new Object[]{ enableAssertions ? Boolean.TRUE : Boolean.FALSE }; |
158 | 0 | if ( parent != null ) |
159 | |
{ |
160 | 0 | assertionStatusMethod.invoke( parent, args ); |
161 | |
} |
162 | 0 | assertionStatusMethod.invoke( classLoader, args ); |
163 | |
} |
164 | 0 | catch ( IllegalAccessException e ) |
165 | |
{ |
166 | 0 | throw new NestedRuntimeException( "Unable to access the assertion enablement method", e ); |
167 | |
} |
168 | 0 | catch ( InvocationTargetException e ) |
169 | |
{ |
170 | 0 | throw new NestedRuntimeException( "Unable to invoke the assertion enablement method", e ); |
171 | 0 | } |
172 | |
} |
173 | 0 | for ( Iterator iter = urls.iterator(); iter.hasNext(); ) |
174 | |
{ |
175 | 0 | URL url = (URL) iter.next(); |
176 | 0 | classLoader.addURL( url ); |
177 | 0 | } |
178 | 0 | return classLoader; |
179 | |
} |
180 | |
|
181 | |
public Classpath getTestClasspath() |
182 | |
{ |
183 | 0 | return classpathUrls; |
184 | |
} |
185 | |
|
186 | |
public void addClasspathUrl( String path ) |
187 | |
{ |
188 | 0 | classpathUrls.addClassPathElementUrl( path ); |
189 | 0 | } |
190 | |
|
191 | |
public void addSurefireClasspathUrl( String path ) |
192 | |
{ |
193 | 0 | surefireClasspathUrls.addClassPathElementUrl( path ); |
194 | 0 | } |
195 | |
} |