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