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.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  /**
47   * 
48   */
49  public abstract class HttpWagonTests
50  {
51  
52      private ServerFixture serverFixture;
53  
54      private static PlexusContainer container;
55  
56      private Wagon wagon;
57  
58      private static WagonTestCaseConfigurator configurator;
59  
60      private String baseUrl;
61  
62      private static final Set<File> TMP_FILES = new HashSet<File>();
63  
64      private Repository repo;
65  
66      private final Set<Object> notificationTargets = new HashSet<Object>();
67  
68      protected static final Logger LOGGER = Logger.getLogger( HttpWagonTests.class );
69  
70      @Before
71      public void beforeEach()
72          throws Exception
73      {
74          serverFixture = new ServerFixture( isSsl() );
75          serverFixture.start();
76          wagon = (Wagon) container.lookup( Wagon.ROLE, configurator.getWagonHint() );
77      }
78  
79      @BeforeClass
80      public static void beforeAll()
81          throws Exception
82      {
83          File keystore = getResource( ServerFixture.SERVER_SSL_KEYSTORE_RESOURCE_PATH );
84  
85          System.setProperty( "javax.net.ssl.keyStore", keystore.getAbsolutePath() );
86          System.setProperty( "javax.net.ssl.keyStorePassword", ServerFixture.SERVER_SSL_KEYSTORE_PASSWORD );
87          System.setProperty( "javax.net.ssl.trustStore", keystore.getAbsolutePath() );
88          System.setProperty( "javax.net.ssl.trustStorePassword", ServerFixture.SERVER_SSL_KEYSTORE_PASSWORD );
89  
90          container = new DefaultPlexusContainer();
91          //container.initialize();
92          //container.start();
93  
94          configurator = (WagonTestCaseConfigurator) container.lookup( WagonTestCaseConfigurator.class.getName() );
95      }
96  
97      @After
98      public void afterEach()
99      {
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 }