1
package org.apache.maven.wagon.tck.http;
4
* Licensed to the Apache Software Foundation (ASF) under one
5
* or more contributor license agreements. See the NOTICE file
6
* distributed with this work for additional information
7
* regarding copyright ownership. The ASF licenses this file
8
* to you under the Apache License, Version 2.0 (the
9
* "License"); you may not use this file except in compliance
10
* with the License. You may obtain a copy of the License at
12
* http://www.apache.org/licenses/LICENSE-2.0
14
* Unless required by applicable law or agreed to in writing,
15
* software distributed under the License is distributed on an
16
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17
* KIND, either express or implied. See the License for the
18
* specific language governing permissions and limitations
23
import java.io.IOException;
24
import java.util.HashSet;
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;
46
public abstract class HttpWagonTests
49
private ServerFixture serverFixture;
51
private static PlexusContainer container;
55
private static WagonTestCaseConfigurator configurator;
57
private String baseUrl;
59
private static final Set<File> tmpFiles = new HashSet<File>();
61
private Repository repo;
63
private final Set<Object> notificationTargets = new HashSet<Object>();
65
protected static Logger logger = Logger.getLogger( HttpWagonTests.class );
68
public void beforeEach()
71
serverFixture = new ServerFixture( isSsl() );
72
serverFixture.start();
73
wagon = (Wagon) container.lookup( Wagon.ROLE, configurator.getWagonHint() );
77
public static void beforeAll()
80
File keystore = getResource( ServerFixture.SERVER_SSL_KEYSTORE_RESOURCE_PATH );
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 );
87
container = new DefaultPlexusContainer();
88
//container.initialize();
91
configurator = (WagonTestCaseConfigurator) container.lookup( WagonTestCaseConfigurator.class.getName() );
95
public void afterEach()
101
catch ( ConnectionException e )
106
for ( Object obj : notificationTargets )
114
if ( serverFixture != null )
118
serverFixture.stop();
120
catch ( Exception e )
128
container.release( wagon );
130
catch ( ComponentLifecycleException e )
137
public static void afterAll()
139
for ( File f : tmpFiles )
145
FileUtils.forceDelete( f );
147
catch ( IOException e )
154
if ( container != null )
158
container.release( configurator );
160
catch ( ComponentLifecycleException e )
169
protected void addNotificationTarget( final Object target )
171
notificationTargets.add( target );
174
protected File newTempFile()
177
File f = File.createTempFile( "wagon-target.", ".file" );
183
protected boolean isSsl()
188
protected ProxyInfo newProxyInfo()
190
ProxyInfo info = new ProxyInfo();
191
info.setType( isSsl() ? "https" : "http" );
192
info.setHost( ServerFixture.SERVER_HOST );
193
info.setPort( getPort() );
198
protected boolean isSupported()
200
StackTraceElement[] elements = new Throwable().getStackTrace();
201
String testCaseId = null;
202
String lastMethodName = null;
203
for ( StackTraceElement e : elements )
205
if ( !e.getClassName().startsWith( getClass().getPackage().getName() ) )
207
testCaseId = lastMethodName;
212
lastMethodName = e.getMethodName();
216
if ( testCaseId == null || !configurator.isSupported( testCaseId ) )
218
logger.error( "Cannot run test: " + testCaseId
219
+ ". Wagon under test does not support this test case." );
226
protected boolean initTest( final AuthenticationInfo auth, final ProxyInfo proxy )
227
throws ComponentConfigurationException, ConnectionException, AuthenticationException
229
return initTest( getBaseUrl(), auth, proxy );
232
protected boolean initTest( final String baseUrl, final AuthenticationInfo auth, final ProxyInfo proxy )
233
throws ComponentConfigurationException, ConnectionException, AuthenticationException
235
StackTraceElement[] elements = new Throwable().getStackTrace();
236
String testCaseId = null;
237
String lastMethodName = null;
238
for ( StackTraceElement e : elements )
240
if ( !e.getClassName().startsWith( getClass().getPackage().getName() ) )
242
testCaseId = lastMethodName;
247
lastMethodName = e.getMethodName();
251
if ( testCaseId == null || !configurator.configureWagonForTest( wagon, testCaseId ) )
253
logger.error( "Cannot run test: " + testCaseId
254
+ ". Wagon under test does not support this test case." );
261
serverFixture.start();
263
catch ( Exception e )
265
throw new IllegalStateException( "Failed to start: " + e.getMessage(), e );
268
repo = new Repository( "test", baseUrl );
270
wagon.connect( repo, auth, proxy );
275
protected int getPort()
277
return serverFixture.getHttpPort();
280
protected int getPortPropertyValue()
282
return Integer.parseInt( System.getProperty( "test.port", "-1" ) );
285
protected String getBaseUrl()
287
if ( baseUrl == null )
289
StringBuilder sb = new StringBuilder();
290
sb.append( isSsl() ? "https" : "http" );
291
sb.append( "://" + ServerFixture.SERVER_HOST + ":" );
292
sb.append( getPort() );
294
baseUrl = sb.toString();
300
protected ServerFixture getServerFixture()
302
return serverFixture;
305
protected static PlexusContainer getContainer()
310
protected Wagon getWagon()
315
protected static WagonTestCaseConfigurator getConfigurator()
320
protected static Set<File> getTmpfiles()
325
protected Repository getRepo()