001    package org.apache.maven.scm.tck.command.update;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     * http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.io.File;
023    import java.util.Date;
024    import java.util.Iterator;
025    import java.util.List;
026    import java.util.TreeSet;
027    
028    import org.apache.maven.scm.ChangeSet;
029    import org.apache.maven.scm.ScmFile;
030    import org.apache.maven.scm.ScmFileSet;
031    import org.apache.maven.scm.ScmFileStatus;
032    import org.apache.maven.scm.ScmTckTestCase;
033    import org.apache.maven.scm.ScmTestCase;
034    import org.apache.maven.scm.command.checkin.CheckInScmResult;
035    import org.apache.maven.scm.command.update.UpdateScmResult;
036    import org.apache.maven.scm.manager.ScmManager;
037    import org.apache.maven.scm.repository.ScmRepository;
038    import org.codehaus.plexus.util.FileUtils;
039    import org.codehaus.plexus.util.StringUtils;
040    
041    /**
042     * This test tests the update command.
043     * <p/>
044     * It works like this:
045     * <p/>
046     * <ol>
047     * <li>Check out the files to directory getWorkingCopy().
048     * <li>Check out the files to directory getUpdatingCopy().
049     * <li>Change the files in getWorkingCopy().
050     * <li>Commit the files in getWorkingCopy(). Note that the provider <b>must</b> not
051     * use the check in command as it can be guaranteed to work as it's not yet tested.
052     * <li>Use the update command in getUpdatingCopy() to assert that the files
053     * that was supposed to be updated actually was updated.
054     * </ol>
055     *
056     * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
057     * @version $Id: UpdateCommandTckTest.java 1241523 2012-02-07 17:08:26Z hboutemy $
058     */
059    public abstract class UpdateCommandTckTest
060        extends ScmTckTestCase
061    {
062    
063        private void commit( File workingDirectory, ScmRepository repository )
064            throws Exception
065        {
066            CheckInScmResult result = getScmManager().checkIn( repository, new ScmFileSet( workingDirectory ), "No msg" );
067    
068            assertTrue( "Check result was successful, output: " + result.getCommandOutput(), result.isSuccess() );
069    
070            List<ScmFile> committedFiles = result.getCheckedInFiles();
071    
072            assertEquals(
073                "Expected 3 files in the committed files list:\n  " + StringUtils.join( committedFiles.iterator(), "\n  " ),
074                3, committedFiles.size() );
075        }
076    
077        public void testUpdateCommand()
078            throws Exception
079        {
080            
081            FileUtils.deleteDirectory( getUpdatingCopy() );
082            
083            assertFalse( getUpdatingCopy().exists() );    
084            
085            //FileUtils.deleteDirectory( getWorkingCopy() );
086            
087            //assertFalse( getUpdatingCopy().exists() );
088            
089            ScmRepository repository = makeScmRepository( getScmUrl() );
090    
091            checkOut( getUpdatingCopy(), repository );
092    
093            // ----------------------------------------------------------------------
094            // Change the files
095            // ----------------------------------------------------------------------
096    
097            /*
098             * readme.txt is changed (changed file in the root directory)
099             * 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            ScmTestCase.makeFile( getWorkingCopy(), "/readme.txt", "changed readme.txt" );
108    
109            // /project.xml
110            ScmTestCase.makeFile( getWorkingCopy(), "/project.xml", "changed project.xml" );
111    
112            addToWorkingTree( getWorkingCopy(), new File( "project.xml" ), repository );
113    
114            // /src/test/java/org
115            ScmTestCase.makeDirectory( getWorkingCopy(), "/src/test/java/org" );
116    
117            addToWorkingTree( getWorkingCopy(), new File( "src/test/java/org" ), repository );
118    
119            // /src/main/java/org/Foo.java
120            ScmTestCase.makeFile( getWorkingCopy(), "/src/main/java/org/Foo.java" );
121    
122            addToWorkingTree( getWorkingCopy(), new File( "src/main/java/org" ), repository );
123    
124            // src/main/java/org/Foo.java
125            addToWorkingTree( getWorkingCopy(), new File( "src/main/java/org/Foo.java" ), repository );
126    
127            ScmManager scmManager = getScmManager();
128    
129            Date lastUpdate = new Date( System.currentTimeMillis() - 1000000 );
130    
131            commit( getWorkingCopy(), repository );
132    
133            Thread.sleep( 5000 );
134            
135            // ----------------------------------------------------------------------
136            // Update the project
137            // ----------------------------------------------------------------------
138           
139            UpdateScmResult result = scmManager.update( repository, new ScmFileSet( getUpdatingCopy() ), lastUpdate );
140    
141            assertNotNull( "The command returned a null result.", result );
142    
143            assertResultIsSuccess( result );
144    
145            List<ScmFile> updatedFiles = result.getUpdatedFiles();
146    
147            List<ChangeSet> changedSets = result.getChanges();
148    
149            assertEquals( "Expected 3 files in the updated files list " + updatedFiles, 3, updatedFiles.size() );
150    
151            assertNotNull( "The changed files list is null", changedSets );
152    
153            assertFalse( "The changed files list is empty ", changedSets.isEmpty() );
154    
155            for ( ChangeSet changeSet : changedSets )
156            {
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        
184    }