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