001package 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
022import java.io.File;
023import java.util.Date;
024import java.util.Iterator;
025import java.util.List;
026import java.util.TreeSet;
027
028import org.apache.maven.scm.ChangeSet;
029import org.apache.maven.scm.ScmFile;
030import org.apache.maven.scm.ScmFileSet;
031import org.apache.maven.scm.ScmFileStatus;
032import org.apache.maven.scm.ScmTckTestCase;
033import org.apache.maven.scm.ScmTestCase;
034import org.apache.maven.scm.command.checkin.CheckInScmResult;
035import org.apache.maven.scm.command.update.UpdateScmResult;
036import org.apache.maven.scm.manager.ScmManager;
037import org.apache.maven.scm.repository.ScmRepository;
038import org.codehaus.plexus.util.StringUtils;
039import org.junit.Test;
040
041import static org.junit.Assert.assertEquals;
042import static org.junit.Assert.assertFalse;
043import static org.junit.Assert.assertNotNull;
044import static org.junit.Assert.assertTrue;
045
046/**
047 * This test tests the update command.
048 * <p>
049 * It works like this:
050 * <p>
051 * <ol>
052 * <li>Check out the files to directory getWorkingCopy().
053 * <li>Check out the files to directory getUpdatingCopy().
054 * <li>Change the files in getWorkingCopy().
055 * <li>Commit the files in getWorkingCopy(). Note that the provider <b>must</b> not
056 * use the check in command as it can be guaranteed to work as it's not yet tested.
057 * <li>Use the update command in getUpdatingCopy() to assert that the files
058 * that was supposed to be updated actually was updated.
059 * </ol>
060 *
061 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
062 *
063 */
064public abstract class UpdateCommandTckTest
065    extends ScmTckTestCase
066{
067
068    private void commit( File workingDirectory, ScmRepository repository )
069        throws Exception
070    {
071        CheckInScmResult result = getScmManager().checkIn( repository, new ScmFileSet( workingDirectory ), "No msg" );
072
073        assertTrue( "Check result was successful, output: " + result.getCommandOutput(), result.isSuccess() );
074
075        List<ScmFile> committedFiles = result.getCheckedInFiles();
076
077        assertEquals(
078            "Expected 3 files in the committed files list:\n  " + StringUtils.join( committedFiles.iterator(), "\n  " ),
079            3, committedFiles.size() );
080    }
081
082    @Test
083    public void testUpdateCommand()
084        throws Exception
085    {
086
087        deleteDirectory( getUpdatingCopy() );
088
089        assertFalse( getUpdatingCopy().exists() );
090
091        //deleteDirectory( getWorkingCopy() );
092
093        //assertFalse( getUpdatingCopy().exists() );
094
095        ScmRepository repository = makeScmRepository( getScmUrl() );
096
097        checkOut( getUpdatingCopy(), repository );
098
099        // ----------------------------------------------------------------------
100        // Change the files
101        // ----------------------------------------------------------------------
102
103        /*
104         * readme.txt is changed (changed file in the root directory)
105         * project.xml is added (added file in the root directory)
106         * src/test/resources is untouched (a empty directory is left untouched)
107         * src/test/java is untouched (a non empty directory is left untouched)
108         * src/test/java/org (a empty directory is added)
109         * src/main/java/org/Foo.java (a non empty directory is added)
110         */
111
112        // /readme.txt
113        this.edit( getWorkingCopy(), "readme.txt", null, getScmRepository() );
114        ScmTestCase.makeFile( getWorkingCopy(), "/readme.txt", "changed readme.txt" );
115
116        // /project.xml
117        ScmTestCase.makeFile( getWorkingCopy(), "/project.xml", "changed project.xml" );
118
119        addToWorkingTree( getWorkingCopy(), new File( "project.xml" ), getScmRepository() );
120
121        // /src/test/java/org
122        ScmTestCase.makeDirectory( getWorkingCopy(), "/src/test/java/org" );
123
124        addToWorkingTree( getWorkingCopy(), new File( "src/test/java/org" ), getScmRepository() );
125
126        // /src/main/java/org/Foo.java
127        ScmTestCase.makeFile( getWorkingCopy(), "/src/main/java/org/Foo.java" );
128
129        addToWorkingTree( getWorkingCopy(), new File( "src/main/java/org" ), getScmRepository() );
130
131        // src/main/java/org/Foo.java
132        addToWorkingTree( getWorkingCopy(), new File( "src/main/java/org/Foo.java" ), getScmRepository() );
133
134        ScmManager scmManager = getScmManager();
135
136        Date lastUpdate = new Date( System.currentTimeMillis() - 1000000 );
137
138        commit( getWorkingCopy(), getScmRepository() );
139
140        Thread.sleep( 5000 );
141
142        // ----------------------------------------------------------------------
143        // Update the project
144        // ----------------------------------------------------------------------
145
146        UpdateScmResult result = scmManager.update( repository, new ScmFileSet( getUpdatingCopy() ), lastUpdate );
147
148        assertNotNull( "The command returned a null result.", result );
149
150        assertResultIsSuccess( result );
151
152        List<ScmFile> updatedFiles = result.getUpdatedFiles();
153
154        List<ChangeSet> changedSets = result.getChanges();
155
156        assertEquals( "Expected 3 files in the updated files list " + updatedFiles, 3, updatedFiles.size() );
157
158        assertNotNull( "The changed files list is null", changedSets );
159
160        assertFalse( "The changed files list is empty ", changedSets.isEmpty() );
161
162        for ( ChangeSet changeSet : changedSets )
163        {
164            System.out.println( changeSet.toXML() );
165        }
166
167        // ----------------------------------------------------------------------
168        // Assert the files in the updated files list
169        // ----------------------------------------------------------------------
170
171        Iterator<ScmFile> files = new TreeSet<ScmFile>( updatedFiles ).iterator();
172
173        //Foo.java
174        ScmFile file = files.next();
175        assertPath( "src/main/java/org/Foo.java", file.getPath() );
176        //TODO : Consolidate file status so that we can remove "|| ADDED" term
177        assertTrue( file.getStatus().isUpdate() || file.getStatus() == ScmFileStatus.ADDED );
178
179        //readme.txt
180        file = files.next();
181        assertPath( "readme.txt", file.getPath() );
182        assertTrue( file.getStatus().isUpdate() );
183
184        //project.xml
185        file = files.next();
186        assertPath( "project.xml", file.getPath() );
187        //TODO : Consolidate file status so that we can remove "|| ADDED" term
188        assertTrue( file.getStatus().isUpdate() || file.getStatus() == ScmFileStatus.ADDED );
189    }
190
191}