View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.tck.command.blame;
20  
21  import java.util.Date;
22  
23  import org.apache.maven.scm.ScmFileSet;
24  import org.apache.maven.scm.ScmTckTestCase;
25  import org.apache.maven.scm.ScmTestCase;
26  import org.apache.maven.scm.command.blame.BlameLine;
27  import org.apache.maven.scm.command.blame.BlameScmRequest;
28  import org.apache.maven.scm.command.blame.BlameScmResult;
29  import org.apache.maven.scm.command.checkin.CheckInScmResult;
30  import org.apache.maven.scm.manager.ScmManager;
31  import org.apache.maven.scm.provider.ScmProvider;
32  import org.apache.maven.scm.repository.ScmRepository;
33  import org.junit.Test;
34  
35  import static org.junit.Assert.assertEquals;
36  import static org.junit.Assert.assertNotEquals;
37  import static org.junit.Assert.assertNotNull;
38  import static org.junit.Assert.assertTrue;
39  
40  /**
41   * @author Evgeny Mandrikov
42   */
43  public abstract class BlameCommandTckTest extends ScmTckTestCase {
44      private static final String COMMIT_MSG = "Second changelog";
45  
46      @Test
47      public void testBlameCommand() throws Exception {
48          ScmRepository repository = getScmRepository();
49          ScmManager manager = getScmManager();
50          ScmProvider provider = manager.getProviderByRepository(getScmRepository());
51          ScmFileSet fileSet = new ScmFileSet(getWorkingCopy());
52  
53          BlameScmResult result;
54          BlameLine line;
55  
56          // === readme.txt ===
57          BlameScmRequest blameScmRequest = new BlameScmRequest(repository, fileSet);
58          blameScmRequest.setFilename("readme.txt");
59          // result = manager.blame( repository, fileSet, "readme.txt" );
60          result = manager.blame(blameScmRequest);
61          assertNotNull("The command returned a null result.", result);
62          assertResultIsSuccess(result);
63          assertEquals("Expected 1 line in blame", 1, result.getLines().size());
64          line = result.getLines().get(0);
65          String initialRevision = line.getRevision();
66  
67          // Make a timestamp that we know are after initial revision but before the second
68          Date timeBeforeSecond = new Date(); // Current time
69          // pause a couple seconds...
70          Thread.sleep(2000);
71          // Make a change to the readme.txt and commit the change
72          this.edit(getWorkingCopy(), "readme.txt", null, getScmRepository());
73          ScmTestCase.makeFile(getWorkingCopy(), "/readme.txt", "changed readme.txt");
74          CheckInScmResult checkInResult = provider.checkIn(getScmRepository(), fileSet, COMMIT_MSG);
75          assertTrue("Unable to checkin changes to the repository", checkInResult.isSuccess());
76  
77          result = manager.blame(repository, fileSet, "readme.txt");
78  
79          // pause a couple seconds...
80          Thread.sleep(2000);
81          Date timeAfterSecond = new Date(); // Current time
82  
83          assertNotNull("The command returned a null result.", result);
84          assertResultIsSuccess(result);
85  
86          assertEquals("Expected 1 line in blame", 1, result.getLines().size());
87          line = result.getLines().get(0);
88  
89          assertNotNull("Expected not null author", line.getAuthor());
90          assertNotNull("Expected not null revision", line.getRevision());
91          assertNotNull("Expected not null date", line.getDate());
92  
93          assertNotEquals("Expected another revision", initialRevision, line.getRevision());
94          if (isTestDateTime()) {
95              assertDateBetween(timeBeforeSecond, timeAfterSecond, line.getDate());
96          }
97  
98          // === pom.xml ===
99          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 }