View Javadoc
1   package org.apache.maven.scm.provider.git.repository;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.scm.ScmTestCase;
23  import org.apache.maven.scm.manager.NoSuchScmProviderException;
24  import org.apache.maven.scm.manager.ScmManager;
25  import org.apache.maven.scm.repository.ScmRepository;
26  import org.apache.maven.scm.repository.ScmRepositoryException;
27  
28  /**
29   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
30   *
31   */
32  public class GitScmProviderRepositoryTest
33      extends ScmTestCase
34  {
35      private ScmManager scmManager;
36  
37      public void setUp()
38          throws Exception
39      {
40          super.setUp();
41  
42          scmManager = getScmManager();
43      }
44  
45      // ----------------------------------------------------------------------
46      // Testing legal URLs
47      // ----------------------------------------------------------------------
48  
49      public void testLegalFileURL()
50          throws Exception
51      {
52          testUrl( "scm:git:file:///tmp/repo", null, "file:///tmp/repo", null, null, null, null, 0, null);
53      }
54  
55      public void testLegalFileHomeURL()
56      throws Exception
57      {
58          testUrl( "scm:git:file://~/repo", null, "file://~/repo", null, null, null, null, 0, null);
59      }
60  
61      public void testLegalSshHomeURL()
62      throws Exception
63      {
64          testUrl( "scm:git:ssh://~/repo", null, "ssh://~/repo", null, null, null, null, 0, null);
65      }
66  
67      public void testLegalLocalhostFileURL()
68          throws Exception
69      {
70          testUrl( "scm:git:file://somedirectory/tmp/repo", null, "file://somedirectory/tmp/repo", null, null,
71                  null, null, 0, "somedirectory/tmp/repo");
72      }
73  
74      public void testLegalHistnameFileURL()
75          throws Exception
76      {
77          testUrl( "scm:git:file://my_server/tmp/repo", null, "file://my_server/tmp/repo", null, null,
78                  null, null, 0, "my_server/tmp/repo");
79      }
80  
81      public void testLegalHttpURL()
82          throws Exception
83      {
84          testUrl( "scm:git:http://gitrepos.apache.org", null, "http://gitrepos.apache.org", null, null,
85                   null, "gitrepos.apache.org", 0, null);
86      }
87  
88      public void testLegalHttpURLWithUser()
89          throws Exception
90      {
91          testUrl( "scm:git:http://user@gitrepos.apache.org", null, "http://user@gitrepos.apache.org", null, "user",
92                   null, "gitrepos.apache.org", 0, null);
93      }
94  
95      public void testLegalHttpURLWithUserPassword()
96          throws Exception
97      {
98          testUrl( "scm:git:http://user:password@gitrepos.apache.org", null, "http://user:password@gitrepos.apache.org",
99                   null, "user", "password", "gitrepos.apache.org", 0, null);
100     }
101 
102     public void testLegalHttpsURL()
103         throws Exception
104     {
105         testUrl( "scm:git:https://gitrepos.apache.org/repos/projectA", null, "https://gitrepos.apache.org/repos/projectA", null, null,
106                  null, "gitrepos.apache.org", 0, "repos/projectA");
107     }
108 
109     public void testLegalFileWindowsURL()
110             throws Exception
111     {
112         // FIXME This URL is invalid, hell knows why Git accepts it. It should be by us right away
113         testUrl( "scm:git:file://c:\\tmp\\repo", null, "file://c:\\tmp\\repo", null, null, null, null, 0, null);
114     }
115 
116     public void testLegalHttpsURLWithUser()
117         throws Exception
118     {
119         testUrl( "scm:git:https://user@gitrepos.apache.org", null, "https://user@gitrepos.apache.org", null, "user",
120                  null, "gitrepos.apache.org", 0, null);
121     }
122 
123     public void testLegalHttpsURLWithUserPassword()
124         throws Exception
125     {
126         testUrl( "scm:git:https://user:password@gitrepos.apache.org", null, "https://user:password@gitrepos.apache.org",
127                  null, "user", "password", "gitrepos.apache.org", 0, null);
128     }
129 
130     public void testLegalSshURLWithUser()
131     throws Exception
132     {
133         testUrl( "scm:git:ssh://user@gitrepos.apache.org", null, "ssh://user@gitrepos.apache.org", null, "user",
134                  null, "gitrepos.apache.org", 0, null);
135     }
136 
137     public void testLegalSshURLWithUserPassword()
138     throws Exception
139     {
140         testUrl( "scm:git:ssh://user:password@gitrepos.apache.org", null, "ssh://user:password@gitrepos.apache.org",
141                  null, "user", "password", "gitrepos.apache.org", 0, null);
142     }
143 
144     public void testLegalGitURL()
145         throws Exception
146     {
147         testUrl( "scm:git:git://gitrepos.apache.org", null, "git://gitrepos.apache.org", null, null,
148                  null, "gitrepos.apache.org", 0, null);
149     }
150 
151     public void testGitDevURL()
152         throws Exception, ScmRepositoryException
153     {
154         testUrl( "scm:git:git@github.com:olamy/scm-git-test-one-module.git",
155                  null, "git@github.com:olamy/scm-git-test-one-module.git", null, "git" , null, "github.com", 0, null);
156     }
157 
158     public void testGitDevURLWIthPort()
159         throws Exception, ScmRepositoryException
160     {
161         testUrl( "scm:git:git@github.com:222:olamy/scm-git-test-one-module.git",
162                  null, "git@github.com:222:olamy/scm-git-test-one-module.git", null, "git", null, "github.com", 222, null);
163     }
164 
165     // For SCM-639
166     public void testGitDevUrlWithNumberedRepoAndNoPort()
167         throws Exception, ScmRepositoryException
168     {
169         testUrl( "scm:git:git@github.com:4sh/blah.git",
170                  null, "git@github.com:4sh/blah.git", null, "git", null, "github.com", 0, null);
171     }
172 
173 
174     // For SCM-629
175     public void testGitDevUrlWithNumberedRepoAndMinus()
176         throws Exception, ScmRepositoryException
177     {
178         testUrl( "scm:git:ssh://git@github.com/360-Innovations/FJPAQuery.git",
179                  null, "ssh://git@github.com/360-Innovations/FJPAQuery.git", null, "git", null, "github.com", 0, null);
180     }
181 
182     // For SCM-707
183     public void testSpecialCharacters()
184         throws Exception
185     {
186         testUrl( "scm:git:http://gitrepos.apache.org", "@_&_:_?_#_%20", "pass word", null, "http://gitrepos.apache.org", null,
187                  "http://%40_&_:_%3F_%23_%2520:pass%20word@gitrepos.apache.org", null,
188                  "gitrepos.apache.org", 0, null );
189 
190         testUrl( "scm:git:http://gitrepos.apache.org", "user name", "@_&_:_?_#_%20", null, "http://gitrepos.apache.org", null,
191                  "http://user%20name:%40_&_:_%3F_%23_%2520@gitrepos.apache.org", null,
192                  "gitrepos.apache.org", 0, null );
193 
194     }
195 
196     public void testLegalGitPortUrl()
197         throws Exception
198     {
199         testUrl( "scm:git:http://username@gitrepos.apache.org:8800/pmgt/trunk",
200                  null, "http://username@gitrepos.apache.org:8800/pmgt/trunk",
201                  null, "username", null, "gitrepos.apache.org", 8800, null);
202 
203 
204 
205         testUrl( "scm:git:https://username@gitrepos.apache.org:20443/pmgt/trunk",
206                  null, "https://username@gitrepos.apache.org:20443/pmgt/trunk",
207                  null, "username", null, "gitrepos.apache.org", 20443, null);
208 
209         testUrl( "scm:git:git://username@gitrepos.apache.org:8800/pmgt/trunk",
210                  null, "git://username@gitrepos.apache.org:8800/pmgt/trunk",
211                  null, "username", null, "gitrepos.apache.org", 8800, null);
212 
213         testUrl( "scm:git:ssh://username@gitrepos.apache.org:8080/pmgt/trunk",
214                  null, "ssh://username@gitrepos.apache.org:8080/pmgt/trunk",
215                  null, "username", null, "gitrepos.apache.org", 8080, null);
216 
217         testUrl( "scm:git:ssh://username:password@gitrepos.apache.org/pmgt/trunk",
218                  null, "ssh://username:password@gitrepos.apache.org/pmgt/trunk",
219                  null, "username", "password", "gitrepos.apache.org", 0, null);
220 
221     }
222 
223     public void testUsernameWithAtAndPasswordInUrl() throws ScmRepositoryException, Exception{
224         testUrl( "scm:git:http://username@site.com:password@gitrepos.apache.org:8800/pmgt/trunk",
225             null, "http://username%40site.com:password@gitrepos.apache.org:8800/pmgt/trunk",
226             null, "username@site.com", "password", "gitrepos.apache.org", 8800, null);
227 
228     }
229 
230     // ----------------------------------------------------------------------
231     // the following tests are for combined fetch + push URLs
232     // ----------------------------------------------------------------------
233 
234     public void testHttpFetchSshPushUrl()
235         throws Exception
236     {
237         testUrl( "scm:git:[fetch=]http://git.apache.org/myprj.git[push=]ssh://myuser:mypassword@git.apache.org/~/myrepo/myprj.git",
238                  "[fetch=]http://myuser:mypassword@git.apache.org/myprj.git[push=]ssh://myuser:mypassword@git.apache.org/~/myrepo/myprj.git",
239                  "http://myuser:mypassword@git.apache.org/myprj.git",
240                  "ssh://myuser:mypassword@git.apache.org/~/myrepo/myprj.git", "myuser", "mypassword", "git.apache.org", 0, null);
241 
242         testUrl( "scm:git:[push=]ssh://myuser:mypassword@git.apache.org/~/myrepo/myprj.git[fetch=]http://git.apache.org/myprj.git",
243                  "[fetch=]http://myuser:mypassword@git.apache.org/myprj.git[push=]ssh://myuser:mypassword@git.apache.org/~/myrepo/myprj.git",
244                  "http://myuser:mypassword@git.apache.org/myprj.git",
245                  "ssh://myuser:mypassword@git.apache.org/~/myrepo/myprj.git", "myuser", "mypassword", "git.apache.org", 0, null);
246     }
247 
248     // ----------------------------------------------------------------------
249     // Testing illegal URLs
250     // ----------------------------------------------------------------------
251 
252     //X in fact this url is perfectly valid from a technical perspective
253     //X it will be interpreted by git as git://file/tmp/git
254     public void nottestIllegalFileUrl()
255         throws Exception
256     {
257         testIllegalUrl( "file:/tmp/git" );
258     }
259 
260     // ----------------------------------------------------------------------
261     //
262     // ----------------------------------------------------------------------
263 
264     private GitScmProviderRepository testUrl(String scmUrl, String expectedToString,
265                                              String expectedFetchUrl, String expectedPushUrl,
266                                              String expectedUser, String expectedPassword,
267                                              String expectedHost, int expectedPort, String expectedPath)
268         throws Exception, ScmRepositoryException
269     {
270 
271         ScmRepository repository = testScmRepository( scmUrl, expectedToString, expectedFetchUrl );
272 
273         GitScmProviderRepository providerRepository = (GitScmProviderRepository) repository.getProviderRepository();
274 
275         return testScmProviderRepository( expectedToString, expectedFetchUrl, expectedPushUrl, expectedUser,
276                                           expectedPassword, expectedHost, expectedPort, providerRepository );
277 
278     }
279 
280     private GitScmProviderRepository testUrl(String scmUrl, String username, String password,
281                                              String expectedScmRepositoryToString, String expectedScmRepositoryFetchUrl,
282                                              String expectedScmProviderRepositoryToString, String expectedScmProviderRepositoryFetchUrl, String expectedPushUrl,
283                                              String expectedHost, int expectedPort, String expectedPath)
284         throws Exception, ScmRepositoryException
285     {
286 
287         ScmRepository repository = testScmRepository( scmUrl, expectedScmRepositoryToString, expectedScmRepositoryFetchUrl );
288 
289         GitScmProviderRepository providerRepository = (GitScmProviderRepository) repository.getProviderRepository();
290 
291         providerRepository.setUser( username );
292 
293         providerRepository.setPassword( password );
294 
295         return testScmProviderRepository( expectedScmProviderRepositoryToString, expectedScmProviderRepositoryFetchUrl, expectedPushUrl, username,
296                                           password, expectedHost, expectedPort, providerRepository );
297     }
298 
299     private GitScmProviderRepository testScmProviderRepository( String expectedToString, String expectedFetchUrl,
300                                                                 String expectedPushUrl, String expectedUser,
301                                                                 String expectedPassword, String expectedHost,
302                                                                 int expectedPort,
303                                                                 GitScmProviderRepository providerRepository )
304     {
305         assertEquals( "fetch url is incorrect", expectedFetchUrl, providerRepository.getFetchUrl() );
306 
307         if ( expectedPushUrl != null )
308         {
309             assertEquals( "push url is incorrect", expectedPushUrl, providerRepository.getPushUrl() );
310         }
311 
312         assertEquals( "User is incorrect", expectedUser, providerRepository.getUser() );
313 
314         assertEquals( "Password is incorrect", expectedPassword, providerRepository.getPassword() );
315 
316         assertEquals( "Host is incorrect", expectedHost == null ? "" : expectedHost, providerRepository.getHost() );
317 
318         if ( expectedPort > 0 )
319         {
320             assertEquals( "Port is incorrect", expectedPort, providerRepository.getPort() );
321         }
322 
323         return providerRepository;
324     }
325 
326     private ScmRepository testScmRepository( String scmUrl, String expectedToString, String expectedFetchUrl )
327         throws ScmRepositoryException, NoSuchScmProviderException
328     {
329         ScmRepository repository = scmManager.makeScmRepository( scmUrl );
330 
331         assertNotNull( "ScmManager.makeScmRepository() returned null", repository );
332 
333         assertNotNull( "The provider repository was null.", repository.getProviderRepository() );
334 
335         assertTrue( "The SCM Repository isn't a " + GitScmProviderRepository.class.getName() + ".", repository
336             .getProviderRepository() instanceof GitScmProviderRepository );
337 
338         if ( expectedToString != null )
339         {
340             assertEquals( "toString is incorrect", "git:" + expectedToString, repository.toString() );
341         }
342         else
343         {
344             assertEquals( "toString is incorrect", "git:" + expectedFetchUrl, repository.toString() );
345         }
346 
347         return repository;
348     }
349 
350     private void testIllegalUrl( String url )
351         throws Exception
352     {
353         try
354         {
355             scmManager.makeScmRepository( "scm:git:" + url );
356 
357             fail( "Expected a ScmRepositoryException while testing the url '" + url + "'." );
358         }
359         catch ( ScmRepositoryException e )
360         {
361             // expected
362         }
363     }
364 
365     public void testGetParent() throws Exception
366     {
367         new GitScmProviderRepository( "http://gitrepos.apache.org" );
368     }
369 
370 }