001package org.apache.maven.wagon; 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 java.io.File; 023 024import junit.framework.TestCase; 025 026/** 027 * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a> 028 * 029 */ 030public class PathUtilsTest 031 extends TestCase 032{ 033 public void testFilenameResolving() 034 { 035 assertEquals( "filename", PathUtils.filename( "dir/filename" ) ); 036 037 assertEquals( "filename", PathUtils.filename( "filename" ) ); 038 039 assertEquals( "filename", PathUtils.filename( "dir1/dir2/filename" ) ); 040 } 041 042 public void testDirResolving() 043 { 044 assertEquals( "dir", PathUtils.dirname( "dir/filename" ) ); 045 046 assertEquals( "", PathUtils.dirname( "filename" ) ); 047 048 assertEquals( "dir1/dir2", PathUtils.dirname( "dir1/dir2/filename" ) ); 049 } 050 051 public void testDirSpliting() 052 { 053 final String path = "a/b/c"; 054 055 final String[] dirs = PathUtils.dirnames( path ); 056 057 assertNotNull( dirs ); 058 059 assertEquals( 2, dirs.length ); 060 061 assertEquals( "a", dirs[0] ); 062 063 assertEquals( "b", dirs[1] ); 064 065 } 066 067 public void testHostResolving() 068 { 069 assertEquals( "www.codehaus.org", PathUtils.host( "http://www.codehaus.org" ) ); 070 assertEquals( "www.codehaus.org", PathUtils.host( "HTTP://www.codehaus.org" ) ); 071 072 assertEquals( "localhost", PathUtils.host( null ) ); 073 assertEquals( "localhost", PathUtils.host( "file:///c:/temp" ) ); 074 assertEquals( "localhost", PathUtils.host( "FILE:///c:/temp" ) ); 075 076 } 077 078 public void testScmHostResolving() 079 { 080 assertEquals( "www.codehaus.org", PathUtils.host( "scm:svn:http://www.codehaus.org" ) ); 081 assertEquals( "www.codehaus.org", PathUtils.host( "SCM:SVN:HTTP://www.codehaus.org" ) ); 082 assertEquals( "www.codehaus.org", PathUtils.host( "scm:svn:http://www.codehaus.org/repos/module" ) ); 083 assertEquals( "www.codehaus.org", PathUtils.host( "SCM:SVN:HTTP://www.codehaus.org/repos/module" ) ); 084 assertEquals( "www.codehaus.org", PathUtils.host( "scm:cvs:pserver:anoncvs@www.codehaus.org:/root" ) ); 085 assertEquals( "www.codehaus.org", PathUtils.host( "SCM:CVS:pserver:anoncvs@www.codehaus.org:/root" ) ); 086 } 087 088 public void testProtocolResolving() 089 { 090 assertEquals( "http", PathUtils.protocol( "http://www.codehause.org" ) ); 091 assertEquals( "HTTP", PathUtils.protocol( "HTTP://www.codehause.org" ) ); 092 assertEquals( "file", PathUtils.protocol( "file:///c:/temp" ) ); 093 assertEquals( "scm", PathUtils.protocol( "scm:svn:http://localhost/repos/module" ) ); 094 assertEquals( "scm", PathUtils.protocol( "scm:cvs:pserver:anoncvs@cvs.apache.org:/home/cvspublic" ) ); 095 } 096 097 public void testUserInfo() 098 { 099 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 testIpV4() 257 { 258 assertUrl( "http://127.0.0.1", "http", null, null, "127.0.0.1", -1, "/" ); 259 assertUrl( "http://127.0.0.1:8080", "http", null, null, "127.0.0.1", 8080, "/" ); 260 assertUrl( "http://127.0.0.1/oo/rest/users", "http", null, null, "127.0.0.1", -1, "/oo/rest/users" ); 261 assertUrl( "http://127.0.0.1:8080/oo/rest/users", "http", null, null, "127.0.0.1", 8080, "/oo/rest/users" ); 262 263 assertUrl( "http://user:password@127.0.0.1", "http", "user", "password", "127.0.0.1", -1, "/" ); 264 assertUrl( "http://user:password@127.0.0.1:8080", "http", "user", "password", "127.0.0.1", 8080, "/" ); 265 assertUrl( "http://user:password@127.0.0.1/oo/rest/users", "http", "user", "password", "127.0.0.1", -1, 266 "/oo/rest/users" ); 267 assertUrl( "http://user:password@127.0.0.1:8080/oo/rest/users", "http", "user", "password", "127.0.0.1", 8080, 268 "/oo/rest/users" ); 269 270 assertUrl( "scm:svn:http://user:password@127.0.0.1:8080/oo/rest/users", "scm", "user", "password", "127.0.0.1", 271 8080, "/oo/rest/users" ); 272 } 273 274 public void testIPv6() 275 { 276 assertUrl( "http://user:password@[fff:::1]:7891/oo/rest/users", "http", "user", "password", "fff:::1", 7891, 277 "/oo/rest/users" ); 278 assertUrl( "http://[fff:::1]:7891/oo/rest/users", "http", null, null, "fff:::1", 7891, "/oo/rest/users" ); 279 assertUrl( "http://user:password@[fff:::1]/oo/rest/users", "http", "user", "password", "fff:::1", -1, 280 "/oo/rest/users" ); 281 assertUrl( "http://user:password@[fff:::1]:7891", "http", "user", "password", "fff:::1", 7891, "/" ); 282 283 assertUrl( "http://user:password@[fff:000::222:1111]:7891/oo/rest/users", "http", "user", "password", 284 "fff:000::222:1111", 7891, "/oo/rest/users" ); 285 assertUrl( "http://[fff:000::222:1111]:7891/oo/rest/users", "http", null, null, "fff:000::222:1111", 7891, 286 "/oo/rest/users" ); 287 assertUrl( "http://user:password@[fff:000::222:1111]/oo/rest/users", "http", "user", "password", 288 "fff:000::222:1111", -1, "/oo/rest/users" ); 289 assertUrl( "http://user:password@[fff:000::222:1111]:7891", "http", "user", "password", "fff:000::222:1111", 290 7891, "/" ); 291 292 assertUrl( "http://user:password@16.60.56.58:7891/oo/rest/users", "http", "user", "password", "16.60.56.58", 293 7891, "/oo/rest/users" ); 294 assertUrl( "http://16.60.56.58:7891/oo/rest/users", "http", null, null, "16.60.56.58", 7891, "/oo/rest/users" ); 295 assertUrl( "http://user:password@16.60.56.58/oo/rest/users", "http", "user", "password", "16.60.56.58", -1, 296 "/oo/rest/users" ); 297 assertUrl( "http://user:password@16.60.56.58:7891", "http", "user", "password", "16.60.56.58", 7891, "/" ); 298 299 assertUrl( "http://user:password@16.60.56.58:7891/oo/rest/users", "http", "user", "password", "16.60.56.58", 300 7891, "/oo/rest/users" ); 301 assertUrl( "http://16.60.56.58:7891/oo/rest/users", "http", null, null, "16.60.56.58", 7891, "/oo/rest/users" ); 302 assertUrl( "http://user:password@16.60.56.58/oo/rest/users", "http", "user", "password", "16.60.56.58", -1, 303 "/oo/rest/users" ); 304 assertUrl( "http://user:password@16.60.56.58:7891", "http", "user", "password", "16.60.56.58", 7891, "/" ); 305 } 306 307 private void assertUrl( String url, String protocol, String user, String password, String host, int port, 308 String basedir ) 309 { 310 assertEquals( protocol, PathUtils.protocol( url ) ); 311 assertEquals( user, PathUtils.user( url ) ); 312 assertEquals( password, PathUtils.password( url ) ); 313 assertEquals( host, PathUtils.host( url ) ); 314 assertEquals( port, PathUtils.port( url ) ); 315 assertEquals( basedir, PathUtils.basedir( url ) ); 316 } 317 318 public void testToRelative() 319 { 320 assertEquals( "dir", PathUtils.toRelative( new File( "/home/user" ).getAbsoluteFile(), 321 new File( "/home/user/dir" ).getAbsolutePath() ) ); 322 assertEquals( "dir", PathUtils.toRelative( new File( "C:/home/user" ).getAbsoluteFile(), 323 new File( "C:/home/user/dir" ).getAbsolutePath() ) ); 324 325 assertEquals( "dir/subdir", PathUtils.toRelative( new File( "/home/user" ).getAbsoluteFile(), 326 new File( "/home/user/dir/subdir" ).getAbsolutePath() ) ); 327 assertEquals( "dir/subdir", PathUtils.toRelative( new File( "C:/home/user" ).getAbsoluteFile(), 328 new File( "C:/home/user/dir/subdir" ).getAbsolutePath() ) ); 329 330 assertEquals( ".", PathUtils.toRelative( new File( "/home/user" ).getAbsoluteFile(), 331 new File( "/home/user" ).getAbsolutePath() ) ); 332 assertEquals( ".", PathUtils.toRelative( new File( "C:/home/user" ).getAbsoluteFile(), 333 new File( "C:/home/user" ).getAbsolutePath() ) ); 334 } 335 336}