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