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.status;
20  
21  import java.io.File;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.TreeSet;
25  
26  import org.apache.maven.scm.CommandParameter;
27  import org.apache.maven.scm.CommandParameters;
28  import org.apache.maven.scm.ScmFile;
29  import org.apache.maven.scm.ScmFileSet;
30  import org.apache.maven.scm.ScmFileStatus;
31  import org.apache.maven.scm.ScmTckTestCase;
32  import org.apache.maven.scm.ScmTestCase;
33  import org.apache.maven.scm.command.checkin.CheckInScmResult;
34  import org.apache.maven.scm.command.status.StatusScmResult;
35  import org.apache.maven.scm.manager.ScmManager;
36  import org.apache.maven.scm.repository.ScmRepository;
37  import org.junit.Test;
38  
39  import static org.junit.Assert.assertEquals;
40  import static org.junit.Assert.assertFalse;
41  import static org.junit.Assert.assertNotNull;
42  import static org.junit.Assert.assertTrue;
43  
44  /**
45   * This test tests the status command.
46   * <p>
47   * It works like this:
48   * <p>
49   * <ol>
50   * <li>Check out the files to directory getWorkingCopy().
51   * <li>Check out the files to directory getUpdatingCopy().
52   * <li>Change the files in getWorkingCopy().
53   * <li>Commit the files in getWorkingCopy(). Note that the provider <b>must</b> not
54   * use the check in command as it can be guaranteed to work as it's not yet tested.
55   * <li>Use the update command in getUpdatingCopy() to assert that the files
56   * that was supposed to be updated actually was updated.
57   * </ol>
58   *
59   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
60   *
61   */
62  public abstract class StatusCommandTckTest extends ScmTckTestCase {
63  
64      protected void commit(File workingDirectory, ScmRepository repository) throws Exception {
65          CommandParameters commandParameters = new CommandParameters();
66          commandParameters.setString(CommandParameter.MESSAGE, "No msg");
67          commandParameters.setString(CommandParameter.SCM_COMMIT_SIGN, "false");
68          CheckInScmResult result =
69                  getScmManager().checkIn(repository, new ScmFileSet(workingDirectory), commandParameters);
70  
71          assertTrue("Check result was successful, output: " + result.getCommandOutput(), result.isSuccess());
72  
73          List<ScmFile> committedFiles = result.getCheckedInFiles();
74  
75          assertEquals("Expected 2 files in the committed files list " + committedFiles, 2, committedFiles.size());
76      }
77  
78      protected boolean commitUpdateCopy() {
79          return false;
80      }
81  
82      @Test
83      public void testStatusCommand() throws Exception {
84          ScmRepository repository = makeScmRepository(getScmUrl());
85  
86          checkOut(getUpdatingCopy(), repository);
87  
88          // ----------------------------------------------------------------------
89          // Change the files
90          // ----------------------------------------------------------------------
91  
92          /*
93           * readme.txt is changed (changed file in the root directory)
94           * project.xml is added (added file in the root directory)
95           */
96  
97          // /readme.txt
98          this.edit(getWorkingCopy(), "readme.txt", null, getScmRepository());
99          ScmTestCase.makeFile(getWorkingCopy(), "/readme.txt", "changed readme.txt");
100 
101         // /project.xml
102         ScmTestCase.makeFile(getWorkingCopy(), "/project.xml", "changed project.xml");
103 
104         addToWorkingTree(getWorkingCopy(), new File("project.xml"), getScmRepository());
105 
106         commit(getWorkingCopy(), getScmRepository());
107 
108         // /pom.xml
109         this.edit(getUpdatingCopy(), "pom.xml", null, repository);
110         ScmTestCase.makeFile(getUpdatingCopy(), "/pom.xml", "changed pom.xml");
111 
112         // /src/test/java/org
113         ScmTestCase.makeDirectory(getUpdatingCopy(), "/src/test/java/org");
114 
115         addToWorkingTree(getUpdatingCopy(), new File("src/test/java/org"), repository);
116 
117         // /src/main/java/org/Foo.java
118         ScmTestCase.makeFile(getUpdatingCopy(), "/src/main/java/org/Foo.java");
119 
120         addToWorkingTree(getUpdatingCopy(), new File("src/main/java/org"), repository);
121 
122         // src/main/java/org/Foo.java
123         addToWorkingTree(getUpdatingCopy(), new File("src/main/java/org/Foo.java"), repository);
124 
125         ScmManager scmManager = getScmManager();
126 
127         // ----------------------------------------------------------------------
128         // Check status the project
129         // src/main/java/org/Foo.java is added
130         // /pom.xml is modified
131         // check that readme and project.xml are not updated/created
132         // ----------------------------------------------------------------------
133 
134         StatusScmResult result =
135                 scmManager.getProviderByUrl(getScmUrl()).status(repository, new ScmFileSet(getUpdatingCopy()));
136 
137         if (this.commitUpdateCopy()) {
138             // this is needed for perforce so that teardown can remove its client workspace, no harm for cvs/svn/git
139             commit(getUpdatingCopy(), repository);
140         }
141 
142         assertNotNull("The command returned a null result.", result);
143 
144         assertResultIsSuccess(result);
145 
146         List<ScmFile> changedFiles = result.getChangedFiles();
147 
148         assertEquals("Expected 2 files in the updated files list " + changedFiles, 2, changedFiles.size());
149 
150         // ----------------------------------------------------------------------
151         // Assert the files in the updated files list
152         // ----------------------------------------------------------------------
153 
154         Iterator<ScmFile> files = new TreeSet<>(changedFiles).iterator();
155 
156         ScmFile file = files.next();
157         assertPath("src/main/java/org/Foo.java", file.getPath());
158         assertEquals(ScmFileStatus.ADDED, file.getStatus());
159 
160         file = files.next();
161         assertPath("pom.xml", file.getPath());
162         assertEquals(ScmFileStatus.MODIFIED, file.getStatus());
163 
164         assertFile(getUpdatingCopy(), "/readme.txt");
165 
166         assertFalse("project.xml created incorrectly", new File(getUpdatingCopy(), "/project.xml").exists());
167     }
168 }