1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs.test;
18  
19  import junit.framework.Assert;
20  import org.apache.commons.vfs.FileObject;
21  import org.apache.commons.vfs.FileSelectInfo;
22  import org.apache.commons.vfs.FileSelector;
23  import org.apache.commons.vfs.FileSystemException;
24  import org.apache.commons.vfs.FileType;
25  
26  import java.util.ArrayList;
27  import java.util.HashSet;
28  import java.util.List;
29  import java.util.Set;
30  
31  /***
32   * A file selector that asserts that all files are visited, in the correct
33   * order.
34   *
35   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
36   * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
37   */
38  public class VerifyingFileSelector
39      extends Assert
40      implements FileSelector
41  {
42      private final FileInfo rootFile;
43      private final List files = new ArrayList();
44  
45      private FileInfo currentFolderInfo;
46      private FileObject currentFolder;
47      private Set children;
48      private List stack = new ArrayList();
49  
50      public VerifyingFileSelector(final FileInfo fileInfo)
51      {
52          this.rootFile = fileInfo;
53          children = new HashSet();
54          children.add(rootFile.baseName);
55      }
56  
57      /***
58       * Determines if a file or folder should be selected.
59       */
60      public boolean includeFile(final FileSelectInfo fileInfo)
61          throws FileSystemException
62      {
63          final FileObject file = fileInfo.getFile();
64          if (file == currentFolder)
65          {
66              // Pop current folder
67              assertEquals(0, children.size());
68              currentFolder = currentFolder.getParent();
69              currentFolderInfo = currentFolderInfo.getParent();
70              children = (Set) stack.remove(0);
71          }
72  
73          final String baseName = file.getName().getBaseName();
74  
75          final FileInfo childInfo = getChild(baseName);
76          assertSame(childInfo.type, file.getType());
77  
78          final boolean isChild = children.remove(baseName);
79          assertTrue(isChild);
80  
81          files.add(file);
82          return true;
83      }
84  
85      /***
86       * Determines whether a folder should be traversed.
87       */
88      public boolean traverseDescendents(final FileSelectInfo fileInfo)
89          throws FileSystemException
90      {
91          // Check that the given file is a folder
92          final FileObject folder = fileInfo.getFile();
93          assertSame(FileType.FOLDER, folder.getType());
94  
95          // Locate the info for the folder
96          final String baseName = folder.getName().getBaseName();
97          if (currentFolder == null)
98          {
99              assertEquals(rootFile.baseName, baseName);
100             currentFolderInfo = rootFile;
101         }
102         else
103         {
104             assertSame(currentFolder, folder.getParent());
105 
106             // Locate the info for the child, and make sure it is folder
107             currentFolderInfo = getChild(baseName);
108             assertSame(FileType.FOLDER, currentFolderInfo.type);
109         }
110 
111         // Push the folder
112         stack.add(0, children);
113         children = new HashSet(currentFolderInfo.children.keySet());
114         currentFolder = folder;
115 
116         return true;
117     }
118 
119     /***
120      * Finds a child of the current folder.
121      */
122     private FileInfo getChild(final String baseName)
123     {
124         if (currentFolderInfo == null)
125         {
126             assertEquals(rootFile.baseName, baseName);
127             return rootFile;
128         }
129         else
130         {
131             final FileInfo child = (FileInfo) currentFolderInfo.children.get(baseName);
132             assertNotNull(child);
133             return child;
134         }
135     }
136 
137     /***
138      * Asserts that the selector has seen all the files.
139      *
140      * @return The files in the order they where visited.
141      */
142     public List finish()
143     {
144         assertEquals(0, children.size());
145         return files;
146     }
147 }