View Javadoc

1   package org.apache.torque.generator.file;
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 static org.junit.Assert.assertEquals;
23  import static org.apache.torque.generator.file.FileTestUtils.assertFileListEquals;
24  import static org.apache.torque.generator.file.FileTestUtils.createSetFrom;
25  
26  import java.io.File;
27  import java.io.IOException;
28  import java.util.HashSet;
29  import java.util.Iterator;
30  import java.util.List;
31  
32  import org.apache.torque.generator.file.Fileset;
33  import org.junit.Test;
34  
35  /**
36   * Tests for the class Fileset.
37   *
38   * @version $Id: FilesetTest.java 1331190 2012-04-27 02:41:35Z tfischer $
39   */
40  public class FilesetTest
41  {
42      private static File TEST_BASE_DIR = new File("src/test/file");
43  
44      @Test
45      public void testAnalyzePathNoWildcards()
46      {
47          String path = "/tmp/schema.xml";
48          int baseDirSeparatorPos = Fileset.getWildcardFreeSeparatorPos(path);
49          assertEquals(4, baseDirSeparatorPos);
50          String wildcardFree
51                  = Fileset.getPathPartBefore(path, baseDirSeparatorPos);
52          assertEquals("/tmp", wildcardFree);
53          String wildcardPattern
54                  = Fileset.getPathPartAfter(path, baseDirSeparatorPos);
55          assertEquals("schema.xml", wildcardPattern);
56      }
57  
58      @Test
59      public void testAnalyzePathOnlyFilename()
60      {
61          String path = "schema.xml";
62          int baseDirSeparatorPos = Fileset.getWildcardFreeSeparatorPos(path);
63          assertEquals(-1, baseDirSeparatorPos);
64          String wildcardFree
65                  = Fileset.getPathPartBefore(path, baseDirSeparatorPos);
66          assertEquals(".", wildcardFree);
67          String wildcardPattern
68                  = Fileset.getPathPartAfter(path, baseDirSeparatorPos);
69          assertEquals("schema.xml", wildcardPattern);
70      }
71  
72      @Test
73      public void testAnalyzePathQuestionMark()
74      {
75          String path = "C:\\schema\\?\\schema.xml";
76          int baseDirSeparatorPos = Fileset.getWildcardFreeSeparatorPos(path);
77          assertEquals(9, baseDirSeparatorPos);
78          String wildcardFree
79                  = Fileset.getPathPartBefore(path, baseDirSeparatorPos);
80          assertEquals("C:\\schema", wildcardFree);
81          String wildcardPattern
82                  = Fileset.getPathPartAfter(path, baseDirSeparatorPos);
83          assertEquals("?\\schema.xml", wildcardPattern);
84      }
85  
86      @Test
87      public void testAnalyzePathAsterisk()
88      {
89          String path = "C:\\schema\\*\\schema.xml";
90          int baseDirSeparatorPos = Fileset.getWildcardFreeSeparatorPos(path);
91          assertEquals(9, baseDirSeparatorPos);
92          String wildcardFree
93                  = Fileset.getPathPartBefore(path, baseDirSeparatorPos);
94          assertEquals("C:\\schema", wildcardFree);
95          String wildcardPattern
96                  = Fileset.getPathPartAfter(path, baseDirSeparatorPos);
97          assertEquals("*\\schema.xml", wildcardPattern);
98      }
99  
100     @Test
101     public void testAnalyzePathMultipleWildcards()
102     {
103         String path = "/tmp/*/???/schema.xml";
104         int baseDirSeparatorPos = Fileset.getWildcardFreeSeparatorPos(path);
105         assertEquals(4, baseDirSeparatorPos);
106         String wildcardFree
107                 = Fileset.getPathPartBefore(path, baseDirSeparatorPos);
108         assertEquals("/tmp", wildcardFree);
109         String wildcardPattern
110                 = Fileset.getPathPartAfter(path, baseDirSeparatorPos);
111         assertEquals("*/???/schema.xml", wildcardPattern);
112     }
113 
114     @Test
115     public void testAnalyzePathWildcardFirst()
116     {
117         String path = "*/???/schema.xml";
118         int baseDirSeparatorPos = Fileset.getWildcardFreeSeparatorPos(path);
119         assertEquals(-1, baseDirSeparatorPos);
120         String wildcardFree
121                 = Fileset.getPathPartBefore(path, baseDirSeparatorPos);
122         assertEquals(".", wildcardFree);
123         String wildcardPattern
124                 = Fileset.getPathPartAfter(path, baseDirSeparatorPos);
125         assertEquals("*/???/schema.xml", wildcardPattern);
126     }
127 
128     @Test
129     public void testFilelistFixedNameBasedir() throws IOException
130     {
131         Fileset fileset = new Fileset(
132                 TEST_BASE_DIR,
133                 createSetFrom("1.properties"),
134                 null);
135         assertFileListEquals(
136                 fileset,
137                 new File(TEST_BASE_DIR, "./1.properties"));
138     }
139 
140     @Test
141     public void testFilelistFixedNameSubdir() throws IOException
142     {
143         Fileset fileset = new Fileset(
144                 TEST_BASE_DIR,
145                 createSetFrom("subfolder/2.properties"),
146                 null);
147         assertFileListEquals(
148                 fileset,
149                 new File(TEST_BASE_DIR, "subfolder/2.properties"));
150     }
151 
152     @Test
153     public void testFilelistMixedSlashBackslash() throws IOException
154     {
155         Fileset fileset = new Fileset(
156                 TEST_BASE_DIR,
157                 createSetFrom("subfolder/subsubfolder\\3.properties"),
158                 null);
159         assertFileListEquals(
160                 fileset,
161                 new File(TEST_BASE_DIR, "subfolder/subsubfolder/3.properties"));
162     }
163 
164     @Test
165     public void testFilelistWildcardDir() throws IOException
166     {
167         Fileset fileset = new Fileset(
168                 TEST_BASE_DIR,
169                 createSetFrom("*//2.properties"),
170                 null);
171         assertFileListEquals(
172                 fileset,
173                 new File(TEST_BASE_DIR, "./subfolder/2.properties"));
174     }
175 
176     @Test
177     public void testFilelistQuestionmarkFilename() throws IOException
178     {
179         Fileset fileset = new Fileset(
180                 TEST_BASE_DIR,
181                 createSetFrom("?.properties"),
182                 null);
183         assertFileListEquals(
184                 fileset,
185                 new File(TEST_BASE_DIR, "./1.properties"));
186     }
187 
188     @Test
189     public void testFilelistWildcardFilename() throws IOException
190     {
191         Fileset fileset = new Fileset(
192                 TEST_BASE_DIR,
193                 createSetFrom("*.properties"),
194                 null);
195         assertFileListEquals(
196                 fileset,
197                 new File(TEST_BASE_DIR, "./1.properties"),
198                 new File(TEST_BASE_DIR, "./11.properties"));
199     }
200 
201     @Test
202     public void testFilelistDoubleDotsStayingInBaseDir() throws IOException
203     {
204         Fileset fileset = new Fileset(
205                 TEST_BASE_DIR,
206                 createSetFrom("subfolder/../1.properties"),
207                 null);
208         assertFileListEquals(
209                 fileset,
210                 new File(TEST_BASE_DIR, "subfolder/../1.properties"));
211     }
212 
213     @Test
214     public void testFilelistDoubleDotsLeavingBaseDir() throws IOException
215     {
216         Fileset fileset = new Fileset(
217                 new File(TEST_BASE_DIR, "subfolder"),
218                 createSetFrom("../1.properties"),
219                 null);
220         assertFileListEquals(
221                 fileset,
222                 new File(TEST_BASE_DIR, "subfolder/../1.properties"));
223     }
224 
225     @Test
226     public void testFilelistAllNull() throws IOException
227     {
228         Fileset fileset = new Fileset(
229                 TEST_BASE_DIR,
230                 null,
231                 null);
232         List<File> fileList = fileset.getFiles();
233         Iterator<File> fileIt = fileList.iterator();
234         while (fileIt.hasNext())
235         {
236             File file = fileIt.next();
237             if (file.getPath().indexOf(".svn") != -1)
238             {
239                 fileIt.remove();
240             }
241         }
242         assertFileListEquals(
243                 fileList,
244                 new File(TEST_BASE_DIR, "1.properties"),
245                 new File(TEST_BASE_DIR, "11.properties"),
246                 new File(TEST_BASE_DIR, "package.html"),
247                 new File(TEST_BASE_DIR, "subfolder/2.properties"),
248                 new File(TEST_BASE_DIR, "subfolder/subsubfolder/3.properties"));
249     }
250 
251     @Test
252     public void testFilelistAllEmpty() throws IOException
253     {
254         Fileset fileset = new Fileset(
255                 TEST_BASE_DIR,
256                 new HashSet<String>(),
257                 new HashSet<String>());
258         List<File> fileList = fileset.getFiles();
259         Iterator<File> fileIt = fileList.iterator();
260         while (fileIt.hasNext())
261         {
262             File file = fileIt.next();
263             if (file.getPath().indexOf(".svn") != -1)
264             {
265                 fileIt.remove();
266             }
267         }
268         assertFileListEquals(
269                 fileList,
270                 new File(TEST_BASE_DIR, "1.properties"),
271                 new File(TEST_BASE_DIR, "11.properties"),
272                 new File(TEST_BASE_DIR, "package.html"),
273                 new File(TEST_BASE_DIR, "subfolder/2.properties"),
274                 new File(TEST_BASE_DIR, "subfolder/subsubfolder/3.properties"));
275     }
276 
277     @Test
278     public void testFilelistExcludeInBasedir() throws IOException
279     {
280         Fileset fileset = new Fileset(
281                 TEST_BASE_DIR,
282                 createSetFrom("*"),
283                 createSetFrom("11.properties"));
284         assertFileListEquals(
285                 fileset,
286                 new File(TEST_BASE_DIR, "./package.html"),
287                 new File(TEST_BASE_DIR, "./1.properties"));
288     }
289 
290     @Test
291     public void testFilelistExcludeInSubdir() throws IOException
292     {
293         Fileset fileset = new Fileset(
294                 TEST_BASE_DIR,
295                 createSetFrom("subfolder/*"),
296                 createSetFrom("subfolder/2.properties"));
297         assertFileListEquals(
298                 fileset,
299                 new File[] {});
300     }
301 
302     @Test
303     public void testFilelistExcludeWithBackslashes() throws IOException
304     {
305         Fileset fileset = new Fileset(
306                 TEST_BASE_DIR,
307                 createSetFrom("subfolder/*"),
308                 createSetFrom("subfolder\\2.properties"));
309         assertFileListEquals(
310                 fileset,
311                 new File[] {});
312     }
313 
314     @Test
315     public void testFilelistExcludeAsteriskSubdir() throws IOException
316     {
317         Fileset fileset = new Fileset(
318                 TEST_BASE_DIR,
319                 createSetFrom("subfolder/*"),
320                 createSetFrom("*/2.properties"));
321         assertFileListEquals(
322                 fileset,
323                 new File[] {});
324     }
325 
326     @Test
327     public void testFilelistExcludeQuestionMarkSubdir() throws IOException
328     {
329         Fileset fileset = new Fileset(
330                 TEST_BASE_DIR,
331                 createSetFrom("subfolder/*"),
332                 createSetFrom("su??old?r/2.properties"));
333         assertFileListEquals(
334                 fileset,
335         new File[] {});
336     }
337 
338     @Test
339     public void testFilelistExcludeDot() throws IOException
340     {
341         Fileset fileset = new Fileset(
342                 TEST_BASE_DIR,
343                 createSetFrom("subfolder/*"),
344                 createSetFrom("su??old?r/./2.properties"));
345         assertFileListEquals(
346                 fileset,
347                 new File[] {});
348     }
349 
350     @Test
351     public void testFilelistExcludeDoubleDot() throws IOException
352     {
353         Fileset fileset = new Fileset(
354                 TEST_BASE_DIR,
355                 createSetFrom("*"),
356                 createSetFrom("subfolder/../11.properties"));
357         assertFileListEquals(
358                 fileset,
359                 new File(TEST_BASE_DIR, "./package.html"),
360                 new File(TEST_BASE_DIR, "./1.properties"));
361     }
362 
363     @Test
364     public void testFilelistExcludeDoubleDotLeavingBaseDir() throws IOException
365     {
366         Fileset fileset = new Fileset(
367                 new File(TEST_BASE_DIR, "subfolder"),
368                 createSetFrom("../*"),
369                 createSetFrom("../1.properties", ".././package.html"));
370         assertFileListEquals(
371                 fileset,
372                 new File(TEST_BASE_DIR, "subfolder/../11.properties"));
373     }
374 
375     @Test
376     public void testFilelistBasedirNul() throws IOException
377     {
378         Fileset fileset = new Fileset(
379                 null,
380                 createSetFrom(TEST_BASE_DIR + "/1.properties"),
381                 null);
382         assertFileListEquals(
383                 fileset,
384                 new File(TEST_BASE_DIR, "/1.properties"));
385     }
386 
387 }