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 */
061public abstract class StatusCommandTckTest extends ScmTckTestCase {
062
063    protected void commit(File workingDirectory, ScmRepository repository) throws Exception {
064        CommandParameters commandParameters = new CommandParameters();
065        commandParameters.setString(CommandParameter.MESSAGE, "No msg");
066        CheckInScmResult result =
067                getScmManager().checkIn(repository, new ScmFileSet(workingDirectory), commandParameters);
068
069        assertTrue("Check result was successful, output: " + result.getCommandOutput(), result.isSuccess());
070
071        List<ScmFile> committedFiles = result.getCheckedInFiles();
072
073        assertEquals("Expected 2 files in the committed files list " + committedFiles, 2, committedFiles.size());
074    }
075
076    protected boolean commitUpdateCopy() {
077        return false;
078    }
079
080    @Test
081    public void testStatusCommand() throws Exception {
082        ScmRepository repository = makeScmRepository(getScmUrl());
083
084        checkOut(getUpdatingCopy(), repository);
085
086        // ----------------------------------------------------------------------
087        // Change the files
088        // ----------------------------------------------------------------------
089
090        /*
091         * readme.txt is changed (changed file in the root directory)
092         * project.xml is added (added file in the root directory)
093         */
094
095        // /readme.txt
096        this.edit(getWorkingCopy(), "readme.txt", null, getScmRepository());
097        ScmTestCase.makeFile(getWorkingCopy(), "/readme.txt", "changed readme.txt");
098
099        // /project.xml
100        ScmTestCase.makeFile(getWorkingCopy(), "/project.xml", "changed project.xml");
101
102        addToWorkingTree(getWorkingCopy(), new File("project.xml"), getScmRepository());
103
104        commit(getWorkingCopy(), getScmRepository());
105
106        // /pom.xml
107        this.edit(getUpdatingCopy(), "pom.xml", null, repository);
108        ScmTestCase.makeFile(getUpdatingCopy(), "/pom.xml", "changed pom.xml");
109
110        // /src/test/java/org
111        ScmTestCase.makeDirectory(getUpdatingCopy(), "/src/test/java/org");
112
113        addToWorkingTree(getUpdatingCopy(), new File("src/test/java/org"), repository);
114
115        // /src/main/java/org/Foo.java
116        ScmTestCase.makeFile(getUpdatingCopy(), "/src/main/java/org/Foo.java");
117
118        addToWorkingTree(getUpdatingCopy(), new File("src/main/java/org"), repository);
119
120        // src/main/java/org/Foo.java
121        addToWorkingTree(getUpdatingCopy(), new File("src/main/java/org/Foo.java"), repository);
122
123        ScmManager scmManager = getScmManager();
124
125        // ----------------------------------------------------------------------
126        // Check status the project
127        // src/main/java/org/Foo.java is added
128        // /pom.xml is modified
129        // check that readme and project.xml are not updated/created
130        // ----------------------------------------------------------------------
131
132        StatusScmResult result =
133                scmManager.getProviderByUrl(getScmUrl()).status(repository, new ScmFileSet(getUpdatingCopy()));
134
135        if (this.commitUpdateCopy()) {
136            // this is needed for perforce so that teardown can remove its client workspace, no harm for cvs/svn/git
137            commit(getUpdatingCopy(), repository);
138        }
139
140        assertNotNull("The command returned a null result.", result);
141
142        assertResultIsSuccess(result);
143
144        List<ScmFile> changedFiles = result.getChangedFiles();
145
146        assertEquals("Expected 2 files in the updated files list " + changedFiles, 2, changedFiles.size());
147
148        // ----------------------------------------------------------------------
149        // Assert the files in the updated files list
150        // ----------------------------------------------------------------------
151
152        Iterator<ScmFile> files = new TreeSet<>(changedFiles).iterator();
153
154        ScmFile file = files.next();
155        assertPath("src/main/java/org/Foo.java", file.getPath());
156        assertEquals(ScmFileStatus.ADDED, file.getStatus());
157
158        file = files.next();
159        assertPath("pom.xml", file.getPath());
160        assertEquals(ScmFileStatus.MODIFIED, file.getStatus());
161
162        assertFile(getUpdatingCopy(), "/readme.txt");
163
164        assertFalse("project.xml created incorrectly", new File(getUpdatingCopy(), "/project.xml").exists());
165    }
166}