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