1 package org.apache.maven.scm.provider.git.gitexe.command.status;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.BufferedReader;
23 import java.io.File;
24 import java.io.FileReader;
25 import java.io.IOException;
26 import java.net.URI;
27 import java.util.List;
28
29 import org.apache.commons.io.FileUtils;
30 import org.apache.commons.io.IOUtils;
31 import org.apache.maven.scm.ScmFile;
32 import org.apache.maven.scm.ScmFileStatus;
33 import org.apache.maven.scm.log.DefaultLog;
34 import org.codehaus.plexus.PlexusTestCase;
35
36
37
38
39 public class GitStatusConsumerTest
40 extends PlexusTestCase
41 {
42
43 private List<ScmFile> getChangedFiles( File gitlog )
44 throws IOException
45 {
46 return getChangedFiles( gitlog, null );
47 }
48
49 private List<ScmFile> getChangedFiles( File gitlog, URI relativeRepoPath )
50 throws IOException
51 {
52 GitStatusConsumer consumer = new GitStatusConsumer( new DefaultLog(), null, relativeRepoPath );
53
54 BufferedReader r = new BufferedReader( new FileReader( gitlog ) );
55
56 try
57 {
58 String line;
59
60 while ( ( line = r.readLine() ) != null )
61 {
62 consumer.consumeLine( line );
63 }
64 }
65 finally
66 {
67 IOUtils.closeQuietly( r );
68 }
69
70 return consumer.getChangedFiles();
71 }
72
73 private List<ScmFile> getChangedFiles( String line, File workingDirectory )
74 {
75 GitStatusConsumer consumer = new GitStatusConsumer( new DefaultLog(), workingDirectory );
76
77 consumer.consumeLine( line );
78
79 return consumer.getChangedFiles();
80 }
81
82 private List<ScmFile> getChangedFiles( String line, File workingDirectory, URI relativeRepoPath )
83 {
84 GitStatusConsumer consumer = new GitStatusConsumer( new DefaultLog(), workingDirectory, relativeRepoPath );
85
86 consumer.consumeLine( line );
87
88 return consumer.getChangedFiles();
89 }
90
91 public void testConsumerUntrackedFile()
92 {
93 List<ScmFile> changedFiles = getChangedFiles( "?? project.xml", null );
94
95 assertNotNull( changedFiles );
96 assertEquals( 0, changedFiles.size() );
97 }
98
99 public void testConsumerAddedFile()
100 {
101 List<ScmFile> changedFiles = getChangedFiles( "A project.xml", null );
102
103 assertNotNull( changedFiles );
104 assertEquals( 1, changedFiles.size() );
105 }
106
107 public void testConsumerAddedAndModifiedFile()
108 {
109 List<ScmFile> changedFiles = getChangedFiles( "AM project.xml", null );
110
111 assertNotNull( changedFiles );
112 assertEquals( 1, changedFiles.size() );
113 assertEquals( ScmFileStatus.ADDED, changedFiles.get( 0 ).getStatus() );
114 }
115
116 public void testConsumerAddedFileWithDirectoryAndNoFile()
117 throws IOException
118 {
119 File dir = createTempDirectory();
120
121 List<ScmFile> changedFiles = getChangedFiles( "A project.xml", dir );
122
123 assertNotNull( changedFiles );
124 assertEquals( 0, changedFiles.size() );
125
126 FileUtils.deleteDirectory( dir );
127 }
128
129 public void testConsumerAddedFileWithDirectoryAndFile()
130 throws IOException
131 {
132 File dir = createTempDirectory();
133 FileUtils.write( new File( dir, "project.xml" ), "data" );
134
135 List<ScmFile> changedFiles = getChangedFiles( "A project.xml", dir );
136
137 assertNotNull( changedFiles );
138 assertEquals( 1, changedFiles.size() );
139
140 FileUtils.deleteDirectory( dir );
141 }
142
143 public void testConsumerModifiedFile()
144 {
145 List<ScmFile> changedFiles = getChangedFiles( "M project.xml", null );
146
147 assertNotNull( changedFiles );
148 assertEquals( 1, changedFiles.size() );
149 }
150
151
152 public void testConsumerModifiedFileInComplexDirectorySetup() throws IOException {
153
154 File dir = createTempDirectory();
155 File subdir = new File( dir.getAbsolutePath() + "/subDirectory/" );
156 subdir.mkdir();
157 FileUtils.write( new File( subdir, "project.xml" ), "data" );
158
159 List<ScmFile> changedFiles = getChangedFiles( "M subDirectory/project.xml", subdir, dir.toURI() );
160
161 assertNotNull( changedFiles );
162 assertEquals( 1, changedFiles.size() );
163 }
164
165 public void testConsumerModifiedFileUnstaged()
166 {
167 List<ScmFile> changedFiles = getChangedFiles( "M project.xml", null );
168
169 assertNotNull( changedFiles );
170 assertEquals( 1, changedFiles.size() );
171 assertEquals( ScmFileStatus.MODIFIED, changedFiles.get( 0 ).getStatus() );
172 }
173
174 public void testConsumerModifiedFileBothStagedAndUnstaged()
175 {
176 List<ScmFile> changedFiles = getChangedFiles( "MM project.xml", null );
177
178 assertNotNull( changedFiles );
179 assertEquals( 1, changedFiles.size() );
180 assertEquals( ScmFileStatus.MODIFIED, changedFiles.get( 0 ).getStatus() );
181 }
182
183 public void testConsumerModifiedFileWithDirectoryAndNoFile()
184 throws IOException
185 {
186 File dir = createTempDirectory();
187
188 List<ScmFile> changedFiles = getChangedFiles( "M project.xml", dir );
189
190 assertNotNull( changedFiles );
191 assertEquals( 0, changedFiles.size() );
192
193 FileUtils.deleteDirectory( dir );
194 }
195
196 public void testConsumerModifiedFileWithDirectoryAndFile()
197 throws IOException
198 {
199 File dir = createTempDirectory();
200 FileUtils.write( new File( dir, "project.xml" ), "data" );
201
202 List<ScmFile> changedFiles = getChangedFiles( "M project.xml", dir );
203
204 assertNotNull( changedFiles );
205 assertEquals( 1, changedFiles.size() );
206
207 FileUtils.deleteDirectory( dir );
208 }
209
210 public void testConsumerRemovedFile()
211 {
212 List<ScmFile> changedFiles = getChangedFiles( "D Capfile", null );
213
214 assertNotNull( changedFiles );
215 assertEquals( 1, changedFiles.size() );
216 }
217
218 public void testConsumerRemovedFileUnstaged()
219 {
220 List<ScmFile> changedFiles = getChangedFiles( "D Capfile", null );
221
222 assertNotNull( changedFiles );
223 assertEquals( 1, changedFiles.size() );
224 assertEquals( ScmFileStatus.DELETED, changedFiles.get( 0 ).getStatus() );
225 }
226
227 public void testConsumerRemovedFileWithDirectoryAndNoFile()
228 throws IOException
229 {
230 File dir = createTempDirectory();
231
232 List<ScmFile> changedFiles = getChangedFiles( "D Capfile", dir );
233
234 assertNotNull( changedFiles );
235 assertEquals( 1, changedFiles.size() );
236 FileUtils.deleteDirectory( dir );
237 }
238
239 public void testConsumerRemovedFileWithDirectoryAndFile()
240 throws IOException
241 {
242 File dir = createTempDirectory();
243 FileUtils.write( new File( dir, "Capfile" ), "data" );
244
245 List<ScmFile> changedFiles = getChangedFiles( "D Capfile", dir );
246
247 assertNotNull( changedFiles );
248 assertEquals( 0, changedFiles.size() );
249 FileUtils.deleteDirectory( dir );
250 }
251
252
253 public void testConsumeRenamedFile()
254 throws Exception
255 {
256 File dir = createTempDirectory();
257
258 File tmpFile = new File( dir, "NewCapFile" );
259
260 FileUtils.write( tmpFile, "data" );
261
262 List<ScmFile> changedFiles = getChangedFiles( "R OldCapfile -> NewCapFile", dir );
263
264 assertNotNull( changedFiles );
265 assertEquals( 2, changedFiles.size() );
266 assertEquals( "OldCapfile", changedFiles.get(0).getPath() );
267 assertEquals( "NewCapFile", changedFiles.get(1).getPath() );
268 FileUtils.deleteDirectory( dir );
269 }
270
271 public void testLog1Consumer()
272 throws Exception
273 {
274 List<ScmFile> changedFiles = getChangedFiles( getTestFile( "/src/test/resources/git/status/gitstatus1.gitlog" ) );
275
276 assertEquals( 2, changedFiles.size() );
277
278 testScmFile( changedFiles.get( 0 ), "project.xml", ScmFileStatus.ADDED );
279 testScmFile( changedFiles.get( 1 ), "readme.txt", ScmFileStatus.MODIFIED );
280 }
281
282 public void testEmptyLogConsumer()
283 throws Exception
284 {
285 List<ScmFile> changedFiles = getChangedFiles( getTestFile( "/src/test/resources/git/status/gitstatus-empty.gitlog" ) );
286
287 assertEquals( 0, changedFiles.size() );
288 }
289
290
291 public void testLog2Consumer()
292 throws Exception
293 {
294 List<ScmFile> changedFiles = getChangedFiles( getTestFile( "/src/test/resources/git/status/gitstatus2.gitlog" ) );
295
296 assertEquals( 4, changedFiles.size() );
297
298 testScmFile( changedFiles.get( 0 ),
299 "maven-scm-provider-gitexe/src/main/java/org/apache/maven/scm/provider/git/gitexe/command/add/GitAddCommand.java",
300 ScmFileStatus.MODIFIED );
301 testScmFile( changedFiles.get( 1 ),
302 "maven-scm-provider-gitexe/src/main/java/org/apache/maven/scm/provider/git/gitexe/command/checkin/GitCheckInCommand.java",
303 ScmFileStatus.MODIFIED );
304 testScmFile( changedFiles.get( 2 ),
305 "maven-scm-provider-gitexe/src/main/java/org/apache/maven/scm/provider/git/gitexe/command/checkin/GitCheckInConsumer.java",
306 ScmFileStatus.DELETED );
307 testScmFile( changedFiles.get( 3 ),
308 "maven-scm-provider-gitexe/src/main/java/org/apache/maven/scm/provider/git/gitexe/command/status/GitStatusConsumer.java",
309 ScmFileStatus.MODIFIED );
310 }
311
312 public void testLog3Consumer()
313 throws Exception
314 {
315 List<ScmFile> changedFiles = getChangedFiles( getTestFile( "/src/test/resources/git/status/gitstatus2.gitlog" ), URI.create( "maven-scm-provider-gitexe" ) );
316
317 assertEquals( 4, changedFiles.size() );
318
319 testScmFile( changedFiles.get( 0 ),
320 "src/main/java/org/apache/maven/scm/provider/git/gitexe/command/add/GitAddCommand.java",
321 ScmFileStatus.MODIFIED );
322 testScmFile( changedFiles.get( 1 ),
323 "src/main/java/org/apache/maven/scm/provider/git/gitexe/command/checkin/GitCheckInCommand.java",
324 ScmFileStatus.MODIFIED );
325 testScmFile( changedFiles.get( 2 ),
326 "src/main/java/org/apache/maven/scm/provider/git/gitexe/command/checkin/GitCheckInConsumer.java",
327 ScmFileStatus.DELETED );
328 testScmFile( changedFiles.get( 3 ),
329 "src/main/java/org/apache/maven/scm/provider/git/gitexe/command/status/GitStatusConsumer.java",
330 ScmFileStatus.MODIFIED );
331 }
332
333
334 public void testResolvePath()
335 {
336 File repositoryRoot = getTestFile( "repo" );
337 File workingDirectory = getTestFile( "repo/work" );
338
339 URI path = repositoryRoot.toURI().relativize( workingDirectory.toURI() );
340
341 assertEquals( "work", path.getPath() );
342
343 assertEquals( "pom.xml", GitStatusConsumer.resolvePath( "work/pom.xml", path ) );
344 assertEquals( "work/pom.xml", GitStatusConsumer.resolvePath( "work/pom.xml", null ) );
345
346
347 repositoryRoot = getTestFile( "repo" );
348 workingDirectory = getTestFile( "repo/work with spaces" );
349
350 path = repositoryRoot.toURI().relativize( workingDirectory.toURI() );
351
352 assertEquals( "work with spaces", path.getPath() );
353
354 assertEquals( "pom.xml", GitStatusConsumer.resolvePath( "work with spaces/pom.xml", path ) );
355 assertEquals( "work with spaces/pom.xml", GitStatusConsumer.resolvePath( "work with spaces/pom.xml", null ) );
356 }
357
358 private void testScmFile( ScmFile fileToTest, String expectedFilePath, ScmFileStatus expectedStatus )
359 {
360 assertEquals( expectedFilePath, fileToTest.getPath() );
361 assertEquals( expectedStatus, fileToTest.getStatus() );
362 }
363
364 private File createTempDirectory()
365 throws IOException
366 {
367 File dir = File.createTempFile( "gitexe", "test" );
368 dir.delete();
369 dir.mkdir();
370 return dir;
371 }
372
373 }