001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.provider.git.command.changelog;
020
021import java.io.File;
022import java.util.ArrayList;
023import java.util.Arrays;
024import java.util.Collections;
025import java.util.List;
026
027import org.apache.maven.scm.ChangeSet;
028import org.apache.maven.scm.ScmFileSet;
029import org.apache.maven.scm.ScmRevision;
030import org.apache.maven.scm.command.changelog.ChangeLogScmRequest;
031import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
032import org.apache.maven.scm.command.checkout.CheckOutScmResult;
033import org.apache.maven.scm.provider.ScmProvider;
034import org.apache.maven.scm.provider.git.GitScmTestUtils;
035import org.apache.maven.scm.repository.ScmRepository;
036import org.apache.maven.scm.tck.command.changelog.ChangeLogCommandTckTest;
037import org.junit.Test;
038
039import static org.hamcrest.CoreMatchers.startsWith;
040import static org.hamcrest.MatcherAssert.assertThat;
041import static org.junit.Assert.assertEquals;
042
043/**
044 * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
045 */
046public abstract class GitChangeLogCommandTckTest extends ChangeLogCommandTckTest {
047    public static final long SLEEP_TIME_IN_MILLIS = 250L;
048
049    /** {@inheritDoc} */
050    public void initRepo() throws Exception {
051        GitScmTestUtils.initRepo("src/test/resources/linear-changelog/", getRepositoryRoot(), getWorkingCopy());
052    }
053
054    @Override
055    protected CheckOutScmResult checkOut(File workingDirectory, ScmRepository repository) throws Exception {
056        try {
057            return super.checkOut(workingDirectory, repository);
058        } finally {
059            GitScmTestUtils.setDefaulGitConfig(workingDirectory);
060        }
061    }
062
063    @Test
064    public void testChangeLogCommandFromHeadAncestorAndHead() throws Exception {
065        Thread.sleep(SLEEP_TIME_IN_MILLIS);
066        ScmRepository scmRepository = getScmRepository();
067        ScmProvider provider = getScmManager().getProviderByRepository(scmRepository);
068        ScmFileSet fileSet = new ScmFileSet(getWorkingCopy());
069
070        ChangeLogScmRequest clr = new ChangeLogScmRequest(scmRepository, fileSet);
071        String startVersion = "HEAD~1";
072        clr.setStartRevision(new ScmRevision(startVersion));
073        String endVersion = "HEAD";
074        clr.setEndRevision(new ScmRevision(endVersion));
075        ChangeLogScmResult changelogResult = provider.changeLog(clr);
076
077        List<ChangeSet> logEntries = changelogResult.getChangeLog().getChangeSets();
078        assertEquals(
079                String.format("changelog for %s..%s returned bad number of commits", startVersion, endVersion),
080                1,
081                logEntries.size());
082
083        assertThat("bad head commit SHA1 retrieved", logEntries.get(0).getRevision(), startsWith("464921b"));
084    }
085
086    @Test
087    public void testChangeLogCommandFromHeadToHead() throws Exception {
088        Thread.sleep(SLEEP_TIME_IN_MILLIS);
089        ScmRepository scmRepository = getScmRepository();
090        ScmProvider provider = getScmManager().getProviderByRepository(scmRepository);
091        ScmFileSet fileSet = new ScmFileSet(getWorkingCopy());
092
093        ChangeLogScmRequest clr = new ChangeLogScmRequest(scmRepository, fileSet);
094        String startVersion = "HEAD";
095        clr.setStartRevision(new ScmRevision(startVersion));
096        String endVersion = "HEAD";
097        clr.setEndRevision(new ScmRevision(endVersion));
098        ChangeLogScmResult changelogResult = provider.changeLog(clr);
099
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}