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