1
1
package org .scm4j .vcs ;
2
2
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
+
3
26
import org .apache .commons .io .FileUtils ;
4
27
import org .eclipse .jgit .api .Git ;
5
28
import org .eclipse .jgit .api .errors .GitAPIException ;
6
29
import org .eclipse .jgit .diff .DiffEntry ;
30
+ import org .eclipse .jgit .lib .Ref ;
7
31
import org .eclipse .jgit .lib .Repository ;
32
+ import org .eclipse .jgit .revwalk .RevWalk ;
8
33
import org .eclipse .jgit .transport .CredentialItem ;
34
+ import org .eclipse .jgit .transport .RefSpec ;
9
35
import org .junit .After ;
10
36
import org .junit .Test ;
11
37
import org .mockito .Mockito ;
12
38
import org .mockito .exceptions .verification .WantedButNotInvoked ;
13
39
import org .scm4j .vcs .api .IVCS ;
14
40
import org .scm4j .vcs .api .VCSChangeType ;
41
+ import org .scm4j .vcs .api .VCSTag ;
15
42
import org .scm4j .vcs .api .abstracttest .VCSAbstractTest ;
43
+ import org .scm4j .vcs .api .workingcopy .IVCSLockedWorkingCopy ;
16
44
import org .scm4j .vcs .api .workingcopy .IVCSRepositoryWorkspace ;
17
45
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
-
28
46
public class GitVCSTest extends VCSAbstractTest {
29
47
30
48
private Repository localGitRepo ;
31
49
private ProxySelector proxySelectorBackup ;
32
50
private final RuntimeException testGitResetException = new RuntimeException ("test exception on git.reset()" );
51
+ private GitVCS git ;
33
52
34
53
@ Override
35
54
public void setUp () throws Exception {
36
55
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 ();
39
58
proxySelectorBackup = ProxySelector .getDefault ();
40
59
ProxySelector .setDefault (null );
60
+ git = (GitVCS ) vcs ;
41
61
}
42
62
43
63
@ After
@@ -62,10 +82,10 @@ protected void setMakeFailureOnVCSReset(Boolean doMakeFailure) throws Exception
62
82
Git mockedGit ;
63
83
if (doMakeFailure ) {
64
84
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 );
66
86
Mockito .doThrow (testGitResetException ).when (mockedGit ).reset ();
67
87
} else {
68
- Mockito .doCallRealMethod ().when ((GitVCS ) vcs ).getLocalGit (mockedLWC );
88
+ Mockito .doCallRealMethod ().when ((( GitVCS ) vcs ) ).getLocalGit (mockedLWC );
69
89
mockedGit = null ;
70
90
}
71
91
}
@@ -80,7 +100,7 @@ public void testSetCredentials() {
80
100
vcs .setCredentials ("user" , "password" );
81
101
CredentialItem .Username u = new CredentialItem .Username ();
82
102
CredentialItem .Password p = new CredentialItem .Password ();
83
- assertTrue ((( GitVCS ) vcs ) .getCredentials ().get (null , u , p ));
103
+ assertTrue (git .getCredentials ().get (null , u , p ));
84
104
assertEquals (u .getValue (), "user" );
85
105
assertEquals (new String (p .getValue ()), "password" );
86
106
}
@@ -183,30 +203,37 @@ public void testExceptions() throws Exception {
183
203
testExceptionThrowing (eCommon , m , params );
184
204
}
185
205
}
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 {
190
208
try {
191
209
m .invoke (vcs , params );
192
210
if (wasGetLocalGitInvoked (vcs )) {
193
211
fail ();
194
212
}
195
213
} catch (InvocationTargetException e ) {
196
214
if (wasGetLocalGitInvoked (vcs )) {
197
- assertTrue (e .getCause () instanceof RuntimeException );
215
+ // InvocationTargetException <- EVCSException <- GitAPIException
216
+ assertTrue (e .getCause ().getCause ().getClass ().isAssignableFrom (testException .getClass ()));
198
217
assertTrue (e .getCause ().getMessage ().contains (testException .getMessage ()));
199
218
}
200
219
} catch (Exception e ) {
201
220
if (wasGetLocalGitInvoked (vcs )) {
202
221
fail ();
203
222
}
204
223
}
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
+
205
232
}
206
233
207
234
private Boolean wasGetLocalGitInvoked (IVCS vcs ) throws Exception {
208
235
try {
209
- Mockito .verify (( GitVCS ) vcs ).getLocalGit (mockedLWC );
236
+ Mockito .verify (git ).getLocalGit (mockedLWC );
210
237
return true ;
211
238
} catch (WantedButNotInvoked e1 ) {
212
239
return false ;
@@ -217,7 +244,7 @@ private Boolean wasGetLocalGitInvoked(IVCS vcs) throws Exception {
217
244
public void testDefaultChangeTypeToVCSType () {
218
245
for (DiffEntry .ChangeType ct : DiffEntry .ChangeType .values ()) {
219
246
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 );
221
248
}
222
249
}
223
250
}
@@ -239,6 +266,65 @@ public void testGetFileContentExceptions() {
239
266
public void testGitVCSUtilsCreation () {
240
267
assertNotNull (new GitVCSUtils ());
241
268
}
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
+ }
243
329
}
244
330
0 commit comments