001package org.apache.maven.scm.provider.hg.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 junit.framework.TestCase;
023
024public class HgScmProviderRepositoryTest
025    extends TestCase
026{
027
028//    public void testInvalidRepo()
029//    {
030//        //No protocol - makes it invalid
031//        String url = "username:password@myhost.com/~/dev/maven";
032//        HgScmProviderRepository repo = new HgScmProviderRepository( url );
033//        assertNotNull( repo.validateURI() );
034//    }
035
036    public void testFileRepo()
037    {
038        //1. Test *nix like paths
039        String url = "/home/username/dev/maven";
040        HgScmProviderRepository repo = new HgScmProviderRepository( url );
041        assertNull( repo.validateURI() );
042
043        //2. Test windows like paths (with slash)
044        url = "C:/Documents and Settings/username/dev/maven";
045        repo = new HgScmProviderRepository( url );
046        assertNull( repo.validateURI() );
047
048        //3. Test windows like paths (with backslash)
049        url = "C:\\Documents and Settings\\username\\dev\\maven";
050        repo = new HgScmProviderRepository( url );
051        assertNull( repo.validateURI() );
052
053//        //4. Test invalid file url
054//        url = "file:/C:\\Documents and Settings\\username\\dev\\maven";
055//        repo = new HgScmProviderRepository( url );
056//        assertNotNull( repo.validateURI() );
057    }
058
059    public void testSSHRepo()
060    {
061        //todo: check assert
062        //1. Test with relativ path
063        String url = "ssh://username:password@myhost.com/~/dev/maven";
064        HgScmProviderRepository repo = new HgScmProviderRepository( url );
065        assertEquals( url, repo.getURI() );
066        assertNull( repo.validateURI() );
067
068        //2. Test with absolute path
069        url = "ssh://username:password@myhost.com/home/username/dev/maven";
070        repo = new HgScmProviderRepository( url );
071        assertEquals( url, repo.getURI() );
072        assertNull( repo.validateURI() );
073
074        //3. Test with passwordless (Public-key auth)
075        String incompleteUrl = "ssh://username@myhost.com/home/username/dev/maven";
076        repo = new HgScmProviderRepository( incompleteUrl );
077        assertEquals( incompleteUrl, repo.getURI() );
078        assertNull( repo.validateURI() );
079    }
080
081    public void testHTTPRepo()
082    {
083        //todo: check assert
084        //1. Test with relativ path
085        String url = "http://www.myhost.com/~username/dev/maven";
086        HgScmProviderRepository repo = new HgScmProviderRepository( url );
087        assertEquals( url, repo.getURI() );
088        assertEquals( null, repo.validateURI() );
089
090        //2. Test with absolute path
091        url = "http://www.myhost.com/dev/maven";
092        repo = new HgScmProviderRepository( url );
093        assertEquals( url, repo.getURI() );
094        assertNull( repo.validateURI() );
095
096        //3. Test with authentication information
097        repo.setPassword( "Password" );
098        repo.setUser( "User" );
099        repo.setPassphrase( "Passphrase" );
100        assertEquals( "http://User:Password@www.myhost.com/dev/maven", repo.getURI() );
101        assertNull( repo.validateURI() );
102        repo.setPort( 81 );
103        assertEquals( "http://User:Password@www.myhost.com:81/dev/maven", repo.getURI() );
104        assertNull( repo.validateURI() );
105        assertTrue( true );
106    }
107
108    public void testHTTPRepoWithHgInUrl()
109    {
110        String url = "http://hg/hg/maven";
111        HgScmProviderRepository repo = new HgScmProviderRepository( url );
112        assertEquals( url, repo.getURI() );
113        assertEquals( null, repo.validateURI() );
114    }
115
116    /**
117     * Test SCM-391
118     *
119     * @throws Exception
120     */
121    public void testParseHostAndPort()
122        throws Exception
123    {
124        String url = "http://localhost:8000/";
125        HgScmProviderRepository repo = new HgScmProviderRepository( url );
126        assertEquals( repo.getURI(), url );
127
128        url = "http://localhost/";
129        repo = new HgScmProviderRepository( url );
130        assertEquals( repo.getURI(), url );
131
132        url = "http://www.myhost.com:81/dev/maven";
133        repo = new HgScmProviderRepository( url );
134        assertEquals( repo.getURI(), url );
135    }
136
137    /**
138     * Test SCM-431
139     *
140     * @throws Exception
141     */
142    public void testParseBasicAuth()
143        throws Exception
144    {
145        String url = "http://a:b@localhost:8000/";
146        HgScmProviderRepository repo = new HgScmProviderRepository( url );
147        assertEquals( url, repo.getURI() );
148
149        url = "http://aa@localhost/";
150        repo = new HgScmProviderRepository( url );
151        assertEquals( url, repo.getURI() );
152
153        url = "http://SCM:431@www.myhost.com:81/dev/maven";
154        repo = new HgScmProviderRepository( url );
155        assertEquals( url, repo.getURI() );
156    }
157}