You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PathUtilTest.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package org.apache.archiva.common.utils;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import junit.framework.TestCase;
  21. import org.apache.commons.lang.StringUtils;
  22. import java.nio.file.Path;
  23. import java.nio.file.Paths;
  24. /**
  25. * PathUtilTest
  26. *
  27. *
  28. */
  29. public class PathUtilTest
  30. extends TestCase
  31. {
  32. public void testToRelativeWithoutSlash()
  33. {
  34. assertEquals( "path/to/resource.xml", PathUtil.getRelative( "/home/user/foo/repository",
  35. "/home/user/foo/repository/path/to/resource.xml" ) );
  36. }
  37. public void testToRelativeWithSlash()
  38. {
  39. assertEquals( "path/to/resource.xml", PathUtil.getRelative( "/home/user/foo/repository/",
  40. "/home/user/foo/repository/path/to/resource.xml" ) );
  41. }
  42. public void testToUrlRelativePath()
  43. {
  44. Path workingDir = Paths.get( "" );
  45. String workingDirname = StringUtils.replaceChars( workingDir.toAbsolutePath().toString(), '\\', '/' );
  46. // Some JVM's retain the "." at the end of the path. Drop it.
  47. if ( workingDirname.endsWith( "/." ) )
  48. {
  49. workingDirname = workingDirname.substring( 0, workingDirname.length() - 2 );
  50. }
  51. if ( !workingDirname.startsWith( "/" ) )
  52. {
  53. workingDirname = "/" + workingDirname;
  54. }
  55. String path = "path/to/resource.xml";
  56. String expectedPath = "file:" + workingDirname + "/" + path;
  57. assertEquals( expectedPath, PathUtil.toUrl( path ) );
  58. }
  59. public void testToUrlUsingFileUrl()
  60. {
  61. Path workingDir = Paths.get( "." );
  62. String workingDirname = StringUtils.replaceChars( workingDir.toAbsolutePath().toString(), '\\', '/' );
  63. // Some JVM's retain the "." at the end of the path. Drop it.
  64. if ( workingDirname.endsWith( "/." ) )
  65. {
  66. workingDirname = workingDirname.substring( 0, workingDirname.length() - 2 );
  67. }
  68. if ( !workingDirname.startsWith( "/" ) )
  69. {
  70. workingDirname = "/" + workingDirname;
  71. }
  72. String path = "path/to/resource.xml";
  73. String expectedPath = "file:" + workingDirname + "/" + path;
  74. assertEquals( expectedPath, PathUtil.toUrl( expectedPath ) );
  75. }
  76. }