001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.tck.command.status;
020
021import java.io.File;
022import java.util.Iterator;
023import java.util.List;
024import java.util.TreeSet;
025
026import org.apache.maven.scm.ScmFile;
027import org.apache.maven.scm.ScmFileSet;
028import org.apache.maven.scm.ScmFileStatus;
029import org.apache.maven.scm.ScmTckTestCase;
030import org.apache.maven.scm.ScmTestCase;
031import org.apache.maven.scm.command.checkin.CheckInScmResult;
032import org.apache.maven.scm.command.status.StatusScmResult;
033import org.apache.maven.scm.manager.ScmManager;
034import org.apache.maven.scm.repository.ScmRepository;
035import org.junit.Test;
036
037import static org.junit.Assert.assertEquals;
038import static org.junit.Assert.assertFalse;
039import static org.junit.Assert.assertNotNull;
040import static org.junit.Assert.assertTrue;
041
042/**
043 * This test tests the status command.
044 * <p>
045 * It works like this:
046 * <p>
047 * <ol>
048 * <li>Check out the files to directory getWorkingCopy().
049 * <li>Check out the files to directory getUpdatingCopy().
050 * <li>Change the files in getWorkingCopy().
051 * <li>Commit the files in getWorkingCopy(). Note that the provider <b>must</b> not
052 * use the check in command as it can be guaranteed to work as it's not yet tested.
053 * <li>Use the update command in getUpdatingCopy() to assert that the files
054 * that was supposed to be updated actually was updated.
055 * </ol>
056 *
057 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
058 *
059 */
060public abstract class StatusCommandTckTest extends ScmTckTestCase {
061
062    protected void commit(File workingDirectory, ScmRepository repository) throws Exception {
063        CheckInScmResult result = getScmManager().checkIn(repository, new ScmFileSet(workingDirectory), "No msg");
064
065        assertTrue("Check result was successful, output: " + result.getCommandOutput(), result.isSuccess());
066
067        List<ScmFile> committedFiles = result.getCheckedInFiles();
068
069        assertEquals("Expected 2 files in the committed files list " + committedFiles, 2, committedFiles.size());
070    }
071
072    protected boolean commitUpdateCopy() {
073        return false;
074    }
075
076    @Test
077    public void testStatusCommand() throws Exception {
078        ScmRepository repository = makeScmRepository(getScmUrl());
079
080        checkOut(getUpdatingCopy(), repository);
081
082        // ----------------------------------------------------------------------
083        // Change the files
084        // ----------------------------------------------------------------------
085
086        /*
087         * readme.txt is changed (changed file in the root directory)
088         * project.xml is added (added file in the root directory)
089         */
090
091        // /readme.txt
092        this.edit(getWorkingCopy(), "readme.txt", null, getScmRepository());
093        ScmTestCase.makeFile(getWorkingCopy(), "/readme.txt", "changed readme.txt");
094
095        // /project.xml
096        ScmTestCase.makeFile(getWorkingCopy(), "/project.xml", "changed project.xml");
097
098        addToWorkingTree(getWorkingCopy(), new File("project.xml"), getScmRepository());
099
100        commit(getWorkingCopy(), getScmRepository());
101
102        // /pom.xml
103        this.edit(getUpdatingCopy(), "pom.xml", null, repository);
104        ScmTestCase.makeFile(getUpdatingCopy(), "/pom.xml", "changed pom.xml");
105
106        // /src/test/java/org
107        ScmTestCase.makeDirectory(getUpdatingCopy(), "/src/test/java/org");
108
109        addToWorkingTree(getUpdatingCopy(), new File("src/test/java/org"), repository);
110
111        // /src/main/java/org/Foo.java
112        ScmTestCase.makeFile(getUpdatingCopy(), "/src/main/java/org/Foo.java");
113
114        addToWorkingTree(getUpdatingCopy(), new File("src/main/java/org"), repository);
115
116        // src/main/java/org/Foo.java
117        addToWorkingTree(getUpdatingCopy(), new File("src/main/java/org/Foo.java"), repository);
118
119        ScmManager scmManager = getScmManager();
120
121        // ----------------------------------------------------------------------
122        // Check status the project
123        // src/main/java/org/Foo.java is added
124        // /pom.xml is modified
125        // check that readme and project.xml are not updated/created
126        // ----------------------------------------------------------------------
127
128        StatusScmResult result =
129                scmManager.getProviderByUrl(getScmUrl()).status(repository, new ScmFileSet(getUpdatingCopy()));
130
131        if (this.commitUpdateCopy()) {
132            // this is needed for perforce so that teardown can remove its client workspace, no harm for cvs/svn/git
133            commit(getUpdatingCopy(), repository);
134        }
135
136        assertNotNull("The command returned a null result.", result);
137
138        assertResultIsSuccess(result);
139
140        List<ScmFile> changedFiles = result.getChangedFiles();
141
142        assertEquals("Expected 2 files in the updated files list " + changedFiles, 2, changedFiles.size());
143
144        // ----------------------------------------------------------------------
145        // Assert the files in the updated files list
146        // ----------------------------------------------------------------------
147
148        Iterator<ScmFile> files = new TreeSet<ScmFile>(changedFiles).iterator();
149
150        ScmFile file = files.next();
151        assertPath("src/main/java/org/Foo.java", file.getPath());
152        assertEquals(ScmFileStatus.ADDED, file.getStatus());
153
154        file = files.next();
155        assertPath("pom.xml", file.getPath());
156        assertEquals(ScmFileStatus.MODIFIED, file.getStatus());
157
158        assertFile(getUpdatingCopy(), "/readme.txt");
159
160        assertFalse("project.xml created incorrectly", new File(getUpdatingCopy(), "/project.xml").exists());
161    }
162}