KickJava   Java API By Example, From Geeks To Geeks.

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


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.AbstractVfsTestCase;
19 import org.apache.commons.vfs.Capability;
20 import org.apache.commons.vfs.FileContent;
21 import org.apache.commons.vfs.FileObject;
22 import org.apache.commons.vfs.FileSystemException;
23 import org.apache.commons.vfs.FileType;
24 import org.apache.commons.vfs.impl.DefaultFileSystemManager;
25 import org.apache.commons.vfs.provider.AbstractFileSystem;
26
27 import java.io.ByteArrayOutputStream JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.lang.reflect.InvocationTargetException JavaDoc;
30 import java.lang.reflect.Method JavaDoc;
31 import java.net.URLConnection JavaDoc;
32 import java.util.Arrays JavaDoc;
33
34 /**
35  * File system test cases, which verifies the structure and naming
36  * functionality.
37  * <p/>
38  * Works from a base folder, and assumes a particular structure under
39  * that base folder.
40  *
41  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
42  */

43 public abstract class AbstractProviderTestCase
44     extends AbstractVfsTestCase
45 {
46     private FileObject baseFolder;
47     private FileObject readFolder;
48     private FileObject writeFolder;
49     private DefaultFileSystemManager manager;
50     private Method JavaDoc method;
51
52     // Expected contents of "file1.txt"
53
public static final String JavaDoc FILE1_CONTENT = "This is a test file.";
54
55     // Expected contents of test files
56
public static final String JavaDoc TEST_FILE_CONTENT = "A test file.";
57
58     /**
59      * Sets the test method.
60      */

61     public void setMethod(final Method JavaDoc method)
62     {
63         this.method = method;
64     }
65
66     /**
67      * Configures this test.
68      */

69     public void setConfig(final DefaultFileSystemManager manager,
70                           final FileObject baseFolder,
71                           final FileObject readFolder,
72                           final FileObject writeFolder)
73     {
74         this.manager = manager;
75         this.baseFolder = baseFolder;
76         this.readFolder = readFolder;
77         this.writeFolder = writeFolder;
78     }
79
80     /**
81      * Returns the file system manager used by this test.
82      */

83     protected DefaultFileSystemManager getManager()
84     {
85         return manager;
86     }
87
88     /**
89      * Returns the base test folder. This is the parent of both the read
90      * test and write test folders.
91      */

92     public FileObject getBaseFolder()
93     {
94         return baseFolder;
95     }
96
97     /**
98      * Returns the read test folder.
99      */

100     protected FileObject getReadFolder()
101     {
102         return readFolder;
103     }
104
105     /**
106      * Returns the write test folder.
107      */

108     protected FileObject getWriteFolder()
109     {
110         return writeFolder;
111     }
112
113     /**
114      * Returns the capabilities required by the tests of this test case. The
115      * tests are not run if the provider being tested does not support all
116      * the required capabilities. Return null or an empty array to always
117      * run the tests.
118      * <p/>
119      * <p>This implementation returns null.
120      */

121     protected Capability[] getRequiredCaps()
122     {
123         return null;
124     }
125
126     /**
127      * Runs the test. This implementation short-circuits the test if the
128      * provider being tested does not have the capabilities required by this
129      * test.
130      *
131      * @todo Handle negative caps as well - ie, only run a test if the provider does not have certain caps.
132      * @todo Figure out how to remove the test from the TestResult if the test is skipped.
133      */

134     protected void runTest() throws Throwable JavaDoc
135     {
136         // Check the capabilities
137
final Capability[] caps = getRequiredCaps();
138         if (caps != null)
139         {
140             for (int i = 0; i < caps.length; i++)
141             {
142                 final Capability cap = caps[i];
143                 if (!readFolder.getFileSystem().hasCapability(cap))
144                 {
145                     System.out.println("skipping " + getName() + " because fs does not have cap " + cap);
146                     return;
147                 }
148             }
149         }
150
151         // Provider has all the capabilities - execute the test
152
if (method != null)
153         {
154             try
155             {
156                 method.invoke(this, null);
157             }
158             catch (final InvocationTargetException JavaDoc e)
159             {
160                 throw e.getTargetException();
161             }
162         }
163         else
164         {
165             super.runTest();
166         }
167
168         if (((AbstractFileSystem) readFolder.getFileSystem()).isOpen())
169         {
170             String JavaDoc name = "unknown";
171             if (method != null)
172             {
173                 name = method.getName();
174             }
175
176             throw new IllegalStateException JavaDoc(getClass().getName() + ": filesystem has open streams after: " + name);
177         }
178     }
179
180     /**
181      * Asserts that the content of a file is the same as expected. Checks the
182      * length reported by getContentLength() is correct, then reads the content
183      * as a byte stream and compares the result with the expected content.
184      * Assumes files are encoded using UTF-8.
185      */

186     protected void assertSameURLContent(final String JavaDoc expected,
187                                         final URLConnection JavaDoc connection)
188         throws Exception JavaDoc
189     {
190         // Get file content as a binary stream
191
final byte[] expectedBin = expected.getBytes("utf-8");
192
193         // Check lengths
194
assertEquals("same content length", expectedBin.length, connection.getContentLength());
195
196         // Read content into byte array
197
final InputStream JavaDoc instr = connection.getInputStream();
198         final ByteArrayOutputStream JavaDoc outstr;
199         try
200         {
201             outstr = new ByteArrayOutputStream JavaDoc();
202             final byte[] buffer = new byte[256];
203             int nread = 0;
204             while (nread >= 0)
205             {
206                 outstr.write(buffer, 0, nread);
207                 nread = instr.read(buffer);
208             }
209         }
210         finally
211         {
212             instr.close();
213         }
214
215         // Compare
216
assertTrue("same binary content", Arrays.equals(expectedBin, outstr.toByteArray()));
217     }
218
219     /**
220      * Asserts that the content of a file is the same as expected. Checks the
221      * length reported by getSize() is correct, then reads the content as
222      * a byte stream and compares the result with the expected content.
223      * Assumes files are encoded using UTF-8.
224      */

225     protected void assertSameContent(final String JavaDoc expected,
226                                      final FileObject file)
227         throws Exception JavaDoc
228     {
229         // Check the file exists, and is a file
230
assertTrue(file.exists());
231         assertSame(FileType.FILE, file.getType());
232
233         // Get file content as a binary stream
234
final byte[] expectedBin = expected.getBytes("utf-8");
235
236         // Check lengths
237
final FileContent content = file.getContent();
238         assertEquals("same content length", expectedBin.length, content.getSize());
239
240         // Read content into byte array
241
final InputStream JavaDoc instr = content.getInputStream();
242         final ByteArrayOutputStream JavaDoc outstr;
243         try
244         {
245             outstr = new ByteArrayOutputStream JavaDoc(expectedBin.length);
246             final byte[] buffer = new byte[256];
247             int nread = 0;
248             while (nread >= 0)
249             {
250                 outstr.write(buffer, 0, nread);
251                 nread = instr.read(buffer);
252             }
253         }
254         finally
255         {
256             instr.close();
257         }
258
259         // Compare
260
assertTrue("same binary content", Arrays.equals(expectedBin, outstr.toByteArray()));
261     }
262
263     /**
264      * Builds the expected structure of the read tests folder.
265      */

266     protected FileInfo buildExpectedStructure() throws FileSystemException
267     {
268         // Build the expected structure
269
final FileInfo base = new FileInfo(getReadFolder().getName().getBaseName(), FileType.FOLDER);
270         base.addFile("file1.txt", FILE1_CONTENT);
271         // file%.txt - test out encoding
272
base.addFile("file%25.txt", FILE1_CONTENT);
273
274         // file?test.txt - test out encoding (test.txt is not the queryString)
275
// as we do not know if the current file provider we need to
276
// ask it to normalize the name
277
// todo: move this into the FileInfo class to do it generally?
278
/* webdav-bug?: didnt manage to get the "?" correctly through webdavlib
279         FileSystemManager fsm = getReadFolder().getFileSystem().getFileSystemManager();
280         FileName fn = fsm.resolveName(getReadFolder().getName(), "file%3ftest.txt");
281         String baseName = fn.getBaseName();
282         base.addFile(baseName, FILE1_CONTENT);
283         */

284         base.addFile("file space.txt", FILE1_CONTENT);
285
286         base.addFile("empty.txt", "");
287         base.addFolder("emptydir");
288
289         final FileInfo dir = base.addFolder("dir1");
290         dir.addFile("file1.txt", TEST_FILE_CONTENT);
291         dir.addFile("file2.txt", TEST_FILE_CONTENT);
292         dir.addFile("file3.txt", TEST_FILE_CONTENT);
293
294         final FileInfo subdir1 = dir.addFolder("subdir1");
295         subdir1.addFile("file1.txt", TEST_FILE_CONTENT);
296         subdir1.addFile("file2.txt", TEST_FILE_CONTENT);
297         subdir1.addFile("file3.txt", TEST_FILE_CONTENT);
298
299         final FileInfo subdir2 = dir.addFolder("subdir2");
300         subdir2.addFile("file1.txt", TEST_FILE_CONTENT);
301         subdir2.addFile("file2.txt", TEST_FILE_CONTENT);
302         subdir2.addFile("file3.txt", TEST_FILE_CONTENT);
303
304         final FileInfo subdir3 = dir.addFolder("subdir3");
305         subdir3.addFile("file1.txt", TEST_FILE_CONTENT);
306         subdir3.addFile("file2.txt", TEST_FILE_CONTENT);
307         subdir3.addFile("file3.txt", TEST_FILE_CONTENT);
308
309         return base;
310     }
311 }
312
Popular Tags