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