1 package org.apache.archiva.common.utils;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import org.apache.commons.lang.StringUtils;
24 import junit.framework.TestCase;
33 public class BaseFileTest
36 public void testFileString()
38 File repoDir = new File( "/home/user/foo/repository" );
39 String pathFile = "path/to/resource.xml";
40 BaseFile file = new BaseFile( repoDir, pathFile );
42 assertAbsolutePath( "/home/user/foo/repository/path/to/resource.xml", file );
43 assertRelativePath( "path/to/resource.xml", file );
44 assertBasedir( "/home/user/foo/repository", file );
47 public void testFileFile()
49 File repoDir = new File( "/home/user/foo/repository" );
50 File pathFile = new File( "/home/user/foo/repository/path/to/resource.xml" );
51 BaseFile file = new BaseFile( repoDir, pathFile );
53 assertAbsolutePath( "/home/user/foo/repository/path/to/resource.xml", file );
54 assertRelativePath( "path/to/resource.xml", file );
55 assertBasedir( "/home/user/foo/repository", file );
58 public void testStringFile()
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( repoDir, pathFile );
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 );
69 public void testFileThenSetBaseString()
71 String repoDir = "/home/user/foo/repository";
72 File pathFile = new File( "/home/user/foo/repository/path/to/resource.xml" );
73 BaseFile file = new BaseFile( pathFile );
74 file.setBaseDir( repoDir );
76 assertAbsolutePath( "/home/user/foo/repository/path/to/resource.xml", file );
77 assertRelativePath( "path/to/resource.xml", file );
78 assertBasedir( "/home/user/foo/repository", file );
81 public void testFileThenSetBaseFile()
83 File repoDir = new File( "/home/user/foo/repository" );
84 File pathFile = new File( "/home/user/foo/repository/path/to/resource.xml" );
85 BaseFile file = new BaseFile( pathFile );
86 file.setBaseDir( repoDir );
88 assertAbsolutePath( "/home/user/foo/repository/path/to/resource.xml", file );
89 assertRelativePath( "path/to/resource.xml", file );
90 assertBasedir( "/home/user/foo/repository", file );
93 public void testStringThenSetBaseString()
95 String repoDir = "/home/user/foo/repository";
96 String pathFile = "/home/user/foo/repository/path/to/resource.xml";
97 BaseFile file = new BaseFile( pathFile );
98 file.setBaseDir( repoDir );
100 assertAbsolutePath( "/home/user/foo/repository/path/to/resource.xml", file );
101 assertRelativePath( "path/to/resource.xml", file );
102 assertBasedir( "/home/user/foo/repository", file );
105 public void testStringThenSetBaseFile()
107 File repoDir = new File( "/home/user/foo/repository" );
108 String pathFile = "/home/user/foo/repository/path/to/resource.xml";
109 BaseFile file = new BaseFile( pathFile );
110 file.setBaseDir( repoDir );
112 assertAbsolutePath( "/home/user/foo/repository/path/to/resource.xml", file );
113 assertRelativePath( "path/to/resource.xml", file );
114 assertBasedir( "/home/user/foo/repository", file );
117 private void assertAbsolutePath( String expectedPath, BaseFile actualFile )
119 assertEquals( new File( expectedPath ).getAbsolutePath(), actualFile.getAbsolutePath() );
122 private void assertRelativePath( String expectedPath, BaseFile actualFile )
124 assertEquals( expectedPath, StringUtils.replace( actualFile.getRelativePath(), "\\", "/" ) );
127 private void assertBasedir( String expectedPath, BaseFile actualFile )
129 assertEquals( new File( expectedPath ), actualFile.getBaseDir() );