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