1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  package org.apache.maven.scm;
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.TreeMap;
27  
28  import org.apache.commons.lang3.StringUtils;
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.command.remove.RemoveScmResult;
34  import org.apache.maven.scm.provider.ScmProvider;
35  import org.apache.maven.scm.repository.ScmRepository;
36  import org.junit.After;
37  import org.junit.Before;
38  
39  import static org.junit.Assert.assertEquals;
40  import static org.junit.Assert.assertTrue;
41  import static org.junit.Assume.assumeTrue;
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  public abstract class ScmTckTestCase extends ScmTestCase {
57      private ScmRepository scmRepository;
58  
59      private List<String> scmFileNames;
60  
61      
62  
63  
64  
65  
66  
67      public String getScmProviderCommand() {
68          return null;
69      }
70  
71      
72  
73  
74  
75      public abstract String getScmUrl() throws Exception;
76  
77      
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90      protected List<String> getScmFileNames() {
91          return scmFileNames;
92      }
93  
94      
95  
96  
97  
98  
99  
100 
101 
102 
103 
104 
105 
106     public abstract void initRepo() throws Exception;
107 
108     public void checkScmPresence() {
109         String scmProviderCommand = getScmProviderCommand();
110         if (scmProviderCommand != null) {
111             assumeTrue(
112                     "Skipping tests because the required command '" + scmProviderCommand + "' is not available.",
113                     ScmTestCase.isSystemCmd(scmProviderCommand));
114         }
115     }
116 
117     
118 
119 
120     @Before
121     @Override
122     public void setUp() throws Exception {
123         checkScmPresence();
124         super.setUp();
125 
126         scmRepository = null;
127 
128         scmFileNames = new ArrayList<>(4);
129         scmFileNames.add("/pom.xml");
130         scmFileNames.add("/readme.txt");
131         scmFileNames.add("/src/main/java/Application.java");
132         scmFileNames.add("/src/test/java/Test.java");
133 
134         initRepo();
135 
136         checkOut(getWorkingCopy(), getScmRepository());
137 
138         Iterator<String> it = getScmFileNames().iterator();
139         while (it.hasNext()) {
140             assertFile(getWorkingCopy(), it.next());
141         }
142     }
143 
144     
145 
146 
147 
148 
149 
150     public void removeRepo() throws Exception {}
151 
152     
153 
154 
155     @After
156     @Override
157     public void tearDown() throws Exception {
158         removeRepo();
159         super.tearDown();
160     }
161 
162     
163 
164 
165     protected ScmRepository getScmRepository() throws Exception {
166         if (scmRepository == null) {
167             scmRepository = getScmManager().makeScmRepository(getScmUrl());
168         }
169 
170         return scmRepository;
171     }
172 
173     
174 
175 
176     protected CheckOutScmResult checkOut(File workingDirectory, ScmRepository repository) throws Exception {
177         CheckOutScmResult result = getScmManager()
178                 .getProviderByUrl(getScmUrl())
179                 .checkOut(repository, new ScmFileSet(workingDirectory), (ScmVersion) null);
180 
181         assertTrue("Check result was successful, output: " + result.getCommandOutput(), result.isSuccess());
182 
183         return result;
184     }
185 
186     
187 
188 
189     protected CheckInScmResult checkIn(File workingDirectory, ScmRepository repository) throws Exception {
190         CheckInScmResult result = getScmManager()
191                 .getProviderByUrl(getScmUrl())
192                 .checkIn(repository, new ScmFileSet(workingDirectory), (ScmVersion) null, "Initial Checkin");
193 
194         assertTrue("Check result was successful, output: " + result.getCommandOutput(), result.isSuccess());
195 
196         return result;
197     }
198 
199     
200 
201 
202     protected RemoveScmResult remove(File workingDirectory, ScmRepository repository) throws Exception {
203         RemoveScmResult result = getScmManager()
204                 .getProviderByUrl(getScmUrl())
205                 .remove(repository, new ScmFileSet(workingDirectory), "Initial Checkin");
206 
207         assertTrue("Remove result was successful, output: " + result.getCommandOutput(), result.isSuccess());
208 
209         return result;
210     }
211 
212     
213 
214 
215     protected void addToWorkingTree(File workingDirectory, File file, ScmRepository repository) throws Exception {
216         ScmProvider provider = getScmManager().getProviderByUrl(getScmUrl());
217 
218         CommandParameters commandParameters = new CommandParameters();
219         commandParameters.setString(CommandParameter.FORCE_ADD, Boolean.TRUE.toString());
220 
221         AddScmResult result = provider.add(repository, new ScmFileSet(workingDirectory, file), commandParameters);
222 
223         assertTrue("Check result was successful, output: " + result.getCommandOutput(), result.isSuccess());
224 
225         List<ScmFile> addedFiles = result.getAddedFiles();
226 
227         if (new File(workingDirectory, file.getPath()).isFile()) {
228             
229             assertEquals("Expected 1 file in the added files list " + addedFiles, 1, addedFiles.size());
230         }
231     }
232 
233     
234 
235 
236 
237 
238 
239 
240 
241 
242 
243 
244 
245     protected Map<String, ScmFile> mapFilesByPath(List<ScmFile> files) {
246         if (files == null) {
247             return null;
248         }
249 
250         Map<String, ScmFile> mappedFiles = new TreeMap<>();
251         for (ScmFile scmFile : files) {
252             String path = StringUtils.replace(scmFile.getPath(), "\\", "/");
253             mappedFiles.put(path, scmFile);
254         }
255 
256         return mappedFiles;
257     }
258 
259     protected EditScmResult edit(File basedir, String includes, String excludes, ScmRepository repository)
260             throws Exception {
261         if (this.getScmManager()
262                 .getProviderByRepository(this.getScmRepository())
263                 .requiresEditMode()) {
264             ScmFileSet fileSet = new ScmFileSet(basedir, includes, excludes);
265             return getScmManager().edit(getScmRepository(), fileSet);
266         }
267         return new EditScmResult("", "", "", true);
268     }
269 }