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.

BaseFileTest.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.io.File;
  23. /**
  24. * BaseFileTest
  25. *
  26. *
  27. */
  28. public class BaseFileTest
  29. extends TestCase
  30. {
  31. public void testFileString()
  32. {
  33. File repoDir = new File( "/home/user/foo/repository" );
  34. String pathFile = "path/to/resource.xml";
  35. BaseFile file = new BaseFile( repoDir, pathFile );
  36. assertAbsolutePath( "/home/user/foo/repository/path/to/resource.xml", file );
  37. assertRelativePath( "path/to/resource.xml", file );
  38. assertBasedir( "/home/user/foo/repository", file );
  39. }
  40. public void testFileFile()
  41. {
  42. File repoDir = new File( "/home/user/foo/repository" );
  43. File pathFile = new File( "/home/user/foo/repository/path/to/resource.xml" );
  44. BaseFile file = new BaseFile( repoDir, pathFile );
  45. assertAbsolutePath( "/home/user/foo/repository/path/to/resource.xml", file );
  46. assertRelativePath( "path/to/resource.xml", file );
  47. assertBasedir( "/home/user/foo/repository", file );
  48. }
  49. public void testStringFile()
  50. {
  51. String repoDir = "/home/user/foo/repository";
  52. File pathFile = new File( "/home/user/foo/repository/path/to/resource.xml" );
  53. BaseFile file = new BaseFile( repoDir, pathFile );
  54. assertAbsolutePath( "/home/user/foo/repository/path/to/resource.xml", file );
  55. assertRelativePath( "path/to/resource.xml", file );
  56. assertBasedir( "/home/user/foo/repository", file );
  57. }
  58. public void testFileThenSetBaseString()
  59. {
  60. String repoDir = "/home/user/foo/repository";
  61. File pathFile = new File( "/home/user/foo/repository/path/to/resource.xml" );
  62. BaseFile file = new BaseFile( pathFile );
  63. file.setBaseDir( repoDir );
  64. assertAbsolutePath( "/home/user/foo/repository/path/to/resource.xml", file );
  65. assertRelativePath( "path/to/resource.xml", file );
  66. assertBasedir( "/home/user/foo/repository", file );
  67. }
  68. public void testFileThenSetBaseFile()
  69. {
  70. File repoDir = new File( "/home/user/foo/repository" );
  71. File pathFile = new File( "/home/user/foo/repository/path/to/resource.xml" );
  72. BaseFile file = new BaseFile( pathFile );
  73. file.setBaseDir( repoDir );
  74. assertAbsolutePath( "/home/user/foo/repository/path/to/resource.xml", file );
  75. assertRelativePath( "path/to/resource.xml", file );
  76. assertBasedir( "/home/user/foo/repository", file );
  77. }
  78. public void testStringThenSetBaseString()
  79. {
  80. String repoDir = "/home/user/foo/repository";
  81. String pathFile = "/home/user/foo/repository/path/to/resource.xml";
  82. BaseFile file = new BaseFile( pathFile );
  83. file.setBaseDir( repoDir );
  84. assertAbsolutePath( "/home/user/foo/repository/path/to/resource.xml", file );
  85. assertRelativePath( "path/to/resource.xml", file );
  86. assertBasedir( "/home/user/foo/repository", file );
  87. }
  88. public void testStringThenSetBaseFile()
  89. {
  90. File repoDir = new File( "/home/user/foo/repository" );
  91. String pathFile = "/home/user/foo/repository/path/to/resource.xml";
  92. BaseFile file = new BaseFile( pathFile );
  93. file.setBaseDir( repoDir );
  94. assertAbsolutePath( "/home/user/foo/repository/path/to/resource.xml", file );
  95. assertRelativePath( "path/to/resource.xml", file );
  96. assertBasedir( "/home/user/foo/repository", file );
  97. }
  98. private void assertAbsolutePath( String expectedPath, BaseFile actualFile )
  99. {
  100. assertEquals( new File( expectedPath ).getAbsolutePath(), actualFile.getAbsolutePath() );
  101. }
  102. private void assertRelativePath( String expectedPath, BaseFile actualFile )
  103. {
  104. assertEquals( expectedPath, StringUtils.replace( actualFile.getRelativePath(), "\\", "/" ) );
  105. }
  106. private void assertBasedir( String expectedPath, BaseFile actualFile )
  107. {
  108. assertEquals( new File( expectedPath ), actualFile.getBaseDir() );
  109. }
  110. }