001package org.apache.maven.scm.provider.bazaar.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 BazaarScmProviderRepositoryTest
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        BazaarScmProviderRepository repo = new BazaarScmProviderRepository( url );
033        assertNotNull( repo.validateURI() );
034    }
035
036    public void testFileRepo()
037    {
038        //1. Test *nix like paths
039        String url = "file:///home/username/dev/maven";
040        BazaarScmProviderRepository repo = new BazaarScmProviderRepository( url );
041        assertEquals( url, repo.getURI() );
042        assertNull( repo.validateURI() );
043
044        //2. Test windows like paths (with slash)
045        url = "file://C:/Documents and Settings/username/dev/maven";
046        repo = new BazaarScmProviderRepository( url );
047        assertEquals( url, repo.getURI() );
048        assertNull( repo.validateURI() );
049
050        url = "file:///c:/program files/cygwin/tmp/test";
051        repo = new BazaarScmProviderRepository( url );
052        assertEquals( url, repo.getURI() );
053        assertNull( repo.validateURI() );
054
055        //3. Test windows like paths (with backslash)
056        url = "file://C:\\Documents and Settings\\username\\dev\\maven";
057        repo = new BazaarScmProviderRepository( url );
058        assertEquals( url, repo.getURI() );
059        assertNull( repo.validateURI() );
060
061        //4. Test invalid file url
062        url = "file:/C:\\Documents and Settings\\username\\dev\\maven";
063        repo = new BazaarScmProviderRepository( url );
064        assertEquals( url, repo.getURI() );
065        assertNotNull( repo.validateURI() );
066    }
067
068    public void testSFTPRepo()
069    {
070        //1. Test with relativ path
071        String url = "sftp://username:password@myhost.com/~/dev/maven";
072        BazaarScmProviderRepository repo = new BazaarScmProviderRepository( url );
073        assertEquals( url, repo.getURI() );
074        assertNull( repo.validateURI() );
075
076        //2. Test with absolute path
077        url = "sftp://username:password@myhost.com/home/username/dev/maven";
078        repo = new BazaarScmProviderRepository( url );
079        assertEquals( url, repo.getURI() );
080        assertNull( repo.validateURI() );
081
082        //3. Test with incomplete URL but set password later
083        String incompleteUrl = "sftp://username@myhost.com/home/username/dev/maven";
084        repo = new BazaarScmProviderRepository( incompleteUrl );
085        assertEquals( incompleteUrl, repo.getURI() ); //This should still work...
086        assertNotNull( repo.validateURI() );
087
088        //Set password
089        repo.setPassword( "password" );
090        assertEquals( url, repo.getURI() );
091        assertNull( repo.validateURI() );
092    }
093
094    public void testHTTPRepo()
095    {
096        //1. Test with relativ path
097        String url = "http://www.myhost.com/~username/dev/maven";
098        BazaarScmProviderRepository repo = new BazaarScmProviderRepository( url );
099        assertEquals( url, repo.getURI() );
100        assertNull( repo.validateURI() );
101
102        //2. Test with absolute path
103        url = "http://www.myhost.com/dev/maven";
104        repo = new BazaarScmProviderRepository( url );
105        assertEquals( url, repo.getURI() );
106        assertNull( repo.validateURI() );
107
108        //3. Test with unessesary authentication information
109        repo.setPassword( "Password" );
110        repo.setUser( "User" );
111        repo.setPassphrase( "Passphrase" );
112        assertEquals( "http://www.myhost.com/dev/maven", repo.getURI() );
113        assertNull( repo.validateURI() );
114        repo.setPort( 81 );
115        assertEquals( "http://www.myhost.com:81/dev/maven", repo.getURI() );
116        assertNull( repo.validateURI() );
117    }
118
119    public void testBzrRepo() {
120        testAuthenticationProtocolRepo( "bzr" );
121    }
122
123    public void testBzrPlusSshRepo() {
124        testAuthenticationProtocolRepo( "bzr+ssh" );
125    }
126
127    public void testSshRepo() {
128        testAuthenticationProtocolRepo( "ssh" );
129    }
130
131    private void testAuthenticationProtocolRepo(String protocol) {
132        String url = protocol + "://myserver.net/testroot/myproject/trunk/";
133        BazaarScmProviderRepository repo = new BazaarScmProviderRepository( url );
134
135        repo.setPassword( "Password" );
136        repo.setUser( "User" );
137        repo.setPassphrase( "Passphrase" );
138        assertNull( repo.validateURI() );
139
140        assertEquals( protocol +  "://User:Password@myserver.net/testroot/myproject/trunk/", repo.getURI() );
141        assertNull( repo.validateURI() );
142        repo.setPort( 4776 );
143        assertEquals( protocol + "://User:Password@myserver.net:4776/testroot/myproject/trunk/", repo.getURI() );
144        assertNull( repo.validateURI() );
145   
146    }
147    /**
148     * @throws Exception
149     */
150    public void testParseHostAndPort()
151        throws Exception
152    {
153        String url = "http://localhost:8000/";
154        BazaarScmProviderRepository repo = new BazaarScmProviderRepository( url );
155        System.out.println(repo.getURI());
156        assertEquals( repo.getURI(), url );
157
158        url = "http://localhost/";
159        repo = new BazaarScmProviderRepository( url );
160        assertEquals( repo.getURI(), url );
161
162        url = "http://www.myhost.com:81/dev/maven";
163        repo = new BazaarScmProviderRepository( url );
164        assertEquals( repo.getURI(), url );
165    }
166}