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   */
63  public abstract class UpdateCommandTckTest extends ScmTckTestCase {
64  
65      private void commit(File workingDirectory, ScmRepository repository) throws Exception {
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<ScmFile> committedFiles = result.getCheckedInFiles();
71  
72          assertEquals(
73                  "Expected 3 files in the committed files list:\n  "
74                          + StringUtils.join(committedFiles.iterator(), "\n  "),
75                  3,
76                  committedFiles.size());
77      }
78  
79      @Test
80      public void testUpdateCommand() throws Exception {
81  
82          deleteDirectory(getUpdatingCopy());
83  
84          assertFalse(getUpdatingCopy().exists());
85  
86          // deleteDirectory( getWorkingCopy() );
87  
88          // assertFalse( getUpdatingCopy().exists() );
89  
90          ScmRepository repository = makeScmRepository(getScmUrl());
91  
92          checkOut(getUpdatingCopy(), repository);
93  
94          // ----------------------------------------------------------------------
95          // Change the files
96          // ----------------------------------------------------------------------
97  
98          /*
99           * readme.txt is changed (changed file in the root directory)
100          * project.xml is added (added file in the root directory)
101          * src/test/resources is untouched (a empty directory is left untouched)
102          * src/test/java is untouched (a non empty directory is left untouched)
103          * src/test/java/org (a empty directory is added)
104          * src/main/java/org/Foo.java (a non empty directory is added)
105          */
106 
107         // /readme.txt
108         this.edit(getWorkingCopy(), "readme.txt", null, getScmRepository());
109         ScmTestCase.makeFile(getWorkingCopy(), "/readme.txt", "changed readme.txt");
110 
111         // /project.xml
112         ScmTestCase.makeFile(getWorkingCopy(), "/project.xml", "changed project.xml");
113 
114         addToWorkingTree(getWorkingCopy(), new File("project.xml"), getScmRepository());
115 
116         // /src/test/java/org
117         ScmTestCase.makeDirectory(getWorkingCopy(), "/src/test/java/org");
118 
119         addToWorkingTree(getWorkingCopy(), new File("src/test/java/org"), getScmRepository());
120 
121         // /src/main/java/org/Foo.java
122         ScmTestCase.makeFile(getWorkingCopy(), "/src/main/java/org/Foo.java");
123 
124         addToWorkingTree(getWorkingCopy(), new File("src/main/java/org"), getScmRepository());
125 
126         // src/main/java/org/Foo.java
127         addToWorkingTree(getWorkingCopy(), new File("src/main/java/org/Foo.java"), getScmRepository());
128 
129         ScmManager scmManager = getScmManager();
130 
131         Date lastUpdate = new Date(System.currentTimeMillis() - 1000000);
132 
133         commit(getWorkingCopy(), getScmRepository());
134 
135         Thread.sleep(5000);
136 
137         // ----------------------------------------------------------------------
138         // Update the project
139         // ----------------------------------------------------------------------
140 
141         UpdateScmResult result = scmManager.update(repository, new ScmFileSet(getUpdatingCopy()), lastUpdate);
142 
143         assertNotNull("The command returned a null result.", result);
144 
145         assertResultIsSuccess(result);
146 
147         List<ScmFile> updatedFiles = result.getUpdatedFiles();
148 
149         List<ChangeSet> changedSets = result.getChanges();
150 
151         assertEquals("Expected 3 files in the updated files list " + updatedFiles, 3, updatedFiles.size());
152 
153         assertNotNull("The changed files list is null", changedSets);
154 
155         assertFalse("The changed files list is empty ", changedSets.isEmpty());
156 
157         for (ChangeSet changeSet : changedSets) {
158             System.out.println(changeSet.toXML());
159         }
160 
161         // ----------------------------------------------------------------------
162         // Assert the files in the updated files list
163         // ----------------------------------------------------------------------
164 
165         Iterator<ScmFile> files = new TreeSet<ScmFile>(updatedFiles).iterator();
166 
167         // Foo.java
168         ScmFile file = files.next();
169         assertPath("src/main/java/org/Foo.java", file.getPath());
170         // TODO : Consolidate file status so that we can remove "|| ADDED" term
171         assertTrue(file.getStatus().isUpdate() || file.getStatus() == ScmFileStatus.ADDED);
172 
173         // readme.txt
174         file = files.next();
175         assertPath("readme.txt", file.getPath());
176         assertTrue(file.getStatus().isUpdate());
177 
178         // project.xml
179         file = files.next();
180         assertPath("project.xml", file.getPath());
181         // TODO : Consolidate file status so that we can remove "|| ADDED" term
182         assertTrue(file.getStatus().isUpdate() || file.getStatus() == ScmFileStatus.ADDED);
183     }
184 }