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.ScmManager;
24  import org.apache.maven.scm.repository.ScmRepository;
25  import org.apache.maven.scm.repository.ScmRepositoryException;
26  
27  /**
28   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
29   * @version $Id: GitScmProviderRepositoryTest.java 1241119 2012-02-06 19:17:32Z struberg $
30   */
31  public class GitScmProviderRepositoryTest
32      extends ScmTestCase
33  {
34      private ScmManager scmManager;
35  
36      public void setUp()
37          throws Exception
38      {
39          super.setUp();
40  
41          scmManager = getScmManager();
42      }
43  
44      // ----------------------------------------------------------------------
45      // Testing legal URLs
46      // ----------------------------------------------------------------------
47  
48      public void testLegalFileURL()
49          throws Exception
50      {
51          testUrl( "scm:git:file:///tmp/repo", null, "file:///tmp/repo", null, null, null, null, 0, null);
52      }
53  
54      public void testLegalFileHomeURL()
55      throws Exception
56      {
57          testUrl( "scm:git:file://~/repo", null, "file://~/repo", null, null, null, null, 0, null);
58      }
59  
60      public void testLegalSshHomeURL()
61      throws Exception
62      {
63          testUrl( "scm:git:ssh://~/repo", null, "ssh://~/repo", null, null, null, null, 0, null);
64      }
65  
66      public void testLegalLocalhostFileURL()
67          throws Exception
68      {
69          testUrl( "scm:git:file://somedirectory/tmp/repo", null, "file://somedirectory/tmp/repo", null, null,
70                  null, null, 0, "somedirectory/tmp/repo");
71      }
72  
73      public void testLegalHistnameFileURL()
74          throws Exception
75      {
76          testUrl( "scm:git:file://my_server/tmp/repo", null, "file://my_server/tmp/repo", null, null,
77                  null, null, 0, "my_server/tmp/repo");
78      }
79  
80      public void testLegalHttpURL()
81          throws Exception
82      {
83          testUrl( "scm:git:http://gitrepos.apache.org", null, "http://gitrepos.apache.org", null, null,
84                   null, "gitrepos.apache.org", 0, null);
85      }
86  
87      public void testLegalHttpURLWithUser()
88          throws Exception
89      {
90          testUrl( "scm:git:http://user@gitrepos.apache.org", null, "http://user@gitrepos.apache.org", null, "user",
91                   null, "gitrepos.apache.org", 0, null);
92      }
93  
94      public void testLegalHttpURLWithUserPassword()
95          throws Exception
96      {
97          testUrl( "scm:git:http://user:password@gitrepos.apache.org", null, "http://user:password@gitrepos.apache.org",
98                   null, "user", "password", "gitrepos.apache.org", 0, null);
99      }
100 
101     public void testLegalHttpsURL()
102         throws Exception
103     {
104         testUrl( "scm:git:https://gitrepos.apache.org/repos/projectA", null, "https://gitrepos.apache.org/repos/projectA", null, null,
105                  null, "gitrepos.apache.org", 0, "repos/projectA");
106     }
107 
108     public void testLegalFileWindowsURL()
109             throws Exception
110     {
111         testUrl( "scm:git:file://c:\\tmp\\repo", null, "file://c:\\tmp\\repo", null, null, null, null, 0, null);
112     }
113 
114     public void testLegalHttpsURLWithUser()
115         throws Exception
116     {
117         testUrl( "scm:git:https://user@gitrepos.apache.org", null, "https://user@gitrepos.apache.org", null, "user",
118                  null, "gitrepos.apache.org", 0, null);
119     }
120         
121     public void testLegalHttpsURLWithUserPassword()
122         throws Exception
123     {
124         testUrl( "scm:git:https://user:password@gitrepos.apache.org", null, "https://user:password@gitrepos.apache.org",
125                  null, "user", "password", "gitrepos.apache.org", 0, null);
126     }
127 
128     public void testLegalSshURLWithUser()
129     throws Exception
130     {
131         testUrl( "scm:git:ssh://user@gitrepos.apache.org", null, "ssh://user@gitrepos.apache.org", null, "user",
132                  null, "gitrepos.apache.org", 0, null);
133     }
134 
135     public void testLegalSshURLWithUserPassword()
136     throws Exception
137     {
138         testUrl( "scm:git:ssh://user:password@gitrepos.apache.org", null, "ssh://user:password@gitrepos.apache.org",  
139                  null, "user", "password", "gitrepos.apache.org", 0, null);
140     }
141     
142     public void testLegalGitURL()
143         throws Exception
144     {
145         testUrl( "scm:git:git://gitrepos.apache.org", null, "git://gitrepos.apache.org", null, null,
146                  null, "gitrepos.apache.org", 0, null);
147     }
148     
149     public void testGitDevURL()
150         throws Exception, ScmRepositoryException
151     {
152         testUrl( "scm:git:git@github.com:olamy/scm-git-test-one-module.git",
153                  null, "git@github.com:olamy/scm-git-test-one-module.git", null, "git" , null, "github.com", 0, null);
154     }
155 
156     public void testGitDevURLWIthPort()
157         throws Exception, ScmRepositoryException
158     {
159         testUrl( "scm:git:git@github.com:222:olamy/scm-git-test-one-module.git",
160                  null, "git@github.com:222:olamy/scm-git-test-one-module.git", null, "git", null, "github.com", 222, null);
161     }
162 
163     // For SCM-639
164     public void testGitDevUrlWithNumberedRepoAndNoPort()
165         throws Exception, ScmRepositoryException
166     {
167         testUrl( "scm:git:git@github.com:4sh/blah.git",
168                  null, "git@github.com:4sh/blah.git", null, "git", null, "github.com", 0, null);
169     }
170 
171 
172     // For SCM-629
173     public void testGitDevUrlWithNumberedRepoAndMinus()
174         throws Exception, ScmRepositoryException
175     {
176         testUrl( "scm:git:ssh://git@github.com/360-Innovations/FJPAQuery.git",
177                  null, "ssh://git@github.com/360-Innovations/FJPAQuery.git", null, "git", null, "github.com", 0, null);
178     }
179     
180     
181     public void testLegalGitPortUrl()
182         throws Exception
183     {
184         testUrl( "scm:git:http://username@gitrepos.apache.org:8800/pmgt/trunk",
185                  null, "http://username@gitrepos.apache.org:8800/pmgt/trunk",
186                  null, "username", null, "gitrepos.apache.org", 8800, null);
187 
188         testUrl( "scm:git:https://username@gitrepos.apache.org:20443/pmgt/trunk",
189                  null, "https://username@gitrepos.apache.org:20443/pmgt/trunk",
190                  null, "username", null, "gitrepos.apache.org", 20443, null);
191         
192         testUrl( "scm:git:git://username@gitrepos.apache.org:8800/pmgt/trunk",
193                  null, "git://username@gitrepos.apache.org:8800/pmgt/trunk", 
194                  null, "username", null, "gitrepos.apache.org", 8800, null);
195         
196         testUrl( "scm:git:ssh://username@gitrepos.apache.org:8080/pmgt/trunk",
197                  null, "ssh://username@gitrepos.apache.org:8080/pmgt/trunk", 
198                  null, "username", null, "gitrepos.apache.org", 8080, null);
199 
200         testUrl( "scm:git:ssh://username:password@gitrepos.apache.org/pmgt/trunk",
201                  null, "ssh://username:password@gitrepos.apache.org/pmgt/trunk", 
202                  null, "username", "password", "gitrepos.apache.org", 0, null);
203     }
204 
205     // ----------------------------------------------------------------------
206     // the following tests are for combined fetch + push URLs
207     // ----------------------------------------------------------------------
208 
209     public void testHttpFetchSshPushUrl()
210         throws Exception
211     {
212         testUrl( "scm:git:[fetch=]http://git.apache.org/myprj.git[push=]ssh://myuser:mypassword@git.apache.org/~/myrepo/myprj.git", 
213                  "[fetch=]http://myuser:mypassword@git.apache.org/myprj.git[push=]ssh://myuser:mypassword@git.apache.org/~/myrepo/myprj.git",
214                  "http://myuser:mypassword@git.apache.org/myprj.git",
215                  "ssh://myuser:mypassword@git.apache.org/~/myrepo/myprj.git", "myuser", "mypassword", "git.apache.org", 0, null);
216 
217         testUrl( "scm:git:[push=]ssh://myuser:mypassword@git.apache.org/~/myrepo/myprj.git[fetch=]http://git.apache.org/myprj.git", 
218                  "[fetch=]http://myuser:mypassword@git.apache.org/myprj.git[push=]ssh://myuser:mypassword@git.apache.org/~/myrepo/myprj.git",
219                  "http://myuser:mypassword@git.apache.org/myprj.git",
220                  "ssh://myuser:mypassword@git.apache.org/~/myrepo/myprj.git", "myuser", "mypassword", "git.apache.org", 0, null);
221     }
222     
223     // ----------------------------------------------------------------------
224     // Testing illegal URLs
225     // ----------------------------------------------------------------------
226 
227     //X in fact this url is perfectly valid from a technical perspective
228     //X it will be interpreted by git as git://file/tmp/git
229     public void nottestIllegalFileUrl()
230         throws Exception
231     {
232         testIllegalUrl( "file:/tmp/git" );
233     }
234 
235     // ----------------------------------------------------------------------
236     //
237     // ----------------------------------------------------------------------
238 
239     private GitScmProviderRepository testUrl(String scmUrl, String expectedToString,
240                                              String expectedFetchUrl, String expectedPushUrl,
241                                              String expectedUser, String expectedPassword,
242                                              String expectedHost, int expectedPort, String expectedPath)
243         throws Exception, ScmRepositoryException
244     {
245         ScmRepository repository = scmManager.makeScmRepository( scmUrl );
246 
247         assertNotNull( "ScmManager.makeScmRepository() returned null", repository );
248 
249         assertNotNull( "The provider repository was null.", repository.getProviderRepository() );
250 
251         assertTrue( "The SCM Repository isn't a " + GitScmProviderRepository.class.getName() + ".", repository
252             .getProviderRepository() instanceof GitScmProviderRepository );
253 
254         GitScmProviderRepository providerRepository = (GitScmProviderRepository) repository.getProviderRepository();
255 
256         assertEquals( "fetch url is incorrect", expectedFetchUrl, providerRepository.getFetchUrl() );
257         
258         if ( expectedPushUrl != null )
259         {
260             assertEquals( "push url is incorrect", expectedPushUrl, providerRepository.getPushUrl() );
261         }
262 
263         if ( expectedToString != null )
264         {
265             assertEquals( "toString is incorrect", "git:" + expectedToString, repository.toString() );
266         }
267         else
268         {
269             assertEquals( "toString is incorrect", "git:" + expectedFetchUrl, repository.toString() );
270         }
271 
272         assertEquals( "User is incorrect", expectedUser, providerRepository.getUser() );
273 
274         assertEquals( "Password is incorrect", expectedPassword, providerRepository.getPassword() );
275 
276         assertEquals( "Host is incorrect", expectedHost == null ? "" : expectedHost, providerRepository.getHost() );
277         
278         if ( expectedPort > 0 )
279         {
280             assertEquals( "Port is incorrect", expectedPort, providerRepository.getPort() );
281         }
282 
283         return providerRepository;
284     }
285 
286     private void testIllegalUrl( String url )
287         throws Exception
288     {
289         try
290         {
291             scmManager.makeScmRepository( "scm:git:" + url );
292 
293             fail( "Expected a ScmRepositoryException while testing the url '" + url + "'." );
294         }
295         catch ( ScmRepositoryException e )
296         {
297             // expected
298         }
299     }
300 
301     public void testGetParent() throws Exception
302     {
303         new GitScmProviderRepository( "http://gitrepos.apache.org" );
304     }
305 
306 }