View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.war.util;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.HashSet;
24  import java.util.Iterator;
25  import java.util.Set;
26  
27  import junit.framework.TestCase;
28  import org.codehaus.plexus.util.StringUtils;
29  
30  public class PathSetTest extends TestCase {
31  
32      /* --------------- Normalization tests --------------*/
33  
34      /**
35       * Test method for 'org.apache.maven.plugin.war.PathSet.normalizeSubPath(String)'
36       */
37      public void testNormalizeSubPath() {
38          assertEquals("Normalized path error", "", PathSet.normalizeSubPath(""));
39          assertEquals("Normalized path error", "", PathSet.normalizeSubPath("/"));
40          assertEquals("Normalized path error", "", PathSet.normalizeSubPath("////"));
41          assertEquals("Normalized path error", "", PathSet.normalizeSubPath("\\"));
42          assertEquals("Normalized path error", "", PathSet.normalizeSubPath("\\\\\\\\"));
43  
44          assertEquals("Normalized path error", "abc", PathSet.normalizeSubPath("abc"));
45          assertEquals("Normalized path error", "abc", PathSet.normalizeSubPath("/abc"));
46          assertEquals("Normalized path error", "abc", PathSet.normalizeSubPath("////abc"));
47          assertEquals("Normalized path error", "abc", PathSet.normalizeSubPath("\\abc"));
48          assertEquals("Normalized path error", "abc", PathSet.normalizeSubPath("\\\\\\\\abc"));
49  
50          assertEquals("Normalized path error", "abc/def/xyz", PathSet.normalizeSubPath("abc/def\\xyz\\"));
51          assertEquals("Normalized path error", "abc/def/xyz", PathSet.normalizeSubPath("/abc/def/xyz/"));
52          assertEquals("Normalized path error", "abc/def/xyz", PathSet.normalizeSubPath("////abc/def/xyz/"));
53          assertEquals("Normalized path error", "abc/def/xyz", PathSet.normalizeSubPath("\\abc/def/xyz/"));
54          assertEquals("Normalized path error", "abc/def/xyz", PathSet.normalizeSubPath("\\\\\\\\abc/def/xyz/"));
55          // MWAR-371
56          assertEquals("Normalized path error", "abc/def/ghi", PathSet.normalizeSubPath("///abc/////def////ghi//"));
57      }
58  
59      /* -------------- Operations tests ------------------*/
60  
61      /**
62       * Test method for:
63       * <ul>
64       * <li>org.apache.maven.plugin.war.PathSet.PathSet()</li>
65       * <li>org.apache.maven.plugin.war.PathSet.size()</li>
66       * <li>org.apache.maven.plugin.war.PathSet.add()</li>
67       * <li>org.apache.maven.plugin.war.PathSet.addAll()</li>
68       * <li>org.apache.maven.plugin.war.PathSet.iterate()</li>
69       * <li>org.apache.maven.plugin.war.PathSet.contains()</li>
70       * <li>org.apache.maven.plugin.war.PathSet.addPrefix(String)</li>
71       * </ul>
72       */
73      public void testPathsSetBasic() {
74          PathSet ps = new PathSet();
75          assertEquals("Unexpected PathSet size", ps.size(), 0);
76          Iterator<String> iter = ps.iterator();
77          assertNotNull("Iterator is null", iter);
78          assertFalse("Can iterate on empty set", iter.hasNext());
79  
80          ps.add("abc");
81          assertEquals("Unexpected PathSet size", ps.size(), 1);
82          ps.add("abc");
83          assertEquals("Unexpected PathSet size", ps.size(), 1);
84          ps.add("xyz/abc");
85          assertEquals("Unexpected PathSet size", ps.size(), 2);
86          ps.add("///abc");
87          assertEquals("Unexpected PathSet size", ps.size(), 2);
88          ps.add("///xyz\\abc");
89          assertEquals("Unexpected PathSet size", ps.size(), 2);
90  
91          ps.addAll(ps);
92          assertEquals("Unexpected PathSet size", ps.size(), 2);
93  
94          int i = 0;
95          for (String pathstr : ps) {
96              i++;
97              assertTrue(ps.contains(pathstr));
98              assertTrue(ps.contains("/" + pathstr));
99              assertTrue(ps.contains("/" + StringUtils.replace(pathstr, '/', '\\')));
100             assertFalse(ps.contains("/" + StringUtils.replace(pathstr, '/', '\\') + "/a"));
101             assertFalse(ps.contains("/a/" + StringUtils.replace(pathstr, '/', '\\')));
102         }
103         assertEquals("Wrong count of iterations", 2, i);
104 
105         ps.addPrefix("/ab/c/");
106         i = 0;
107         for (String pathstr : ps) {
108             i++;
109             assertTrue(pathstr.startsWith("ab/c/"));
110             assertFalse(pathstr.startsWith("ab/c//"));
111             assertTrue(ps.contains(pathstr));
112             assertTrue(ps.contains("/" + pathstr));
113             assertTrue(ps.contains("/" + StringUtils.replace(pathstr, '/', '\\')));
114             assertFalse(ps.contains("/" + StringUtils.replace(pathstr, '/', '\\') + "/a"));
115             assertFalse(ps.contains("/ab/" + StringUtils.replace(pathstr, '/', '\\')));
116         }
117         assertEquals("Wrong count of iterations", 2, i);
118     }
119 
120     /**
121      * Test method for:
122      * <ul>
123      * <li>org.apache.maven.plugin.war.PathSet.PathSet(Collection)</li>
124      * <li>org.apache.maven.plugin.war.PathSet.PathSet(String[])</li>
125      * <li>org.apache.maven.plugin.war.PathSet.Add</li>
126      * <li>org.apache.maven.plugin.war.PathSet.AddAll(String[],String)</li>
127      * <li>org.apache.maven.plugin.war.PathSet.AddAll(Collection,String)</li>
128      * </ul>
129      */
130     public void testPathsSetAddAlls() {
131         Set<String> s1set = new HashSet<>();
132         s1set.add("/a/b");
133         s1set.add("a/b/c");
134         s1set.add("a\\b/c");
135         s1set.add("//1//2\3a");
136 
137         String[] s2ar = new String[] {"/a/b", "a2/b2/c2", "a2\\b2/c2", "//21//22\23a"};
138 
139         PathSet ps1 = new PathSet(s1set);
140         assertEquals("Unexpected PathSet size", 3, ps1.size());
141 
142         PathSet ps2 = new PathSet(s2ar);
143         assertEquals("Unexpected PathSet size", 3, ps2.size());
144 
145         ps1.addAll(s2ar);
146         assertEquals("Unexpected PathSet size", 5, ps1.size());
147 
148         ps2.addAll(s1set);
149         assertEquals("Unexpected PathSet size", 5, ps2.size());
150 
151         for (String str : ps1) {
152             assertTrue(str, ps2.contains(str));
153             assertTrue(ps2.contains("/" + str));
154             assertTrue(ps1.contains(str));
155             assertTrue(ps1.contains("/" + str));
156         }
157 
158         for (String str : ps2) {
159             assertTrue(ps1.contains(str));
160             assertTrue(ps1.contains("/" + str));
161             assertTrue(ps2.contains(str));
162             assertTrue(ps2.contains("/" + str));
163         }
164 
165         ps1.addAll(s2ar, "/pref/");
166         assertEquals("Unexpected PathSet size", 8, ps1.size());
167 
168         ps2.addAll(s2ar, "/pref/");
169         assertEquals("Unexpected PathSet size", 8, ps2.size());
170 
171         for (String str : ps1) {
172             assertTrue(str, ps2.contains(str));
173             assertTrue(ps2.contains("/" + str));
174             assertTrue(ps1.contains(str));
175             assertTrue(ps1.contains("/" + str));
176         }
177 
178         for (String str : ps2) {
179             assertTrue(ps1.contains(str));
180             assertTrue(ps1.contains("/" + str));
181             assertTrue(ps2.contains(str));
182             assertTrue(ps2.contains("/" + str));
183         }
184 
185         PathSet ps3 = new PathSet();
186         ps3.addAll(new String[] {"a/b/c"}, "d");
187         assertTrue("Unexpected PathSet path", ps3.contains("d/a/b/c"));
188     }
189 
190     /**
191      * Test method for 'org.apache.maven.plugin.war.PathSet.addAllFilesInDirectory(File, String)'
192      *
193      * @throws IOException if an io error occurred
194      */
195     public void testAddAllFilesInDirectory() throws IOException {
196         PathSet ps = new PathSet();
197 
198         /* Preparing directory structure*/
199         File testDir = new File("target/testAddAllFilesInDirectory");
200         testDir.mkdirs();
201 
202         File f1 = new File(testDir, "f1");
203         f1.createNewFile();
204         File f2 = new File(testDir, "f2");
205         f2.createNewFile();
206 
207         File d1 = new File(testDir, "d1");
208         File d1d2 = new File(testDir, "d1/d2");
209         d1d2.mkdirs();
210         File d1d2f1 = new File(d1d2, "f1");
211         d1d2f1.createNewFile();
212         File d1d2f2 = new File(d1d2, "f2");
213         d1d2f2.createNewFile();
214 
215         ps.addAllFilesInDirectory(new File("target/testAddAllFilesInDirectory"), "123/");
216         assertEquals("Unexpected PathSet size", 4, ps.size());
217 
218         /*No changes after adding duplicates*/
219         ps.addAllFilesInDirectory(new File("target/testAddAllFilesInDirectory"), "123/");
220         assertEquals("Unexpected PathSet size", 4, ps.size());
221 
222         /*Cleanup*/
223 
224         f1.delete();
225         f2.delete();
226 
227         /*No changes after adding a subset of files*/
228         ps.addAllFilesInDirectory(new File("target/testAddAllFilesInDirectory"), "123/");
229         assertEquals("Unexpected PathSet size", 4, ps.size());
230 
231         d1d2f1.delete();
232         d1d2f2.delete();
233         d1d2.delete();
234         d1.delete();
235         testDir.delete();
236 
237         assertTrue(ps.contains("123/f1"));
238         assertTrue(ps.contains("/123/f1"));
239         assertTrue(ps.contains("123\\f1"));
240         assertTrue(ps.contains("123\\f2"));
241         assertTrue(ps.contains("\\123/d1\\d2/f1"));
242         assertTrue(ps.contains("123\\d1/d2\\f2"));
243         assertFalse(ps.contains("123\\f3"));
244     }
245 }