View Javadoc
1   package org.apache.maven.wagon.providers.scm;
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 org.apache.maven.scm.manager.plexus.DefaultScmManager;
23  import org.apache.maven.scm.provider.ScmProvider;
24  import org.apache.maven.wagon.FileTestUtils;
25  import org.apache.maven.wagon.ResourceDoesNotExistException;
26  import org.apache.maven.wagon.TransferFailedException;
27  import org.apache.maven.wagon.Wagon;
28  import org.apache.maven.wagon.WagonConstants;
29  import org.apache.maven.wagon.WagonTestCase;
30  import org.apache.maven.wagon.authorization.AuthorizationException;
31  import org.apache.maven.wagon.repository.Repository;
32  import org.apache.maven.wagon.resource.Resource;
33  import org.codehaus.plexus.util.FileUtils;
34  
35  import java.io.File;
36  import java.io.IOException;
37  import java.util.List;
38  
39  /**
40   * Test for {@link ScmWagon}. You need a subclass for each SCM provider you want to test.
41   *
42   * @author <a href="carlos@apache.org">Carlos Sanchez</a>
43   *
44   */
45  public abstract class AbstractScmWagonTest
46      extends WagonTestCase
47  {
48  
49      @Override
50      public void testWagonPutDirectory() throws Exception
51      {
52          super.testWagonPutDirectory();
53          // repeat the test on a non-empty repo
54          // ScmWagon should checkout all involved subdirs before calling
55          // FileUtils.copyDirectoryStructure()
56          super.testWagonPutDirectory();
57      }
58  
59      private ScmWagon wagon;
60  
61      private String providerClassName;
62  
63      protected void setUp()
64          throws Exception
65      {
66          super.setUp();
67  
68          FileUtils.deleteDirectory( getCheckoutDirectory() );
69  
70          if ( wagon == null )
71          {
72              wagon = (ScmWagon) super.getWagon();
73  
74              DefaultScmManager scmManager = (DefaultScmManager) wagon.getScmManager();
75  
76              if ( getScmProvider() != null )
77              {
78                  scmManager.setScmProvider( getScmId(), getScmProvider() );
79  
80                  providerClassName = getScmProvider().getClass().getName();
81              }
82              else
83              {
84                  providerClassName = scmManager.getProviderByType( getScmId() ).getClass().getName();
85              }
86  
87              wagon.setCheckoutDirectory( getCheckoutDirectory() );
88          }
89      }
90  
91      /**
92       * Allows overriding the {@link ScmProvider} injected by default in the {@link ScmWagon}.
93       * Useful to force the implementation to use for a particular SCM type.
94       * If this method returns <code>null</code> {@link ScmWagon} will use the default {@link ScmProvider}.
95       *
96       * @return the {@link ScmProvider} to use in the {@link ScmWagon}
97       */
98      protected ScmProvider getScmProvider()
99      {
100         return null;
101     }
102 
103     protected Wagon getWagon()
104         throws Exception
105     {
106         return wagon;
107     }
108 
109     private File getCheckoutDirectory()
110     {
111         return new File( FileTestUtils.getTestOutputDir(), "/checkout-" + providerClassName );
112     }
113 
114     protected int getExpectedContentLengthOnGet( int expectedSize )
115     {
116         return WagonConstants.UNKNOWN_LENGTH;
117     }
118     
119     protected long getExpectedLastModifiedOnGet( Repository repository, Resource resource )
120     {
121         return 0;
122     }
123 
124     /**
125      * The SCM id, eg. <code>svn</code>, <code>cvs</code>
126      *
127      * @return the SCM id
128      */
129     protected abstract String getScmId();
130 
131     protected String getProtocol()
132     {
133         return "scm";
134     }
135 
136     protected void createDirectory( Wagon wagon, String resourceToCreate, String dirName )
137         throws Exception
138     {
139         super.createDirectory( wagon, resourceToCreate, dirName );
140         FileUtils.deleteDirectory( getCheckoutDirectory() );
141     }
142 
143     protected void assertResourcesAreInRemoteSide( Wagon wagon, List<String> resourceNames )
144         throws IOException, TransferFailedException, ResourceDoesNotExistException, AuthorizationException
145     {
146         FileUtils.deleteDirectory( getCheckoutDirectory() );
147         super.assertResourcesAreInRemoteSide( wagon, resourceNames );
148     }
149 }