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