001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.provider.hg.repository;
020
021import org.junit.Test;
022
023import static org.junit.Assert.assertEquals;
024import static org.junit.Assert.assertNull;
025import static org.junit.Assert.assertTrue;
026
027public class HgScmProviderRepositoryTest {
028
029    //    public void testInvalidRepo()
030    //    {
031    //        //No protocol - makes it invalid
032    //        String url = "username:password@myhost.com/~/dev/maven";
033    //        HgScmProviderRepository repo = new HgScmProviderRepository( url );
034    //        assertNotNull( repo.validateURI() );
035    //    }
036
037    @Test
038    public void testFileRepo() {
039        // 1. Test *nix like paths
040        String url = "/home/username/dev/maven";
041        HgScmProviderRepository repo = new HgScmProviderRepository(url);
042        assertNull(repo.validateURI());
043
044        // 2. Test windows like paths (with slash)
045        url = "C:/Documents and Settings/username/dev/maven";
046        repo = new HgScmProviderRepository(url);
047        assertNull(repo.validateURI());
048
049        // 3. Test windows like paths (with backslash)
050        url = "C:\\Documents and Settings\\username\\dev\\maven";
051        repo = new HgScmProviderRepository(url);
052        assertNull(repo.validateURI());
053
054        //        //4. Test invalid file url
055        //        url = "file:/C:\\Documents and Settings\\username\\dev\\maven";
056        //        repo = new HgScmProviderRepository( url );
057        //        assertNotNull( repo.validateURI() );
058    }
059
060    @Test
061    public void testSSHRepo() {
062        // todo: check assert
063        // 1. Test with relativ path
064        String url = "ssh://username:password@myhost.com/~/dev/maven";
065        HgScmProviderRepository repo = new HgScmProviderRepository(url);
066        assertEquals(url, repo.getURI());
067        assertNull(repo.validateURI());
068
069        // 2. Test with absolute path
070        url = "ssh://username:password@myhost.com/home/username/dev/maven";
071        repo = new HgScmProviderRepository(url);
072        assertEquals(url, repo.getURI());
073        assertNull(repo.validateURI());
074
075        // 3. Test with passwordless (Public-key auth)
076        String incompleteUrl = "ssh://username@myhost.com/home/username/dev/maven";
077        repo = new HgScmProviderRepository(incompleteUrl);
078        assertEquals(incompleteUrl, repo.getURI());
079        assertNull(repo.validateURI());
080    }
081
082    @Test
083    public void testHTTPRepo() {
084        // todo: check assert
085        // 1. Test with relativ path
086        String url = "http://www.myhost.com/~username/dev/maven";
087        HgScmProviderRepository repo = new HgScmProviderRepository(url);
088        assertEquals(url, repo.getURI());
089        assertNull(repo.validateURI());
090
091        // 2. Test with absolute path
092        url = "http://www.myhost.com/dev/maven";
093        repo = new HgScmProviderRepository(url);
094        assertEquals(url, repo.getURI());
095        assertNull(repo.validateURI());
096
097        // 3. Test with authentication information
098        repo.setPassword("Password");
099        repo.setUser("User");
100        repo.setPassphrase("Passphrase");
101        assertEquals("http://User:Password@www.myhost.com/dev/maven", repo.getURI());
102        assertNull(repo.validateURI());
103        repo.setPort(81);
104        assertEquals("http://User:Password@www.myhost.com:81/dev/maven", repo.getURI());
105        assertNull(repo.validateURI());
106        assertTrue(true);
107    }
108
109    @Test
110    public void testHTTPRepoWithHgInUrl() {
111        String url = "http://hg/hg/maven";
112        HgScmProviderRepository repo = new HgScmProviderRepository(url);
113        assertEquals(url, repo.getURI());
114        assertNull(repo.validateURI());
115    }
116
117    /**
118     * Test SCM-391
119     *
120     * @throws Exception
121     */
122    @Test
123    public void testParseHostAndPort() throws Exception {
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    @Test
143    public void testParseBasicAuth() throws Exception {
144        String url = "http://a:b@localhost:8000/";
145        HgScmProviderRepository repo = new HgScmProviderRepository(url);
146        assertEquals(url, repo.getURI());
147
148        url = "http://aa@localhost/";
149        repo = new HgScmProviderRepository(url);
150        assertEquals(url, repo.getURI());
151
152        url = "http://SCM:431@www.myhost.com:81/dev/maven";
153        repo = new HgScmProviderRepository(url);
154        assertEquals(url, repo.getURI());
155    }
156}