View Javadoc

1   /*
2    * #%L
3    * Native ARchive plugin for Maven
4    * %%
5    * Copyright (C) 2002 - 2014 NAR Maven Plugin developers.
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * 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, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package com.github.maven_nar.cpptasks;
21  
22  import java.io.File;
23  import java.io.IOException;
24  
25  import junit.framework.TestCase;
26  
27  /**
28   * Tests for CUtil class
29   */
30  public class TestCUtil extends TestCase {
31    public TestCUtil(final String name) {
32      super(name);
33    }
34  
35    public void testGetPathFromEnvironment() {
36      final File[] files = CUtil.getPathFromEnvironment("LIB", ";");
37      assertNotNull(files);
38    }
39  
40    public void testGetRelativePath1() throws IOException {
41      final String canonicalBase = new File("/foo/bar/").getCanonicalPath();
42      final String rel = CUtil.getRelativePath(canonicalBase, new File("/foo/bar/baz"));
43      assertEquals("baz", rel);
44    }
45  
46    public void testGetRelativePath10() throws IOException {
47      final String canonicalBase = new File("/foo/bar/something").getCanonicalPath();
48      final String rel = CUtil.getRelativePath(canonicalBase, new File("/foo/bar/something else"));
49      final String expected = ".." + File.separator + "something else";
50      assertEquals(expected, rel);
51    }
52  
53    public void testGetRelativePath2() throws IOException {
54      final String canonicalBase = new File("/foo/bar/").getCanonicalPath();
55      final String rel = CUtil.getRelativePath(canonicalBase, new File("/foo/bar/"));
56      assertEquals(".", rel);
57    }
58  
59    public void testGetRelativePath3() throws IOException {
60      final String canonicalBase = new File("/foo/bar/").getCanonicalPath();
61      final String rel = CUtil.getRelativePath(canonicalBase, new File("/foo/bar/a"));
62      assertEquals("a", rel);
63    }
64  
65    public void testGetRelativePath4() throws IOException {
66      final String canonicalBase = new File("/foo/bar/").getCanonicalPath();
67      final String rel = CUtil.getRelativePath(canonicalBase, new File("/foo/"));
68      assertEquals("..", rel);
69    }
70  
71    public void testGetRelativePath5() throws IOException {
72      final String canonicalBase = new File("/foo/bar/").getCanonicalPath();
73      final String rel = CUtil.getRelativePath(canonicalBase, new File("/a"));
74      final String expected = ".." + File.separator + ".." + File.separator + "a";
75      assertEquals(expected, rel);
76    }
77  
78    public void testGetRelativePath6() throws IOException {
79      final String canonicalBase = new File("/foo/bar/").getCanonicalPath();
80      final String rel = CUtil.getRelativePath(canonicalBase, new File("/foo/baz/bar"));
81      final String expected = ".." + File.separator + "baz" + File.separator + "bar";
82      assertEquals(expected, rel);
83    }
84  
85    public void testGetRelativePath7() throws IOException {
86      final String canonicalBase = new File("/foo/bar/").getCanonicalPath();
87      //
88      // skip the UNC test unless running on Windows
89      //
90      final String osName = System.getProperty("os.name");
91      if (osName.contains("Windows")) {
92        final File uncFile = new File("\\\\fred\\foo.bar");
93        String uncPath;
94        try {
95          uncPath = uncFile.getCanonicalPath();
96        } catch (final IOException ex) {
97          uncPath = uncFile.toString();
98        }
99        final String rel = CUtil.getRelativePath(canonicalBase, uncFile);
100       assertEquals(uncPath, rel);
101     }
102   }
103 
104   public void testGetRelativePath8() throws IOException {
105     final String canonicalBase = new File("/foo/bar/something").getCanonicalPath();
106     final String rel = CUtil.getRelativePath(canonicalBase, new File("/foo/bar/something.extension"));
107     final String expected = ".." + File.separator + "something.extension";
108     assertEquals(expected, rel);
109   }
110 
111   public void testGetRelativePath9() throws IOException {
112     final String canonicalBase = new File("/foo/bar/something").getCanonicalPath();
113     final String rel = CUtil.getRelativePath(canonicalBase, new File("/foo/bar/somethingElse"));
114     final String expected = ".." + File.separator + "somethingElse";
115     assertEquals(expected, rel);
116   }
117 
118   public void testParsePath1() {
119     final File[] files = CUtil.parsePath("", ";");
120     assertEquals(0, files.length);
121   }
122 
123   public void testParsePath2() {
124     final String workingDir = System.getProperty("user.dir");
125     final File[] files = CUtil.parsePath(workingDir, ";");
126     assertEquals(1, files.length);
127     final File workingDirFile = new File(workingDir);
128     assertEquals(workingDirFile, files[0]);
129   }
130 
131   public void testParsePath3() {
132     final String workingDir = System.getProperty("user.dir");
133     final File[] files = CUtil.parsePath(workingDir + ";", ";");
134     assertEquals(1, files.length);
135     assertEquals(new File(workingDir), files[0]);
136   }
137 
138   public void testParsePath4() {
139     final String workingDir = System.getProperty("user.dir");
140     final String javaHome = System.getProperty("java.home");
141     final File[] files = CUtil.parsePath(workingDir + ";" + javaHome, ";");
142     assertEquals(2, files.length);
143     assertEquals(new File(workingDir), files[0]);
144     assertEquals(new File(javaHome), files[1]);
145   }
146 
147   public void testParsePath5() {
148     final String workingDir = System.getProperty("user.dir");
149     final String javaHome = System.getProperty("java.home");
150     final File[] files = CUtil.parsePath(workingDir + ";" + javaHome + ";", ";");
151     assertEquals(2, files.length);
152     assertEquals(new File(workingDir), files[0]);
153     assertEquals(new File(javaHome), files[1]);
154   }
155 
156   /**
157    * Test of xmlAttributeEncode.
158    *
159    * See patch 1267472 and bug 1032302.
160    */
161   public void testXmlEncode() {
162     assertEquals("&lt;&quot;boo&quot;&gt;", CUtil.xmlAttribEncode("<\"boo\">"));
163   }
164 }