001package org.apache.maven.scm.tck.command.changelog;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.scm.ChangeSet;
023import org.apache.maven.scm.ScmBranch;
024import org.apache.maven.scm.ScmFileSet;
025import org.apache.maven.scm.ScmTagParameters;
026import org.apache.maven.scm.ScmTckTestCase;
027import org.apache.maven.scm.ScmTestCase;
028import org.apache.maven.scm.command.changelog.ChangeLogScmRequest;
029import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
030import org.apache.maven.scm.command.checkin.CheckInScmResult;
031import org.apache.maven.scm.command.tag.TagScmResult;
032import org.apache.maven.scm.provider.ScmProvider;
033import org.junit.Test;
034
035import java.util.Collections;
036import java.util.Date;
037import java.util.List;
038
039import static org.junit.Assert.assertEquals;
040import static org.junit.Assert.assertTrue;
041
042/**
043 * Test Changlog command. <br>
044 * 1. Get initial log <br>
045 * 2. Add one revision <br>
046 * 3. Get the two logs <br>
047 * 4. Get the last log based on date <br>
048 * 5. Test last log for date and comment <br>
049 *
050 * @author <a href="mailto:torbjorn@smorgrav.org">Torbj�rn Eikli Sm�rgrav</a>
051 */
052public abstract class ChangeLogCommandTckTest
053    extends ScmTckTestCase
054{
055    private static final String COMMIT_MSG = "Second changelog";
056    private static final String COMMIT_TAG = "v3.14";
057
058    /**
059     * In some SCMs (Hg) adding a tag creates an extra commit which offsets the expectations.
060     * @return If an extra commit will be present for a tag.
061     */
062    public boolean isTagAnExtraCommit()
063    {
064        return false;
065    }
066
067    @Test
068    public void testChangeLogCommand()
069        throws Exception
070    {
071        Thread.sleep( 1000 );
072        ScmProvider provider = getScmManager().getProviderByRepository( getScmRepository() );
073        ScmFileSet fileSet = new ScmFileSet( getWorkingCopy() );
074
075        ChangeLogScmResult firstResult =
076            provider.changeLog( getScmRepository(), fileSet, null, null, 0, (ScmBranch) null, null );
077        assertTrue( firstResult.getProviderMessage() + ": " + firstResult.getCommandLine() + "\n"
078                        + firstResult.getCommandOutput(), firstResult.isSuccess() );
079
080        // for svn and git the repo get recreated for each test and therefore initial changelog size is 1
081        int firstLogSize = firstResult.getChangeLog().getChangeSets().size();
082        assertTrue( "Unexpected initial log size", firstLogSize >= 1 );
083
084        // Make a timestamp that we know are after initial revision but before the second
085        Date timeBeforeSecond = new Date(); //Current time
086
087        // pause a couple seconds... [SCM-244]
088        Thread.sleep( 2000 );
089
090        //Make a change to the readme.txt and commit the change
091        this.edit( getWorkingCopy(), "readme.txt", null, getScmRepository() );
092        ScmTestCase.makeFile( getWorkingCopy(), "/readme.txt", "changed readme.txt" );
093        CheckInScmResult checkInResult = provider.checkIn( getScmRepository(), fileSet, COMMIT_MSG );
094        assertTrue( "Unable to checkin changes to the repository", checkInResult.isSuccess() );
095
096        ScmTagParameters scmTagParameters = new ScmTagParameters();
097        TagScmResult tagResult = provider.tag( getScmRepository(), fileSet, COMMIT_TAG, scmTagParameters );
098        assertTrue( "Unable to tag the changes in the repository", tagResult.isSuccess() );
099
100        ChangeLogScmRequest changeLogScmRequest = new ChangeLogScmRequest( getScmRepository(), fileSet );
101        ChangeLogScmResult secondResult = provider.changeLog( changeLogScmRequest );
102        assertTrue( secondResult.getProviderMessage(), secondResult.isSuccess() );
103
104        List<ChangeSet> changeSets = secondResult.getChangeLog().getChangeSets();
105
106        int expectedChangeSets = firstLogSize + 1;
107        boolean lastCommitIsCausedByTagging = false;
108        int lastCodeCommitIndex = 0;
109
110        if ( isTagAnExtraCommit() )
111        {
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}