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.normalizeFilePathStatic(String)'
41       */
42      public void testNormalizeFilePathStatic()
43      {
44          assertEquals( "Normalized path error", "", PathSet.normalizeFilePathStatic( "" ) );
45          assertEquals( "Normalized path error", "", PathSet.normalizeFilePathStatic( "/" ) );
46          assertEquals( "Normalized path error", "", PathSet.normalizeFilePathStatic( "////" ) );
47          assertEquals( "Normalized path error", "", PathSet.normalizeFilePathStatic( "\\" ) );
48          assertEquals( "Normalized path error", "", PathSet.normalizeFilePathStatic( "\\\\\\\\" ) );
49  
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          assertEquals( "Normalized path error", "abc", PathSet.normalizeFilePathStatic( "\\abc" ) );
54          assertEquals( "Normalized path error", "abc", PathSet.normalizeFilePathStatic( "\\\\\\\\abc" ) );
55  
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/", PathSet.normalizeFilePathStatic( "////abc/def/xyz/" ) );
59          assertEquals( "Normalized path error", "abc/def/xyz/", PathSet.normalizeFilePathStatic( "\\abc/def/xyz/" ) );
60          assertEquals( "Normalized path error", "abc/def/xyz/",
61                        PathSet.normalizeFilePathStatic( "\\\\\\\\abc/def/xyz/" ) );
62      }
63  
64      /**
65       * Test method for 'org.apache.maven.plugin.war.PathSet.trimTrailingSlashes(String)'
66       */
67      public void testTrimTrailingSlashes()
68      {
69          assertEquals( "Trimed path error", "", PathSet.trimTrailingSlashes( "" ) );
70          assertEquals( "Trimed path error", "", PathSet.trimTrailingSlashes( "/" ) );
71          assertEquals( "Trimed path error", "", PathSet.trimTrailingSlashes( "//" ) );
72          assertEquals( "Trimed path error", "\\", PathSet.trimTrailingSlashes( "\\" ) );
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/", PathSet.trimTrailingSlashes( "////abc/def/xyz/" ) );
76          assertEquals( "Trimed path error", "\\abc/def/xyz\\", PathSet.trimTrailingSlashes( "\\abc/def/xyz\\" ) );
77          assertEquals( "Trimed path error", "\\\\\\\\abc/def/xyz",
78                        PathSet.trimTrailingSlashes( "\\\\\\\\abc/def/xyz" ) );
79      }
80  
81      /* -------------- Operations tests ------------------*/
82  
83      /**
84       * Test method for:
85       * <ul>
86       * <li>org.apache.maven.plugin.war.PathSet.PathSet()</li>
87       * <li>org.apache.maven.plugin.war.PathSet.size()</li>
88       * <li>org.apache.maven.plugin.war.PathSet.add()</li>
89       * <li>org.apache.maven.plugin.war.PathSet.addAll()</li>
90       * <li>org.apache.maven.plugin.war.PathSet.iterate()</li>
91       * <li>org.apache.maven.plugin.war.PathSet.contains()</li>
92       * <li>org.apache.maven.plugin.war.PathSet.addPrefix(String)</li>
93       * </ul>
94       */
95      public void testPathsSetBasic()
96      {
97          PathSet ps = new PathSet();
98          assertEquals( "Unexpected PathSet size", ps.size(), 0 );
99          Iterator<String> iter = ps.iterator();
100         assertNotNull( "Iterator is null", iter );
101         assertFalse( "Can iterate on empty set", iter.hasNext() );
102 
103         ps.add( "abc" );
104         assertEquals( "Unexpected PathSet size", ps.size(), 1 );
105         ps.add( "abc" );
106         assertEquals( "Unexpected PathSet size", ps.size(), 1 );
107         ps.add( "xyz/abc" );
108         assertEquals( "Unexpected PathSet size", ps.size(), 2 );
109         ps.add( "///abc" );
110         assertEquals( "Unexpected PathSet size", ps.size(), 2 );
111         ps.add( "///xyz\\abc" );
112         assertEquals( "Unexpected PathSet size", ps.size(), 2 );
113 
114         ps.addAll( ps );
115         assertEquals( "Unexpected PathSet size", ps.size(), 2 );
116 
117         int i = 0;
118         for (String p1 : ps) {
119             i++;
120             String pathstr = p1;
121             assertTrue(ps.contains(pathstr));
122             assertTrue(ps.contains("/" + pathstr));
123             assertTrue(ps.contains("/" + StringUtils.replace(pathstr, '/', '\\')));
124             assertFalse(ps.contains("/" + StringUtils.replace(pathstr, '/', '\\') + "/a"));
125             assertFalse(ps.contains("/a/" + StringUtils.replace(pathstr, '/', '\\')));
126         }
127         assertEquals( "Wrong count of iterations", 2, i );
128 
129         ps.addPrefix( "/ab/c/" );
130         i = 0;
131         for (String p : ps) {
132             i++;
133             String pathstr = p;
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<String> s1set = new HashSet<String>();
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 (String str : ps1) {
178             assertTrue(str, ps2.contains(str));
179             assertTrue(ps2.contains("/" + str));
180             assertTrue(ps1.contains(str));
181             assertTrue(ps1.contains("/" + str));
182         }
183 
184         for (String str : ps2) {
185             assertTrue(ps1.contains(str));
186             assertTrue(ps1.contains("/" + str));
187             assertTrue(ps2.contains(str));
188             assertTrue(ps2.contains("/" + str));
189         }
190 
191         ps1.addAll( s2ar, "/pref/" );
192         assertEquals( "Unexpected PathSet size", 8, ps1.size() );
193 
194         ps2.addAll( s2ar, "/pref/" );
195         assertEquals( "Unexpected PathSet size", 8, ps2.size() );
196 
197         for (String str : ps1) {
198             assertTrue(str, ps2.contains(str));
199             assertTrue(ps2.contains("/" + str));
200             assertTrue(ps1.contains(str));
201             assertTrue(ps1.contains("/" + str));
202         }
203 
204         for (String str : ps2) {
205             assertTrue(ps1.contains(str));
206             assertTrue(ps1.contains("/" + str));
207             assertTrue(ps2.contains(str));
208             assertTrue(ps2.contains("/" + str));
209         }
210 
211     }
212 
213     /**
214      * Test method for 'org.apache.maven.plugin.war.PathSet.addAllFilesInDirectory(File, String)'
215      *
216      * @throws IOException if an io error occurred
217      */
218     public void testAddAllFilesInDirectory()
219         throws IOException
220     {
221         PathSet ps = new PathSet();
222 
223         /* Preparing directory structure*/
224         File testDir = new File( "target/testAddAllFilesInDirectory" );
225         testDir.mkdirs();
226 
227         File f1 = new File( testDir, "f1" );
228         f1.createNewFile();
229         File f2 = new File( testDir, "f2" );
230         f2.createNewFile();
231 
232         File d1 = new File( testDir, "d1" );
233         File d1d2 = new File( testDir, "d1/d2" );
234         d1d2.mkdirs();
235         File d1d2f1 = new File( d1d2, "f1" );
236         d1d2f1.createNewFile();
237         File d1d2f2 = new File( d1d2, "f2" );
238         d1d2f2.createNewFile();
239 
240         ps.addAllFilesInDirectory( new File( "target/testAddAllFilesInDirectory" ), "123/" );
241         assertEquals( "Unexpected PathSet size", 4, ps.size() );
242 
243         /*No changes after adding duplicates*/
244         ps.addAllFilesInDirectory( new File( "target/testAddAllFilesInDirectory" ), "123/" );
245         assertEquals( "Unexpected PathSet size", 4, ps.size() );
246 
247         /*Cleanup*/
248 
249         f1.delete();
250         f2.delete();
251 
252         /*No changes after adding a subset of files*/
253         ps.addAllFilesInDirectory( new File( "target/testAddAllFilesInDirectory" ), "123/" );
254         assertEquals( "Unexpected PathSet size", 4, ps.size() );
255 
256         d1d2f1.delete();
257         d1d2f2.delete();
258         d1d2.delete();
259         d1.delete();
260         testDir.delete();
261 
262         assertTrue( ps.contains( "123/f1" ) );
263         assertTrue( ps.contains( "/123/f1" ) );
264         assertTrue( ps.contains( "123\\f1" ) );
265         assertTrue( ps.contains( "123\\f2" ) );
266         assertTrue( ps.contains( "\\123/d1\\d2/f1" ) );
267         assertTrue( ps.contains( "123\\d1/d2\\f2" ) );
268         assertFalse( ps.contains( "123\\f3" ) );
269     }
270 }