001package org.apache.maven.scm.tck.command.blame;
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.ScmFileSet;
023import org.apache.maven.scm.ScmTckTestCase;
024import org.apache.maven.scm.ScmTestCase;
025import org.apache.maven.scm.command.blame.BlameLine;
026import org.apache.maven.scm.command.blame.BlameScmRequest;
027import org.apache.maven.scm.command.blame.BlameScmResult;
028import org.apache.maven.scm.command.checkin.CheckInScmResult;
029import org.apache.maven.scm.manager.ScmManager;
030import org.apache.maven.scm.provider.ScmProvider;
031import org.apache.maven.scm.repository.ScmRepository;
032
033import java.util.Date;
034
035import static org.junit.Assert.assertNotEquals;
036
037/**
038 * @author Evgeny Mandrikov
039 */
040public abstract class BlameCommandTckTest
041    extends ScmTckTestCase
042{
043    private static final String COMMIT_MSG = "Second changelog";
044
045    public void testBlameCommand()
046        throws Exception
047    {
048        ScmRepository repository = getScmRepository();
049        ScmManager manager = getScmManager();
050        ScmProvider provider = manager.getProviderByRepository( getScmRepository() );
051        ScmFileSet fileSet = new ScmFileSet( getWorkingCopy() );
052
053        BlameScmResult result;
054        BlameLine line;
055
056        // === readme.txt ===
057        BlameScmRequest blameScmRequest = new BlameScmRequest( repository, fileSet );
058        blameScmRequest.setFilename( "readme.txt" );
059        //result = manager.blame( repository, fileSet, "readme.txt" );
060        result = manager.blame( blameScmRequest );
061        assertNotNull( "The command returned a null result.", result );
062        assertResultIsSuccess( result );
063        assertEquals( "Expected 1 line in blame", 1, result.getLines().size() );
064        line = result.getLines().get( 0 );
065        String initialRevision = line.getRevision();
066
067        //Make a timestamp that we know are after initial revision but before the second
068        Date timeBeforeSecond = new Date(); // Current time
069        // pause a couple seconds...
070        Thread.sleep( 2000 );
071        //Make a change to the readme.txt and commit the change
072        this.edit( getWorkingCopy(), "readme.txt", null, getScmRepository() );
073        ScmTestCase.makeFile( getWorkingCopy(), "/readme.txt", "changed readme.txt" );
074        CheckInScmResult checkInResult = provider.checkIn( getScmRepository(), fileSet, COMMIT_MSG );
075        assertTrue( "Unable to checkin changes to the repository", checkInResult.isSuccess() );
076
077        result = manager.blame( repository, fileSet, "readme.txt" );
078
079        // pause a couple seconds...
080        Thread.sleep( 2000 );
081        Date timeAfterSecond = new Date(); // Current time
082
083        assertNotNull( "The command returned a null result.", result );
084        assertResultIsSuccess( result );
085
086        assertEquals( "Expected 1 line in blame", 1, result.getLines().size() );
087        line = result.getLines().get( 0 );
088
089        assertNotNull( "Expected not null author", line.getAuthor() );
090        assertNotNull( "Expected not null revision", line.getRevision() );
091        assertNotNull( "Expected not null date", line.getDate() );
092
093        assertNotEquals( "Expected another revision", initialRevision, line.getRevision() );
094        if ( isTestDateTime() )
095        {
096            assertDateBetween( timeBeforeSecond, timeAfterSecond, line.getDate() );
097        }
098
099        // === pom.xml ===
100        result = manager.blame( repository, fileSet, "pom.xml" );
101
102        assertNotNull( "The command returned a null result.", result );
103
104        assertResultIsSuccess( result );
105
106        verifyResult( result );
107    }
108
109    protected boolean isTestDateTime()
110    {
111        return true;
112    }
113
114    protected void assertDateBetween( Date start, Date end, Date actual )
115    {
116        assertTrue( "Expected date between " + start + " and " + end + ", but was " + actual,
117                    start.before( actual ) && actual.before( end ) );
118    }
119
120    protected abstract void verifyResult( BlameScmResult result );
121}