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