KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > test > ProviderReadTests


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26
27 /**
28  * Read-only test cases for file providers.
29  *
30  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
31  * @todo Test getLastModified(), getAttribute()
32  */

33 public class ProviderReadTests
34     extends AbstractProviderTestCase
35 {
36     /**
37      * Returns the capabilities required by the tests of this test case.
38      */

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     /**
50      * Walks the base folder structure, asserting it contains exactly the
51      * expected files and folders.
52      */

53     public void testStructure() throws Exception JavaDoc
54     {
55         final FileInfo baseInfo = buildExpectedStructure();
56         assertSameStructure(getReadFolder(), baseInfo);
57     }
58
59     /**
60      * Walks a folder structure, asserting it contains exactly the
61      * expected files and folders.
62      */

63     protected void assertSameStructure(final FileObject folder,
64                                        final FileInfo expected)
65         throws Exception JavaDoc
66     {
67         // Setup the structure
68
final List JavaDoc queueExpected = new ArrayList JavaDoc();
69         queueExpected.add(expected);
70
71         final List JavaDoc queueActual = new ArrayList JavaDoc();
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             // Check the type is correct
80
assertSame(info.type, file.getType());
81
82             if (info.type == FileType.FILE)
83             {
84                 continue;
85             }
86
87             // Check children
88
final FileObject[] children = file.getChildren();
89
90             // Make sure all children were found
91
assertNotNull(children);
92             assertEquals("count children of \"" + file.getName() + "\"", info.children.size(), children.length);
93
94             // Recursively check each child
95
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                 // Make sure the child is expected
101
assertNotNull(childInfo);
102
103                 // Add to the queue of files to check
104
queueExpected.add(childInfo);
105                 queueActual.add(child);
106             }
107         }
108     }
109
110     /**
111      * Tests type determination.
112      */

113     public void testType() throws Exception JavaDoc
114     {
115         // Test a file
116
FileObject file = getReadFolder().resolveFile("file1.txt");
117         assertSame(FileType.FILE, file.getType());
118
119         // Test a folder
120
file = getReadFolder().resolveFile("dir1");
121         assertSame(FileType.FOLDER, file.getType());
122
123         // Test an unknown file
124
file = getReadFolder().resolveFile("unknown-child");
125         assertSame(FileType.IMAGINARY, file.getType());
126     }
127
128     /**
129      * Tests the contents of root of file system can be listed.
130      */

131     public void testRoot() throws FileSystemException
132     {
133         final FileObject file = getReadFolder().getFileSystem().getRoot();
134         file.getChildren();
135     }
136
137     /**
138      * Tests that folders have no content.
139      */

140     public void testFolderContent() throws Exception JavaDoc
141     {
142         // Try getting the content of a folder
143
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     /**
156      * Tests can perform operations on a folder while reading from a different files.
157      */

158     public void testConcurrentReadFolder() throws Exception JavaDoc
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         // Start reading from the file
166
final InputStream JavaDoc instr = file.getContent().getInputStream();
167         try
168         {
169             // Do some operations
170
folder.exists();
171             folder.getType();
172             folder.getChildren();
173         }
174         finally
175         {
176             instr.close();
177         }
178     }
179
180     /**
181      * Tests that findFiles() works.
182      */

183     public void testFindFiles() throws Exception JavaDoc
184     {
185         final FileInfo fileInfo = buildExpectedStructure();
186         final VerifyingFileSelector selector = new VerifyingFileSelector(fileInfo);
187
188         // Find the files
189
final FileObject[] actualFiles = getReadFolder().findFiles(selector);
190
191         // Compare actual and expected list of files
192
final List JavaDoc 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