001package org.apache.maven.scm.provider.git.repository;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.scm.ScmTestCase;
023import org.apache.maven.scm.manager.NoSuchScmProviderException;
024import org.apache.maven.scm.manager.ScmManager;
025import org.apache.maven.scm.repository.ScmRepository;
026import org.apache.maven.scm.repository.ScmRepositoryException;
027
028/**
029 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
030 *
031 */
032public class GitScmProviderRepositoryTest
033    extends ScmTestCase
034{
035    private ScmManager scmManager;
036
037    public void setUp()
038        throws Exception
039    {
040        super.setUp();
041
042        scmManager = getScmManager();
043    }
044
045    // ----------------------------------------------------------------------
046    // Testing legal URLs
047    // ----------------------------------------------------------------------
048
049    public void testLegalFileURL()
050        throws Exception
051    {
052        testUrl( "scm:git:file:///tmp/repo", null, "file:///tmp/repo", null, null, null, null, 0, null);
053    }
054
055    public void testLegalFileHomeURL()
056    throws Exception
057    {
058        testUrl( "scm:git:file://~/repo", null, "file://~/repo", null, null, null, null, 0, null);
059    }
060
061    public void testLegalSshHomeURL()
062    throws Exception
063    {
064        testUrl( "scm:git:ssh://~/repo", null, "ssh://~/repo", null, null, null, null, 0, null);
065    }
066
067    public void testLegalLocalhostFileURL()
068        throws Exception
069    {
070        testUrl( "scm:git:file://somedirectory/tmp/repo", null, "file://somedirectory/tmp/repo", null, null,
071                null, null, 0, "somedirectory/tmp/repo");
072    }
073
074    public void testLegalHistnameFileURL()
075        throws Exception
076    {
077        testUrl( "scm:git:file://my_server/tmp/repo", null, "file://my_server/tmp/repo", null, null,
078                null, null, 0, "my_server/tmp/repo");
079    }
080
081    public void testLegalHttpURL()
082        throws Exception
083    {
084        testUrl( "scm:git:http://gitrepos.apache.org", null, "http://gitrepos.apache.org", null, null,
085                 null, "gitrepos.apache.org", 0, null);
086    }
087
088    public void testLegalHttpURLWithUser()
089        throws Exception
090    {
091        testUrl( "scm:git:http://user@gitrepos.apache.org", null, "http://user@gitrepos.apache.org", null, "user",
092                 null, "gitrepos.apache.org", 0, null);
093    }
094
095    public void testLegalHttpURLWithUserPassword()
096        throws Exception
097    {
098        testUrl( "scm:git:http://user:password@gitrepos.apache.org", null, "http://user:password@gitrepos.apache.org",
099                 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}