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