View Javadoc
1   package org.apache.maven.scm.tck.command.update;
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 java.io.File;
23  import java.util.Date;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.TreeSet;
27  
28  import org.apache.maven.scm.ChangeSet;
29  import org.apache.maven.scm.ScmFile;
30  import org.apache.maven.scm.ScmFileSet;
31  import org.apache.maven.scm.ScmFileStatus;
32  import org.apache.maven.scm.ScmTckTestCase;
33  import org.apache.maven.scm.ScmTestCase;
34  import org.apache.maven.scm.command.checkin.CheckInScmResult;
35  import org.apache.maven.scm.command.update.UpdateScmResult;
36  import org.apache.maven.scm.manager.ScmManager;
37  import org.apache.maven.scm.repository.ScmRepository;
38  import org.codehaus.plexus.util.StringUtils;
39  import org.junit.Test;
40  
41  import static org.junit.Assert.assertEquals;
42  import static org.junit.Assert.assertFalse;
43  import static org.junit.Assert.assertNotNull;
44  import static org.junit.Assert.assertTrue;
45  
46  /**
47   * This test tests the update command.
48   * <p>
49   * It works like this:
50   * <p>
51   * <ol>
52   * <li>Check out the files to directory getWorkingCopy().
53   * <li>Check out the files to directory getUpdatingCopy().
54   * <li>Change the files in getWorkingCopy().
55   * <li>Commit the files in getWorkingCopy(). Note that the provider <b>must</b> not
56   * use the check in command as it can be guaranteed to work as it's not yet tested.
57   * <li>Use the update command in getUpdatingCopy() to assert that the files
58   * that was supposed to be updated actually was updated.
59   * </ol>
60   *
61   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
62   *
63   */
64  public abstract class UpdateCommandTckTest
65      extends ScmTckTestCase
66  {
67  
68      private void commit( File workingDirectory, ScmRepository repository )
69          throws Exception
70      {
71          CheckInScmResult result = getScmManager().checkIn( repository, new ScmFileSet( workingDirectory ), "No msg" );
72  
73          assertTrue( "Check result was successful, output: " + result.getCommandOutput(), result.isSuccess() );
74  
75          List<ScmFile> committedFiles = result.getCheckedInFiles();
76  
77          assertEquals(
78              "Expected 3 files in the committed files list:\n  " + StringUtils.join( committedFiles.iterator(), "\n  " ),
79              3, committedFiles.size() );
80      }
81  
82      @Test
83      public void testUpdateCommand()
84          throws Exception
85      {
86  
87          deleteDirectory( getUpdatingCopy() );
88  
89          assertFalse( getUpdatingCopy().exists() );
90  
91          //deleteDirectory( getWorkingCopy() );
92  
93          //assertFalse( getUpdatingCopy().exists() );
94  
95          ScmRepository repository = makeScmRepository( getScmUrl() );
96  
97          checkOut( getUpdatingCopy(), repository );
98  
99          // ----------------------------------------------------------------------
100         // Change the files
101         // ----------------------------------------------------------------------
102 
103         /*
104          * readme.txt is changed (changed file in the root directory)
105          * project.xml is added (added file in the root directory)
106          * src/test/resources is untouched (a empty directory is left untouched)
107          * src/test/java is untouched (a non empty directory is left untouched)
108          * src/test/java/org (a empty directory is added)
109          * src/main/java/org/Foo.java (a non empty directory is added)
110          */
111 
112         // /readme.txt
113         this.edit( getWorkingCopy(), "readme.txt", null, getScmRepository() );
114         ScmTestCase.makeFile( getWorkingCopy(), "/readme.txt", "changed readme.txt" );
115 
116         // /project.xml
117         ScmTestCase.makeFile( getWorkingCopy(), "/project.xml", "changed project.xml" );
118 
119         addToWorkingTree( getWorkingCopy(), new File( "project.xml" ), getScmRepository() );
120 
121         // /src/test/java/org
122         ScmTestCase.makeDirectory( getWorkingCopy(), "/src/test/java/org" );
123 
124         addToWorkingTree( getWorkingCopy(), new File( "src/test/java/org" ), getScmRepository() );
125 
126         // /src/main/java/org/Foo.java
127         ScmTestCase.makeFile( getWorkingCopy(), "/src/main/java/org/Foo.java" );
128 
129         addToWorkingTree( getWorkingCopy(), new File( "src/main/java/org" ), getScmRepository() );
130 
131         // src/main/java/org/Foo.java
132         addToWorkingTree( getWorkingCopy(), new File( "src/main/java/org/Foo.java" ), getScmRepository() );
133 
134         ScmManager scmManager = getScmManager();
135 
136         Date lastUpdate = new Date( System.currentTimeMillis() - 1000000 );
137 
138         commit( getWorkingCopy(), getScmRepository() );
139 
140         Thread.sleep( 5000 );
141 
142         // ----------------------------------------------------------------------
143         // Update the project
144         // ----------------------------------------------------------------------
145 
146         UpdateScmResult result = scmManager.update( repository, new ScmFileSet( getUpdatingCopy() ), lastUpdate );
147 
148         assertNotNull( "The command returned a null result.", result );
149 
150         assertResultIsSuccess( result );
151 
152         List<ScmFile> updatedFiles = result.getUpdatedFiles();
153 
154         List<ChangeSet> changedSets = result.getChanges();
155 
156         assertEquals( "Expected 3 files in the updated files list " + updatedFiles, 3, updatedFiles.size() );
157 
158         assertNotNull( "The changed files list is null", changedSets );
159 
160         assertFalse( "The changed files list is empty ", changedSets.isEmpty() );
161 
162         for ( ChangeSet changeSet : changedSets )
163         {
164             System.out.println( changeSet.toXML() );
165         }
166 
167         // ----------------------------------------------------------------------
168         // Assert the files in the updated files list
169         // ----------------------------------------------------------------------
170 
171         Iterator<ScmFile> files = new TreeSet<ScmFile>( updatedFiles ).iterator();
172 
173         //Foo.java
174         ScmFile file = files.next();
175         assertPath( "src/main/java/org/Foo.java", file.getPath() );
176         //TODO : Consolidate file status so that we can remove "|| ADDED" term
177         assertTrue( file.getStatus().isUpdate() || file.getStatus() == ScmFileStatus.ADDED );
178 
179         //readme.txt
180         file = files.next();
181         assertPath( "readme.txt", file.getPath() );
182         assertTrue( file.getStatus().isUpdate() );
183 
184         //project.xml
185         file = files.next();
186         assertPath( "project.xml", file.getPath() );
187         //TODO : Consolidate file status so that we can remove "|| ADDED" term
188         assertTrue( file.getStatus().isUpdate() || file.getStatus() == ScmFileStatus.ADDED );
189     }
190 
191 }