| 1 | |
package org.webslinger.commons.vfs.tests; |
| 2 | |
|
| 3 | |
import org.webslinger.httpunit.WebslingerTestCase; |
| 4 | |
|
| 5 | |
import java.io.File; |
| 6 | |
import java.io.FileInputStream; |
| 7 | |
import java.io.IOException; |
| 8 | |
import java.util.Arrays; |
| 9 | |
import java.util.HashSet; |
| 10 | |
|
| 11 | |
import org.apache.commons.vfs.FileContent; |
| 12 | |
import org.apache.commons.vfs.FileContentInfo; |
| 13 | |
import org.apache.commons.vfs.FileObject; |
| 14 | |
import org.apache.commons.vfs.operations.FileOperation; |
| 15 | |
import org.apache.commons.vfs.operations.FileOperations; |
| 16 | |
import org.apache.commons.vfs.Selectors; |
| 17 | |
import org.apache.commons.vfs.impl.StandardFileSystemManager; |
| 18 | |
|
| 19 | |
import org.webslinger.containers.TestContainer; |
| 20 | |
import org.webslinger.commons.vfs.VFSUtil; |
| 21 | |
import org.webslinger.commons.vfs.operations.AttributeValueOperation; |
| 22 | |
import org.webslinger.commons.vfs.tests.CreationDeletionHelper; |
| 23 | |
import org.webslinger.commons.vfs.flat.tests.CreationDeletionTest; |
| 24 | |
import org.webslinger.io.IOUtil; |
| 25 | |
|
| 26 | |
public class TestFlat extends TestContainer.TestContainerTestCase { |
| 27 | |
protected StandardFileSystemManager sfsm; |
| 28 | |
protected FileObject root; |
| 29 | |
|
| 30 | |
public TestFlat(String name) { |
| 31 | 5 | super(name); |
| 32 | 5 | } |
| 33 | |
|
| 34 | |
protected void setUp() throws Exception { |
| 35 | 5 | super.setUp(); |
| 36 | 5 | sfsm = VFSUtil.createStandardFileSystemManager(); |
| 37 | 5 | sfsm.setBaseFile(getBase()); |
| 38 | 5 | root = sfsm.createFileSystem("flat", sfsm.resolveFile(".")); |
| 39 | 5 | root.delete(Selectors.SELECT_ALL); |
| 40 | 5 | root.createFolder(); |
| 41 | 5 | } |
| 42 | |
|
| 43 | |
protected void tearDown() throws Exception { |
| 44 | |
try { |
| 45 | 5 | IOUtil.delete(getBase()); |
| 46 | 0 | } catch (Exception e) { |
| 47 | 0 | e.printStackTrace(); |
| 48 | 5 | } |
| 49 | 5 | super.tearDown(); |
| 50 | 5 | } |
| 51 | |
|
| 52 | |
public void testFileCreationDeletion() throws Exception { |
| 53 | 1 | new CreationDeletionTest(root, getBase()).doTest(new CreationDeletionHelper() { |
| 54 | |
public void create(File file) throws IOException { |
| 55 | 1 | IOUtil.touch(file); |
| 56 | 1 | } |
| 57 | |
|
| 58 | |
public void create(FileObject file) throws IOException { |
| 59 | 3 | file.createFile(); |
| 60 | 3 | } |
| 61 | |
}); |
| 62 | 1 | } |
| 63 | |
|
| 64 | |
public void testFolderCreationDeletion() throws Exception { |
| 65 | 1 | new CreationDeletionTest(root, getBase()).doTest(new CreationDeletionHelper() { |
| 66 | |
public void create(File file) throws IOException { |
| 67 | 1 | file.mkdirs(); |
| 68 | 1 | } |
| 69 | |
|
| 70 | |
public void create(FileObject file) throws IOException { |
| 71 | 3 | file.createFolder(); |
| 72 | 3 | } |
| 73 | |
}); |
| 74 | 1 | } |
| 75 | |
|
| 76 | |
public void testFile() throws Exception { |
| 77 | 1 | FileObject vfsFile = root.resolveFile("file"); |
| 78 | 1 | File realFile = new File(getBase(), "file"); |
| 79 | 1 | assertFalse("file(real) does not exist", realFile.exists()); |
| 80 | 1 | assertFalse("file(vfs) does not exist", vfsFile.exists()); |
| 81 | 1 | IOUtil.writeString(realFile, "file content"); |
| 82 | 1 | assertTrue("file(real) exists", realFile.exists()); |
| 83 | 1 | assertFalse("file(vfs) does not exist", vfsFile.exists()); |
| 84 | 1 | vfsFile.refresh(); |
| 85 | 1 | assertTrue("file(real) exists", realFile.exists()); |
| 86 | 1 | assertTrue("file(vfs) does not exist", vfsFile.exists()); |
| 87 | 1 | FileContent fc = vfsFile.getContent(); |
| 88 | 1 | assertEquals("sizes are the same", realFile.length(), fc.getSize()); |
| 89 | 1 | assertTrue("content is the same", IOUtil.cmp(new FileInputStream(realFile), fc.getInputStream())); |
| 90 | 1 | } |
| 91 | |
|
| 92 | |
public void testContentInfo() throws Exception { |
| 93 | 1 | FileObject file = root.resolveFile("file"); |
| 94 | 1 | FileContent fc = file.getContent(); |
| 95 | 1 | FileContentInfo fci = fc.getContentInfo(); |
| 96 | 1 | file.createFile(); |
| 97 | 1 | assertNull("no content type", fci.getContentType()); |
| 98 | 1 | assertNull("no content encoding", fci.getContentEncoding()); |
| 99 | 1 | fc.setAttribute("content-type", "application/octet-stream"); |
| 100 | 1 | fc.setAttribute("content-encoding", "raw"); |
| 101 | 1 | assertEquals("content type", "application/octet-stream", fci.getContentType()); |
| 102 | 1 | assertEquals("content encoding", "raw", fci.getContentEncoding()); |
| 103 | 1 | } |
| 104 | |
|
| 105 | |
public void testOperations() throws Exception { |
| 106 | 1 | FileObject file = root.resolveFile("file"); |
| 107 | 1 | file.createFile(); |
| 108 | 1 | FileContent fc = file.getContent(); |
| 109 | 1 | fc.setAttribute("first", "First's value"); |
| 110 | 1 | fc.setAttribute("second", "The value for second"); |
| 111 | 1 | FileOperations operations = file.getFileOperations(); |
| 112 | 1 | Class[] fileOperationClasses = operations.getOperations(); |
| 113 | 2 | for (int i = 0; i < fileOperationClasses.length; i++) { |
| 114 | 1 | FileOperation operation = operations.getOperation(fileOperationClasses[i]); |
| 115 | 1 | if (operation instanceof AttributeValueOperation) { |
| 116 | 1 | AttributeValueOperation avo = (AttributeValueOperation) operation; |
| 117 | 1 | avo.getAttributeValue("first"); |
| 118 | 1 | avo.getAttributeValue("second"); |
| 119 | |
} |
| 120 | |
} |
| 121 | 1 | } |
| 122 | |
} |