1 16 package org.apache.commons.vfs.test; 17 18 import org.apache.commons.vfs.FileContent; 19 import org.apache.commons.vfs.FileObject; 20 import org.apache.commons.vfs.FileSystem; 21 import org.apache.commons.vfs.FileSystemException; 22 import org.apache.commons.vfs.FileType; 23 import org.apache.commons.vfs.NameScope; 24 25 import java.io.InputStream ; 26 import java.util.Iterator ; 27 28 33 public class ContentTests 34 extends AbstractProviderTestCase 35 { 36 39 public void testAllContent() throws Exception 40 { 41 final FileInfo baseInfo = buildExpectedStructure(); 42 final FileObject baseFolder = getReadFolder(); 43 44 assertSameContent(baseInfo, baseFolder); 45 } 46 47 50 private void assertSameContent(final FileInfo expected, 51 final FileObject folder) throws Exception 52 { 53 for (Iterator iterator = expected.children.values().iterator(); iterator.hasNext();) 54 { 55 final FileInfo fileInfo = (FileInfo) iterator.next(); 56 final FileObject child = folder.resolveFile(fileInfo.baseName, NameScope.CHILD); 57 58 assertTrue(child.getName().toString(), child.exists()); 59 if (fileInfo.type == FileType.FILE) 60 { 61 assertSameContent(fileInfo.content, child); 62 } 63 else 64 { 65 assertSameContent(fileInfo, child); 66 } 67 } 68 } 69 70 73 public void testExists() throws Exception 74 { 75 FileObject file = getReadFolder().resolveFile("file1.txt"); 77 assertTrue("file exists", file.exists()); 78 assertTrue("file exists", file.getType() != FileType.IMAGINARY); 79 80 file = getReadFolder().resolveFile("dir1"); 82 assertTrue("folder exists", file.exists()); 83 assertTrue("folder exists", file.getType() != FileType.IMAGINARY); 84 85 file = getReadFolder().resolveFile("unknown-child"); 87 assertTrue("unknown file does not exist", !file.exists()); 88 assertTrue("unknown file does not exist", 89 file.getType() == FileType.IMAGINARY); 90 91 file = getReadFolder().resolveFile("unknown-folder/unknown-child"); 93 assertTrue("unknown file does not exist", !file.exists()); 94 assertTrue("unknown file does not exist", 95 file.getType() == FileType.IMAGINARY); 96 } 97 98 101 public void testRoot() throws FileSystemException 102 { 103 final FileObject file = getReadFolder().getFileSystem().getRoot(); 104 assertTrue(file.exists()); 105 assertTrue(file.getType() != FileType.IMAGINARY); 106 } 107 108 111 public void testParent() throws FileSystemException 112 { 113 FileObject folder = getReadFolder().resolveFile("dir1"); 115 FileObject child = folder.resolveFile("file3.txt"); 116 assertTrue("folder exists", folder.exists()); 117 assertTrue("child exists", child.exists()); 118 assertSame(folder, child.getParent()); 119 120 child = folder.resolveFile("unknown-file"); 122 assertTrue("folder exists", folder.exists()); 123 assertTrue("child does not exist", !child.exists()); 124 assertSame(folder, child.getParent()); 125 126 folder = getReadFolder().resolveFile("unknown-folder"); 128 child = folder.resolveFile("unknown-file"); 129 assertTrue("folder does not exist", !folder.exists()); 130 assertTrue("child does not exist", !child.exists()); 131 assertSame(folder, child.getParent()); 132 133 final FileSystem fileSystem = getReadFolder().getFileSystem(); 136 FileObject root = fileSystem.getRoot(); 137 if (fileSystem.getParentLayer() == null) 138 { 139 assertNull("root has null parent", root.getParent()); 141 } 142 else 143 { 144 assertSame(fileSystem.getParentLayer().getParent(), root.getParent()); 146 } 147 } 148 149 152 public void testChildren() throws FileSystemException 153 { 154 FileObject file = getReadFolder().resolveFile("file1.txt"); 156 assertSame(FileType.FILE, file.getType()); 157 try 158 { 159 file.getChildren(); 160 fail(); 161 } 162 catch (FileSystemException e) 163 { 164 assertSameMessage("vfs.provider/list-children-not-folder.error", file, e); 165 } 166 167 file = file.resolveFile("some-child"); 169 assertNotNull(file); 170 171 file = getReadFolder().resolveFile("unknown-file"); 173 assertTrue(!file.exists()); 174 try 175 { 176 file.getChildren(); 177 fail(); 178 } 179 catch (final FileSystemException e) 180 { 181 assertSameMessage("vfs.provider/list-children-not-folder.error", file, e); 182 } 183 184 FileObject child = file.resolveFile("some-child"); 186 assertNotNull(child); 187 } 188 189 192 public void testContent() throws Exception 193 { 194 FileObject file = getReadFolder().resolveFile("file1.txt"); 196 assertSameContent(FILE1_CONTENT, file); 197 198 file = getReadFolder().resolveFile("empty.txt"); 200 assertSameContent("", file); 201 } 202 203 206 public void testUnknownContent() throws Exception 207 { 208 209 FileObject unknownFile = getReadFolder().resolveFile("unknown-file"); 211 FileContent content = unknownFile.getContent(); 212 try 213 { 214 content.getInputStream(); 215 fail(); 216 } 217 catch (FileSystemException e) 218 { 219 assertSameMessage("vfs.provider/read-not-file.error", unknownFile, e); 220 } 221 try 222 { 223 content.getSize(); 224 fail(); 225 } 226 catch (final FileSystemException e) 227 { 228 assertSameMessage("vfs.provider/get-size-not-file.error", unknownFile, e); 229 } 230 } 231 232 235 public void testConcurrentRead() throws Exception 236 { 237 final FileObject file = getReadFolder().resolveFile("file1.txt"); 238 assertTrue(file.exists()); 239 240 final InputStream instr = file.getContent().getInputStream(); 242 try 243 { 244 file.getContent().getInputStream().close(); 246 } 247 finally 248 { 249 instr.close(); 250 } 251 } 252 253 256 public void testConcurrentReadFiles() throws Exception 257 { 258 final FileObject file = getReadFolder().resolveFile("file1.txt"); 259 assertTrue(file.exists()); 260 final FileObject emptyFile = getReadFolder().resolveFile("empty.txt"); 261 assertTrue(emptyFile.exists()); 262 263 final InputStream instr = file.getContent().getInputStream(); 265 try 266 { 267 assertSameContent("", emptyFile); 269 } 270 finally 271 { 272 instr.close(); 273 } 274 } 275 276 279 public void testReuse() throws Exception 280 { 281 FileObject file = getReadFolder().resolveFile("file1.txt"); 283 assertEquals(FileType.FILE, file.getType()); 284 285 assertSameContent(FILE1_CONTENT, file); 287 288 assertSameContent(FILE1_CONTENT, file); 290 291 file.getContent().close(); 293 file.close(); 294 295 assertSameContent(FILE1_CONTENT, file); 297 } 298 299 302 public void testInstrCleanup() throws Exception 303 { 304 FileObject file = getReadFolder().resolveFile("file1.txt"); 306 assertEquals(FileType.FILE, file.getType()); 307 308 final InputStream instr1 = file.getContent().getInputStream(); 310 assertTrue(instr1.read() == FILE1_CONTENT.charAt(0)); 311 final InputStream instr2 = file.getContent().getInputStream(); 312 assertTrue(instr2.read() == FILE1_CONTENT.charAt(0)); 313 314 file.close(); 316 317 assertTrue(instr1.read() == -1); 319 assertTrue(instr2.read() == -1); 320 } 321 } 322 | Popular Tags |