1   package org.apache.maven.plugin.reactor;
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 org.apache.maven.plugin.reactor.RelativePather.DifferentRootsException;
25  
26  import junit.framework.TestCase;
27  
28  public class RelativePatherTest extends TestCase {
29      File root;
30      File differentRoot;
31      char S = File.separatorChar;
32      
33      public void setUp() {
34          File[] roots = File.listRoots();
35          root = roots[0];
36          if (roots.length > 1) {
37              differentRoot = roots[1];
38          }
39      }
40      
41      public void testIdenticalRoots() {
42          assertEquals("", getRelativePath(root, root));
43      }
44      
45      public void testDifferentRoots() {
46          // skip this test on systems with only one root
47          if (differentRoot == null) return;
48          try {
49              getRelativePath(root, differentRoot);
50              fail("Expected different roots exception");
51          } catch (DifferentRootsException e) {}
52      }
53      
54      public void testIdenticalFoo() {
55          File foo = new File(root, "foo");
56          assertEquals("", getRelativePath(foo, foo));
57      }
58      
59      public void testIdenticalFooFoo() {
60          File foo = new File(root, "foo/foo");
61          assertEquals("", getRelativePath(foo, foo));
62      }
63      
64      public void testFooBar() {
65          File foo = new File(root, "foo");
66          File bar = new File(root, "bar");   
67          assertEquals(".." + S + "bar", getRelativePath(foo, bar));
68      }
69      
70      public void testRootFoo() {
71          File foo = new File(root, "foo");
72          assertEquals("foo", getRelativePath(root, foo));
73      }
74      
75      public void testFooRoot() {
76          File foo = new File(root, "foo");
77          assertEquals("..", getRelativePath(foo, root));
78      }
79  
80      public void testFooFooBarBar() {
81          File foo = new File(root, "foo/foo");
82          File bar = new File(root, "bar/bar");   
83          assertEquals(".." + S + ".." + S + "bar" + S + "bar", getRelativePath(foo, bar));
84      }
85      
86      public String getRelativePath(File context, File dest) {
87          return RelativePather.getRelativePath(context, dest);
88      }
89  }