1 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 ; 26 import java.util.HashSet ; 27 import java.util.List ; 28 import java.util.Set ; 29 30 36 public class VerifyingFileSelector 37 extends Assert 38 implements FileSelector 39 { 40 private final FileInfo rootFile; 41 private final List files = new ArrayList (); 42 43 private FileInfo currentFolderInfo; 44 private FileObject currentFolder; 45 private Set children; 46 private List stack = new ArrayList (); 47 48 public VerifyingFileSelector(final FileInfo fileInfo) 49 { 50 this.rootFile = fileInfo; 51 children = new HashSet (); 52 children.add(rootFile.baseName); 53 } 54 55 58 public boolean includeFile(final FileSelectInfo fileInfo) 59 throws FileSystemException 60 { 61 final FileObject file = fileInfo.getFile(); 62 if (file == currentFolder) 63 { 64 assertEquals(0, children.size()); 66 currentFolder = currentFolder.getParent(); 67 currentFolderInfo = currentFolderInfo.getParent(); 68 children = (Set ) stack.remove(0); 69 } 70 71 final String 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 86 public boolean traverseDescendents(final FileSelectInfo fileInfo) 87 throws FileSystemException 88 { 89 final FileObject folder = fileInfo.getFile(); 91 assertSame(FileType.FOLDER, folder.getType()); 92 93 final String 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 currentFolderInfo = getChild(baseName); 106 assertSame(FileType.FOLDER, currentFolderInfo.type); 107 } 108 109 stack.add(0, children); 111 children = new HashSet (currentFolderInfo.children.keySet()); 112 currentFolder = folder; 113 114 return true; 115 } 116 117 120 private FileInfo getChild(final String 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 140 public List finish() 141 { 142 assertEquals(0, children.size()); 143 return files; 144 } 145 } 146 | Popular Tags |