View Javadoc
1   package org.apache.maven.scm.provider.local.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.io.FileReader;
24  import java.io.Reader;
25  import java.util.Date;
26  import java.util.Iterator;
27  import java.util.List;
28  import java.util.TreeSet;
29  
30  import org.apache.maven.scm.ScmFile;
31  import org.apache.maven.scm.ScmFileSet;
32  import org.apache.maven.scm.ScmTestCase;
33  import org.apache.maven.scm.command.update.UpdateScmResult;
34  import org.apache.maven.scm.manager.ScmManager;
35  import org.apache.maven.scm.provider.local.metadata.LocalScmMetadata;
36  import org.apache.maven.scm.provider.local.metadata.io.xpp3.LocalScmMetadataXpp3Reader;
37  import org.apache.maven.scm.repository.ScmRepository;
38  import org.apache.maven.scm.tck.command.update.UpdateCommandTckTest;
39  import org.codehaus.plexus.util.FileUtils;
40  import org.codehaus.plexus.util.IOUtil;
41  
42  /**
43   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
44   *
45   */
46  public class LocalUpdateCommandTckTest
47      extends UpdateCommandTckTest
48  {
49      private static final String moduleName = "update-tck";
50  
51      public String getScmUrl()
52          throws Exception
53      {
54          return "scm:local|" + getRepositoryRoot() + "|" + moduleName;
55      }
56  
57      public void initRepo()
58          throws Exception
59      {
60          makeRepo( getRepositoryRoot() );
61      }
62  
63      /**
64       * Tests that a file that has been deleted from repository after checkout will be removed by scm-local. Local
65       * additions must not be deleted.
66       */
67      public void testDeletion()
68          throws Exception
69      {
70          FileUtils.deleteDirectory( getUpdatingCopy() );
71  
72          ScmRepository repository = makeScmRepository( getScmUrl() );
73  
74          checkOut( getUpdatingCopy(), repository );
75  
76          // Check preconditions
77          File readmeFileLocal = new File( getUpdatingCopy(), "readme.txt" );
78          assertTrue( readmeFileLocal.exists() );
79          File newFileLocal = new File( getUpdatingCopy(), "newfile.xml" );
80          assertTrue( !newFileLocal.exists() );
81  
82          // Delete readme.txt from repository
83          File readmeFileRepo = new File( getRepositoryRoot(), moduleName + "/readme.txt" );
84          assertTrue( readmeFileRepo.exists() );
85          assertTrue( "Could not delete", readmeFileRepo.delete() );
86          assertFalse( readmeFileRepo.exists() );
87  
88          // Make local addition to updating copy - this one must not be touched
89          ScmTestCase.makeFile( getUpdatingCopy(), "newfile.xml", "added newfile.xml locally" );
90          assertTrue( newFileLocal.exists() );
91  
92          // ----------------------------------------------------------------------
93          // Update the project
94          // ----------------------------------------------------------------------
95  
96          ScmManager scmManager = getScmManager();
97          Date lastUpdate = new Date( System.currentTimeMillis() );
98          Thread.sleep( 1000 );
99          UpdateScmResult result =
100             scmManager.update( repository, new ScmFileSet( getUpdatingCopy() ), lastUpdate );
101 
102         assertNotNull( "The command returned a null result.", result );
103 
104         assertResultIsSuccess( result );
105 
106         List<ScmFile> updatedFiles = result.getUpdatedFiles();
107 
108         assertEquals( "Expected 1 files in the updated files list " + updatedFiles, 1, updatedFiles.size() );
109 
110         // ----------------------------------------------------------------------
111         // Assert the files in the updated files list
112         // ----------------------------------------------------------------------
113 
114         Iterator<ScmFile> files = new TreeSet<ScmFile>( updatedFiles ).iterator();
115 
116         // readme.txt
117         ScmFile file = (ScmFile) files.next();
118         assertPath( "/readme.txt", file.getPath() );
119         assertTrue( file.getStatus().isUpdate() );
120 
121         // ----------------------------------------------------------------------
122         // Assert working directory contents
123         // ----------------------------------------------------------------------
124 
125         // readme.txt
126         assertTrue( "Expected local copy of readme.txt to be deleted", !readmeFileLocal.exists() );
127 
128         // newfile.xml
129         assertTrue( "Expected local copy of newfile.xml NOT to be deleted", newFileLocal.exists() );
130 
131         // ----------------------------------------------------------------------
132         // Assert metadata file
133         // ----------------------------------------------------------------------
134         File metadataFile = new File( getUpdatingCopy(), ".maven-scm-local" );
135         assertTrue( "Expected metadata file .maven-scm-local does not exist", metadataFile.exists() );
136         Reader reader = new FileReader( metadataFile );
137         LocalScmMetadata metadata;
138         try
139         {
140             metadata = new LocalScmMetadataXpp3Reader().read( reader );
141         }
142         finally
143         {
144             IOUtil.close( reader );
145         }
146         File root = new File( getRepositoryRoot() + "/" + moduleName );
147         @SuppressWarnings( "unchecked" )
148         List<String> fileNames = FileUtils.getFileNames( root, "**", null, false );
149         assertEquals( fileNames, metadata.getRepositoryFileNames() );
150 
151     }
152 
153 
154     private void makeRepo( File workingDirectory )
155         throws Exception
156     {
157         makeFile( workingDirectory, moduleName + "/pom.xml", "/pom.xml" );
158 
159         makeFile( workingDirectory, moduleName + "/readme.txt", "/readme.txt" );
160 
161         makeFile( workingDirectory, moduleName + "/src/main/java/Application.java", "/src/main/java/Application.java" );
162 
163         makeFile( workingDirectory, moduleName + "/src/test/java/Test.java", "/src/test/java/Test.java" );
164 
165         makeDirectory( workingDirectory, moduleName + "/src/test/resources" );
166     }
167 }