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