001package org.apache.maven.wagon.tck.http; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.io.File; 023import java.io.IOException; 024import java.util.HashSet; 025import java.util.Set; 026 027import org.apache.log4j.Logger; 028import org.apache.maven.wagon.ConnectionException; 029import org.apache.maven.wagon.Wagon; 030import org.apache.maven.wagon.authentication.AuthenticationException; 031import org.apache.maven.wagon.authentication.AuthenticationInfo; 032import org.apache.maven.wagon.proxy.ProxyInfo; 033import org.apache.maven.wagon.repository.Repository; 034import org.apache.maven.wagon.tck.http.fixture.ServerFixture; 035import org.codehaus.plexus.DefaultPlexusContainer; 036import org.codehaus.plexus.PlexusContainer; 037import org.codehaus.plexus.component.configurator.ComponentConfigurationException; 038import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException; 039import org.codehaus.plexus.util.FileUtils; 040import org.junit.After; 041import org.junit.AfterClass; 042import org.junit.Before; 043import org.junit.BeforeClass; 044import static org.apache.maven.wagon.tck.http.util.TestUtil.getResource; 045 046/** 047 * 048 */ 049public abstract class HttpWagonTests 050{ 051 052 private ServerFixture serverFixture; 053 054 private static PlexusContainer container; 055 056 private Wagon wagon; 057 058 private static WagonTestCaseConfigurator configurator; 059 060 private String baseUrl; 061 062 private static final Set<File> TMP_FILES = new HashSet<File>(); 063 064 private Repository repo; 065 066 private final Set<Object> notificationTargets = new HashSet<Object>(); 067 068 protected static final Logger LOGGER = Logger.getLogger( HttpWagonTests.class ); 069 070 @Before 071 public void beforeEach() 072 throws Exception 073 { 074 serverFixture = new ServerFixture( isSsl() ); 075 serverFixture.start(); 076 wagon = (Wagon) container.lookup( Wagon.ROLE, configurator.getWagonHint() ); 077 } 078 079 @BeforeClass 080 public static void beforeAll() 081 throws Exception 082 { 083 File keystore = getResource( ServerFixture.SERVER_SSL_KEYSTORE_RESOURCE_PATH ); 084 085 System.setProperty( "javax.net.ssl.keyStore", keystore.getAbsolutePath() ); 086 System.setProperty( "javax.net.ssl.keyStorePassword", ServerFixture.SERVER_SSL_KEYSTORE_PASSWORD ); 087 System.setProperty( "javax.net.ssl.trustStore", keystore.getAbsolutePath() ); 088 System.setProperty( "javax.net.ssl.trustStorePassword", ServerFixture.SERVER_SSL_KEYSTORE_PASSWORD ); 089 090 container = new DefaultPlexusContainer(); 091 //container.initialize(); 092 //container.start(); 093 094 configurator = (WagonTestCaseConfigurator) container.lookup( WagonTestCaseConfigurator.class.getName() ); 095 } 096 097 @After 098 public void afterEach() 099 { 100 try 101 { 102 wagon.disconnect(); 103 } 104 catch ( ConnectionException e ) 105 { 106 e.printStackTrace(); 107 } 108 109 for ( Object obj : notificationTargets ) 110 { 111 synchronized ( obj ) 112 { 113 obj.notify(); 114 } 115 } 116 117 if ( serverFixture != null ) 118 { 119 try 120 { 121 serverFixture.stop(); 122 } 123 catch ( Exception e ) 124 { 125 e.printStackTrace(); 126 } 127 } 128 129 try 130 { 131 container.release( wagon ); 132 } 133 catch ( ComponentLifecycleException e ) 134 { 135 e.printStackTrace(); 136 } 137 } 138 139 @AfterClass 140 public static void afterAll() 141 { 142 for ( File f : TMP_FILES ) 143 { 144 if ( f.exists() ) 145 { 146 try 147 { 148 FileUtils.forceDelete( f ); 149 } 150 catch ( IOException e ) 151 { 152 e.printStackTrace(); 153 } 154 } 155 } 156 157 if ( container != null ) 158 { 159 try 160 { 161 container.release( configurator ); 162 } 163 catch ( ComponentLifecycleException e ) 164 { 165 e.printStackTrace(); 166 } 167 168 container.dispose(); 169 } 170 } 171 172 protected void addNotificationTarget( final Object target ) 173 { 174 notificationTargets.add( target ); 175 } 176 177 protected File newTempFile() 178 throws IOException 179 { 180 File f = File.createTempFile( "wagon-target.", ".file" ); 181 f.deleteOnExit(); 182 183 return f; 184 } 185 186 protected boolean isSsl() 187 { 188 return false; 189 } 190 191 protected ProxyInfo newProxyInfo() 192 { 193 ProxyInfo info = new ProxyInfo(); 194 info.setType( isSsl() ? "https" : "http" ); 195 info.setHost( ServerFixture.SERVER_HOST ); 196 info.setPort( getPort() ); 197 198 return info; 199 } 200 201 protected boolean isSupported() 202 { 203 StackTraceElement[] elements = new Throwable().getStackTrace(); 204 String testCaseId = null; 205 String lastMethodName = null; 206 for ( StackTraceElement e : elements ) 207 { 208 if ( !e.getClassName().startsWith( getClass().getPackage().getName() ) ) 209 { 210 testCaseId = lastMethodName; 211 break; 212 } 213 else 214 { 215 lastMethodName = e.getMethodName(); 216 } 217 } 218 219 if ( testCaseId == null || !configurator.isSupported( testCaseId ) ) 220 { 221 LOGGER.error( "Cannot run test: " + testCaseId 222 + ". Wagon under test does not support this test case." ); 223 return false; 224 } 225 226 return true; 227 } 228 229 protected boolean initTest( final AuthenticationInfo auth, final ProxyInfo proxy ) 230 throws ComponentConfigurationException, ConnectionException, AuthenticationException 231 { 232 return initTest( getBaseUrl(), auth, proxy ); 233 } 234 235 protected boolean initTest( final String baseUrl, final AuthenticationInfo auth, final ProxyInfo proxy ) 236 throws ComponentConfigurationException, ConnectionException, AuthenticationException 237 { 238 StackTraceElement[] elements = new Throwable().getStackTrace(); 239 String testCaseId = null; 240 String lastMethodName = null; 241 for ( StackTraceElement e : elements ) 242 { 243 if ( !e.getClassName().startsWith( getClass().getPackage().getName() ) ) 244 { 245 testCaseId = lastMethodName; 246 break; 247 } 248 else 249 { 250 lastMethodName = e.getMethodName(); 251 } 252 } 253 254 if ( testCaseId == null || !configurator.configureWagonForTest( wagon, testCaseId ) ) 255 { 256 LOGGER.error( "Cannot run test: " + testCaseId 257 + ". Wagon under test does not support this test case." ); 258 259 return false; 260 } 261 262 try 263 { 264 serverFixture.start(); 265 } 266 catch ( Exception e ) 267 { 268 throw new IllegalStateException( "Failed to start: " + e.getMessage(), e ); 269 } 270 271 repo = new Repository( "test", baseUrl ); 272 273 wagon.connect( repo, auth, proxy ); 274 275 return true; 276 } 277 278 protected int getPort() 279 { 280 return serverFixture.getHttpPort(); 281 } 282 283 protected int getPortPropertyValue() 284 { 285 return Integer.parseInt( System.getProperty( "test.port", "-1" ) ); 286 } 287 288 protected String getBaseUrl() 289 { 290 if ( baseUrl == null ) 291 { 292 StringBuilder sb = new StringBuilder(); 293 sb.append( isSsl() ? "https" : "http" ); 294 sb.append( "://" + ServerFixture.SERVER_HOST + ":" ); 295 sb.append( getPort() ); 296 297 baseUrl = sb.toString(); 298 } 299 300 return baseUrl; 301 } 302 303 protected ServerFixture getServerFixture() 304 { 305 return serverFixture; 306 } 307 308 protected static PlexusContainer getContainer() 309 { 310 return container; 311 } 312 313 protected Wagon getWagon() 314 { 315 return wagon; 316 } 317 318 protected static WagonTestCaseConfigurator getConfigurator() 319 { 320 return configurator; 321 } 322 323 protected static Set<File> getTmpfiles() 324 { 325 return TMP_FILES; 326 } 327 328 protected Repository getRepo() 329 { 330 return repo; 331 } 332 333}