KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > test > VerifyingFileSelector


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.vfs.test;
17
18 import junit.framework.Assert;
19 import org.apache.commons.vfs.FileObject;
20 import org.apache.commons.vfs.FileSelectInfo;
21 import org.apache.commons.vfs.FileSelector;
22 import org.apache.commons.vfs.FileSystemException;
23 import org.apache.commons.vfs.FileType;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Set JavaDoc;
29
30 /**
31  * A file selector that asserts that all files are visited, in the correct
32  * order.
33  *
34  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
35  */

36 public class VerifyingFileSelector
37     extends Assert
38     implements FileSelector
39 {
40     private final FileInfo rootFile;
41     private final List JavaDoc files = new ArrayList JavaDoc();
42
43     private FileInfo currentFolderInfo;
44     private FileObject currentFolder;
45     private Set JavaDoc children;
46     private List JavaDoc stack = new ArrayList JavaDoc();
47
48     public VerifyingFileSelector(final FileInfo fileInfo)
49     {
50         this.rootFile = fileInfo;
51         children = new HashSet JavaDoc();
52         children.add(rootFile.baseName);
53     }
54
55     /**
56      * Determines if a file or folder should be selected.
57      */

58     public boolean includeFile(final FileSelectInfo fileInfo)
59         throws FileSystemException
60     {
61         final FileObject file = fileInfo.getFile();
62         if (file == currentFolder)
63         {
64             // Pop current folder
65
assertEquals(0, children.size());
66             currentFolder = currentFolder.getParent();
67             currentFolderInfo = currentFolderInfo.getParent();
68             children = (Set JavaDoc) stack.remove(0);
69         }
70
71         final String JavaDoc baseName = file.getName().getBaseName();
72
73         final FileInfo childInfo = getChild(baseName);
74         assertSame(childInfo.type, file.getType());
75
76         final boolean isChild = children.remove(baseName);
77         assertTrue(isChild);
78
79         files.add(file);
80         return true;
81     }
82
83     /**
84      * Determines whether a folder should be traversed.
85      */

86     public boolean traverseDescendents(final FileSelectInfo fileInfo)
87         throws FileSystemException
88     {
89         // Check that the given file is a folder
90
final FileObject folder = fileInfo.getFile();
91         assertSame(FileType.FOLDER, folder.getType());
92
93         // Locate the info for the folder
94
final String JavaDoc baseName = folder.getName().getBaseName();
95         if (currentFolder == null)
96         {
97             assertEquals(rootFile.baseName, baseName);
98             currentFolderInfo = rootFile;
99         }
100         else
101         {
102             assertSame(currentFolder, folder.getParent());
103
104             // Locate the info for the child, and make sure it is folder
105
currentFolderInfo = getChild(baseName);
106             assertSame(FileType.FOLDER, currentFolderInfo.type);
107         }
108
109         // Push the folder
110
stack.add(0, children);
111         children = new HashSet JavaDoc(currentFolderInfo.children.keySet());
112         currentFolder = folder;
113
114         return true;
115     }
116
117     /**
118      * Finds a child of the current folder.
119      */

120     private FileInfo getChild(final String JavaDoc baseName)
121     {
122         if (currentFolderInfo == null)
123         {
124             assertEquals(rootFile.baseName, baseName);
125             return rootFile;
126         }
127         else
128         {
129             final FileInfo child = (FileInfo) currentFolderInfo.children.get(baseName);
130             assertNotNull(child);
131             return child;
132         }
133     }
134
135     /**
136      * Asserts that the selector has seen all the files.
137      *
138      * @return The files in the order they where visited.
139      */

140     public List JavaDoc finish()
141     {
142         assertEquals(0, children.size());
143         return files;
144     }
145 }
146
Popular Tags