4
4
import org .eclipse .jgit .api .Git ;
5
5
import org .eclipse .jgit .api .errors .GitAPIException ;
6
6
import org .eclipse .jgit .diff .DiffEntry ;
7
+ import org .eclipse .jgit .lib .Ref ;
7
8
import org .eclipse .jgit .lib .Repository ;
9
+ import org .eclipse .jgit .revwalk .RevWalk ;
8
10
import org .eclipse .jgit .transport .CredentialItem ;
11
+ import org .eclipse .jgit .transport .RefSpec ;
9
12
import org .junit .After ;
10
13
import org .junit .Test ;
11
14
import org .mockito .Mockito ;
12
15
import org .mockito .exceptions .verification .WantedButNotInvoked ;
13
16
import org .scm4j .vcs .api .IVCS ;
14
17
import org .scm4j .vcs .api .VCSChangeType ;
18
+ import org .scm4j .vcs .api .VCSTag ;
15
19
import org .scm4j .vcs .api .abstracttest .VCSAbstractTest ;
20
+ import org .scm4j .vcs .api .workingcopy .IVCSLockedWorkingCopy ;
16
21
import org .scm4j .vcs .api .workingcopy .IVCSRepositoryWorkspace ;
17
22
18
23
import java .io .File ;
21
26
import java .lang .reflect .Method ;
22
27
import java .net .*;
23
28
import java .nio .charset .IllegalCharsetNameException ;
29
+ import java .util .ArrayList ;
24
30
import java .util .List ;
25
31
26
32
import static org .junit .Assert .*;
@@ -30,14 +36,16 @@ public class GitVCSTest extends VCSAbstractTest {
30
36
private Repository localGitRepo ;
31
37
private ProxySelector proxySelectorBackup ;
32
38
private final RuntimeException testGitResetException = new RuntimeException ("test exception on git.reset()" );
39
+ private GitVCS git ;
33
40
34
41
@ Override
35
42
public void setUp () throws Exception {
36
43
super .setUp ();
37
- Git git = GitVCSUtils .createRepository (new File (localVCSWorkspace .getHomeFolder (), repoName ));
38
- localGitRepo = git .getRepository ();
44
+ Git fileGitRepo = GitVCSUtils .createRepository (new File (localVCSWorkspace .getHomeFolder (), repoName ));
45
+ localGitRepo = fileGitRepo .getRepository ();
39
46
proxySelectorBackup = ProxySelector .getDefault ();
40
47
ProxySelector .setDefault (null );
48
+ git = (GitVCS ) vcs ;
41
49
}
42
50
43
51
@ After
@@ -62,10 +70,10 @@ protected void setMakeFailureOnVCSReset(Boolean doMakeFailure) throws Exception
62
70
Git mockedGit ;
63
71
if (doMakeFailure ) {
64
72
mockedGit = Mockito .spy (((GitVCS ) vcs ).getLocalGit (mockedLWC ));
65
- Mockito .doReturn (mockedGit ).when ((GitVCS ) vcs ).getLocalGit (mockedLWC );
73
+ Mockito .doReturn (mockedGit ).when ((( GitVCS ) vcs ) ).getLocalGit (mockedLWC );
66
74
Mockito .doThrow (testGitResetException ).when (mockedGit ).reset ();
67
75
} else {
68
- Mockito .doCallRealMethod ().when ((GitVCS ) vcs ).getLocalGit (mockedLWC );
76
+ Mockito .doCallRealMethod ().when ((( GitVCS ) vcs ) ).getLocalGit (mockedLWC );
69
77
mockedGit = null ;
70
78
}
71
79
}
@@ -80,7 +88,7 @@ public void testSetCredentials() {
80
88
vcs .setCredentials ("user" , "password" );
81
89
CredentialItem .Username u = new CredentialItem .Username ();
82
90
CredentialItem .Password p = new CredentialItem .Password ();
83
- assertTrue ((( GitVCS ) vcs ) .getCredentials ().get (null , u , p ));
91
+ assertTrue (git .getCredentials ().get (null , u , p ));
84
92
assertEquals (u .getValue (), "user" );
85
93
assertEquals (new String (p .getValue ()), "password" );
86
94
}
@@ -183,30 +191,37 @@ public void testExceptions() throws Exception {
183
191
testExceptionThrowing (eCommon , m , params );
184
192
}
185
193
}
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 );
194
+
195
+ private void testExceptionThrowingNoMock (Exception testException , Method m , Object [] params ) throws Exception {
190
196
try {
191
197
m .invoke (vcs , params );
192
198
if (wasGetLocalGitInvoked (vcs )) {
193
199
fail ();
194
200
}
195
201
} catch (InvocationTargetException e ) {
196
202
if (wasGetLocalGitInvoked (vcs )) {
197
- assertTrue (e .getCause () instanceof RuntimeException );
203
+ // InvocationTargetException <- EVCSException <- GitAPIException
204
+ assertTrue (e .getCause ().getCause ().getClass ().isAssignableFrom (testException .getClass ()));
198
205
assertTrue (e .getCause ().getMessage ().contains (testException .getMessage ()));
199
206
}
200
207
} catch (Exception e ) {
201
208
if (wasGetLocalGitInvoked (vcs )) {
202
209
fail ();
203
210
}
204
211
}
212
+
213
+ }
214
+
215
+ private void testExceptionThrowing (Exception testException , Method m , Object [] params ) throws Exception {
216
+ Mockito .reset (git );
217
+ Mockito .doThrow (testException ).when (git ).getLocalGit (mockedLWC );
218
+ testExceptionThrowingNoMock (testException , m , params );
219
+
205
220
}
206
221
207
222
private Boolean wasGetLocalGitInvoked (IVCS vcs ) throws Exception {
208
223
try {
209
- Mockito .verify (( GitVCS ) vcs ).getLocalGit (mockedLWC );
224
+ Mockito .verify (git ).getLocalGit (mockedLWC );
210
225
return true ;
211
226
} catch (WantedButNotInvoked e1 ) {
212
227
return false ;
@@ -217,7 +232,7 @@ private Boolean wasGetLocalGitInvoked(IVCS vcs) throws Exception {
217
232
public void testDefaultChangeTypeToVCSType () {
218
233
for (DiffEntry .ChangeType ct : DiffEntry .ChangeType .values ()) {
219
234
if (ct != DiffEntry .ChangeType .ADD && ct != DiffEntry .ChangeType .DELETE && ct != DiffEntry .ChangeType .MODIFY ) {
220
- assertEquals ((( GitVCS ) vcs ) .gitChangeTypeToVCSChangeType (ct ), VCSChangeType .UNKNOWN );
235
+ assertEquals (git .gitChangeTypeToVCSChangeType (ct ), VCSChangeType .UNKNOWN );
221
236
}
222
237
}
223
238
}
@@ -239,6 +254,65 @@ public void testGetFileContentExceptions() {
239
254
public void testGitVCSUtilsCreation () {
240
255
assertNotNull (new GitVCSUtils ());
241
256
}
242
-
257
+
258
+ @ Test
259
+ public void testGetLastTagEmptyTagRefsList () throws Exception {
260
+ Mockito .doReturn (new ArrayList <Ref >()).when (git ).getTagRefs ();
261
+ assertNull (vcs .getLastTag ());
262
+ }
263
+
264
+ @ Test
265
+ public void testGetLastTagUnannotatedTag () throws Exception {
266
+ createUnannotatedTag (null , TAG_NAME_1 );
267
+ VCSTag tag = vcs .getLastTag ();
268
+ assertNull (tag .getAuthor ());
269
+ assertNull (tag .getTagMessage ());
270
+ assertEquals (tag .getTagName (), TAG_NAME_1 );
271
+ assertEquals (tag .getRelatedCommit (), vcs .getHeadCommit (null ));
272
+ }
273
+
274
+ public void createUnannotatedTag (String branchName , String tagName ) throws Exception {
275
+ try (IVCSLockedWorkingCopy wc = localVCSRepo .getVCSLockedWorkingCopy ();
276
+ Git localGit = git .getLocalGit (wc );
277
+ Repository gitRepo = localGit .getRepository ();
278
+ RevWalk rw = new RevWalk (gitRepo )) {
279
+
280
+ git .checkout (localGit , gitRepo , branchName );
281
+
282
+ Ref ref = localGit
283
+ .tag ()
284
+ .setAnnotated (false )
285
+ .setName (tagName )
286
+ .setObjectId (null )
287
+ .call ();
288
+
289
+ localGit
290
+ .push ()
291
+ .setPushAll ()
292
+ .setRefSpecs (new RefSpec (ref .getName ()))
293
+ .setRemote ("origin" )
294
+ .setCredentialsProvider (git .getCredentials ())
295
+ .call ();
296
+ }
297
+ }
298
+
299
+ @ Test
300
+ public void testGetLastTagExceptions () throws Exception {
301
+ Ref dummyRef = Mockito .mock (Ref .class );
302
+ List <Ref > refList = new ArrayList <>();
303
+ refList .add (dummyRef );
304
+ Mockito .doReturn (refList ).when (git ).getTagRefs ();
305
+ @ SuppressWarnings ("serial" )
306
+ GitAPIException eApi = new GitAPIException ("test git exception" ) {};
307
+ Exception eCommon = new Exception ("test common exception" );
308
+ //Mockito.reset(git);
309
+ Mockito .doThrow (eApi ).when (git ).getLocalGit (mockedLWC );
310
+ testExceptionThrowingNoMock (eApi , vcs .getClass ().getDeclaredMethod ("getLastTag" ), new Object [0 ]);
311
+
312
+ Mockito .reset (git );
313
+ Mockito .doReturn (refList ).when (git ).getTagRefs ();
314
+ Mockito .doThrow (eCommon ).when (git ).getLocalGit (mockedLWC );
315
+ testExceptionThrowingNoMock (eCommon , vcs .getClass ().getDeclaredMethod ("getLastTag" ), new Object [0 ]);
316
+ }
243
317
}
244
318
0 commit comments