Skip to content

Commit 6460429

Browse files
committed
getLastTag() fixed
refactor tests fixed
1 parent 06ad2e3 commit 6460429

File tree

2 files changed

+125
-58
lines changed

2 files changed

+125
-58
lines changed

src/main/java/org/scm4j/vcs/GitVCS.java

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import org.eclipse.jgit.diff.DiffFormatter;
3636
import org.eclipse.jgit.lib.Constants;
3737
import org.eclipse.jgit.lib.ObjectId;
38-
import org.eclipse.jgit.lib.ObjectIdRef.Unpeeled;
3938
import org.eclipse.jgit.lib.ObjectReader;
4039
import org.eclipse.jgit.lib.Ref;
4140
import org.eclipse.jgit.lib.Repository;
@@ -45,7 +44,6 @@
4544
import org.eclipse.jgit.revwalk.RevWalk;
4645
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
4746
import org.eclipse.jgit.transport.CredentialsProvider;
48-
import org.eclipse.jgit.transport.PushResult;
4947
import org.eclipse.jgit.transport.RefSpec;
5048
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
5149
import org.eclipse.jgit.treewalk.CanonicalTreeParser;
@@ -155,36 +153,37 @@ public void deleteBranch(String branchName, String commitMessage) {
155153
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy();
156154
Git git = getLocalGit(wc);
157155
Repository gitRepo = git.getRepository()) {
158-
try {
156+
//try {
159157

160158
checkout(git, gitRepo, MASTER_BRANCH_NAME);
161159

162-
git
163-
.branchDelete()
164-
.setBranchNames(branchName)
160+
git
161+
.branchDelete()
162+
.setBranchNames(branchName)
165163
.setForce(true) // avoid "not merged" exception
166-
.call();
167-
168-
RefSpec refSpec = new RefSpec( ":refs/heads/" + branchName);
164+
.call();
165+
166+
RefSpec refSpec = new RefSpec( ":refs/heads/" + branchName);
167+
169168
push(git, refSpec);
170-
} finally {
171-
git.getRepository().close();
172-
}
169+
// } finally {
170+
// git.getRepository().close();
171+
// }
173172
} catch (GitAPIException e) {
174173
throw new EVCSException(e);
175174
} catch (Exception e) {
176175
throw new RuntimeException(e);
177176
}
178177
}
179178

180-
private Iterable<PushResult> push(Git git, RefSpec refSpec) throws GitAPIException {
179+
private void push(Git git, RefSpec refSpec) throws GitAPIException {
181180
PushCommand cmd = git
182181
.push()
183182
.setPushAll();
184183
if (refSpec != null) {
185184
cmd.setRefSpecs(refSpec);
186185
}
187-
return cmd
186+
cmd
188187
.setRemote("origin")
189188
.setCredentialsProvider(credentials)
190189
.call();
@@ -347,7 +346,7 @@ public VCSCommit setFileContent(String branchName, String filePath, String conte
347346
}
348347
}
349348

350-
private void checkout(Git git, Repository gitRepo, String branchName) throws Exception {
349+
void checkout(Git git, Repository gitRepo, String branchName) throws Exception {
351350
String bn = getRealBranchName(branchName);
352351
git
353352
.pull()
@@ -684,17 +683,6 @@ public List<VCSTag> getTags() {
684683
Repository gitRepo = git.getRepository();
685684
RevWalk rw = new RevWalk(gitRepo)) {
686685

687-
// git
688-
// .checkout()
689-
// .setCreateBranch(gitRepo.exactRef("refs/heads/master") == null)
690-
// .setName("master")
691-
// .call();
692-
//
693-
// git
694-
// .pull()
695-
// .setCredentialsProvider(credentials)
696-
// .call();
697-
698686
List<Ref> tagRefs = getTagRefs();
699687
List<VCSTag> res = new ArrayList<>();
700688
RevTag revTag;
@@ -713,7 +701,7 @@ public List<VCSTag> getTags() {
713701

714702
}
715703

716-
private List<Ref> getTagRefs() throws Exception {
704+
List<Ref> getTagRefs() throws Exception {
717705
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy();
718706
Git git = getLocalGit(wc);
719707
Repository gitRepo = git.getRepository()) {
@@ -725,13 +713,6 @@ private List<Ref> getTagRefs() throws Exception {
725713
.call();
726714

727715
return refs;
728-
//return new ArrayList<>(Git
729-
//.lsRemoteRepository()
730-
//.setTags(true)
731-
//.setHeads(false)
732-
//.setRemote(repo.getRepoUrl())
733-
//.setCredentialsProvider(credentials)
734-
//.call());
735716
}
736717
}
737718

@@ -754,7 +735,7 @@ public VCSTag getLastTag() {
754735
Ref ref = tagRefs.get(tagRefs.size() - 1);
755736
RevCommit revCommit = rw.parseCommit(ref.getObjectId());
756737
VCSCommit relatedCommit = new VCSCommit(revCommit.getName(), revCommit.getFullMessage(), revCommit.getAuthorIdent().getName());
757-
if (ref instanceof Unpeeled) {
738+
if (git.getRepository().peel(ref).getPeeledObjectId() == null) {
758739
return new VCSTag(ref.getName().replace("refs/tags/", ""), null, null, relatedCommit);
759740
}
760741
RevTag revTag = rw.parseTag(ref.getObjectId());

src/test/java/org/scm4j/vcs/GitVCSTest.java

Lines changed: 109 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,63 @@
11
package org.scm4j.vcs;
22

3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertNull;
6+
import static org.junit.Assert.assertTrue;
7+
import static org.junit.Assert.fail;
8+
9+
import java.io.File;
10+
import java.io.IOException;
11+
import java.lang.reflect.InvocationTargetException;
12+
import java.lang.reflect.Method;
13+
import java.net.Authenticator;
14+
import java.net.InetAddress;
15+
import java.net.InetSocketAddress;
16+
import java.net.PasswordAuthentication;
17+
import java.net.Proxy;
18+
import java.net.ProxySelector;
19+
import java.net.SocketAddress;
20+
import java.net.URI;
21+
import java.net.URISyntaxException;
22+
import java.nio.charset.IllegalCharsetNameException;
23+
import java.util.ArrayList;
24+
import java.util.List;
25+
326
import org.apache.commons.io.FileUtils;
427
import org.eclipse.jgit.api.Git;
528
import org.eclipse.jgit.api.errors.GitAPIException;
629
import org.eclipse.jgit.diff.DiffEntry;
30+
import org.eclipse.jgit.lib.Ref;
731
import org.eclipse.jgit.lib.Repository;
32+
import org.eclipse.jgit.revwalk.RevWalk;
833
import org.eclipse.jgit.transport.CredentialItem;
34+
import org.eclipse.jgit.transport.RefSpec;
935
import org.junit.After;
1036
import org.junit.Test;
1137
import org.mockito.Mockito;
1238
import org.mockito.exceptions.verification.WantedButNotInvoked;
1339
import org.scm4j.vcs.api.IVCS;
1440
import org.scm4j.vcs.api.VCSChangeType;
41+
import org.scm4j.vcs.api.VCSTag;
1542
import org.scm4j.vcs.api.abstracttest.VCSAbstractTest;
43+
import org.scm4j.vcs.api.workingcopy.IVCSLockedWorkingCopy;
1644
import org.scm4j.vcs.api.workingcopy.IVCSRepositoryWorkspace;
1745

18-
import java.io.File;
19-
import java.io.IOException;
20-
import java.lang.reflect.InvocationTargetException;
21-
import java.lang.reflect.Method;
22-
import java.net.*;
23-
import java.nio.charset.IllegalCharsetNameException;
24-
import java.util.List;
25-
26-
import static org.junit.Assert.*;
27-
2846
public class GitVCSTest extends VCSAbstractTest {
2947

3048
private Repository localGitRepo;
3149
private ProxySelector proxySelectorBackup;
3250
private final RuntimeException testGitResetException = new RuntimeException("test exception on git.reset()");
51+
private GitVCS git;
3352

3453
@Override
3554
public void setUp() throws Exception {
3655
super.setUp();
37-
Git git = GitVCSUtils.createRepository(new File(localVCSWorkspace.getHomeFolder(), repoName));
38-
localGitRepo = git.getRepository();
56+
Git fileGitRepo = GitVCSUtils.createRepository(new File(localVCSWorkspace.getHomeFolder(), repoName));
57+
localGitRepo = fileGitRepo.getRepository();
3958
proxySelectorBackup = ProxySelector.getDefault();
4059
ProxySelector.setDefault(null);
60+
git = (GitVCS) vcs;
4161
}
4262

4363
@After
@@ -62,10 +82,10 @@ protected void setMakeFailureOnVCSReset(Boolean doMakeFailure) throws Exception
6282
Git mockedGit;
6383
if (doMakeFailure) {
6484
mockedGit = Mockito.spy(((GitVCS) vcs).getLocalGit(mockedLWC));
65-
Mockito.doReturn(mockedGit).when((GitVCS) vcs).getLocalGit(mockedLWC);
85+
Mockito.doReturn(mockedGit).when(((GitVCS) vcs)).getLocalGit(mockedLWC);
6686
Mockito.doThrow(testGitResetException).when(mockedGit).reset();
6787
} else {
68-
Mockito.doCallRealMethod().when((GitVCS) vcs).getLocalGit(mockedLWC);
88+
Mockito.doCallRealMethod().when(((GitVCS) vcs)).getLocalGit(mockedLWC);
6989
mockedGit = null;
7090
}
7191
}
@@ -80,7 +100,7 @@ public void testSetCredentials() {
80100
vcs.setCredentials("user", "password");
81101
CredentialItem.Username u = new CredentialItem.Username();
82102
CredentialItem.Password p = new CredentialItem.Password();
83-
assertTrue(((GitVCS) vcs).getCredentials().get(null, u, p));
103+
assertTrue(git.getCredentials().get(null, u, p));
84104
assertEquals(u.getValue(), "user");
85105
assertEquals(new String(p.getValue()), "password");
86106
}
@@ -183,30 +203,37 @@ public void testExceptions() throws Exception {
183203
testExceptionThrowing(eCommon, m, params);
184204
}
185205
}
186-
187-
private void testExceptionThrowing(Exception testException, Method m, Object[] params) throws Exception {
188-
Mockito.reset((GitVCS) vcs);
189-
Mockito.doThrow(testException).when((GitVCS) vcs).getLocalGit(mockedLWC);
206+
207+
private void testExceptionThrowingNoMock(Exception testException, Method m, Object[] params) throws Exception {
190208
try {
191209
m.invoke(vcs, params);
192210
if (wasGetLocalGitInvoked(vcs)) {
193211
fail();
194212
}
195213
} catch (InvocationTargetException e) {
196214
if (wasGetLocalGitInvoked(vcs)) {
197-
assertTrue(e.getCause() instanceof RuntimeException);
215+
// InvocationTargetException <- EVCSException <- GitAPIException
216+
assertTrue(e.getCause().getCause().getClass().isAssignableFrom(testException.getClass()));
198217
assertTrue(e.getCause().getMessage().contains(testException.getMessage()));
199218
}
200219
} catch (Exception e) {
201220
if (wasGetLocalGitInvoked(vcs)) {
202221
fail();
203222
}
204223
}
224+
225+
}
226+
227+
private void testExceptionThrowing(Exception testException, Method m, Object[] params) throws Exception {
228+
Mockito.reset(git);
229+
Mockito.doThrow(testException).when(git).getLocalGit(mockedLWC);
230+
testExceptionThrowingNoMock(testException, m, params);
231+
205232
}
206233

207234
private Boolean wasGetLocalGitInvoked(IVCS vcs) throws Exception {
208235
try {
209-
Mockito.verify((GitVCS) vcs).getLocalGit(mockedLWC);
236+
Mockito.verify(git).getLocalGit(mockedLWC);
210237
return true;
211238
} catch (WantedButNotInvoked e1) {
212239
return false;
@@ -217,7 +244,7 @@ private Boolean wasGetLocalGitInvoked(IVCS vcs) throws Exception {
217244
public void testDefaultChangeTypeToVCSType() {
218245
for (DiffEntry.ChangeType ct : DiffEntry.ChangeType.values()) {
219246
if (ct != DiffEntry.ChangeType.ADD && ct != DiffEntry.ChangeType.DELETE && ct != DiffEntry.ChangeType.MODIFY) {
220-
assertEquals(((GitVCS) vcs).gitChangeTypeToVCSChangeType(ct), VCSChangeType.UNKNOWN);
247+
assertEquals(git.gitChangeTypeToVCSChangeType(ct), VCSChangeType.UNKNOWN);
221248
}
222249
}
223250
}
@@ -239,6 +266,65 @@ public void testGetFileContentExceptions() {
239266
public void testGitVCSUtilsCreation() {
240267
assertNotNull(new GitVCSUtils());
241268
}
242-
269+
270+
@Test
271+
public void testGetLastTagEmptyTagRefsList() throws Exception {
272+
Mockito.doReturn(new ArrayList<Ref>()).when(git).getTagRefs();
273+
assertNull(vcs.getLastTag());
274+
}
275+
276+
@Test
277+
public void testGetLastTagUnannotatedTag() throws Exception {
278+
createUnannotatedTag(null, TAG_NAME_1);
279+
VCSTag tag = vcs.getLastTag();
280+
assertNull(tag.getAuthor());
281+
assertNull(tag.getTagMessage());
282+
assertEquals(tag.getTagName(), TAG_NAME_1);
283+
assertEquals(tag.getRelatedCommit(), vcs.getHeadCommit(null));
284+
}
285+
286+
public void createUnannotatedTag(String branchName, String tagName) throws Exception {
287+
try (IVCSLockedWorkingCopy wc = localVCSRepo.getVCSLockedWorkingCopy();
288+
Git localGit = git.getLocalGit(wc);
289+
Repository gitRepo = localGit.getRepository();
290+
RevWalk rw = new RevWalk(gitRepo)) {
291+
292+
git.checkout(localGit, gitRepo, branchName);
293+
294+
Ref ref = localGit
295+
.tag()
296+
.setAnnotated(false)
297+
.setName(tagName)
298+
.setObjectId(null)
299+
.call();
300+
301+
localGit
302+
.push()
303+
.setPushAll()
304+
.setRefSpecs(new RefSpec(ref.getName()))
305+
.setRemote("origin")
306+
.setCredentialsProvider(git.getCredentials())
307+
.call();
308+
}
309+
}
310+
311+
@Test
312+
public void testGetLastTagExceptions() throws Exception {
313+
Ref dummyRef = Mockito.mock(Ref.class);
314+
List<Ref> refList = new ArrayList<>();
315+
refList.add(dummyRef);
316+
Mockito.doReturn(refList).when(git).getTagRefs();
317+
@SuppressWarnings("serial")
318+
GitAPIException eApi = new GitAPIException("test git exception") {};
319+
Exception eCommon = new Exception("test common exception");
320+
//Mockito.reset(git);
321+
Mockito.doThrow(eApi).when(git).getLocalGit(mockedLWC);
322+
testExceptionThrowingNoMock(eApi, vcs.getClass().getDeclaredMethod("getLastTag"), new Object[0]);
323+
324+
Mockito.reset(git);
325+
Mockito.doReturn(refList).when(git).getTagRefs();
326+
Mockito.doThrow(eCommon).when(git).getLocalGit(mockedLWC);
327+
testExceptionThrowingNoMock(eCommon, vcs.getClass().getDeclaredMethod("getLastTag"), new Object[0]);
328+
}
243329
}
244330

0 commit comments

Comments
 (0)