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 *
062 */
063public abstract class UpdateCommandTckTest extends ScmTckTestCase {
064
065    private void commit(File workingDirectory, ScmRepository repository) throws Exception {
066        CheckInScmResult result = getScmManager().checkIn(repository, new ScmFileSet(workingDirectory), "No msg");
067
068        assertTrue("Check result was successful, output: " + result.getCommandOutput(), result.isSuccess());
069
070        List<ScmFile> committedFiles = result.getCheckedInFiles();
071
072        assertEquals(
073                "Expected 3 files in the committed files list:\n  "
074                        + StringUtils.join(committedFiles.iterator(), "\n  "),
075                3,
076                committedFiles.size());
077    }
078
079    @Test
080    public void testUpdateCommand() throws Exception {
081
082        deleteDirectory(getUpdatingCopy());
083
084        assertFalse(getUpdatingCopy().exists());
085
086        // deleteDirectory( getWorkingCopy() );
087
088        // assertFalse( getUpdatingCopy().exists() );
089
090        ScmRepository repository = makeScmRepository(getScmUrl());
091
092        checkOut(getUpdatingCopy(), repository);
093
094        // ----------------------------------------------------------------------
095        // Change the files
096        // ----------------------------------------------------------------------
097
098        /*
099         * readme.txt is changed (changed file in the root directory)
100         * project.xml is added (added file in the root directory)
101         * src/test/resources is untouched (a empty directory is left untouched)
102         * src/test/java is untouched (a non empty directory is left untouched)
103         * src/test/java/org (a empty directory is added)
104         * src/main/java/org/Foo.java (a non empty directory is added)
105         */
106
107        // /readme.txt
108        this.edit(getWorkingCopy(), "readme.txt", null, getScmRepository());
109        ScmTestCase.makeFile(getWorkingCopy(), "/readme.txt", "changed readme.txt");
110
111        // /project.xml
112        ScmTestCase.makeFile(getWorkingCopy(), "/project.xml", "changed project.xml");
113
114        addToWorkingTree(getWorkingCopy(), new File("project.xml"), getScmRepository());
115
116        // /src/test/java/org
117        ScmTestCase.makeDirectory(getWorkingCopy(), "/src/test/java/org");
118
119        addToWorkingTree(getWorkingCopy(), new File("src/test/java/org"), getScmRepository());
120
121        // /src/main/java/org/Foo.java
122        ScmTestCase.makeFile(getWorkingCopy(), "/src/main/java/org/Foo.java");
123
124        addToWorkingTree(getWorkingCopy(), new File("src/main/java/org"), getScmRepository());
125
126        // src/main/java/org/Foo.java
127        addToWorkingTree(getWorkingCopy(), new File("src/main/java/org/Foo.java"), getScmRepository());
128
129        ScmManager scmManager = getScmManager();
130
131        Date lastUpdate = new Date(System.currentTimeMillis() - 1000000);
132
133        commit(getWorkingCopy(), getScmRepository());
134
135        Thread.sleep(5000);
136
137        // ----------------------------------------------------------------------
138        // Update the project
139        // ----------------------------------------------------------------------
140
141        UpdateScmResult result = scmManager.update(repository, new ScmFileSet(getUpdatingCopy()), lastUpdate);
142
143        assertNotNull("The command returned a null result.", result);
144
145        assertResultIsSuccess(result);
146
147        List<ScmFile> updatedFiles = result.getUpdatedFiles();
148
149        List<ChangeSet> changedSets = result.getChanges();
150
151        assertEquals("Expected 3 files in the updated files list " + updatedFiles, 3, updatedFiles.size());
152
153        assertNotNull("The changed files list is null", changedSets);
154
155        assertFalse("The changed files list is empty ", changedSets.isEmpty());
156
157        for (ChangeSet changeSet : changedSets) {
158            System.out.println(changeSet.toXML());
159        }
160
161        // ----------------------------------------------------------------------
162        // Assert the files in the updated files list
163        // ----------------------------------------------------------------------
164
165        Iterator<ScmFile> files = new TreeSet<ScmFile>(updatedFiles).iterator();
166
167        // Foo.java
168        ScmFile file = files.next();
169        assertPath("src/main/java/org/Foo.java", file.getPath());
170        // TODO : Consolidate file status so that we can remove "|| ADDED" term
171        assertTrue(file.getStatus().isUpdate() || file.getStatus() == ScmFileStatus.ADDED);
172
173        // readme.txt
174        file = files.next();
175        assertPath("readme.txt", file.getPath());
176        assertTrue(file.getStatus().isUpdate());
177
178        // project.xml
179        file = files.next();
180        assertPath("project.xml", file.getPath());
181        // TODO : Consolidate file status so that we can remove "|| ADDED" term
182        assertTrue(file.getStatus().isUpdate() || file.getStatus() == ScmFileStatus.ADDED);
183    }
184}