1
1
package org .scm4j .vcs ;
2
2
3
- import java .io .File ;
4
- import java .io .IOException ;
5
-
6
3
import org .apache .commons .io .FileUtils ;
7
4
import org .eclipse .jgit .api .Git ;
5
+ import org .eclipse .jgit .api .errors .GitAPIException ;
6
+ import org .eclipse .jgit .diff .DiffEntry ;
8
7
import org .eclipse .jgit .lib .Repository ;
8
+ import org .eclipse .jgit .transport .CredentialItem ;
9
9
import org .junit .After ;
10
+ import org .junit .Test ;
10
11
import org .mockito .Mockito ;
12
+ import org .mockito .exceptions .verification .WantedButNotInvoked ;
11
13
import org .scm4j .vcs .api .IVCS ;
14
+ import org .scm4j .vcs .api .VCSChangeType ;
12
15
import org .scm4j .vcs .api .abstracttest .VCSAbstractTest ;
13
16
import org .scm4j .vcs .api .workingcopy .IVCSRepositoryWorkspace ;
14
17
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
+
15
28
public class GitVCSTest extends VCSAbstractTest {
16
29
17
30
private Repository localGitRepo ;
18
- private final RuntimeException testGitResetException = new RuntimeException ("test exeption on git.reset()" );
31
+ private ProxySelector proxySelectorBackup ;
32
+ private final RuntimeException testGitResetException = new RuntimeException ("test exception on git.reset()" );
19
33
20
34
@ Override
21
35
public void setUp () throws Exception {
@@ -26,12 +40,15 @@ public void setUp() throws Exception {
26
40
.commit ()
27
41
.setMessage ("Initial commit" )
28
42
.call ();
43
+ proxySelectorBackup = ProxySelector .getDefault ();
44
+ ProxySelector .setDefault (null );
29
45
}
30
46
31
47
@ After
32
48
public void tearDown () throws IOException {
33
49
localGitRepo .close ();
34
50
FileUtils .deleteDirectory (localGitRepo .getDirectory ());
51
+ ProxySelector .setDefault (proxySelectorBackup );
35
52
}
36
53
37
54
@ Override
@@ -45,7 +62,7 @@ protected IVCS getVCS(IVCSRepositoryWorkspace mockedVCSRepo) {
45
62
}
46
63
47
64
@ Override
48
- protected void setMakeFailureOnVCSReset (Boolean doMakeFailure ) {
65
+ protected void setMakeFailureOnVCSReset (Boolean doMakeFailure ) throws Exception {
49
66
Git mockedGit ;
50
67
if (doMakeFailure ) {
51
68
mockedGit = Mockito .spy (((GitVCS ) vcs ).getLocalGit (mockedLWC ));
@@ -61,5 +78,170 @@ protected void setMakeFailureOnVCSReset(Boolean doMakeFailure) {
61
78
protected String getVCSTypeString () {
62
79
return GitVCS .GIT_VCS_TYPE_STRING ;
63
80
}
81
+
82
+ @ Test
83
+ public void testSetCredentials () {
84
+ vcs .setCredentials ("user" , "password" );
85
+ CredentialItem .Username u = new CredentialItem .Username ();
86
+ CredentialItem .Password p = new CredentialItem .Password ();
87
+ assertTrue (((GitVCS ) vcs ).getCredentials ().get (null , u , p ));
88
+ assertEquals (u .getValue (), "user" );
89
+ assertEquals (new String (p .getValue ()), "password" );
90
+ }
91
+
92
+ @ Test
93
+ public void testProxyAuth () throws Exception {
94
+ PasswordAuthentication initialAuth = Authenticator .requestPasswordAuthentication (InetAddress .getByName ("localhost" ),
95
+ 123 , "http" , "" , "" );
96
+ IVCS vcs = new GitVCS (localVCSWorkspace .getVCSRepositoryWorkspace ("localhost" ));
97
+ vcs .setProxy ("localhost" , 123 , "username" , "pwd" );
98
+
99
+ PasswordAuthentication resultAuth = Authenticator .requestPasswordAuthentication (
100
+ InetAddress .getByName ("localhost" ), 123 , "http" , "" , "" );
101
+ assertEquals (resultAuth .getUserName (), "username" );
102
+ assertEquals (new String (resultAuth .getPassword ()), "pwd" );
103
+
104
+ resultAuth = Authenticator .requestPasswordAuthentication (
105
+ InetAddress .getByName ("localhost" ), 124 , "http" , "" , "" );
106
+ assertEquals (resultAuth , initialAuth );
107
+ }
108
+
109
+ @ Test
110
+ public void testProxySelector () throws URISyntaxException {
111
+ vcs .setProxy ("localhost" , 123 , "username" , "pwd" );
112
+ ProxySelector actualPS = ProxySelector .getDefault ();
113
+ List <Proxy > proxies = actualPS .select (new URI (vcs .getRepoUrl ()));
114
+ assertTrue (proxies .size () == 1 );
115
+ Proxy actualP = proxies .get (0 );
116
+ assertTrue (actualP .address () instanceof InetSocketAddress );
117
+ InetSocketAddress isa = (InetSocketAddress ) actualP .address ();
118
+ assertEquals (isa .getHostName (), "localhost" );
119
+ assertEquals (isa .getPort (), 123 );
120
+ }
121
+
122
+ @ Test
123
+ public void testParentProxySelectorUsage () throws URISyntaxException {
124
+ ProxySelector mockedPS = Mockito .mock (ProxySelector .class );
125
+ ProxySelector .setDefault (mockedPS );
126
+ vcs .setProxy ("localhost" , 123 , "username" , "pwd" );
127
+ ProxySelector actualPS = ProxySelector .getDefault ();
128
+ URI uri = new URI ("http://unknown" );
129
+ actualPS .select (uri );
130
+ Mockito .verify (mockedPS ).select (uri );
131
+ }
132
+
133
+ @ Test
134
+ public void testNullProxySelector () throws URISyntaxException {
135
+ ProxySelector .setDefault (null );
136
+ vcs .setProxy ("localhost" , 123 , "username" , "pwd" );
137
+ ProxySelector actualPS = ProxySelector .getDefault ();
138
+ List <Proxy > proxies = actualPS .select (new URI ("http://unknown" ));
139
+ assertTrue (proxies .size () == 1 );
140
+ assertEquals (proxies .get (0 ), Proxy .NO_PROXY );
141
+ }
142
+
143
+ @ Test
144
+ public void testParentSelectorCallOnConnectFailed () throws URISyntaxException {
145
+ ProxySelector mockedPS = Mockito .mock (ProxySelector .class );
146
+ ProxySelector .setDefault (mockedPS );
147
+ vcs .setProxy ("localhost" , 123 , "username" , "pwd" );
148
+ ProxySelector actualPS = ProxySelector .getDefault ();
149
+ URI testURI = new URI ("http://proxy.net" );
150
+ SocketAddress testSA = InetSocketAddress .createUnresolved ("http://proxy.net" , 123 );
151
+ IOException testException = new IOException ("test exception" );
152
+ actualPS .connectFailed (testURI , testSA , testException );
153
+ Mockito .verify (mockedPS ).connectFailed (testURI , testSA , testException );
154
+ }
155
+ @ Test
156
+ public void testNoParentSelectorOnConnectFailed () throws URISyntaxException {
157
+ ProxySelector .setDefault (null );
158
+ vcs .setProxy ("localhost" , 123 , "username" , "pwd" );
159
+ ProxySelector actualPS = Mockito .spy (ProxySelector .getDefault ());
160
+ URI testURI = new URI ("http://proxy.net" );
161
+ SocketAddress testSA = InetSocketAddress .createUnresolved ("http://proxy.net" , 123 );
162
+ IOException testException = new IOException ("test exception" );
163
+ actualPS .connectFailed (testURI , testSA , testException );
164
+ Mockito .verify (actualPS ).connectFailed (testURI , testSA , testException );
165
+ Mockito .verifyNoMoreInteractions (actualPS );
166
+ }
167
+
168
+ @ Test
169
+ public void testVCSTypeString () {
170
+ assertEquals (vcs .getVCSTypeString (), GitVCS .GIT_VCS_TYPE_STRING );
171
+ }
172
+
173
+ @ Test
174
+ public void testExceptions () throws Exception {
175
+ GitAPIException eApi = new GitAPIException ("test git exception" ) {};
176
+ Exception eCommon = new Exception ("test common exception" );
177
+ for (Method m : IVCS .class .getDeclaredMethods ()) {
178
+ Object [] params = new Object [m .getParameterTypes ().length ];
179
+ Integer i = 0 ;
180
+ for (Class clazz : m .getParameterTypes ()) {
181
+ params [i ] = clazz .isPrimitive () ? 0 : null ;
182
+ i ++;
183
+ }
184
+ testExceptionThrowing (eApi , m , params );
185
+
186
+ testExceptionThrowing (eCommon , m , params );
187
+ }
188
+ }
189
+
190
+ private void testExceptionThrowing (Exception testException , Method m , Object [] params ) throws Exception {
191
+ Mockito .reset ((GitVCS ) vcs );
192
+ Mockito .doThrow (testException ).when ((GitVCS ) vcs ).getLocalGit (mockedLWC );
193
+ try {
194
+ m .invoke (vcs , params );
195
+ if (wasGetLocalGitInvoked (vcs )) {
196
+ fail ();
197
+ }
198
+ } catch (InvocationTargetException e ) {
199
+ if (wasGetLocalGitInvoked (vcs )) {
200
+ assertTrue (e .getCause () instanceof RuntimeException );
201
+ assertTrue (e .getCause ().getMessage ().contains (testException .getMessage ()));
202
+ }
203
+ } catch (Exception e ) {
204
+ if (wasGetLocalGitInvoked (vcs )) {
205
+ fail ();
206
+ }
207
+ }
208
+ }
209
+
210
+ private Boolean wasGetLocalGitInvoked (IVCS vcs ) throws Exception {
211
+ try {
212
+ Mockito .verify ((GitVCS ) vcs ).getLocalGit (mockedLWC );
213
+ return true ;
214
+ } catch (WantedButNotInvoked e1 ) {
215
+ return false ;
216
+ }
217
+ }
218
+
219
+ @ Test
220
+ public void testDefaultChangeTypeToVCSType () {
221
+ for (DiffEntry .ChangeType ct : DiffEntry .ChangeType .values ()) {
222
+ if (ct != DiffEntry .ChangeType .ADD && ct != DiffEntry .ChangeType .DELETE && ct != DiffEntry .ChangeType .MODIFY ) {
223
+ assertEquals (((GitVCS ) vcs ).gitChangeTypeToVCSChangeType (ct ), VCSChangeType .UNKNOWN );
224
+ }
225
+ }
226
+ }
227
+
228
+ @ Test
229
+ public void testGetFileContentExceptions () {
230
+ vcs .setFileContent (null , FILE1_NAME , LINE_1 , FILE1_ADDED_COMMIT_MESSAGE );
231
+ try {
232
+ vcs .getFileContent (null , FILE1_NAME , "wrong encoding" );
233
+ fail ();
234
+ } catch (RuntimeException e ) {
235
+ assertTrue (e .getCause () instanceof IllegalCharsetNameException );
236
+ } catch (Exception e ) {
237
+ fail ();
238
+ }
239
+ }
240
+
241
+ @ Test
242
+ public void testGitVCSUtilsCreation () {
243
+ new GitVCSUtils ();
244
+ }
245
+
64
246
}
65
247
0 commit comments