1 | |
package org.apache.maven.wagon.tck.http; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import static org.apache.maven.wagon.tck.http.util.TestUtil.getResource; |
23 | |
|
24 | |
import org.apache.maven.wagon.ConnectionException; |
25 | |
import org.apache.maven.wagon.Wagon; |
26 | |
import org.apache.maven.wagon.authentication.AuthenticationException; |
27 | |
import org.apache.maven.wagon.authentication.AuthenticationInfo; |
28 | |
import org.apache.maven.wagon.proxy.ProxyInfo; |
29 | |
import org.apache.maven.wagon.repository.Repository; |
30 | |
import org.apache.maven.wagon.tck.http.fixture.ServerFixture; |
31 | |
import org.codehaus.plexus.DefaultPlexusContainer; |
32 | |
import org.codehaus.plexus.PlexusContainer; |
33 | |
import org.codehaus.plexus.component.configurator.ComponentConfigurationException; |
34 | |
import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException; |
35 | |
import org.codehaus.plexus.util.FileUtils; |
36 | |
import org.junit.After; |
37 | |
import org.junit.AfterClass; |
38 | |
import org.junit.Before; |
39 | |
import org.junit.BeforeClass; |
40 | |
|
41 | |
import java.io.File; |
42 | |
import java.io.IOException; |
43 | |
import java.util.HashSet; |
44 | |
import java.util.Set; |
45 | |
|
46 | 0 | public abstract class HttpWagonTests |
47 | |
{ |
48 | |
|
49 | |
private ServerFixture serverFixture; |
50 | |
|
51 | |
private static PlexusContainer container; |
52 | |
|
53 | |
private Wagon wagon; |
54 | |
|
55 | |
private static WagonTestCaseConfigurator configurator; |
56 | |
|
57 | |
private String baseUrl; |
58 | |
|
59 | 0 | private final static Set<File> tmpFiles = new HashSet<File>(); |
60 | |
|
61 | |
private Repository repo; |
62 | |
|
63 | 0 | private final Set<Object> notificationTargets = new HashSet<Object>(); |
64 | |
|
65 | |
@Before |
66 | |
public void beforeEach() |
67 | |
throws Exception |
68 | |
{ |
69 | 0 | serverFixture = new ServerFixture( getPort(), isSsl() ); |
70 | 0 | wagon = (Wagon) container.lookup( Wagon.ROLE, configurator.getWagonHint() ); |
71 | 0 | } |
72 | |
|
73 | |
@BeforeClass |
74 | |
public static void beforeAll() |
75 | |
throws Exception |
76 | |
{ |
77 | 0 | File keystore = getResource( ServerFixture.SERVER_SSL_KEYSTORE_RESOURCE_PATH ); |
78 | |
|
79 | 0 | System.setProperty( "javax.net.ssl.keyStore", keystore.getAbsolutePath() ); |
80 | 0 | System.setProperty( "javax.net.ssl.keyStorePassword", ServerFixture.SERVER_SSL_KEYSTORE_PASSWORD ); |
81 | 0 | System.setProperty( "javax.net.ssl.trustStore", keystore.getAbsolutePath() ); |
82 | 0 | System.setProperty( "javax.net.ssl.trustStorePassword", ServerFixture.SERVER_SSL_KEYSTORE_PASSWORD ); |
83 | |
|
84 | 0 | container = new DefaultPlexusContainer(); |
85 | |
|
86 | |
|
87 | |
|
88 | 0 | configurator = (WagonTestCaseConfigurator) container.lookup( WagonTestCaseConfigurator.class.getName() ); |
89 | 0 | } |
90 | |
|
91 | |
@After |
92 | |
public void afterEach() |
93 | |
{ |
94 | |
try |
95 | |
{ |
96 | 0 | wagon.disconnect(); |
97 | |
} |
98 | 0 | catch ( ConnectionException e ) |
99 | |
{ |
100 | 0 | e.printStackTrace(); |
101 | 0 | } |
102 | |
|
103 | 0 | for ( Object obj : notificationTargets ) |
104 | |
{ |
105 | 0 | synchronized ( obj ) |
106 | |
{ |
107 | 0 | obj.notify(); |
108 | 0 | } |
109 | |
} |
110 | |
|
111 | 0 | if ( serverFixture != null ) |
112 | |
{ |
113 | |
try |
114 | |
{ |
115 | 0 | serverFixture.stop(); |
116 | |
} |
117 | 0 | catch ( Exception e ) |
118 | |
{ |
119 | 0 | e.printStackTrace(); |
120 | 0 | } |
121 | |
} |
122 | |
|
123 | |
try |
124 | |
{ |
125 | 0 | container.release( wagon ); |
126 | |
} |
127 | 0 | catch ( ComponentLifecycleException e ) |
128 | |
{ |
129 | 0 | e.printStackTrace(); |
130 | 0 | } |
131 | 0 | } |
132 | |
|
133 | |
@AfterClass |
134 | |
public static void afterAll() |
135 | |
{ |
136 | 0 | for ( File f : tmpFiles ) |
137 | |
{ |
138 | 0 | if ( f.exists() ) |
139 | |
{ |
140 | |
try |
141 | |
{ |
142 | 0 | FileUtils.forceDelete( f ); |
143 | |
} |
144 | 0 | catch ( IOException e ) |
145 | |
{ |
146 | 0 | e.printStackTrace(); |
147 | 0 | } |
148 | |
} |
149 | |
} |
150 | |
|
151 | 0 | if ( container != null ) |
152 | |
{ |
153 | |
try |
154 | |
{ |
155 | 0 | container.release( configurator ); |
156 | |
} |
157 | 0 | catch ( ComponentLifecycleException e ) |
158 | |
{ |
159 | 0 | e.printStackTrace(); |
160 | 0 | } |
161 | |
|
162 | 0 | container.dispose(); |
163 | |
} |
164 | 0 | } |
165 | |
|
166 | |
protected void addNotificationTarget( final Object target ) |
167 | |
{ |
168 | 0 | notificationTargets.add( target ); |
169 | 0 | } |
170 | |
|
171 | |
protected File newTempFile() |
172 | |
throws IOException |
173 | |
{ |
174 | 0 | File f = File.createTempFile( "wagon-target.", ".file" ); |
175 | 0 | f.deleteOnExit(); |
176 | |
|
177 | 0 | return f; |
178 | |
} |
179 | |
|
180 | |
protected boolean isSsl() |
181 | |
{ |
182 | 0 | return false; |
183 | |
} |
184 | |
|
185 | |
protected ProxyInfo newProxyInfo() |
186 | |
{ |
187 | 0 | ProxyInfo info = new ProxyInfo(); |
188 | 0 | info.setType( isSsl() ? "https" : "http" ); |
189 | 0 | info.setHost( ServerFixture.SERVER_HOST ); |
190 | 0 | info.setPort( getPort() ); |
191 | |
|
192 | 0 | return info; |
193 | |
} |
194 | |
|
195 | |
protected boolean isSupported() |
196 | |
{ |
197 | 0 | StackTraceElement[] elements = new Throwable().getStackTrace(); |
198 | 0 | String testCaseId = null; |
199 | 0 | String lastMethodName = null; |
200 | 0 | for ( StackTraceElement e : elements ) |
201 | |
{ |
202 | 0 | if ( !e.getClassName().startsWith( getClass().getPackage().getName() ) ) |
203 | |
{ |
204 | 0 | testCaseId = lastMethodName; |
205 | 0 | break; |
206 | |
} |
207 | |
else |
208 | |
{ |
209 | 0 | lastMethodName = e.getMethodName(); |
210 | |
} |
211 | |
} |
212 | |
|
213 | 0 | if ( testCaseId == null || !configurator.isSupported( testCaseId ) ) |
214 | |
{ |
215 | 0 | System.out.println( "Cannot run test: " + testCaseId |
216 | |
+ ". Wagon under test does not support this test case." ); |
217 | 0 | return false; |
218 | |
} |
219 | |
|
220 | 0 | return true; |
221 | |
} |
222 | |
|
223 | |
protected boolean initTest( final AuthenticationInfo auth, final ProxyInfo proxy ) |
224 | |
throws ComponentConfigurationException, ConnectionException, AuthenticationException |
225 | |
{ |
226 | 0 | return initTest( getBaseUrl(), auth, proxy ); |
227 | |
} |
228 | |
|
229 | |
protected boolean initTest( final String baseUrl, final AuthenticationInfo auth, final ProxyInfo proxy ) |
230 | |
throws ComponentConfigurationException, ConnectionException, AuthenticationException |
231 | |
{ |
232 | 0 | StackTraceElement[] elements = new Throwable().getStackTrace(); |
233 | 0 | String testCaseId = null; |
234 | 0 | String lastMethodName = null; |
235 | 0 | for ( StackTraceElement e : elements ) |
236 | |
{ |
237 | 0 | if ( !e.getClassName().startsWith( getClass().getPackage().getName() ) ) |
238 | |
{ |
239 | 0 | testCaseId = lastMethodName; |
240 | 0 | break; |
241 | |
} |
242 | |
else |
243 | |
{ |
244 | 0 | lastMethodName = e.getMethodName(); |
245 | |
} |
246 | |
} |
247 | |
|
248 | 0 | if ( testCaseId == null || !configurator.configureWagonForTest( wagon, testCaseId ) ) |
249 | |
{ |
250 | 0 | System.out.println( "Cannot run test: " + testCaseId |
251 | |
+ ". Wagon under test does not support this test case." ); |
252 | |
|
253 | 0 | return false; |
254 | |
} |
255 | |
|
256 | |
try |
257 | |
{ |
258 | 0 | serverFixture.start(); |
259 | |
} |
260 | 0 | catch ( Exception e ) |
261 | |
{ |
262 | 0 | throw new IllegalStateException( "Failed to start: " + e.getMessage(), e ); |
263 | 0 | } |
264 | |
|
265 | 0 | repo = new Repository( "test", baseUrl ); |
266 | |
|
267 | 0 | wagon.connect( repo, auth, proxy ); |
268 | |
|
269 | 0 | return true; |
270 | |
} |
271 | |
|
272 | |
protected int getPort() |
273 | |
{ |
274 | 0 | int port = getPortPropertyValue(); |
275 | 0 | if ( port < 1 ) |
276 | |
{ |
277 | 0 | port = getDefaultPort(); |
278 | |
} |
279 | |
|
280 | 0 | return port; |
281 | |
} |
282 | |
|
283 | |
protected int getDefaultPort() |
284 | |
{ |
285 | 0 | return 9080; |
286 | |
} |
287 | |
|
288 | |
protected int getPortPropertyValue() |
289 | |
{ |
290 | 0 | return Integer.parseInt( System.getProperty( "test.port", "-1" ) ); |
291 | |
} |
292 | |
|
293 | |
protected String getBaseUrl() |
294 | |
{ |
295 | 0 | if ( baseUrl == null ) |
296 | |
{ |
297 | 0 | StringBuilder sb = new StringBuilder(); |
298 | 0 | sb.append( isSsl() ? "https" : "http" ); |
299 | 0 | sb.append( "://" + ServerFixture.SERVER_HOST + ":" ); |
300 | 0 | sb.append( getPort() ); |
301 | |
|
302 | 0 | baseUrl = sb.toString(); |
303 | |
} |
304 | |
|
305 | 0 | return baseUrl; |
306 | |
} |
307 | |
|
308 | |
protected ServerFixture getServerFixture() |
309 | |
{ |
310 | 0 | return serverFixture; |
311 | |
} |
312 | |
|
313 | |
protected static PlexusContainer getContainer() |
314 | |
{ |
315 | 0 | return container; |
316 | |
} |
317 | |
|
318 | |
protected Wagon getWagon() |
319 | |
{ |
320 | 0 | return wagon; |
321 | |
} |
322 | |
|
323 | |
protected static WagonTestCaseConfigurator getConfigurator() |
324 | |
{ |
325 | 0 | return configurator; |
326 | |
} |
327 | |
|
328 | |
protected static Set<File> getTmpfiles() |
329 | |
{ |
330 | 0 | return tmpFiles; |
331 | |
} |
332 | |
|
333 | |
protected Repository getRepo() |
334 | |
{ |
335 | 0 | return repo; |
336 | |
} |
337 | |
|
338 | |
} |