View Javadoc
1   package org.apache.maven.scm.provider.git.command.diff;
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 org.apache.maven.scm.ScmFile;
23  import org.apache.maven.scm.ScmFileStatus;
24  import org.apache.maven.scm.log.DefaultLog;
25  import org.apache.maven.scm.util.ConsumerUtils;
26  import org.codehaus.plexus.PlexusTestCase;
27  
28  import java.io.File;
29  import java.util.List;
30  import java.util.Map;
31  
32  /**
33   * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
34   */
35  public class GitDiffConsumerTest
36      extends PlexusTestCase
37  {
38  
39      public void testEmptyLogConsumer()
40          throws Exception
41      {
42          GitDiffConsumer consumer = new GitDiffConsumer( new DefaultLog(), null );
43  
44          File f = getTestFile( "/src/test/resources/git/diff/git-diff-empty.log" );
45  
46          ConsumerUtils.consumeFile( f, consumer );
47  
48          List<ScmFile> changedFiles = consumer.getChangedFiles();
49  
50          assertEquals( 0, changedFiles.size() );
51      }
52  
53      public void testLog1Consumer()
54          throws Exception
55      {
56          GitDiffConsumer consumer = new GitDiffConsumer( new DefaultLog(), null );
57  
58          File f = getTestFile( "src/test/resources/git/diff/git-diff1.log" );
59  
60          ConsumerUtils.consumeFile( f, consumer );
61  
62          List<ScmFile> changedFiles = consumer.getChangedFiles();
63  
64          assertEquals( 1, changedFiles.size() );
65  
66          testScmFile( (ScmFile) changedFiles.get( 0 ), "olamy.test", ScmFileStatus.MODIFIED );
67  
68          Map<String,CharSequence> differences = consumer.getDifferences();
69          assertNotNull( differences );
70  
71          StringBuilder readmeDiffs = new StringBuilder( differences.get( "olamy.test" ) );
72          assertNotNull( readmeDiffs );
73          assertTrue( readmeDiffs.indexOf( "+new line" ) >= 0 );
74      }
75  
76      public void testLog2Consumer()
77          throws Exception
78      {
79          GitDiffConsumer consumer = new GitDiffConsumer( new DefaultLog(), null );
80  
81          File f = getTestFile( "src/test/resources/git/diff/git-diff2.log" );
82  
83          ConsumerUtils.consumeFile( f, consumer );
84  
85          List<ScmFile> changedFiles = consumer.getChangedFiles();
86  
87          assertEquals( 2, changedFiles.size() );
88  
89          testScmFile( changedFiles.get( 0 ), "pom.xml", ScmFileStatus.MODIFIED );
90  
91          testScmFile( changedFiles.get( 1 ), "test.txt", ScmFileStatus.MODIFIED );
92  
93          Map<String,CharSequence> differences = consumer.getDifferences();
94          assertNotNull( differences );
95  
96          StringBuilder addDiffs = new StringBuilder( differences.get( "pom.xml" ) );
97          assertNotNull( addDiffs );
98          assertTrue( addDiffs.indexOf( "+  <!-- test -->" ) >= 0 );
99  
100         addDiffs = new StringBuilder( differences.get( "test.txt" ) );
101         assertNotNull( addDiffs );
102         assertTrue( addDiffs.indexOf( "+maven-scm git provider works fine :-)" ) >= 0 );
103     }
104 
105     private void testScmFile( ScmFile fileToTest, String expectedFilePath, ScmFileStatus expectedStatus )
106     {
107         assertEquals( expectedFilePath, fileToTest.getPath() );
108         assertEquals( expectedStatus, fileToTest.getStatus() );
109     }
110  
111 }