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