View Javadoc
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<String> 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 (String p1 : ps) {
117             i++;
118             String pathstr = p1;
119             assertTrue(ps.contains(pathstr));
120             assertTrue(ps.contains("/" + pathstr));
121             assertTrue(ps.contains("/" + StringUtils.replace(pathstr, '/', '\\')));
122             assertFalse(ps.contains("/" + StringUtils.replace(pathstr, '/', '\\') + "/a"));
123             assertFalse(ps.contains("/a/" + StringUtils.replace(pathstr, '/', '\\')));
124         }
125         assertEquals( "Wrong count of iterations", 2, i );
126 
127         ps.addPrefix( "/ab/c/" );
128         i = 0;
129         for (String p : ps) {
130             i++;
131             String pathstr = p;
132             assertTrue(pathstr.startsWith("ab/c/"));
133             assertFalse(pathstr.startsWith("ab/c//"));
134             assertTrue(ps.contains(pathstr));
135             assertTrue(ps.contains("/" + pathstr));
136             assertTrue(ps.contains("/" + StringUtils.replace(pathstr, '/', '\\')));
137             assertFalse(ps.contains("/" + StringUtils.replace(pathstr, '/', '\\') + "/a"));
138             assertFalse(ps.contains("/ab/" + StringUtils.replace(pathstr, '/', '\\')));
139         }
140         assertEquals( "Wrong count of iterations", 2, i );
141     }
142 
143     /**
144      * Test method for:
145      * <ul>
146      * <li>org.apache.maven.plugin.war.PathSet.PathSet(Collection)</li>
147      * <li>org.apache.maven.plugin.war.PathSet.PathSet(String[])</li>
148      * <li>org.apache.maven.plugin.war.PathSet.Add</li>
149      * <li>org.apache.maven.plugin.war.PathSet.AddAll(String[],String)</li>
150      * <li>org.apache.maven.plugin.war.PathSet.AddAll(Collection,String)</li>
151      * </ul>
152      */
153     public void testPathsSetAddAlls()
154     {
155         Set<String> s1set = new HashSet<String>();
156         s1set.add( "/a/b" );
157         s1set.add( "a/b/c" );
158         s1set.add( "a\\b/c" );
159         s1set.add( "//1//2\3a" );
160 
161         String[] s2ar = new String[]{"/a/b", "a2/b2/c2", "a2\\b2/c2", "//21//22\23a"};
162 
163         PathSet ps1 = new PathSet( s1set );
164         assertEquals( "Unexpected PathSet size", 3, ps1.size() );
165 
166         PathSet ps2 = new PathSet( s2ar );
167         assertEquals( "Unexpected PathSet size", 3, ps2.size() );
168 
169         ps1.addAll( s2ar );
170         assertEquals( "Unexpected PathSet size", 5, ps1.size() );
171 
172         ps2.addAll( s1set );
173         assertEquals( "Unexpected PathSet size", 5, ps2.size() );
174 
175         for (String str : ps1) {
176             assertTrue(str, ps2.contains(str));
177             assertTrue(ps2.contains("/" + str));
178             assertTrue(ps1.contains(str));
179             assertTrue(ps1.contains("/" + str));
180         }
181 
182         for (String str : ps2) {
183             assertTrue(ps1.contains(str));
184             assertTrue(ps1.contains("/" + str));
185             assertTrue(ps2.contains(str));
186             assertTrue(ps2.contains("/" + str));
187         }
188 
189         ps1.addAll( s2ar, "/pref/" );
190         assertEquals( "Unexpected PathSet size", 8, ps1.size() );
191 
192         ps2.addAll( s2ar, "/pref/" );
193         assertEquals( "Unexpected PathSet size", 8, ps2.size() );
194 
195         for (String str : ps1) {
196             assertTrue(str, ps2.contains(str));
197             assertTrue(ps2.contains("/" + str));
198             assertTrue(ps1.contains(str));
199             assertTrue(ps1.contains("/" + str));
200         }
201 
202         for (String str : ps2) {
203             assertTrue(ps1.contains(str));
204             assertTrue(ps1.contains("/" + str));
205             assertTrue(ps2.contains(str));
206             assertTrue(ps2.contains("/" + str));
207         }
208 
209     }
210 
211     /**
212      * Test method for 'org.apache.maven.plugin.war.PathSet.addAllFilesInDirectory(File, String)'
213      *
214      * @throws IOException if an io error occurred
215      */
216     public void testAddAllFilesInDirectory()
217         throws IOException
218     {
219         PathSet ps = new PathSet();
220 
221         /* Preparing directory structure*/
222         File testDir = new File( "target/testAddAllFilesInDirectory" );
223         testDir.mkdirs();
224 
225         File f1 = new File( testDir, "f1" );
226         f1.createNewFile();
227         File f2 = new File( testDir, "f2" );
228         f2.createNewFile();
229 
230         File d1 = new File( testDir, "d1" );
231         File d1d2 = new File( testDir, "d1/d2" );
232         d1d2.mkdirs();
233         File d1d2f1 = new File( d1d2, "f1" );
234         d1d2f1.createNewFile();
235         File d1d2f2 = new File( d1d2, "f2" );
236         d1d2f2.createNewFile();
237 
238         ps.addAllFilesInDirectory( new File( "target/testAddAllFilesInDirectory" ), "123/" );
239         assertEquals( "Unexpected PathSet size", 4, ps.size() );
240 
241         /*No changes after adding duplicates*/
242         ps.addAllFilesInDirectory( new File( "target/testAddAllFilesInDirectory" ), "123/" );
243         assertEquals( "Unexpected PathSet size", 4, ps.size() );
244 
245         /*Cleanup*/
246 
247         f1.delete();
248         f2.delete();
249 
250         /*No changes after adding a subset of files*/
251         ps.addAllFilesInDirectory( new File( "target/testAddAllFilesInDirectory" ), "123/" );
252         assertEquals( "Unexpected PathSet size", 4, ps.size() );
253 
254         d1d2f1.delete();
255         d1d2f2.delete();
256         d1d2.delete();
257         d1.delete();
258         testDir.delete();
259 
260         assertTrue( ps.contains( "123/f1" ) );
261         assertTrue( ps.contains( "/123/f1" ) );
262         assertTrue( ps.contains( "123\\f1" ) );
263         assertTrue( ps.contains( "123\\f2" ) );
264         assertTrue( ps.contains( "\\123/d1\\d2/f1" ) );
265         assertTrue( ps.contains( "123\\d1/d2\\f2" ) );
266         assertFalse( ps.contains( "123\\f3" ) );
267     }
268 }