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