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        ScmTestCase.makeFile( getWorkingCopy(), "/readme.txt", "changed readme.txt" );
071        CheckInScmResult checkInResult = provider.checkIn( getScmRepository(), fileSet, COMMIT_MSG );
072        assertTrue( "Unable to checkin changes to the repository", checkInResult.isSuccess() );
073
074        result = manager.blame( repository, fileSet, "readme.txt" );
075
076        // pause a couple seconds...
077        Thread.sleep( 2000 );
078        Date timeAfterSecond = new Date(); // Current time
079
080        assertNotNull( "The command returned a null result.", result );
081        assertResultIsSuccess( result );
082
083        assertEquals( "Expected 1 line in blame", 1, result.getLines().size() );
084        line = result.getLines().get( 0 );
085
086        assertNotNull( "Expected not null author", line.getAuthor() );
087        assertNotNull( "Expected not null revision", line.getRevision() );
088        assertNotNull( "Expected not null date", line.getDate() );
089
090        assertTrue( "Expected another revision", !initialRevision.equals( line.getRevision() ) );
091        if ( isTestDateTime() )
092        {
093            assertDateBetween( timeBeforeSecond, timeAfterSecond, line.getDate() );
094        }
095
096        // === pom.xml ===
097        result = manager.blame( repository, fileSet, "pom.xml" );
098
099        assertNotNull( "The command returned a null result.", result );
100
101        assertResultIsSuccess( result );
102
103        verifyResult( result );
104    }
105
106    protected boolean isTestDateTime()
107    {
108        return true;
109    }
110
111    protected void assertDateBetween( Date start, Date end, Date actual )
112    {
113        assertTrue( "Expected date between " + start + " and " + end + ", but was " + actual,
114                    start.before( actual ) && actual.before( end ) );
115    }
116
117    protected abstract void verifyResult( BlameScmResult result );
118}