View Javadoc
1   package org.apache.maven.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.util.ArrayList;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.TreeMap;
28  
29  import org.apache.maven.scm.command.add.AddScmResult;
30  import org.apache.maven.scm.command.checkin.CheckInScmResult;
31  import org.apache.maven.scm.command.checkout.CheckOutScmResult;
32  import org.apache.maven.scm.command.edit.EditScmResult;
33  import org.apache.maven.scm.provider.ScmProvider;
34  import org.apache.maven.scm.repository.ScmRepository;
35  import org.codehaus.plexus.util.StringUtils;
36  
37  /**
38   * <p/>
39   * Base class for all TcK tests.
40   * </p>
41   * <p/>
42   * Basically all it does is to setup a default test enviroment
43   * common for all tck tests. The default setup includes:
44   * <ol>
45   * <li>Delete all default locations (working copy, updating copy etc)</li>
46   * <li>Initialize the repository</li>
47   * <li>Check out the repository to the working copy</li>
48   * </ol>
49   * </p>
50   *
51   * @author <a href="mailto:torbjorn@smorgrav.org">Torbj�rn Eikli Sm�rgrav</a>
52   *
53   */
54  public abstract class ScmTckTestCase
55      extends ScmTestCase
56  {
57      private ScmRepository scmRepository;
58  
59      private List<String> scmFileNames;
60  
61      /**
62       * @return A provider specific and valid url for the repository
63       * @throws Exception if any
64       */
65      public abstract String getScmUrl()
66          throws Exception;
67  
68      /**
69       * <p/>
70       * Get the list of file names that is supposed to be in the test repo.
71       * </p>
72       * <ul>
73       * <li>/pom.xml</li>
74       * <li>/readme.txt</li>
75       * <li>/src/main/java/Application.java</li>
76       * <li>/src/test/java/Test.java</li>
77       * </ul>
78       *
79       * @return {@link List} of {@link String} objects
80       */
81      protected List<String> getScmFileNames()
82      {
83          return scmFileNames;
84      }
85  
86      /**
87       * <p/>
88       * Initialize repository at the {@link #getScmUrl()} location with the files in {@link #getScmFileNames()}
89       * </p>
90       * <p/>
91       * The setup is also asserting on the existence of these files. <br>
92       * This should only be used by this class (thus do not call this method from derived classes)
93       * </p>
94       * <b>Note</b>: 'svnadmin' should be a system command.
95       *
96       * @throws Exception if any
97       */
98      public abstract void initRepo()
99          throws Exception;
100 
101     /**
102      * {@inheritDoc}
103      */
104     protected void setUp()
105         throws Exception
106     {
107         super.setUp();
108 
109         scmRepository = null;
110 
111         scmFileNames = new ArrayList<String>( 4 );
112         scmFileNames.add( "/pom.xml" );
113         scmFileNames.add( "/readme.txt" );
114         scmFileNames.add( "/src/main/java/Application.java" );
115         scmFileNames.add( "/src/test/java/Test.java" );
116 
117         initRepo();
118 
119         checkOut( getWorkingCopy(), getScmRepository() );
120 
121         Iterator<String> it = getScmFileNames().iterator();
122         while ( it.hasNext() )
123         {
124             assertFile( getWorkingCopy(), it.next() );
125         }
126     }
127 
128     /**
129      * This method is available to those SCM clients that need to perform
130      * a cleanup at the end of the tests. It is needed when server side
131      * operations are performed, or the check out dirs are outside
132      * of the normal target directory.
133      */
134     public void removeRepo()
135         throws Exception
136     {
137     }
138 
139     /**
140      * Provided to allow removeRepo() to be called. 
141      * @see junit.framework.TestCase#tearDown()
142      */
143     @Override
144     protected void tearDown()
145         throws Exception
146     {
147         super.tearDown();
148         removeRepo();
149     }
150 
151     /**
152      * Convenience method to get the ScmRepository for this provider
153      */
154     protected ScmRepository getScmRepository()
155         throws Exception
156     {
157         if ( scmRepository == null )
158         {
159             scmRepository = getScmManager().makeScmRepository( getScmUrl() );
160         }
161 
162         return scmRepository;
163     }
164 
165     /**
166      * Convenience method to check out files from the repository
167      */
168     protected CheckOutScmResult checkOut( File workingDirectory, ScmRepository repository )
169         throws Exception
170     {
171         CheckOutScmResult result =
172             getScmManager().getProviderByUrl( getScmUrl() ).checkOut( repository, new ScmFileSet( workingDirectory ),
173                                                                       (ScmVersion) null );
174 
175         assertTrue( "Check result was successful, output: " + result.getCommandOutput(), result.isSuccess() );
176 
177         return result;
178     }
179 
180     /**
181      * Convenience method to check in files to the repository
182      */
183     protected CheckInScmResult checkIn( File workingDirectory, ScmRepository repository )
184         throws Exception
185     {
186         CheckInScmResult result = getScmManager().getProviderByUrl( getScmUrl() )
187             .checkIn( repository, new ScmFileSet( workingDirectory ), (ScmVersion) null, "Initial Checkin" );
188 
189         assertTrue( "Check result was successful, output: " + result.getCommandOutput(), result.isSuccess() );
190 
191         return result;
192     }
193     
194     /**
195      * Convenience method to add a file to the working tree at the working directory
196      */
197     protected void addToWorkingTree( File workingDirectory, File file, ScmRepository repository )
198         throws Exception
199     {
200         ScmProvider provider = getScmManager().getProviderByUrl( getScmUrl() );
201 
202         CommandParameters commandParameters = new CommandParameters();
203         commandParameters.setString( CommandParameter.FORCE_ADD, Boolean.TRUE.toString() );
204 
205         AddScmResult result = provider.add( repository, new ScmFileSet( workingDirectory, file ), commandParameters );
206 
207         assertTrue( "Check result was successful, output: " + result.getCommandOutput(), result.isSuccess() );
208 
209         List<ScmFile> addedFiles = result.getAddedFiles();
210 
211         if ( new File( workingDirectory, file.getPath() ).isFile() )
212         {
213             // Don't check directory add because some SCM tools ignore it
214             assertEquals( "Expected 1 file in the added files list " + addedFiles, 1, addedFiles.size() );
215         }
216     }
217 
218     /**
219      * take the files of the given list, add them to a TreeMap and
220      * use the pathName String as key for the Map.
221      * This function is useful for every TCK which has to check for the
222      * existence of more than 1 file of the returned ScmResult, regardless
223      * of their order in the list.
224      * All backslashes in the path will be replaced by forward slashes
225      * for Windows compatibility.
226      *
227      * @param files List with {@code ScmFile}s
228      * @return Map key=pathName, value=ScmFile
229      */
230     protected Map<String, ScmFile> mapFilesByPath( List<ScmFile> files )
231     {
232         if ( files == null )
233         {
234             return null;
235         }
236 
237         Map<String, ScmFile> mappedFiles = new TreeMap<String, ScmFile>();
238         for ( ScmFile scmFile : files )
239         {
240             String path = StringUtils.replace( scmFile.getPath(), "\\", "/" );
241             mappedFiles.put( path, scmFile );
242         }
243 
244         return mappedFiles;
245     }
246 
247     protected EditScmResult edit( File basedir, String includes, String excludes, ScmRepository repository )
248         throws Exception
249     {
250         if ( this.getScmManager().getProviderByRepository( this.getScmRepository() ).requiresEditMode() )
251         {
252             ScmFileSet fileSet = new ScmFileSet( basedir, includes, excludes );
253             return getScmManager().edit( getScmRepository(), fileSet );
254         }
255         return new EditScmResult( "", "", "", true );
256     }
257 
258 }