1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 package org.objectledge.filesystem;
30
31 import java.io.ByteArrayInputStream;
32 import java.io.ByteArrayOutputStream;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.io.OutputStream;
36 import java.net.URL;
37
38 import junit.framework.TestCase;
39
40 /***
41 *
42 * <p>Created on Jan 8, 2004</p>
43 * @author <a href="Rafal.Krzewski">rafal@caltha.pl</a>
44 * @version $Id: FileSystemTest.java,v 1.10 2004/09/27 19:11:04 zwierzem Exp $
45 */
46 public class FileSystemTest extends TestCase
47 {
48 private FileSystem fs;
49
50 /***
51 * Constructor for FileSystemTest.
52 * @param arg0
53 */
54 public FileSystemTest(String arg0)
55 throws Exception
56 {
57 super(arg0);
58 fs = FileSystem.getStandardFileSystem("src/test/resources");
59 }
60
61 public void setUp()
62 throws Exception
63 {
64 fs.createNewFile("filex");
65 }
66
67 public void tearDown()
68 throws Exception
69 {
70 fs.delete("filex");
71 }
72
73 public void testURL()
74 throws Exception
75 {
76
77 URL url = fs.getResource("/filex");
78 InputStream is = url.openStream();
79 is.read();
80 }
81
82 public void testGetProtocol()
83 throws Exception
84 {
85 assertEquals(fs.getProtocol(),"ledge");
86 }
87
88 public void testGetProviders()
89 throws Exception
90 {
91 assertEquals(fs.getProviders().length,2);
92 }
93
94 public void testGetProviderString()
95 throws Exception
96 {
97 assertEquals(fs.getProvider("local").getName(),"local");
98 try
99 {
100 assertEquals(fs.getProvider("foo").getName(),"foo");
101 fail("should throw the exception");
102 }
103 catch(IllegalArgumentException e)
104 {
105
106 }
107 }
108
109 public void testGetResource()
110 throws Exception
111 {
112 assertNull(fs.getResource("foo/bar"));
113 }
114
115
116 public void testGetOutputStream()
117 throws Exception
118 {
119 OutputStream os = fs.getOutputStream("filex");
120 assertNotNull(os);
121 os.close();
122
123 os = fs.getOutputStream("foo");
124 assertNotNull(os);
125 os.close();
126 assertEquals(fs.exists("foo"),true);
127 fs.delete("foo");
128 assertEquals(fs.exists("foo"),false);
129 os = fs.getOutputStream("filex",true);
130 assertNotNull(os);
131 os.close();
132 os = fs.getOutputStream("foo",true);
133 assertNotNull(os);
134 os.close();
135 fs.delete("foo");
136 assertEquals(fs.exists("foo"),false);
137 }
138
139 public void testGetRandomAccess()
140 throws Exception
141 {
142 RandomAccessFile file = fs.getRandomAccess("filex","rw");
143 assertNotNull(file);
144 file.write("abcd".getBytes(), 0, 4);
145 byte[] buff = new byte[4];
146
147
148 long ptr = file.getFilePointer();
149 assertTrue(4L == ptr);
150 file.seek(0);
151 ptr = file.getFilePointer();
152 assertTrue(0L == ptr);
153 assertTrue(4L == file.length());
154 int r = file.read();
155 ptr = file.getFilePointer();
156 assertTrue(1L == ptr);
157 int rb = file.read(buff);
158 ptr = file.getFilePointer();
159 assertTrue(4L == ptr);
160 assertEquals(3, rb);
161 file.seek(0);
162 int roff = file.read(buff, 0, 4);
163 assertEquals(4, roff);
164 file.seek(0);
165 file.skipBytes(2);
166 ptr = file.getFilePointer();
167 assertTrue(2L == ptr);
168 file.write("efg".getBytes());
169 file.write('h');
170 assertTrue(6L == file.length());
171 file.setLength(4);
172 assertTrue(4L == file.length());
173 file.close();
174 }
175
176 public void testIsFile()
177 throws Exception
178 {
179 assertEquals(fs.isFile("filex"),true);
180 assertEquals(fs.isFile("config"),false);
181 }
182
183
184 public void testIsDirectory()
185 throws Exception
186 {
187 assertEquals(fs.isDirectory("filex"),false);
188 assertEquals(fs.isDirectory("config"),true);
189 }
190
191 public void testCanRead()
192 throws Exception
193 {
194 assertEquals(fs.canRead("filex"),true);
195 assertEquals(fs.canRead("foo"),false);
196 }
197
198 public void testCanWrite()
199 throws Exception
200 {
201 assertEquals(fs.canWrite("filex"),true);
202 assertEquals(fs.canRead("foo"),false);
203 }
204
205 public void testLastModified()
206 throws Exception
207 {
208 assertEquals(fs.lastModified("filex") == -1L,false);
209 assertEquals(fs.lastModified("foo"),-1L);
210 }
211
212 public void testLength()
213 throws Exception
214 {
215 assertEquals(fs.length("filex"),0);
216 assertEquals(fs.length("foo"),-1L);
217 }
218
219 public void testList()
220 throws Exception
221 {
222 assertEquals(fs.list("directory").length,5);
223 try
224 {
225 assertEquals(fs.list("directory_that_does_not_exist").length,2);
226 fail("should throw the exception");
227 }
228 catch(IOException e)
229 {
230
231 }
232 try
233 {
234 assertEquals(fs.list("filex").length,2);
235 fail("should throw the exception");
236 }
237 catch(IOException e)
238 {
239
240 }
241
242 }
243
244 public void testCreateNewFile()
245 throws Exception
246 {
247 boolean result = fs.createNewFile("filex");
248 assertEquals(result,false);
249 result = fs.createNewFile("foo");
250 assertEquals(result,true);
251 fs.delete("foo");
252 }
253
254 public void testMkdirs()
255 throws IOException
256 {
257 assertEquals(fs.exists("foo"),false);
258 try
259 {
260 fs.mkdirs("foo");
261 }
262 catch(UnsupportedCharactersInFilePathException e)
263 {
264 fail("simple ascii characters should be supported: "+e.getMessage());
265 }
266 assertEquals(fs.exists("foo"),true);
267 assertEquals(fs.isDirectory("foo"),true);
268 fs.delete("foo");
269 try
270 {
271 fs.mkdirs("filex");
272 fail("should throw the exception");
273 }
274 catch(Exception e)
275 {
276
277 }
278 }
279
280 public void testDelete()
281 throws IOException
282 {
283 try
284 {
285 fs.delete("foo");
286 fail("should throw the exception");
287 }
288 catch(Exception e)
289 {
290
291 }
292 }
293
294 public void testRename()
295 throws IOException
296 {
297 try
298 {
299 fs.mkdirs("foo");
300 }
301 catch(UnsupportedCharactersInFilePathException e)
302 {
303 fail("simple ascii characters should be supported: "+e.getMessage());
304 }
305 assertEquals(fs.exists("foo"),true);
306 assertEquals(fs.exists("bar"),false);
307 try
308 {
309 fs.rename("foo","bar");
310 }
311 catch(UnsupportedCharactersInFilePathException e)
312 {
313 fail("simple ascii characters should be supported: "+e.getMessage());
314 }
315 assertEquals(fs.exists("foo"),false);
316 assertEquals(fs.exists("bar"),true);
317 fs.delete("bar");
318 try
319 {
320 fs.rename("bar","bar2");
321 fail("should throw the exception");
322 }
323 catch(Exception e)
324 {
325
326 }
327 }
328
329 public void testCopyFile()
330 throws IOException
331 {
332 assertEquals(fs.exists("foo"),false);
333 try
334 {
335 fs.copyFile("filex","foo");
336 }
337 catch(UnsupportedCharactersInFilePathException e)
338 {
339 fail("simple ascii characters should be supported: "+e.getMessage());
340 }
341 assertEquals(fs.exists("foo"),true);
342 fs.delete("foo");
343 try
344 {
345 fs.copyFile("foo","bar");
346 fail("should throw the exception");
347 }
348 catch(Exception e)
349 {
350
351 }
352 try
353 {
354 fs.copyFile("directory","bar");
355 fail("should throw the exception");
356 }
357 catch(Exception e)
358 {
359
360 }
361 }
362
363 public void testCopyDir()
364 throws IOException
365 {
366 assertEquals(fs.exists("directory"),true);
367 int size = fs.list("directory").length;
368 try
369 {
370 fs.copyDir("directory","directory2");
371 }
372 catch(UnsupportedCharactersInFilePathException e)
373 {
374 fail("simple ascii characters should be supported: "+e.getMessage());
375 }
376 assertEquals(fs.exists("directory2"),true);
377 assertEquals(fs.list("directory2").length,size);
378 fs.deleteRecursive("directory2");
379 assertEquals(fs.exists("directory2"),false);
380 try
381 {
382 fs.copyDir("foo","bar");
383 fail("should throw the exception");
384 }
385 catch(Exception e)
386 {
387
388 }
389 try
390 {
391 fs.copyFile("directory","fail");
392 fail("should throw the exception");
393 }
394 catch(Exception e)
395 {
396
397 }
398 }
399
400 public void testReadStringOuputStream()
401 throws Exception
402 {
403 fs.createNewFile("foo");
404 assertEquals(fs.exists("foo"),true);
405 fs.write("foo","bar","ISO-8859-2");
406 assertEquals(fs.exists("foo"),true);
407 assertEquals(fs.read("foo","ISO-8859-2"),"bar");
408 assertEquals(fs.read("foo")[0],"bar".getBytes("ISO-8859-2")[0]);
409 assertEquals(fs.read("foo")[1],"bar".getBytes("ISO-8859-2")[1]);
410 assertEquals(fs.read("foo")[2],"bar".getBytes("ISO-8859-2")[2]);
411 ByteArrayOutputStream baos = new ByteArrayOutputStream(4);
412 fs.read("foo", baos);
413 assertEquals(baos.toString("ISO-8859-2"),"bar");
414
415 fs.createNewFile("foo");
416 fs.write("foo","bar".getBytes("ISO-8859-2"));
417 assertEquals(fs.read("foo","ISO-8859-2"),"bar");
418
419 fs.createNewFile("foo");
420 fs.write("foo",new ByteArrayInputStream("bar".getBytes("ISO-8859-2")));
421 assertEquals(fs.read("foo","ISO-8859-2"),"bar");
422
423 fs.delete("foo");
424 }
425
426 public void testPaths()
427 throws Exception
428 {
429 assertEquals(FileSystem.normalizedPath("foo/bar"),"/foo/bar");
430 assertEquals(FileSystem.normalizedPath("foo//bar"),"/foo/bar");
431 assertEquals(FileSystem.normalizedPath("foo/../bar"),"/bar");
432 assertEquals(FileSystem.normalizedPath("foo/./bar"),"/foo/bar");
433 assertEquals(FileSystem.normalizedPath("/"),"/");
434
435 assertEquals(FileSystem.basePath("/foo/bar"),"bar");
436 assertEquals(FileSystem.basePath("foo"),"foo");
437 assertEquals(FileSystem.basePath("/"),"");
438
439 assertEquals(FileSystem.directoryPath("/"),"");
440 assertEquals(FileSystem.directoryPath("foo"),"");
441 assertEquals(FileSystem.directoryPath("foo/bar"),"/foo");
442
443 assertEquals(FileSystem.relativePath("/foo/bar","/foo"),"/bar");
444 try
445 {
446 assertEquals(FileSystem.relativePath("/foo","/foo/bar"),"/bar");
447 fail("should throw the exception");
448 }
449 catch(IllegalArgumentException e)
450 {
451
452 }
453
454 assertTrue(fs.checkPathChars("/only/ascii/characters"));
455 assertFalse(fs.checkPathChars(""));
456 assertFalse(fs.checkPathChars(null));
457 }
458
459
460 }
461