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 org.codehaus.plexus.util.StringUtils;
28  import org.junit.jupiter.api.Test;
29  
30  import static org.junit.jupiter.api.Assertions.assertEquals;
31  import static org.junit.jupiter.api.Assertions.assertFalse;
32  import static org.junit.jupiter.api.Assertions.assertNotNull;
33  import static org.junit.jupiter.api.Assertions.assertTrue;
34  
35  public class PathSetTest {
36  
37      /* --------------- Normalization tests --------------*/
38  
39      /**
40       * Test method for 'org.apache.maven.plugin.war.PathSet.normalizeSubPath(String)'
41       */
42      @Test
43      public void testNormalizeSubPath() {
44          assertEquals("", PathSet.normalizeSubPath(""), "Normalized path error");
45          assertEquals("", PathSet.normalizeSubPath("/"), "Normalized path error");
46          assertEquals("", PathSet.normalizeSubPath("////"), "Normalized path error");
47          assertEquals("", PathSet.normalizeSubPath("\\"), "Normalized path error");
48          assertEquals("", PathSet.normalizeSubPath("\\\\\\\\"), "Normalized path error");
49  
50          assertEquals("abc", PathSet.normalizeSubPath("abc"), "Normalized path error");
51          assertEquals("abc", PathSet.normalizeSubPath("/abc"), "Normalized path error");
52          assertEquals("abc", PathSet.normalizeSubPath("////abc"), "Normalized path error");
53          assertEquals("abc", PathSet.normalizeSubPath("\\abc"), "Normalized path error");
54          assertEquals("abc", PathSet.normalizeSubPath("\\\\\\\\abc"), "Normalized path error");
55  
56          assertEquals("abc/def/xyz", PathSet.normalizeSubPath("abc/def\\xyz\\"), "Normalized path error");
57          assertEquals("abc/def/xyz", PathSet.normalizeSubPath("/abc/def/xyz/"), "Normalized path error");
58          assertEquals("abc/def/xyz", PathSet.normalizeSubPath("////abc/def/xyz/"), "Normalized path error");
59          assertEquals("abc/def/xyz", PathSet.normalizeSubPath("\\abc/def/xyz/"), "Normalized path error");
60          assertEquals("abc/def/xyz", PathSet.normalizeSubPath("\\\\\\\\abc/def/xyz/"), "Normalized path error");
61          // MWAR-371
62          assertEquals("abc/def/ghi", PathSet.normalizeSubPath("///abc/////def////ghi//"), "Normalized path error");
63      }
64  
65      /* -------------- Operations tests ------------------*/
66  
67      /**
68       * Test method for:
69       * <ul>
70       * <li>org.apache.maven.plugin.war.PathSet.PathSet()</li>
71       * <li>org.apache.maven.plugin.war.PathSet.size()</li>
72       * <li>org.apache.maven.plugin.war.PathSet.add()</li>
73       * <li>org.apache.maven.plugin.war.PathSet.addAll()</li>
74       * <li>org.apache.maven.plugin.war.PathSet.iterate()</li>
75       * <li>org.apache.maven.plugin.war.PathSet.contains()</li>
76       * <li>org.apache.maven.plugin.war.PathSet.addPrefix(String)</li>
77       * </ul>
78       */
79      @Test
80      public void testPathsSetBasic() {
81          PathSet ps = new PathSet();
82          assertEquals(0, ps.size(), "Unexpected PathSet size");
83          Iterator<String> iter = ps.iterator();
84          assertNotNull(iter, "Iterator is null");
85          assertFalse(iter.hasNext(), "Can iterate on empty set");
86  
87          ps.add("abc");
88          assertEquals(1, ps.size(), "Unexpected PathSet size");
89          ps.add("abc");
90          assertEquals(1, ps.size(), "Unexpected PathSet size");
91          ps.add("xyz/abc");
92          assertEquals(2, ps.size(), "Unexpected PathSet size");
93          ps.add("///abc");
94          assertEquals(2, ps.size(), "Unexpected PathSet size");
95          ps.add("///xyz\\abc");
96          assertEquals(2, ps.size(), "Unexpected PathSet size");
97  
98          ps.addAll(ps);
99          assertEquals(2, ps.size(), "Unexpected PathSet size");
100 
101         int i = 0;
102         for (String pathstr : ps) {
103             i++;
104             assertTrue(ps.contains(pathstr));
105             assertTrue(ps.contains("/" + pathstr));
106             assertTrue(ps.contains("/" + StringUtils.replace(pathstr, '/', '\\')));
107             assertFalse(ps.contains("/" + StringUtils.replace(pathstr, '/', '\\') + "/a"));
108             assertFalse(ps.contains("/a/" + StringUtils.replace(pathstr, '/', '\\')));
109         }
110         assertEquals(2, i, "Wrong count of iterations");
111 
112         ps.addPrefix("/ab/c/");
113         i = 0;
114         for (String pathstr : ps) {
115             i++;
116             assertTrue(pathstr.startsWith("ab/c/"));
117             assertFalse(pathstr.startsWith("ab/c//"));
118             assertTrue(ps.contains(pathstr));
119             assertTrue(ps.contains("/" + pathstr));
120             assertTrue(ps.contains("/" + StringUtils.replace(pathstr, '/', '\\')));
121             assertFalse(ps.contains("/" + StringUtils.replace(pathstr, '/', '\\') + "/a"));
122             assertFalse(ps.contains("/ab/" + StringUtils.replace(pathstr, '/', '\\')));
123         }
124         assertEquals(2, i, "Wrong count of iterations");
125     }
126 
127     /**
128      * Test method for:
129      * <ul>
130      * <li>org.apache.maven.plugin.war.PathSet.PathSet(Collection)</li>
131      * <li>org.apache.maven.plugin.war.PathSet.PathSet(String[])</li>
132      * <li>org.apache.maven.plugin.war.PathSet.Add</li>
133      * <li>org.apache.maven.plugin.war.PathSet.AddAll(String[],String)</li>
134      * <li>org.apache.maven.plugin.war.PathSet.AddAll(Collection,String)</li>
135      * </ul>
136      */
137     @Test
138     public void testPathsSetAddAlls() {
139         Set<String> s1set = new HashSet<>();
140         s1set.add("/a/b");
141         s1set.add("a/b/c");
142         s1set.add("a\\b/c");
143         s1set.add("//1//2\3a");
144 
145         String[] s2ar = new String[] {"/a/b", "a2/b2/c2", "a2\\b2/c2", "//21//22\23a"};
146 
147         PathSet ps1 = new PathSet(s1set);
148         assertEquals(3, ps1.size(), "Unexpected PathSet size");
149 
150         PathSet ps2 = new PathSet(s2ar);
151         assertEquals(3, ps2.size(), "Unexpected PathSet size");
152 
153         ps1.addAll(s2ar);
154         assertEquals(5, ps1.size(), "Unexpected PathSet size");
155 
156         ps2.addAll(s1set);
157         assertEquals(5, ps2.size(), "Unexpected PathSet size");
158 
159         for (String str : ps1) {
160             assertTrue(ps2.contains(str), str);
161             assertTrue(ps2.contains("/" + str));
162             assertTrue(ps1.contains(str));
163             assertTrue(ps1.contains("/" + str));
164         }
165 
166         for (String str : ps2) {
167             assertTrue(ps1.contains(str));
168             assertTrue(ps1.contains("/" + str));
169             assertTrue(ps2.contains(str));
170             assertTrue(ps2.contains("/" + str));
171         }
172 
173         ps1.addAll(s2ar, "/pref/");
174         assertEquals(8, ps1.size(), "Unexpected PathSet size");
175 
176         ps2.addAll(s2ar, "/pref/");
177         assertEquals(8, ps2.size(), "Unexpected PathSet size");
178 
179         for (String str : ps1) {
180             assertTrue(ps2.contains(str), str);
181             assertTrue(ps2.contains("/" + str));
182             assertTrue(ps1.contains(str));
183             assertTrue(ps1.contains("/" + str));
184         }
185 
186         for (String str : ps2) {
187             assertTrue(ps1.contains(str));
188             assertTrue(ps1.contains("/" + str));
189             assertTrue(ps2.contains(str));
190             assertTrue(ps2.contains("/" + str));
191         }
192 
193         PathSet ps3 = new PathSet();
194         ps3.addAll(new String[] {"a/b/c"}, "d");
195         assertTrue(ps3.contains("d/a/b/c"), "Unexpected PathSet path");
196     }
197 
198     /**
199      * Test method for 'org.apache.maven.plugin.war.PathSet.addAllFilesInDirectory(File, String)'
200      *
201      * @throws IOException if an io error occurred
202      */
203     @Test
204     public void testAddAllFilesInDirectory() throws IOException {
205         PathSet ps = new PathSet();
206 
207         /* Preparing directory structure*/
208         File testDir = new File("target/testAddAllFilesInDirectory");
209         testDir.mkdirs();
210 
211         File f1 = new File(testDir, "f1");
212         f1.createNewFile();
213         File f2 = new File(testDir, "f2");
214         f2.createNewFile();
215 
216         File d1 = new File(testDir, "d1");
217         File d1d2 = new File(testDir, "d1/d2");
218         d1d2.mkdirs();
219         File d1d2f1 = new File(d1d2, "f1");
220         d1d2f1.createNewFile();
221         File d1d2f2 = new File(d1d2, "f2");
222         d1d2f2.createNewFile();
223 
224         ps.addAllFilesInDirectory(new File("target/testAddAllFilesInDirectory"), "123/");
225         assertEquals(4, ps.size(), "Unexpected PathSet size");
226 
227         /*No changes after adding duplicates*/
228         ps.addAllFilesInDirectory(new File("target/testAddAllFilesInDirectory"), "123/");
229         assertEquals(4, ps.size(), "Unexpected PathSet size");
230 
231         /*Cleanup*/
232 
233         f1.delete();
234         f2.delete();
235 
236         /*No changes after adding a subset of files*/
237         ps.addAllFilesInDirectory(new File("target/testAddAllFilesInDirectory"), "123/");
238         assertEquals(4, ps.size(), "Unexpected PathSet size");
239 
240         d1d2f1.delete();
241         d1d2f2.delete();
242         d1d2.delete();
243         d1.delete();
244         testDir.delete();
245 
246         assertTrue(ps.contains("123/f1"));
247         assertTrue(ps.contains("/123/f1"));
248         assertTrue(ps.contains("123\\f1"));
249         assertTrue(ps.contains("123\\f2"));
250         assertTrue(ps.contains("\\123/d1\\d2/f1"));
251         assertTrue(ps.contains("123\\d1/d2\\f2"));
252         assertFalse(ps.contains("123\\f3"));
253     }
254 }