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