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