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