View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.tck.command.diff;
20  
21  import java.io.File;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.TreeSet;
26  
27  import org.apache.maven.scm.ScmFile;
28  import org.apache.maven.scm.ScmFileSet;
29  import org.apache.maven.scm.ScmTckTestCase;
30  import org.apache.maven.scm.ScmTestCase;
31  import org.apache.maven.scm.ScmVersion;
32  import org.apache.maven.scm.command.diff.DiffScmResult;
33  import org.apache.maven.scm.provider.ScmProvider;
34  import org.apache.maven.scm.repository.ScmRepository;
35  import org.junit.Test;
36  
37  import static org.junit.Assert.assertEquals;
38  import static org.junit.Assert.assertNotNull;
39  import static org.junit.Assert.assertTrue;
40  
41  /**
42   * This test tests the diff command.
43   *
44   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
45   *
46   */
47  public abstract class DiffCommandTckTest extends ScmTckTestCase {
48  
49      @Test
50      public void testDiffCommand() throws Exception {
51          ScmRepository repository = getScmRepository();
52  
53          // ----------------------------------------------------------------------
54          // Change the files
55          // ----------------------------------------------------------------------
56  
57          //
58          // readme.txt is changed (changed file in the root directory)
59          // project.xml is added (added file in the root directory)
60          // src/test/resources is untouched (a empty directory is left untouched)
61          // src/test/java is untouched (a non empty directory is left untouched)
62          // src/test/java/org (a empty directory is added)
63          // src/main/java/org/Foo.java (a non empty directory is added)
64          //
65  
66          // /readme.txt
67          ScmTestCase.makeFile(getWorkingCopy(), "/readme.txt", "changed readme.txt");
68  
69          // /project.xml
70          ScmTestCase.makeFile(getWorkingCopy(), "/project.xml", "changed project.xml");
71  
72          addToWorkingTree(getWorkingCopy(), new File("project.xml"), repository);
73  
74          // /src/test/java/org
75          ScmTestCase.makeDirectory(getWorkingCopy(), "/src/test/java/org");
76  
77          addToWorkingTree(getWorkingCopy(), new File("src/test/java/org"), repository);
78  
79          // /src/main/java/org/Foo.java
80          ScmTestCase.makeFile(getWorkingCopy(), "/src/main/java/org/Foo.java");
81  
82          addToWorkingTree(getWorkingCopy(), new File("src/main/java/org"), repository);
83  
84          // src/main/java/org/Foo.java
85          addToWorkingTree(getWorkingCopy(), new File("src/main/java/org/Foo.java"), repository);
86  
87          // ----------------------------------------------------------------------
88          // Diff the project
89          // ----------------------------------------------------------------------
90  
91          ScmProvider provider = getScmManager().getProviderByUrl(getScmUrl());
92          ScmFileSet fileSet = new ScmFileSet(getWorkingCopy());
93          DiffScmResult result = provider.diff(repository, fileSet, null, (ScmVersion) null);
94  
95          assertNotNull("The command returned a null result.", result);
96  
97          assertResultIsSuccess(result);
98  
99          List<ScmFile> changedFiles = result.getChangedFiles();
100 
101         Map<String, CharSequence> differences = result.getDifferences();
102 
103         assertEquals("Expected 3 files in the changed files list " + changedFiles, 3, changedFiles.size());
104 
105         assertEquals("Expected 3 files in the differences list " + differences, 3, differences.size());
106 
107         // ----------------------------------------------------------------------
108         // Assert the files in the changed files list
109         // ----------------------------------------------------------------------
110 
111         Iterator<ScmFile> files = new TreeSet<ScmFile>(changedFiles).iterator();
112 
113         // Check Foo.java
114         ScmFile file = files.next();
115 
116         assertPath("src/main/java/org/Foo.java", file.getPath());
117 
118         assertTrue(file.getStatus().isDiff());
119 
120         String postRangeStr = "+/src/main/java/org/Foo.java\n\\ No newline at end of file\n";
121         String actualStr = differences.get(file.getPath()).toString();
122         assertTrue(actualStr.endsWith(postRangeStr));
123 
124         // Check readme.txt
125         file = files.next();
126 
127         assertPath("readme.txt", file.getPath());
128 
129         assertTrue(file.getStatus().isDiff());
130 
131         postRangeStr =
132                 "-/readme.txt\n\\ No newline at end of file\n+changed readme.txt\n\\ No newline at end of file\n";
133         actualStr = differences.get(file.getPath()).toString();
134         assertTrue(actualStr.endsWith(postRangeStr));
135 
136         // Check project.xml
137         file = files.next();
138 
139         assertPath("project.xml", file.getPath());
140 
141         postRangeStr = "+changed project.xml\n\\ No newline at end of file\n";
142         actualStr = differences.get(file.getPath()).toString();
143         assertTrue(actualStr.endsWith(postRangeStr));
144 
145         assertTrue(file.getStatus().isDiff());
146     }
147 }