View Javadoc

1   package org.apache.maven.wagon;
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 java.io.File;
23  
24  import junit.framework.TestCase;
25  
26  /**
27   * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
28   *
29   */
30  public class PathUtilsTest
31      extends TestCase
32  {
33      public void testFilenameResolving()
34      {
35          assertEquals( "filename", PathUtils.filename( "dir/filename" ) );
36  
37          assertEquals( "filename", PathUtils.filename( "filename" ) );
38  
39          assertEquals( "filename", PathUtils.filename( "dir1/dir2/filename" ) );
40      }
41  
42      public void testDirResolving()
43      {
44          assertEquals( "dir", PathUtils.dirname( "dir/filename" ) );
45  
46          assertEquals( "", PathUtils.dirname( "filename" ) );
47  
48          assertEquals( "dir1/dir2", PathUtils.dirname( "dir1/dir2/filename" ) );
49      }
50  
51      public void testDirSpliting()
52      {
53          final String path = "a/b/c";
54  
55          final String[] dirs = PathUtils.dirnames( path );
56  
57          assertNotNull( dirs );
58  
59          assertEquals( 2, dirs.length );
60  
61          assertEquals( "a", dirs[0] );
62  
63          assertEquals( "b", dirs[1] );
64  
65      }
66  
67      public void testHostResolving()
68      {
69          assertEquals( "www.codehaus.org", PathUtils.host( "http://www.codehaus.org" ) );
70          assertEquals( "www.codehaus.org", PathUtils.host( "HTTP://www.codehaus.org" ) );
71  
72          assertEquals( "localhost", PathUtils.host( null ) );
73          assertEquals( "localhost", PathUtils.host( "file:///c:/temp" ) );
74          assertEquals( "localhost", PathUtils.host( "FILE:///c:/temp" ) );
75  
76      }
77  
78      public void testScmHostResolving()
79      {
80          assertEquals( "www.codehaus.org", PathUtils.host( "scm:svn:http://www.codehaus.org" ) );
81          assertEquals( "www.codehaus.org", PathUtils.host( "SCM:SVN:HTTP://www.codehaus.org" ) );
82          assertEquals( "www.codehaus.org", PathUtils.host( "scm:svn:http://www.codehaus.org/repos/module" ) );
83          assertEquals( "www.codehaus.org", PathUtils.host( "SCM:SVN:HTTP://www.codehaus.org/repos/module" ) );
84          assertEquals( "www.codehaus.org", PathUtils.host( "scm:cvs:pserver:anoncvs@www.codehaus.org:/root" ) );
85          assertEquals( "www.codehaus.org", PathUtils.host( "SCM:CVS:pserver:anoncvs@www.codehaus.org:/root" ) );
86      }
87  
88      public void testProtocolResolving()
89      {
90          assertEquals( "http", PathUtils.protocol( "http://www.codehause.org" ) );
91          assertEquals( "HTTP", PathUtils.protocol( "HTTP://www.codehause.org" ) );
92          assertEquals( "file", PathUtils.protocol( "file:///c:/temp" ) );
93          assertEquals( "scm", PathUtils.protocol( "scm:svn:http://localhost/repos/module" ) );
94          assertEquals( "scm", PathUtils.protocol( "scm:cvs:pserver:anoncvs@cvs.apache.org:/home/cvspublic" ) );
95      }
96  
97      public void testUserInfo()
98      {
99          String urlWithUsername = "http://brett@www.codehaus.org";
100         assertEquals( "brett", PathUtils.user( urlWithUsername ) );
101         assertNull( PathUtils.password( urlWithUsername ) );
102         assertEquals( "www.codehaus.org", PathUtils.host( urlWithUsername ) );
103         assertEquals( "/", PathUtils.basedir( urlWithUsername ) );
104 
105         String urlWithUsernamePassword = "http://brett:porter@www.codehaus.org";
106         assertEquals( "brett", PathUtils.user( urlWithUsernamePassword ) );
107         assertEquals( "porter", PathUtils.password( urlWithUsernamePassword ) );
108         assertEquals( "www.codehaus.org", PathUtils.host( urlWithUsernamePassword ) );
109         assertEquals( "/", PathUtils.basedir( urlWithUsernamePassword ) );
110     }
111 
112     public void testSubversionUserInfo()
113     {
114         String urlWithUsername = "scm:svn:http://brett@www.codehaus.org";
115         assertEquals( "brett", PathUtils.user( urlWithUsername ) );
116         assertNull( PathUtils.password( urlWithUsername ) );
117         assertEquals( "www.codehaus.org", PathUtils.host( urlWithUsername ) );
118         assertEquals( "/", PathUtils.basedir( urlWithUsername ) );
119 
120         String urlWithUsernamePassword = "scm:svn:http://brett:porter@www.codehaus.org";
121         assertEquals( "brett", PathUtils.user( urlWithUsernamePassword ) );
122         assertEquals( "porter", PathUtils.password( urlWithUsernamePassword ) );
123         assertEquals( "www.codehaus.org", PathUtils.host( urlWithUsernamePassword ) );
124         assertEquals( "/", PathUtils.basedir( urlWithUsernamePassword ) );
125 
126         String urlWithUpperCaseProtocol = "SCM:SVN:HTTP://brett@www.codehaus.org";
127         assertEquals( "brett", PathUtils.user( urlWithUpperCaseProtocol ) );
128         assertNull( PathUtils.password( urlWithUpperCaseProtocol ) );
129         assertEquals( "www.codehaus.org", PathUtils.host( urlWithUpperCaseProtocol ) );
130         assertEquals( "/", PathUtils.basedir( urlWithUpperCaseProtocol ) );
131     }
132 
133     public void testCvsUserInfo()
134     {
135         String urlWithUsername = "scm:cvs:pserver:brett@www.codehaus.org";
136         assertEquals( "brett", PathUtils.user( urlWithUsername ) );
137         assertNull( PathUtils.password( urlWithUsername ) );
138         assertEquals( "www.codehaus.org", PathUtils.host( urlWithUsername ) );
139         assertEquals( "/", PathUtils.basedir( urlWithUsername ) );
140 
141         String urlWithUsernamePassword = "scm:cvs:pserver:brett:porter@www.codehaus.org";
142         assertEquals( "brett", PathUtils.user( urlWithUsernamePassword ) );
143         assertEquals( "porter", PathUtils.password( urlWithUsernamePassword ) );
144         assertEquals( "www.codehaus.org", PathUtils.host( urlWithUsernamePassword ) );
145         assertEquals( "/", PathUtils.basedir( urlWithUsernamePassword ) );
146 
147         String urlWithUpperCaseProtocol = "SCM:CVS:pserver:brett@www.codehaus.org";
148         assertEquals( "brett", PathUtils.user( urlWithUpperCaseProtocol ) );
149         assertNull( PathUtils.password( urlWithUpperCaseProtocol ) );
150         assertEquals( "www.codehaus.org", PathUtils.host( urlWithUpperCaseProtocol ) );
151         assertEquals( "/", PathUtils.basedir( urlWithUpperCaseProtocol ) );
152     }
153 
154     public void testFileBasedir()
155     {
156         // see http://www.mozilla.org/quality/networking/testing/filetests.html
157         
158         // strict forms
159         assertEquals( "c:/temp", PathUtils.basedir( "file:///c|/temp" ) );
160         assertEquals( "localhost", PathUtils.host( "file:///c|/temp" ) );
161         assertEquals( "c:/temp", PathUtils.basedir( "file://localhost/c|/temp" ) );
162         assertEquals( "localhost", PathUtils.host( "file://localhost/c|/temp" ) );
163         assertEquals( "/temp", PathUtils.basedir( "file:///temp" ) );
164         assertEquals( "localhost", PathUtils.host( "file:///temp" ) );
165         assertEquals( "/temp", PathUtils.basedir( "file://localhost/temp" ) );
166         assertEquals( "localhost", PathUtils.host( "file://localhost/temp" ) );
167 
168         // strict form, with : for drive separator
169         assertEquals( "c:/temp", PathUtils.basedir( "file:///c:/temp" ) );
170         assertEquals( "localhost", PathUtils.host( "file:///c:/temp" ) );
171         assertEquals( "c:/temp", PathUtils.basedir( "file://localhost/c:/temp" ) );
172         assertEquals( "localhost", PathUtils.host( "file://localhost/c:/temp" ) );
173 
174         // convenience forms
175         assertEquals( "c:/temp", PathUtils.basedir( "file://c:/temp" ) );
176         assertEquals( "c:/temp", PathUtils.basedir( "file://c|/temp" ) );
177         assertEquals( "c:/temp", PathUtils.basedir( "file:c:/temp" ) );
178         assertEquals( "c:/temp", PathUtils.basedir( "file:c|/temp" ) );
179         assertEquals( "/temp", PathUtils.basedir( "file:/temp" ) );
180 
181         // URL decoding
182         assertEquals( "c:/my docs", PathUtils.basedir( "file:///c:/my docs" ) );
183         assertEquals( "c:/my docs", PathUtils.basedir( "file:///c:/my%20docs" ) );
184         assertEquals( "c:/name #%20?{}[]<>.txt", PathUtils.basedir( "file:///c:/name%20%23%2520%3F%7B%7D%5B%5D%3C%3E.txt" ) );
185 
186         assertEquals( "c:/temp", PathUtils.basedir( "FILE:///c:/temp" ) );
187         assertEquals( "localhost", PathUtils.host( "FILE:///c:/temp" ) );
188     }
189 
190     public void testEmptyBasedir()
191     {
192         assertEquals( "/", PathUtils.basedir( "http://www.codehaus.org:80" ) );
193         assertEquals( "/", PathUtils.basedir( "http://www.codehaus.org" ) );
194         assertEquals( "/", PathUtils.basedir( "http://www.codehaus.org:80/" ) );
195         assertEquals( "/", PathUtils.basedir( "http://www.codehaus.org/" ) );
196         assertEquals( "/", PathUtils.basedir( "HTTP://www.codehaus.org/" ) );
197     }
198 
199     public void testEmptyProtocol()
200     {
201         assertEquals( "", PathUtils.protocol( "placeholder-only" ) );
202         assertEquals( "", PathUtils.protocol( "placeholder-only/module-a" ) );
203 
204         assertEquals( "placeholder-only", PathUtils.authorization( "placeholder-only" ) );
205         assertEquals( "placeholder-only", PathUtils.authorization( "placeholder-only/module-a" ) );
206 
207         assertEquals( -1, PathUtils.port( "placeholder-only" ) );
208         assertEquals( -1, PathUtils.port( "placeholder-only/module-a" ) );
209 
210         assertEquals( "/", PathUtils.basedir( "placeholder-only" ) );
211         assertEquals( "/module-a", PathUtils.basedir( "placeholder-only/module-a" ) );
212     }
213 
214     public void testPortResolving()
215     {
216         assertEquals( 80, PathUtils.port( "http://www.codehause.org:80/maven" ) );
217         assertEquals( 80, PathUtils.port( "HTTP://www.codehause.org:80/maven" ) );
218         assertEquals( WagonConstants.UNKNOWN_PORT, PathUtils.port( "http://localhost/temp" ) );
219 
220         assertEquals( 10, PathUtils.port( "ftp://localhost:10" ) );
221         assertEquals( 10, PathUtils.port( "FTP://localhost:10" ) );
222     }
223 
224     public void testScmPortResolving()
225     {
226         assertEquals( 80, PathUtils.port( "scm:svn:http://www.codehaus.org:80/maven" ) );
227         assertEquals( 80, PathUtils.port( "SCM:SVN:HTTP://www.codehaus.org:80/maven" ) );
228         assertEquals( WagonConstants.UNKNOWN_PORT, PathUtils.port( "scm:cvs:pserver:anoncvs@localhost:/temp:module" ) );
229 
230         assertEquals( 2402, PathUtils.port( "scm:cvs:pserver:anoncvs@localhost:2402/temp:module" ) );
231         assertEquals( 2402, PathUtils.port( "SCM:CVS:pserver:anoncvs@localhost:2402/temp:module" ) );
232     }
233 
234     public void testScmBasedir()
235     {
236         assertEquals( "/maven", PathUtils.basedir( "scm:svn:http://www.codehause.org/maven" ) );
237         assertEquals( "/maven", PathUtils.basedir( "SCM:SVN:HTTP://www.codehause.org/maven" ) );
238         assertEquals( "/maven", PathUtils.basedir( "scm:svn:http://www.codehause.org:80/maven" ) );
239         assertEquals( "/maven", PathUtils.basedir( "scm:cvs:pserver:anoncvs@www.codehause.org:80/maven" ) );
240         assertEquals( "/maven", PathUtils.basedir( "scm:cvs:pserver:anoncvs@www.codehause.org:/maven" ) );
241         assertEquals( "/maven/module", PathUtils.basedir( "scm:cvs:pserver:anoncvs@www.codehause.org:80/maven:module" ) );
242         assertEquals( "/maven/module", PathUtils.basedir( "scm:cvs:pserver:anoncvs@www.codehause.org:/maven:module" ) );
243         assertEquals( "/maven/module", PathUtils.basedir( "SCM:CVS:pserver:anoncvs@www.codehause.org:/maven:module" ) );
244     }
245 
246     public void testPortBasedir()
247     {
248         assertEquals( "/maven", PathUtils.basedir( "http://www.codehause.org:80/maven" ) );
249         assertEquals( "/temp", PathUtils.basedir( "http://localhost/temp" ) );
250 
251         assertEquals( "c:/temp", PathUtils.basedir( "file://c:/temp" ) );
252         assertEquals( "/", PathUtils.basedir( "http://localhost:80/" ) );
253         assertEquals( "/", PathUtils.basedir( "http://localhost/" ) );
254     }
255 
256     public void testToRelative()
257     {
258         assertEquals( "dir", PathUtils.toRelative( new File( "/home/user" ).getAbsoluteFile(),
259                                                    new File( "/home/user/dir" ).getAbsolutePath() ) );
260         assertEquals( "dir", PathUtils.toRelative( new File( "C:/home/user" ).getAbsoluteFile(),
261                                                    new File( "C:/home/user/dir" ).getAbsolutePath() ) );
262 
263         assertEquals( "dir/subdir", PathUtils.toRelative( new File( "/home/user" ).getAbsoluteFile(),
264                                                           new File( "/home/user/dir/subdir" ).getAbsolutePath() ) );
265         assertEquals( "dir/subdir", PathUtils.toRelative( new File( "C:/home/user" ).getAbsoluteFile(),
266                                                           new File( "C:/home/user/dir/subdir" ).getAbsolutePath() ) );
267 
268         assertEquals( ".", PathUtils.toRelative( new File( "/home/user" ).getAbsoluteFile(),
269                                                  new File( "/home/user" ).getAbsolutePath() ) );
270         assertEquals( ".", PathUtils.toRelative( new File( "C:/home/user" ).getAbsoluteFile(),
271                                                  new File( "C:/home/user" ).getAbsolutePath() ) );
272     }
273 
274 }