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