View Javadoc
1   package org.apache.maven.scm.tck.command.changelog;
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.ChangeSet;
23  import org.apache.maven.scm.ScmBranch;
24  import org.apache.maven.scm.ScmFileSet;
25  import org.apache.maven.scm.ScmTagParameters;
26  import org.apache.maven.scm.ScmTckTestCase;
27  import org.apache.maven.scm.ScmTestCase;
28  import org.apache.maven.scm.command.changelog.ChangeLogScmRequest;
29  import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
30  import org.apache.maven.scm.command.checkin.CheckInScmResult;
31  import org.apache.maven.scm.command.tag.TagScmResult;
32  import org.apache.maven.scm.provider.ScmProvider;
33  import org.junit.Test;
34  
35  import java.util.Collections;
36  import java.util.Date;
37  import java.util.List;
38  
39  import static org.junit.Assert.assertEquals;
40  import static org.junit.Assert.assertTrue;
41  
42  /**
43   * Test Changlog command. <br>
44   * 1. Get initial log <br>
45   * 2. Add one revision <br>
46   * 3. Get the two logs <br>
47   * 4. Get the last log based on date <br>
48   * 5. Test last log for date and comment <br>
49   *
50   * @author <a href="mailto:torbjorn@smorgrav.org">Torbj�rn Eikli Sm�rgrav</a>
51   */
52  public abstract class ChangeLogCommandTckTest
53      extends ScmTckTestCase
54  {
55      private static final String COMMIT_MSG = "Second changelog";
56      private static final String COMMIT_TAG = "v3.14";
57  
58      /**
59       * In some SCMs (Hg) adding a tag creates an extra commit which offsets the expectations.
60       * @return If an extra commit will be present for a tag.
61       */
62      public boolean isTagAnExtraCommit()
63      {
64          return false;
65      }
66  
67      @Test
68      public void testChangeLogCommand()
69          throws Exception
70      {
71          Thread.sleep( 1000 );
72          ScmProvider provider = getScmManager().getProviderByRepository( getScmRepository() );
73          ScmFileSet fileSet = new ScmFileSet( getWorkingCopy() );
74  
75          ChangeLogScmResult firstResult =
76              provider.changeLog( getScmRepository(), fileSet, null, null, 0, (ScmBranch) null, null );
77          assertTrue( firstResult.getProviderMessage() + ": " + firstResult.getCommandLine() + "\n"
78                          + firstResult.getCommandOutput(), firstResult.isSuccess() );
79  
80          // for svn and git the repo get recreated for each test and therefore initial changelog size is 1
81          int firstLogSize = firstResult.getChangeLog().getChangeSets().size();
82          assertTrue( "Unexpected initial log size", firstLogSize >= 1 );
83  
84          // Make a timestamp that we know are after initial revision but before the second
85          Date timeBeforeSecond = new Date(); //Current time
86  
87          // pause a couple seconds... [SCM-244]
88          Thread.sleep( 2000 );
89  
90          //Make a change to the readme.txt and commit the change
91          this.edit( getWorkingCopy(), "readme.txt", null, getScmRepository() );
92          ScmTestCase.makeFile( getWorkingCopy(), "/readme.txt", "changed readme.txt" );
93          CheckInScmResult checkInResult = provider.checkIn( getScmRepository(), fileSet, COMMIT_MSG );
94          assertTrue( "Unable to checkin changes to the repository", checkInResult.isSuccess() );
95  
96          ScmTagParameters scmTagParameters = new ScmTagParameters();
97          TagScmResult tagResult = provider.tag( getScmRepository(), fileSet, COMMIT_TAG, scmTagParameters );
98          assertTrue( "Unable to tag the changes in the repository", tagResult.isSuccess() );
99  
100         ChangeLogScmRequest changeLogScmRequest = new ChangeLogScmRequest( getScmRepository(), fileSet );
101         ChangeLogScmResult secondResult = provider.changeLog( changeLogScmRequest );
102         assertTrue( secondResult.getProviderMessage(), secondResult.isSuccess() );
103 
104         List<ChangeSet> changeSets = secondResult.getChangeLog().getChangeSets();
105 
106         int expectedChangeSets = firstLogSize + 1;
107         boolean lastCommitIsCausedByTagging = false;
108         int lastCodeCommitIndex = 0;
109 
110         if ( isTagAnExtraCommit() )
111         {
112             // This is for example Mercurial which creates an extra commit after tagging.
113             lastCommitIsCausedByTagging = true;
114             expectedChangeSets += 1;
115             lastCodeCommitIndex = 1;
116         }
117 
118         assertEquals( expectedChangeSets, changeSets.size() );
119 
120         // Check if the tag has been retrieved again
121         ChangeSet changeSetWithTag = changeSets.get( lastCodeCommitIndex );
122         assertEquals( Collections.singletonList( COMMIT_TAG ), changeSetWithTag.getTags() );
123 
124         //Now only retrieve the changelog after timeBeforeSecondChangeLog
125         Date currentTime = new Date();
126         changeLogScmRequest = new ChangeLogScmRequest( getScmRepository(), fileSet );
127         changeLogScmRequest.setStartDate( timeBeforeSecond );
128         changeLogScmRequest.setEndDate( currentTime );
129         changeLogScmRequest.setScmBranch( new ScmBranch( "" ) );
130         ChangeLogScmResult thirdResult = provider.changeLog( changeLogScmRequest );
131 
132         //Thorough assert of the last result
133         assertTrue( thirdResult.getProviderMessage(), thirdResult.isSuccess() );
134 
135         List<ChangeSet> thirdChangeSets = thirdResult.getChangeLog().getChangeSets();
136         assertEquals( lastCommitIsCausedByTagging ? 2 : 1, thirdChangeSets.size() );
137         ChangeSet changeset = thirdChangeSets.get( lastCodeCommitIndex );
138         assertTrue( changeset.getDate().after( timeBeforeSecond ) );
139         assertTrue( changeset.getComment().startsWith( COMMIT_MSG ) );
140     }
141 }