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