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