001package org.apache.maven.wagon.providers.scm;
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 org.apache.maven.scm.manager.plexus.DefaultScmManager;
023import org.apache.maven.scm.provider.ScmProvider;
024import org.apache.maven.wagon.FileTestUtils;
025import org.apache.maven.wagon.ResourceDoesNotExistException;
026import org.apache.maven.wagon.TransferFailedException;
027import org.apache.maven.wagon.Wagon;
028import org.apache.maven.wagon.WagonConstants;
029import org.apache.maven.wagon.WagonTestCase;
030import org.apache.maven.wagon.authorization.AuthorizationException;
031import org.apache.maven.wagon.repository.Repository;
032import org.apache.maven.wagon.resource.Resource;
033import org.codehaus.plexus.util.FileUtils;
034
035import java.io.File;
036import java.io.IOException;
037import java.util.List;
038
039/**
040 * Test for {@link ScmWagon}. You need a subclass for each SCM provider you want to test.
041 *
042 * @author <a href="carlos@apache.org">Carlos Sanchez</a>
043 *
044 */
045public abstract class AbstractScmWagonTest
046    extends WagonTestCase
047{
048
049    @Override
050    public void testWagonPutDirectory() throws Exception
051    {
052        super.testWagonPutDirectory();
053        // repeat the test on a non-empty repo
054        // ScmWagon should checkout all involved subdirs before calling
055        // FileUtils.copyDirectoryStructure()
056        super.testWagonPutDirectory();
057    }
058
059    private ScmWagon wagon;
060
061    private String providerClassName;
062
063    protected void setUp()
064        throws Exception
065    {
066        super.setUp();
067
068        FileUtils.deleteDirectory( getCheckoutDirectory() );
069
070        if ( wagon == null )
071        {
072            wagon = (ScmWagon) super.getWagon();
073
074            DefaultScmManager scmManager = (DefaultScmManager) wagon.getScmManager();
075
076            if ( getScmProvider() != null )
077            {
078                scmManager.setScmProvider( getScmId(), getScmProvider() );
079
080                providerClassName = getScmProvider().getClass().getName();
081            }
082            else
083            {
084                providerClassName = scmManager.getProviderByType( getScmId() ).getClass().getName();
085            }
086
087            wagon.setCheckoutDirectory( getCheckoutDirectory() );
088        }
089    }
090
091    /**
092     * Allows overriding the {@link ScmProvider} injected by default in the {@link ScmWagon}.
093     * Useful to force the implementation to use for a particular SCM type.
094     * If this method returns <code>null</code> {@link ScmWagon} will use the default {@link ScmProvider}.
095     *
096     * @return the {@link ScmProvider} to use in the {@link ScmWagon}
097     */
098    protected ScmProvider getScmProvider()
099    {
100        return null;
101    }
102
103    @Override
104    protected int getTestRepositoryPort()
105    {
106        return 0; // not used
107    }
108
109    protected Wagon getWagon()
110        throws Exception
111    {
112        return wagon;
113    }
114
115    private File getCheckoutDirectory()
116    {
117        return new File( FileTestUtils.getTestOutputDir(), "/checkout-" + providerClassName );
118    }
119
120    protected int getExpectedContentLengthOnGet( int expectedSize )
121    {
122        return WagonConstants.UNKNOWN_LENGTH;
123    }
124    
125    protected long getExpectedLastModifiedOnGet( Repository repository, Resource resource )
126    {
127        return 0;
128    }
129
130    /**
131     * The SCM id, eg. <code>svn</code>, <code>cvs</code>
132     *
133     * @return the SCM id
134     */
135    protected abstract String getScmId();
136
137    protected String getProtocol()
138    {
139        return "scm";
140    }
141
142    protected void createDirectory( Wagon wagon, String resourceToCreate, String dirName )
143        throws Exception
144    {
145        super.createDirectory( wagon, resourceToCreate, dirName );
146        FileUtils.deleteDirectory( getCheckoutDirectory() );
147    }
148
149    protected void assertResourcesAreInRemoteSide( Wagon wagon, List<String> resourceNames )
150        throws IOException, TransferFailedException, ResourceDoesNotExistException, AuthorizationException
151    {
152        FileUtils.deleteDirectory( getCheckoutDirectory() );
153        super.assertResourcesAreInRemoteSide( wagon, resourceNames );
154    }
155}