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