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.changelog;
020
021import java.util.Collections;
022import java.util.Date;
023import java.util.List;
024
025import org.apache.maven.scm.ChangeSet;
026import org.apache.maven.scm.ScmBranch;
027import org.apache.maven.scm.ScmFileSet;
028import org.apache.maven.scm.ScmTagParameters;
029import org.apache.maven.scm.ScmTckTestCase;
030import org.apache.maven.scm.ScmTestCase;
031import org.apache.maven.scm.command.changelog.ChangeLogScmRequest;
032import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
033import org.apache.maven.scm.command.checkin.CheckInScmResult;
034import org.apache.maven.scm.command.tag.TagScmResult;
035import org.apache.maven.scm.provider.ScmProvider;
036import org.junit.Test;
037
038import static org.junit.Assert.assertEquals;
039import static org.junit.Assert.assertTrue;
040
041/**
042 * Test Changlog command. <br>
043 * 1. Get initial log <br>
044 * 2. Add one revision <br>
045 * 3. Get the two logs <br>
046 * 4. Get the last log based on date <br>
047 * 5. Test last log for date and comment <br>
048 *
049 * @author <a href="mailto:torbjorn@smorgrav.org">Torbj�rn Eikli Sm�rgrav</a>
050 */
051public abstract class ChangeLogCommandTckTest extends ScmTckTestCase {
052    private static final String COMMIT_MSG = "Second changelog";
053    private static final String COMMIT_TAG = "v3.14";
054
055    /**
056     * In some SCMs (Hg) adding a tag creates an extra commit which offsets the expectations.
057     * @return If an extra commit will be present for a tag.
058     */
059    public boolean isTagAnExtraCommit() {
060        return false;
061    }
062
063    @Test
064    public void testChangeLogCommand() throws Exception {
065        Thread.sleep(1000);
066        ScmProvider provider = getScmManager().getProviderByRepository(getScmRepository());
067        ScmFileSet fileSet = new ScmFileSet(getWorkingCopy());
068
069        ChangeLogScmResult firstResult =
070                provider.changeLog(getScmRepository(), fileSet, null, null, 0, (ScmBranch) null, null);
071        assertTrue(
072                firstResult.getProviderMessage() + ": " + firstResult.getCommandLine() + "\n"
073                        + firstResult.getCommandOutput(),
074                firstResult.isSuccess());
075
076        // for svn and git the repo get recreated for each test and therefore initial changelog size is 1
077        int firstLogSize = firstResult.getChangeLog().getChangeSets().size();
078        assertTrue("Unexpected initial log size", firstLogSize >= 1);
079
080        // Make a timestamp that we know are after initial revision but before the second
081        Date timeBeforeSecond = new Date(); // Current time
082
083        // pause a couple seconds... [SCM-244]
084        Thread.sleep(2000);
085
086        // Make a change to the readme.txt and commit the change
087        this.edit(getWorkingCopy(), "readme.txt", null, getScmRepository());
088        ScmTestCase.makeFile(getWorkingCopy(), "/readme.txt", "changed readme.txt");
089        CheckInScmResult checkInResult = provider.checkIn(getScmRepository(), fileSet, COMMIT_MSG);
090        assertTrue("Unable to checkin changes to the repository", checkInResult.isSuccess());
091
092        ScmTagParameters scmTagParameters = new ScmTagParameters();
093        TagScmResult tagResult = provider.tag(getScmRepository(), fileSet, COMMIT_TAG, scmTagParameters);
094        assertTrue("Unable to tag the changes in the repository", tagResult.isSuccess());
095
096        ChangeLogScmRequest changeLogScmRequest = new ChangeLogScmRequest(getScmRepository(), fileSet);
097        ChangeLogScmResult secondResult = provider.changeLog(changeLogScmRequest);
098        assertTrue(secondResult.getProviderMessage(), secondResult.isSuccess());
099
100        List<ChangeSet> changeSets = secondResult.getChangeLog().getChangeSets();
101
102        int expectedChangeSets = firstLogSize + 1;
103        boolean lastCommitIsCausedByTagging = false;
104        int lastCodeCommitIndex = 0;
105
106        if (isTagAnExtraCommit()) {
107            // This is for example Mercurial which creates an extra commit after tagging.
108            lastCommitIsCausedByTagging = true;
109            expectedChangeSets += 1;
110            lastCodeCommitIndex = 1;
111        }
112
113        assertEquals(expectedChangeSets, changeSets.size());
114
115        // Check if the tag has been retrieved again
116        ChangeSet changeSetWithTag = changeSets.get(lastCodeCommitIndex);
117        assertEquals(Collections.singletonList(COMMIT_TAG), changeSetWithTag.getTags());
118
119        // Now only retrieve the changelog after timeBeforeSecondChangeLog
120        Date currentTime = new Date();
121        changeLogScmRequest = new ChangeLogScmRequest(getScmRepository(), fileSet);
122        changeLogScmRequest.setStartDate(timeBeforeSecond);
123        changeLogScmRequest.setEndDate(currentTime);
124        changeLogScmRequest.setScmBranch(new ScmBranch(""));
125        ChangeLogScmResult thirdResult = provider.changeLog(changeLogScmRequest);
126
127        // Thorough assert of the last result
128        assertTrue(thirdResult.getProviderMessage(), thirdResult.isSuccess());
129
130        List<ChangeSet> thirdChangeSets = thirdResult.getChangeLog().getChangeSets();
131        assertEquals(lastCommitIsCausedByTagging ? 2 : 1, thirdChangeSets.size());
132        ChangeSet changeset = thirdChangeSets.get(lastCodeCommitIndex);
133        assertTrue(changeset.getDate().after(timeBeforeSecond));
134        assertTrue(changeset.getComment().startsWith(COMMIT_MSG));
135    }
136}