1 16 package org.apache.commons.vfs.test; 17 18 import org.apache.commons.vfs.Capability; 19 import org.apache.commons.vfs.FileObject; 20 import org.apache.commons.vfs.FileSystemException; 21 import org.apache.commons.vfs.FileType; 22 23 import java.io.InputStream ; 24 import java.util.ArrayList ; 25 import java.util.List ; 26 27 33 public class ProviderReadTests 34 extends AbstractProviderTestCase 35 { 36 39 protected Capability[] getRequiredCaps() 40 { 41 return new Capability[] 42 { 43 Capability.GET_TYPE, 44 Capability.LIST_CHILDREN, 45 Capability.READ_CONTENT 46 }; 47 } 48 49 53 public void testStructure() throws Exception 54 { 55 final FileInfo baseInfo = buildExpectedStructure(); 56 assertSameStructure(getReadFolder(), baseInfo); 57 } 58 59 63 protected void assertSameStructure(final FileObject folder, 64 final FileInfo expected) 65 throws Exception 66 { 67 final List queueExpected = new ArrayList (); 69 queueExpected.add(expected); 70 71 final List queueActual = new ArrayList (); 72 queueActual.add(folder); 73 74 while (queueActual.size() > 0) 75 { 76 final FileObject file = (FileObject) queueActual.remove(0); 77 final FileInfo info = (FileInfo) queueExpected.remove(0); 78 79 assertSame(info.type, file.getType()); 81 82 if (info.type == FileType.FILE) 83 { 84 continue; 85 } 86 87 final FileObject[] children = file.getChildren(); 89 90 assertNotNull(children); 92 assertEquals("count children of \"" + file.getName() + "\"", info.children.size(), children.length); 93 94 for (int i = 0; i < children.length; i++) 96 { 97 final FileObject child = children[i]; 98 final FileInfo childInfo = (FileInfo) info.children.get(child.getName().getBaseName()); 99 100 assertNotNull(childInfo); 102 103 queueExpected.add(childInfo); 105 queueActual.add(child); 106 } 107 } 108 } 109 110 113 public void testType() throws Exception 114 { 115 FileObject file = getReadFolder().resolveFile("file1.txt"); 117 assertSame(FileType.FILE, file.getType()); 118 119 file = getReadFolder().resolveFile("dir1"); 121 assertSame(FileType.FOLDER, file.getType()); 122 123 file = getReadFolder().resolveFile("unknown-child"); 125 assertSame(FileType.IMAGINARY, file.getType()); 126 } 127 128 131 public void testRoot() throws FileSystemException 132 { 133 final FileObject file = getReadFolder().getFileSystem().getRoot(); 134 file.getChildren(); 135 } 136 137 140 public void testFolderContent() throws Exception 141 { 142 FileObject folder = getReadFolder().resolveFile("dir1"); 144 try 145 { 146 folder.getContent().getInputStream(); 147 fail(); 148 } 149 catch (FileSystemException e) 150 { 151 assertSameMessage("vfs.provider/read-not-file.error", folder, e); 152 } 153 } 154 155 158 public void testConcurrentReadFolder() throws Exception 159 { 160 final FileObject file = getReadFolder().resolveFile("file1.txt"); 161 assertTrue(file.exists()); 162 final FileObject folder = getReadFolder().resolveFile("dir1"); 163 assertTrue(folder.exists()); 164 165 final InputStream instr = file.getContent().getInputStream(); 167 try 168 { 169 folder.exists(); 171 folder.getType(); 172 folder.getChildren(); 173 } 174 finally 175 { 176 instr.close(); 177 } 178 } 179 180 183 public void testFindFiles() throws Exception 184 { 185 final FileInfo fileInfo = buildExpectedStructure(); 186 final VerifyingFileSelector selector = new VerifyingFileSelector(fileInfo); 187 188 final FileObject[] actualFiles = getReadFolder().findFiles(selector); 190 191 final List expectedFiles = selector.finish(); 193 assertEquals(expectedFiles.size(), actualFiles.length); 194 final int count = expectedFiles.size(); 195 for (int i = 0; i < count; i++) 196 { 197 final FileObject expected = (FileObject) expectedFiles.get(i); 198 final FileObject actual = actualFiles[i]; 199 assertEquals(expected, actual); 200 } 201 } 202 } 203 | Popular Tags |