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