1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs.test;
18
19 import org.apache.commons.vfs.Capability;
20 import org.apache.commons.vfs.FileObject;
21 import org.apache.commons.vfs.FileSystemException;
22 import org.apache.commons.vfs.FileType;
23
24 import java.io.InputStream;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 /***
29 * Read-only test cases for file providers.
30 *
31 * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
32 * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
33 * @todo Test getLastModified(), getAttribute()
34 */
35 public class ProviderReadTests
36 extends AbstractProviderTestCase
37 {
38 /***
39 * Returns the capabilities required by the tests of this test case.
40 */
41 protected Capability[] getRequiredCaps()
42 {
43 return new Capability[]
44 {
45 Capability.GET_TYPE,
46 Capability.LIST_CHILDREN,
47 Capability.READ_CONTENT
48 };
49 }
50
51 /***
52 * Walks the base folder structure, asserting it contains exactly the
53 * expected files and folders.
54 */
55 public void testStructure() throws Exception
56 {
57 final FileInfo baseInfo = buildExpectedStructure();
58 assertSameStructure(getReadFolder(), baseInfo);
59 }
60
61 /***
62 * Walks a folder structure, asserting it contains exactly the
63 * expected files and folders.
64 */
65 protected void assertSameStructure(final FileObject folder,
66 final FileInfo expected)
67 throws Exception
68 {
69
70 final List queueExpected = new ArrayList();
71 queueExpected.add(expected);
72
73 final List queueActual = new ArrayList();
74 queueActual.add(folder);
75
76 while (queueActual.size() > 0)
77 {
78 final FileObject file = (FileObject) queueActual.remove(0);
79 final FileInfo info = (FileInfo) queueExpected.remove(0);
80
81
82 assertSame(info.type, file.getType());
83
84 if (info.type == FileType.FILE)
85 {
86 continue;
87 }
88
89
90 final FileObject[] children = file.getChildren();
91
92
93 assertNotNull(children);
94 assertEquals("count children of \"" + file.getName() + "\"", info.children.size(), children.length);
95
96
97 for (int i = 0; i < children.length; i++)
98 {
99 final FileObject child = children[i];
100 final FileInfo childInfo = (FileInfo) info.children.get(child.getName().getBaseName());
101
102
103 assertNotNull(childInfo);
104
105
106 queueExpected.add(childInfo);
107 queueActual.add(child);
108 }
109 }
110 }
111
112 /***
113 * Tests type determination.
114 */
115 public void testType() throws Exception
116 {
117
118 FileObject file = getReadFolder().resolveFile("file1.txt");
119 assertSame(FileType.FILE, file.getType());
120
121
122 file = getReadFolder().resolveFile("dir1");
123 assertSame(FileType.FOLDER, file.getType());
124
125
126 file = getReadFolder().resolveFile("unknown-child");
127 assertSame(FileType.IMAGINARY, file.getType());
128 }
129
130 /***
131 * Tests the contents of root of file system can be listed.
132 */
133 public void testRoot() throws FileSystemException
134 {
135 final FileObject file = getReadFolder().getFileSystem().getRoot();
136 file.getChildren();
137 }
138
139 /***
140 * Tests that folders have no content.
141 */
142 public void testFolderContent() throws Exception
143 {
144
145 FileObject folder = getReadFolder().resolveFile("dir1");
146 try
147 {
148 folder.getContent().getInputStream();
149 fail();
150 }
151 catch (FileSystemException e)
152 {
153 assertSameMessage("vfs.provider/read-not-file.error", folder, e);
154 }
155 }
156
157 /***
158 * Tests can perform operations on a folder while reading from a different files.
159 */
160 public void testConcurrentReadFolder() throws Exception
161 {
162 final FileObject file = getReadFolder().resolveFile("file1.txt");
163 assertTrue(file.exists());
164 final FileObject folder = getReadFolder().resolveFile("dir1");
165 assertTrue(folder.exists());
166
167
168 final InputStream instr = file.getContent().getInputStream();
169 try
170 {
171
172 folder.exists();
173 folder.getType();
174 folder.getChildren();
175 }
176 finally
177 {
178 instr.close();
179 }
180 }
181
182 /***
183 * Tests that findFiles() works.
184 */
185 public void testFindFiles() throws Exception
186 {
187 final FileInfo fileInfo = buildExpectedStructure();
188 final VerifyingFileSelector selector = new VerifyingFileSelector(fileInfo);
189
190
191 final FileObject[] actualFiles = getReadFolder().findFiles(selector);
192
193
194 final List expectedFiles = selector.finish();
195 assertEquals(expectedFiles.size(), actualFiles.length);
196 final int count = expectedFiles.size();
197 for (int i = 0; i < count; i++)
198 {
199 final FileObject expected = (FileObject) expectedFiles.get(i);
200 final FileObject actual = actualFiles[i];
201 assertEquals(expected, actual);
202 }
203 }
204 }