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.provider.git.command.changelog;
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.Collections;
25  import java.util.List;
26  
27  import org.apache.maven.scm.ChangeSet;
28  import org.apache.maven.scm.ScmFileSet;
29  import org.apache.maven.scm.ScmRevision;
30  import org.apache.maven.scm.command.changelog.ChangeLogScmRequest;
31  import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
32  import org.apache.maven.scm.command.checkout.CheckOutScmResult;
33  import org.apache.maven.scm.provider.ScmProvider;
34  import org.apache.maven.scm.provider.git.GitScmTestUtils;
35  import org.apache.maven.scm.repository.ScmRepository;
36  import org.apache.maven.scm.tck.command.changelog.ChangeLogCommandTckTest;
37  import org.junit.Test;
38  
39  import static org.hamcrest.CoreMatchers.startsWith;
40  import static org.hamcrest.MatcherAssert.assertThat;
41  import static org.junit.Assert.assertEquals;
42  
43  /**
44   * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
45   */
46  public abstract class GitChangeLogCommandTckTest extends ChangeLogCommandTckTest {
47      public static final long SLEEP_TIME_IN_MILLIS = 250L;
48  
49      /** {@inheritDoc} */
50      public void initRepo() throws Exception {
51          GitScmTestUtils.initRepo("src/test/resources/linear-changelog/", getRepositoryRoot(), getWorkingCopy());
52      }
53  
54      @Override
55      protected CheckOutScmResult checkOut(File workingDirectory, ScmRepository repository) throws Exception {
56          try {
57              return super.checkOut(workingDirectory, repository);
58          } finally {
59              GitScmTestUtils.setDefaulGitConfig(workingDirectory);
60          }
61      }
62  
63      @Test
64      public void testChangeLogCommandFromHeadAncestorAndHead() throws Exception {
65          Thread.sleep(SLEEP_TIME_IN_MILLIS);
66          ScmRepository scmRepository = getScmRepository();
67          ScmProvider provider = getScmManager().getProviderByRepository(scmRepository);
68          ScmFileSet fileSet = new ScmFileSet(getWorkingCopy());
69  
70          ChangeLogScmRequest clr = new ChangeLogScmRequest(scmRepository, fileSet);
71          String startVersion = "HEAD~1";
72          clr.setStartRevision(new ScmRevision(startVersion));
73          String endVersion = "HEAD";
74          clr.setEndRevision(new ScmRevision(endVersion));
75          ChangeLogScmResult changelogResult = provider.changeLog(clr);
76  
77          List<ChangeSet> logEntries = changelogResult.getChangeLog().getChangeSets();
78          assertEquals(
79                  String.format("changelog for %s..%s returned bad number of commits", startVersion, endVersion),
80                  1,
81                  logEntries.size());
82  
83          assertThat("bad head commit SHA1 retrieved", logEntries.get(0).getRevision(), startsWith("464921b"));
84      }
85  
86      @Test
87      public void testChangeLogCommandFromHeadToHead() throws Exception {
88          Thread.sleep(SLEEP_TIME_IN_MILLIS);
89          ScmRepository scmRepository = getScmRepository();
90          ScmProvider provider = getScmManager().getProviderByRepository(scmRepository);
91          ScmFileSet fileSet = new ScmFileSet(getWorkingCopy());
92  
93          ChangeLogScmRequest clr = new ChangeLogScmRequest(scmRepository, fileSet);
94          String startVersion = "HEAD";
95          clr.setStartRevision(new ScmRevision(startVersion));
96          String endVersion = "HEAD";
97          clr.setEndRevision(new ScmRevision(endVersion));
98          ChangeLogScmResult changelogResult = provider.changeLog(clr);
99  
100         List<ChangeSet> logEntries = changelogResult.getChangeLog().getChangeSets();
101         assertEquals(
102                 String.format("changelog for %s..%s returned bad number of commits", startVersion, endVersion),
103                 0,
104                 logEntries.size());
105     }
106 
107     @Test
108     public void testChangeLogCommandFromUndefinedToHead() throws Exception {
109         Thread.sleep(SLEEP_TIME_IN_MILLIS);
110         ScmRepository scmRepository = getScmRepository();
111         ScmProvider provider = getScmManager().getProviderByRepository(scmRepository);
112         ScmFileSet fileSet = new ScmFileSet(getWorkingCopy());
113 
114         ChangeLogScmRequest clr = new ChangeLogScmRequest(scmRepository, fileSet);
115         String endVersion = "HEAD";
116         clr.setEndRevision(new ScmRevision(endVersion));
117         ChangeLogScmResult changelogResult = provider.changeLog(clr);
118 
119         List<ChangeSet> logEntries = changelogResult.getChangeLog().getChangeSets();
120         assertEquals(
121                 String.format("changelog for ..%s returned bad number of commits", endVersion), 0, logEntries.size());
122     }
123 
124     @Test
125     public void testChangeLogCommandFromVersionToUndefined() throws Exception {
126         Thread.sleep(SLEEP_TIME_IN_MILLIS);
127         ScmRepository scmRepository = getScmRepository();
128         ScmProvider provider = getScmManager().getProviderByRepository(scmRepository);
129         ScmFileSet fileSet = new ScmFileSet(getWorkingCopy());
130 
131         ChangeLogScmRequest clr = new ChangeLogScmRequest(scmRepository, fileSet);
132         String startVersion = "e3864d9";
133         clr.setStartRevision(new ScmRevision(startVersion));
134         ChangeLogScmResult changelogResult = provider.changeLog(clr);
135 
136         List<ChangeSet> logEntries = changelogResult.getChangeLog().getChangeSets();
137         assertEquals(
138                 String.format("changelog for %s.. returned bad number of commits", startVersion), 2, logEntries.size());
139 
140         assertThat("bad commit SHA1 retrieved", logEntries.get(0).getRevision(), startsWith("464921b"));
141         assertThat("bad commit SHA1 retrieved", logEntries.get(1).getRevision(), startsWith("db46d63"));
142     }
143 
144     @Test
145     public void testChangeLogCommandFromVoneToVtwo() throws Exception {
146         Thread.sleep(SLEEP_TIME_IN_MILLIS);
147         ScmRepository scmRepository = getScmRepository();
148         ScmProvider provider = getScmManager().getProviderByRepository(scmRepository);
149         ScmFileSet fileSet = new ScmFileSet(getWorkingCopy());
150 
151         ChangeLogScmRequest clr = new ChangeLogScmRequest(scmRepository, fileSet);
152         String startVersion = "0f1e817";
153         clr.setStartRevision(new ScmRevision(startVersion));
154         String endVersion = "db46d63";
155         clr.setEndRevision(new ScmRevision(endVersion));
156         ChangeLogScmResult changelogResult = provider.changeLog(clr);
157 
158         List<ChangeSet> logEntries = changelogResult.getChangeLog().getChangeSets();
159         assertEquals(
160                 String.format("changelog for %s.. returned bad number of commits", startVersion), 2, logEntries.size());
161 
162         assertThat("bad commit SHA1 retrieved", logEntries.get(0).getRevision(), startsWith("db46d63"));
163         assertThat("bad commit SHA1 retrieved", logEntries.get(1).getRevision(), startsWith("e3864d9"));
164     }
165 
166     @Test
167     public void testChangeLogCommandWithStartEndInBadOrder() throws Exception {
168         Thread.sleep(SLEEP_TIME_IN_MILLIS);
169         ScmRepository scmRepository = getScmRepository();
170         ScmProvider provider = getScmManager().getProviderByRepository(scmRepository);
171         ScmFileSet fileSet = new ScmFileSet(getWorkingCopy());
172 
173         ChangeLogScmRequest clr = new ChangeLogScmRequest(scmRepository, fileSet);
174         String startVersion = "db46d63";
175         clr.setStartRevision(new ScmRevision(startVersion));
176         String endVersion = "0f1e817";
177         clr.setEndRevision(new ScmRevision(endVersion));
178         ChangeLogScmResult changelogResult = provider.changeLog(clr);
179 
180         List<ChangeSet> logEntries = changelogResult.getChangeLog().getChangeSets();
181         assertEquals(
182                 String.format("changelog for %s..%s should return no commits", startVersion, endVersion),
183                 0,
184                 logEntries.size());
185     }
186 
187     @Test
188     public void testChangeLogCommandFromHeadToStartOfRepository() throws Exception {
189         Thread.sleep(SLEEP_TIME_IN_MILLIS);
190         ScmRepository scmRepository = getScmRepository();
191         ScmProvider provider = getScmManager().getProviderByRepository(scmRepository);
192         ScmFileSet fileSet = new ScmFileSet(getWorkingCopy());
193 
194         ChangeLogScmRequest clr = new ChangeLogScmRequest(scmRepository, fileSet);
195         String version = "HEAD";
196         clr.setRevision(new ScmRevision(version));
197         ChangeLogScmResult changelogResult = provider.changeLog(clr);
198 
199         List<ChangeSet> logEntries = changelogResult.getChangeLog().getChangeSets();
200         assertEquals(String.format("changelog for %s returned bad number of commits", version), 5, logEntries.size());
201     }
202 
203     @Test
204     public void testChangeLogCommandFromVersionToStartOfRepository() throws Exception {
205         Thread.sleep(SLEEP_TIME_IN_MILLIS);
206         ScmRepository scmRepository = getScmRepository();
207         ScmProvider provider = getScmManager().getProviderByRepository(scmRepository);
208         ScmFileSet fileSet = new ScmFileSet(getWorkingCopy());
209 
210         ChangeLogScmRequest clr = new ChangeLogScmRequest(scmRepository, fileSet);
211         String version = "db46d63";
212         clr.setRevision(new ScmRevision(version));
213         ChangeLogScmResult changelogResult = provider.changeLog(clr);
214 
215         List<ChangeSet> logEntries = changelogResult.getChangeLog().getChangeSets();
216         assertEquals(String.format("changelog for %s returned bad number of commits", version), 4, logEntries.size());
217 
218         assertThat("bad commit SHA1 retrieved", logEntries.get(0).getRevision(), startsWith("db46d63"));
219         assertThat("bad commit SHA1 retrieved", logEntries.get(1).getRevision(), startsWith("e3864d9"));
220         assertThat("bad commit SHA1 retrieved", logEntries.get(2).getRevision(), startsWith("0f1e817"));
221         assertThat("bad commit SHA1 retrieved", logEntries.get(3).getRevision(), startsWith("e75cb5a"));
222 
223         List<String> tags4 = Arrays.asList("Tag4a", "Tag4b");
224         List<String> tags2 = Collections.singletonList("Tag2");
225         List<String> noTags = Collections.emptyList();
226 
227         assertEquals("Incorrect tags found", tags4, sorted(logEntries.get(0).getTags()));
228         assertEquals("Incorrect tags found", noTags, sorted(logEntries.get(1).getTags()));
229         assertEquals("Incorrect tags found", tags2, sorted(logEntries.get(2).getTags()));
230         assertEquals("Incorrect tags found", noTags, sorted(logEntries.get(3).getTags()));
231     }
232 
233     private List<String> sorted(List<String> input) {
234         List<String> result = new ArrayList<>(input);
235         Collections.sort(result);
236         return result;
237     }
238 }