001package org.apache.maven.scm.tck.command.status;
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 org.apache.maven.scm.ScmFile;
023import org.apache.maven.scm.ScmFileSet;
024import org.apache.maven.scm.ScmFileStatus;
025import org.apache.maven.scm.ScmTckTestCase;
026import org.apache.maven.scm.ScmTestCase;
027import org.apache.maven.scm.command.checkin.CheckInScmResult;
028import org.apache.maven.scm.command.status.StatusScmResult;
029import org.apache.maven.scm.manager.ScmManager;
030import org.apache.maven.scm.repository.ScmRepository;
031import org.junit.Test;
032
033import java.io.File;
034import java.util.Iterator;
035import java.util.List;
036import java.util.TreeSet;
037
038import static org.junit.Assert.assertEquals;
039import static org.junit.Assert.assertFalse;
040import static org.junit.Assert.assertNotNull;
041import static org.junit.Assert.assertTrue;
042
043/**
044 * This test tests the status command.
045 * <p>
046 * It works like this:
047 * <p>
048 * <ol>
049 * <li>Check out the files to directory getWorkingCopy().
050 * <li>Check out the files to directory getUpdatingCopy().
051 * <li>Change the files in getWorkingCopy().
052 * <li>Commit the files in getWorkingCopy(). Note that the provider <b>must</b> not
053 * use the check in command as it can be guaranteed to work as it's not yet tested.
054 * <li>Use the update command in getUpdatingCopy() to assert that the files
055 * that was supposed to be updated actually was updated.
056 * </ol>
057 *
058 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
059 *
060 */
061public abstract class StatusCommandTckTest
062    extends ScmTckTestCase
063{
064
065    protected void commit( File workingDirectory, ScmRepository repository )
066        throws Exception
067    {
068        CheckInScmResult result = getScmManager().checkIn( repository, new ScmFileSet( workingDirectory ), "No msg" );
069
070        assertTrue( "Check result was successful, output: " + result.getCommandOutput(), result.isSuccess() );
071
072        List<ScmFile> committedFiles = result.getCheckedInFiles();
073
074        assertEquals( "Expected 2 files in the committed files list " + committedFiles, 2, committedFiles.size() );
075    }
076
077    protected boolean commitUpdateCopy()
078    {
079        return false;
080    }
081
082
083    @Test
084    public void testStatusCommand()
085        throws Exception
086    {
087        ScmRepository repository = makeScmRepository( getScmUrl() );
088
089        checkOut( getUpdatingCopy(), repository );
090
091        // ----------------------------------------------------------------------
092        // Change the files
093        // ----------------------------------------------------------------------
094
095        /*
096         * readme.txt is changed (changed file in the root directory)
097         * project.xml is added (added file in the root directory)
098         */
099
100        // /readme.txt
101        this.edit( getWorkingCopy(), "readme.txt", null, getScmRepository() );
102        ScmTestCase.makeFile( getWorkingCopy(), "/readme.txt", "changed readme.txt" );
103
104        // /project.xml
105        ScmTestCase.makeFile( getWorkingCopy(), "/project.xml", "changed project.xml" );
106
107        addToWorkingTree( getWorkingCopy(), new File( "project.xml" ), getScmRepository() );
108
109        commit( getWorkingCopy(), getScmRepository() );
110
111        // /pom.xml
112        this.edit( getUpdatingCopy(), "pom.xml", null, repository );
113        ScmTestCase.makeFile( getUpdatingCopy(), "/pom.xml", "changed pom.xml" );
114
115        // /src/test/java/org
116        ScmTestCase.makeDirectory( getUpdatingCopy(), "/src/test/java/org" );
117
118        addToWorkingTree( getUpdatingCopy(), new File( "src/test/java/org" ), repository );
119
120        // /src/main/java/org/Foo.java
121        ScmTestCase.makeFile( getUpdatingCopy(), "/src/main/java/org/Foo.java" );
122
123        addToWorkingTree( getUpdatingCopy(), new File( "src/main/java/org" ), repository );
124
125        // src/main/java/org/Foo.java
126        addToWorkingTree( getUpdatingCopy(), new File( "src/main/java/org/Foo.java" ), repository );
127
128        ScmManager scmManager = getScmManager();
129
130        // ----------------------------------------------------------------------
131        // Check status the project
132        // src/main/java/org/Foo.java is added
133        // /pom.xml is modified
134        // check that readme and project.xml are not updated/created
135        // ----------------------------------------------------------------------
136
137        StatusScmResult result = scmManager.getProviderByUrl( getScmUrl() )
138            .status( repository, new ScmFileSet( getUpdatingCopy() ) );
139
140        if ( this.commitUpdateCopy() )
141        {
142          //this is needed for perforce so that teardown can remove its client workspace, no harm for cvs/svn/git
143            commit( getUpdatingCopy(), repository );
144        }
145
146        assertNotNull( "The command returned a null result.", result );
147
148        assertResultIsSuccess( result );
149
150        List<ScmFile> changedFiles = result.getChangedFiles();
151
152        assertEquals( "Expected 2 files in the updated files list " + changedFiles, 2, changedFiles.size() );
153
154        // ----------------------------------------------------------------------
155        // Assert the files in the updated files list
156        // ----------------------------------------------------------------------
157
158        Iterator<ScmFile> files = new TreeSet<ScmFile>( changedFiles ).iterator();
159
160        ScmFile file = files.next();
161        assertPath( "src/main/java/org/Foo.java", file.getPath() );
162        assertEquals( ScmFileStatus.ADDED, file.getStatus() );
163
164        file = files.next();
165        assertPath( "pom.xml", file.getPath() );
166        assertEquals( ScmFileStatus.MODIFIED, file.getStatus() );
167
168        assertFile( getUpdatingCopy(), "/readme.txt" );
169
170        assertFalse( "project.xml created incorrectly", new File( getUpdatingCopy(), "/project.xml" ).exists() );
171    }
172}