View Javadoc
1   package org.apache.maven.wagon.tck.http;
2   
3   /*
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
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
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.maven.wagon.ConnectionException;
28  import org.apache.maven.wagon.Wagon;
29  import org.apache.maven.wagon.authentication.AuthenticationException;
30  import org.apache.maven.wagon.authentication.AuthenticationInfo;
31  import org.apache.maven.wagon.proxy.ProxyInfo;
32  import org.apache.maven.wagon.repository.Repository;
33  import org.apache.maven.wagon.tck.http.fixture.ServerFixture;
34  import org.codehaus.plexus.DefaultPlexusContainer;
35  import org.codehaus.plexus.PlexusContainer;
36  import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
37  import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException;
38  import org.codehaus.plexus.util.FileUtils;
39  import org.junit.After;
40  import org.junit.AfterClass;
41  import org.junit.Before;
42  import org.junit.BeforeClass;
43  import static org.apache.maven.wagon.tck.http.util.TestUtil.getResource;
44  
45  import org.slf4j.Logger;
46  import org.slf4j.LoggerFactory;
47  
48  /**
49   * 
50   */
51  public abstract class HttpWagonTests
52  {
53  
54      private ServerFixture serverFixture;
55  
56      private static PlexusContainer container;
57  
58      private Wagon wagon;
59  
60      private static WagonTestCaseConfigurator configurator;
61  
62      private String baseUrl;
63  
64      private static final Set<File> TMP_FILES = new HashSet<File>();
65  
66      private Repository repo;
67  
68      private final Set<Object> notificationTargets = new HashSet<Object>();
69  
70      // CHECKSTYLE_OFF: ConstantName
71      protected static final Logger logger = LoggerFactory.getLogger( HttpWagonTests.class );
72      // CHECKSTYLE_ON: ConstantName
73  
74      @Before
75      public void beforeEach()
76          throws Exception
77      {
78          serverFixture = new ServerFixture( isSsl() );
79          serverFixture.start();
80          wagon = (Wagon) container.lookup( Wagon.ROLE, configurator.getWagonHint() );
81      }
82  
83      @BeforeClass
84      public static void beforeAll()
85          throws Exception
86      {
87          File keystore = getResource( ServerFixture.SERVER_SSL_KEYSTORE_RESOURCE_PATH );
88  
89          System.setProperty( "javax.net.ssl.keyStore", keystore.getAbsolutePath() );
90          System.setProperty( "javax.net.ssl.keyStorePassword", ServerFixture.SERVER_SSL_KEYSTORE_PASSWORD );
91          System.setProperty( "javax.net.ssl.trustStore", keystore.getAbsolutePath() );
92          System.setProperty( "javax.net.ssl.trustStorePassword", ServerFixture.SERVER_SSL_KEYSTORE_PASSWORD );
93  
94          container = new DefaultPlexusContainer();
95          //container.initialize();
96          //container.start();
97  
98          configurator = (WagonTestCaseConfigurator) container.lookup( WagonTestCaseConfigurator.class.getName() );
99      }
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 }