001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.tck.command.blame;
020
021import java.util.Date;
022
023import org.apache.maven.scm.ScmFileSet;
024import org.apache.maven.scm.ScmTckTestCase;
025import org.apache.maven.scm.ScmTestCase;
026import org.apache.maven.scm.command.blame.BlameLine;
027import org.apache.maven.scm.command.blame.BlameScmRequest;
028import org.apache.maven.scm.command.blame.BlameScmResult;
029import org.apache.maven.scm.command.checkin.CheckInScmResult;
030import org.apache.maven.scm.manager.ScmManager;
031import org.apache.maven.scm.provider.ScmProvider;
032import org.apache.maven.scm.repository.ScmRepository;
033import org.junit.Test;
034
035import static org.junit.Assert.assertEquals;
036import static org.junit.Assert.assertNotEquals;
037import static org.junit.Assert.assertNotNull;
038import static org.junit.Assert.assertTrue;
039
040/**
041 * @author Evgeny Mandrikov
042 */
043public abstract class BlameCommandTckTest extends ScmTckTestCase {
044    private static final String COMMIT_MSG = "Second changelog";
045
046    @Test
047    public void testBlameCommand() throws Exception {
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            assertDateBetween(timeBeforeSecond, timeAfterSecond, line.getDate());
096        }
097
098        // === pom.xml ===
099        result = manager.blame(repository, fileSet, "pom.xml");
100
101        assertNotNull("The command returned a null result.", result);
102
103        assertResultIsSuccess(result);
104
105        verifyResult(result);
106    }
107
108    protected boolean isTestDateTime() {
109        return true;
110    }
111
112    protected void assertDateBetween(Date start, Date end, Date actual) {
113        assertTrue(
114                "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}