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.update;
020
021import java.io.File;
022import java.util.Date;
023import java.util.Iterator;
024import java.util.List;
025import java.util.TreeSet;
026
027import org.apache.commons.lang3.StringUtils;
028import org.apache.maven.scm.ChangeSet;
029import org.apache.maven.scm.ScmFile;
030import org.apache.maven.scm.ScmFileSet;
031import org.apache.maven.scm.ScmFileStatus;
032import org.apache.maven.scm.ScmTckTestCase;
033import org.apache.maven.scm.ScmTestCase;
034import org.apache.maven.scm.command.checkin.CheckInScmResult;
035import org.apache.maven.scm.command.update.UpdateScmResult;
036import org.apache.maven.scm.manager.ScmManager;
037import org.apache.maven.scm.repository.ScmRepository;
038import org.junit.Test;
039
040import static org.junit.Assert.assertEquals;
041import static org.junit.Assert.assertFalse;
042import static org.junit.Assert.assertNotNull;
043import static org.junit.Assert.assertTrue;
044
045/**
046 * This test tests the update command.
047 * <p>
048 * It works like this:
049 * <p>
050 * <ol>
051 * <li>Check out the files to directory getWorkingCopy().
052 * <li>Check out the files to directory getUpdatingCopy().
053 * <li>Change the files in getWorkingCopy().
054 * <li>Commit the files in getWorkingCopy(). Note that the provider <b>must</b> not
055 * use the check in command as it can be guaranteed to work as it's not yet tested.
056 * <li>Use the update command in getUpdatingCopy() to assert that the files
057 * that was supposed to be updated actually was updated.
058 * </ol>
059 *
060 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
061 */
062public abstract class UpdateCommandTckTest extends ScmTckTestCase {
063
064    private void commit(File workingDirectory, ScmRepository repository) throws Exception {
065        CheckInScmResult result = getScmManager().checkIn(repository, new ScmFileSet(workingDirectory), "No msg");
066
067        assertTrue("Check result was successful, output: " + result.getCommandOutput(), result.isSuccess());
068
069        List<ScmFile> committedFiles = result.getCheckedInFiles();
070
071        assertEquals(
072                "Expected 3 files in the committed files list:\n  "
073                        + StringUtils.join(committedFiles.iterator(), "\n  "),
074                3,
075                committedFiles.size());
076    }
077
078    @Test
079    public void testUpdateCommand() throws Exception {
080
081        deleteDirectory(getUpdatingCopy());
082
083        assertFalse(getUpdatingCopy().exists());
084
085        // deleteDirectory( getWorkingCopy() );
086
087        // assertFalse( getUpdatingCopy().exists() );
088
089        ScmRepository repository = makeScmRepository(getScmUrl());
090
091        checkOut(getUpdatingCopy(), repository);
092
093        // ----------------------------------------------------------------------
094        // Change the files
095        // ----------------------------------------------------------------------
096
097        /*
098         * readme.txt is changed (changed file in the root directory)
099         * project.xml is added (added file in the root directory)
100         * src/test/resources is untouched (a empty directory is left untouched)
101         * src/test/java is untouched (a non empty directory is left untouched)
102         * src/test/java/org (a empty directory is added)
103         * src/main/java/org/Foo.java (a non empty directory is added)
104         */
105
106        // /readme.txt
107        this.edit(getWorkingCopy(), "readme.txt", null, getScmRepository());
108        ScmTestCase.makeFile(getWorkingCopy(), "/readme.txt", "changed readme.txt");
109
110        // /project.xml
111        ScmTestCase.makeFile(getWorkingCopy(), "/project.xml", "changed project.xml");
112
113        addToWorkingTree(getWorkingCopy(), new File("project.xml"), getScmRepository());
114
115        // /src/test/java/org
116        ScmTestCase.makeDirectory(getWorkingCopy(), "/src/test/java/org");
117
118        addToWorkingTree(getWorkingCopy(), new File("src/test/java/org"), getScmRepository());
119
120        // /src/main/java/org/Foo.java
121        ScmTestCase.makeFile(getWorkingCopy(), "/src/main/java/org/Foo.java");
122
123        addToWorkingTree(getWorkingCopy(), new File("src/main/java/org"), getScmRepository());
124
125        // src/main/java/org/Foo.java
126        addToWorkingTree(getWorkingCopy(), new File("src/main/java/org/Foo.java"), getScmRepository());
127
128        ScmManager scmManager = getScmManager();
129
130        Date lastUpdate = new Date(System.currentTimeMillis() - 1000000);
131
132        commit(getWorkingCopy(), getScmRepository());
133
134        Thread.sleep(5000);
135
136        // ----------------------------------------------------------------------
137        // Update the project
138        // ----------------------------------------------------------------------
139
140        UpdateScmResult result = scmManager.update(repository, new ScmFileSet(getUpdatingCopy()), lastUpdate);
141
142        assertNotNull("The command returned a null result.", result);
143
144        assertResultIsSuccess(result);
145
146        List<ScmFile> updatedFiles = result.getUpdatedFiles();
147
148        List<ChangeSet> changedSets = result.getChanges();
149
150        assertEquals("Expected 3 files in the updated files list " + updatedFiles, 3, updatedFiles.size());
151
152        assertNotNull("The changed files list is null", changedSets);
153
154        assertFalse("The changed files list is empty ", changedSets.isEmpty());
155
156        for (ChangeSet changeSet : changedSets) {
157            System.out.println(changeSet.toXML());
158        }
159
160        // ----------------------------------------------------------------------
161        // Assert the files in the updated files list
162        // ----------------------------------------------------------------------
163
164        Iterator<ScmFile> files = new TreeSet<ScmFile>(updatedFiles).iterator();
165
166        // Foo.java
167        ScmFile file = files.next();
168        assertPath("src/main/java/org/Foo.java", file.getPath());
169        // TODO : Consolidate file status so that we can remove "|| ADDED" term
170        assertTrue(file.getStatus().isUpdate() || file.getStatus() == ScmFileStatus.ADDED);
171
172        // readme.txt
173        file = files.next();
174        assertPath("readme.txt", file.getPath());
175        assertTrue(file.getStatus().isUpdate());
176
177        // project.xml
178        file = files.next();
179        assertPath("project.xml", file.getPath());
180        // TODO : Consolidate file status so that we can remove "|| ADDED" term
181        assertTrue(file.getStatus().isUpdate() || file.getStatus() == ScmFileStatus.ADDED);
182    }
183}