Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
196   524   45   5,44
0   294   0,23   36
36     1,25  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  FileManagerTest       Line # 44 196 0% 45 9 96,1% 0.9612069
 
  (70)
 
1    /*
2    * Copyright (c) 2003-2006, Simon Brown
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    *
8    * - Redistributions of source code must retain the above copyright
9    * notice, this list of conditions and the following disclaimer.
10    *
11    * - Redistributions in binary form must reproduce the above copyright
12    * notice, this list of conditions and the following disclaimer in
13    * the documentation and/or other materials provided with the
14    * distribution.
15    *
16    * - Neither the name of Pebble nor the names of its contributors may
17    * be used to endorse or promote products derived from this software
18    * without specific prior written permission.
19    *
20    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21    * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22    * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23    * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24    * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28    * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30    * POSSIBILITY OF SUCH DAMAGE.
31    */
32    package net.sourceforge.pebble.domain;
33   
34    import java.io.BufferedWriter;
35    import java.io.File;
36    import java.io.FileWriter;
37    import java.util.List;
38   
39    /**
40    * Tests for the FileManager class.
41    *
42    * @author Simon Brown
43    */
 
44    public class FileManagerTest extends SingleBlogTestCase {
45   
46    private FileManager fileManager;
47   
 
48  70 toggle protected void setUp() throws Exception {
49  70 super.setUp();
50   
51  70 fileManager = new FileManager(blog, FileMetaData.BLOG_FILE);
52    }
53   
54    /**
55    * Tests that a file manager for blog images can be created.
56    */
 
57  2 toggle public void testCreateFileManagerForBlogImages() {
58  2 fileManager = new FileManager(blog, FileMetaData.BLOG_IMAGE);
59  2 assertEquals(new File(blog.getImagesDirectory()), fileManager.getRootDirectory());
60    }
61   
62    /**
63    * Tests that a file manager for blog files can be created.
64    */
 
65  2 toggle public void testCreateFileManagerForBlogFiles() {
66  2 fileManager = new FileManager(blog, FileMetaData.BLOG_FILE);
67  2 assertEquals(new File(blog.getFilesDirectory()), fileManager.getRootDirectory());
68    }
69   
70    /**
71    * Tests that a file manager for theme files can be created.
72    */
 
73  2 toggle public void testCreateFileManagerForThemeFiles() {
74  2 Theme theme = new Theme(blog, "custom", "/some/path");
75  2 blog.setEditableTheme(theme);
76  2 fileManager = new FileManager(blog, FileMetaData.THEME_FILE);
77  2 assertEquals(blog.getEditableTheme().getPathToLiveTheme(), fileManager.getRootDirectory());
78    }
79   
80    /**
81    * Tests that a file manager for blog data can be created.
82    */
 
83  2 toggle public void testCreateFileManagerForBlogData() {
84  2 fileManager = new FileManager(blog, FileMetaData.BLOG_DATA);
85  2 assertEquals(new File(blog.getRoot()), fileManager.getRootDirectory());
86    }
87   
88    /**
89    * Tests that a information about a file can be retrieved using an
90    * absolute path.
91    */
 
92  2 toggle public void testGetFileMetaDataWithAbsolutePath() {
93  2 FileMetaData file = fileManager.getFileMetaData("/afile.zip");
94  2 assertFalse(file.isDirectory());
95  2 assertEquals("/", file.getPath());
96  2 assertEquals("afile.zip", file.getName());
97   
98  2 file = fileManager.getFileMetaData("/a/a.txt");
99  2 assertFalse(file.isDirectory());
100  2 assertEquals("/a", file.getPath());
101  2 assertEquals("a.txt", file.getName());
102    }
103   
104    /**
105    * Tests that a information about a file can be retrieved using a
106    * path and name.
107    */
 
108  2 toggle public void testGetFileMetaDataWithPathAndName() {
109  2 FileMetaData file = fileManager.getFileMetaData("/", "afile.zip");
110  2 assertFalse(file.isDirectory());
111  2 assertEquals("/", file.getPath());
112  2 assertEquals("afile.zip", file.getName());
113   
114  2 file = fileManager.getFileMetaData("/a", "a.txt");
115  2 assertFalse(file.isDirectory());
116  2 assertEquals("/a", file.getPath());
117  2 assertEquals("a.txt", file.getName());
118    }
119   
120    /**
121    * Tests that a java.io.File reference to a path can be created.
122    */
 
123  2 toggle public void testGetFileFromAbsolutePath() {
124  2 File file = fileManager.getFile("/afile.zip");
125  2 assertEquals(new File(blog.getFilesDirectory(), "afile.zip"), file);
126    }
127   
128    /**
129    * Tests that a java.io.File reference to a path can be created.
130    */
 
131  2 toggle public void testGetFileFromRelativePath() throws Exception {
132  2 File file = fileManager.getFile("afile.zip");
133  2 assertEquals(new File(blog.getFilesDirectory(), "afile.zip"), file);
134   
135  2 file = fileManager.getFile("./afile.zip");
136  2 assertEquals(new File(blog.getFilesDirectory(), "afile.zip").getCanonicalFile(), file.getCanonicalFile());
137    }
138   
139    /**
140    * Tests whether whether a file is underneath the root directory.
141    */
 
142  2 toggle public void testFileIsUnderneathRootDirectory() {
143  2 File file = fileManager.getFile("/afile.zip");
144  2 assertTrue(fileManager.isUnderneathRootDirectory(file));
145    }
146   
147    /**
148    * Tests whether whether a file is underneath the root directory.
149    */
 
150  2 toggle public void testFileIsNotUnderneathRootDirectory() {
151  2 File file = fileManager.getFile("../afile.zip");
152  2 assertFalse(fileManager.isUnderneathRootDirectory(file));
153    }
154   
155    /**
156    * Tests that a directory can be created.
157    */
 
158  2 toggle public void testCreateDirectory() throws Exception {
159  2 File newDirectory = fileManager.createDirectory("/", "asubdirectory");
160  2 assertEquals(new File(blog.getFilesDirectory(), "asubdirectory"), newDirectory);
161  2 assertTrue(newDirectory.exists());
162   
163    // and clean up this test
164  2 newDirectory.delete();
165    }
166   
167    /**
168    * Tests that a directory can't be created outside of the root.
169    */
 
170  2 toggle public void testCreateDirectoryThrowsExceptionWhenOutsideOfRoot() {
171  2 try {
172  2 fileManager.createDirectory("/", "../asubdirectory");
173  0 fail("Creating a directory outside of the root isn't allowed");
174    } catch (IllegalFileAccessException ifae) {
175    }
176    }
177   
178    /**
179    * Tests that a file isn't copied when no name is specified.
180    */
 
181  2 toggle public void testFileNotCopiedWhenNoNameGiven() throws Exception {
182  2 assertNull(fileManager.copyFile("/", "afile.txt", null));
183  2 assertNull(fileManager.copyFile("/", "afile.txt", ""));
184    }
185   
186    /**
187    * Tests that a file isn't copied when the same name is specified.
188    */
 
189  2 toggle public void testFileNotCopiedWhenSameNameGiven() throws Exception {
190  2 assertNull(fileManager.copyFile("/", "afile.txt", "afile.txt"));
191    }
192   
193    /**
194    * Tests that a file can't be copied when the original file is outside of
195    * the root.
196    */
 
197  2 toggle public void testCopyFileThrowsExceptionWhenOriginalFileOutsideOfRoot() throws Exception {
198  2 try {
199  2 assertNull(fileManager.copyFile("/", "../afile.txt", "afile.txt"));
200  0 fail("Copying a file outside of the root isn't allowed");
201    } catch (IllegalFileAccessException ifae) {
202    }
203    }
204   
205    /**
206    * Tests that a file can't be copied when the new file is outside of
207    * the root.
208    */
 
209  2 toggle public void testCopyFileThrowsExceptionWhenNewFileOutsideOfRoot() throws Exception {
210  2 try {
211  2 assertNull(fileManager.copyFile("/", "afile.txt", "../afile.txt"));
212  0 fail("Copying a file outside of the root isn't allowed");
213    } catch (IllegalFileAccessException ifae) {
214    }
215    }
216   
217    /**
218    * Tests that a file can be copied.
219    */
 
220  2 toggle public void testCopyFile() throws Exception {
221  2 File file = fileManager.getFile("/afile.txt");
222  2 BufferedWriter writer = new BufferedWriter(new FileWriter(file));
223  2 writer.write("Testing...");
224  2 writer.flush();
225  2 writer.close();
226   
227  2 File newFile = fileManager.copyFile("/", "afile.txt", "anewfile.txt");
228  2 assertNotNull(newFile);
229  2 assertTrue(newFile.exists());
230  2 assertEquals(file.length(), newFile.length());
231   
232    // and clean up
233  2 file.delete();
234  2 newFile.delete();
235    }
236   
237    /**
238    * Tests that a file isn't renamed when no name is specified.
239    */
 
240  2 toggle public void testFileNotRenamedWhenNoNameGiven() throws Exception {
241  2 assertNull(fileManager.renameFile("/", "afile.txt", null));
242  2 assertNull(fileManager.renameFile("/", "afile.txt", ""));
243    }
244   
245    /**
246    * Tests that a file isn't renamed when the same name is specified.
247    */
 
248  2 toggle public void testFileNotRenamedWhenSameNameGiven() throws Exception {
249  2 assertNull(fileManager.renameFile("/", "afile.txt", "afile.txt"));
250    }
251   
252    /**
253    * Tests that a file can't be renamed when the original file is outside of
254    * the root.
255    */
 
256  2 toggle public void testRenameFileThrowsExceptionWhenOriginalFileOutsideOfRoot() throws Exception {
257  2 try {
258  2 assertNull(fileManager.renameFile("/", "../afile.txt", "afile.txt"));
259  0 fail("Renaming a file outside of the root isn't allowed");
260    } catch (IllegalFileAccessException ifae) {
261    }
262    }
263   
264    /**
265    * Tests that a file can't be renamed when the new file is outside of
266    * the root.
267    */
 
268  2 toggle public void testRenameFileThrowsExceptionWhenNewFileOutsideOfRoot() throws Exception {
269  2 try {
270  2 assertNull(fileManager.renameFile("/", "afile.txt", "../afile.txt"));
271  0 fail("Renaming a file outside of the root isn't allowed");
272    } catch (IllegalFileAccessException ifae) {
273    }
274    }
275   
276    /**
277    * Tests that a file can be renamed.
278    */
 
279  2 toggle public void testRenameFile() throws Exception {
280  2 File file = fileManager.getFile("/afile.txt");
281  2 BufferedWriter writer = new BufferedWriter(new FileWriter(file));
282  2 writer.write("Testing...");
283  2 writer.flush();
284  2 writer.close();
285  2 long length = file.length();
286   
287  2 File newFile = fileManager.renameFile("/", "afile.txt", "anewfile.txt");
288  2 assertNotNull(newFile);
289  2 assertFalse(file.exists());
290  2 assertTrue(newFile.exists());
291  2 assertEquals(length, newFile.length());
292   
293    // and clean up
294  2 newFile.delete();
295    }
296   
297    /**
298    * Tests that a file can't be deleted outside of the root.
299    */
 
300  2 toggle public void testDeleteFileThrowsExceptionWhenNewFileOutsideOfRoot() throws Exception {
301  2 try {
302  2 fileManager.deleteFile("/", "../afile.txt");
303  0 fail("Deleting a file outside of the root isn't allowed");
304    } catch (IllegalFileAccessException ifae) {
305    }
306    }
307   
308    /**
309    * Tests that a file can be loaded.
310    */
 
311  2 toggle public void testLoadFile() throws Exception {
312  2 File file = fileManager.getFile("/afile.txt");
313  2 BufferedWriter writer = new BufferedWriter(new FileWriter(file));
314  2 writer.write("First line");
315  2 writer.newLine();
316  2 writer.write("Second line");
317  2 writer.flush();
318  2 writer.close();
319   
320  2 StringBuffer expectedContent = new StringBuffer();
321  2 expectedContent.append("First line");
322  2 expectedContent.append(System.getProperty("line.separator"));
323  2 expectedContent.append("Second line");
324   
325  2 String content = fileManager.loadFile("/", "afile.txt");
326  2 assertEquals(expectedContent.toString(), content);
327   
328    // and clean up
329  2 file.delete();
330    }
331   
332    /**
333    * Tests that a file can't be loaded from outside of the root.
334    */
 
335  2 toggle public void testLoadFileThrowsExceptionWhenNewFileOutsideOfRoot() throws Exception {
336  2 try {
337  2 fileManager.loadFile("/", "../afile.txt");
338  0 fail("Loading a file outside of the root isn't allowed");
339    } catch (IllegalFileAccessException ifae) {
340    }
341    }
342   
343    /**
344    * Tests that a file can be saved.
345    */
 
346  2 toggle public void testSaveFile() throws Exception {
347  2 StringBuffer content = new StringBuffer();
348  2 content.append("First line");
349  2 content.append(System.getProperty("line.separator"));
350  2 content.append("Second line");
351   
352  2 fileManager.saveFile("/", "afile.txt", content.toString());
353  2 assertEquals(content.toString(), fileManager.loadFile("/", "afile.txt"));
354   
355    // and clean up
356  2 File file = fileManager.getFile("/afile.txt");
357  2 file.delete();
358    }
359   
360    /**
361    * Tests that a file can't be saved outside of the root.
362    */
 
363  2 toggle public void testSaveFileThrowsExceptionWhenNewFileOutsideOfRoot() throws Exception {
364  2 try {
365  2 fileManager.saveFile("/", "../afile.txt", "some content");
366  0 fail("Saving a file outside of the root isn't allowed");
367    } catch (IllegalFileAccessException ifae) {
368    }
369    }
370   
371    /**
372    * Tests that files can be accessed.
373    */
 
374  2 toggle public void testGetFiles() throws Exception {
375    // create some files and directories
376  2 fileManager.createDirectory("/", "a");
377  2 fileManager.createDirectory("/", "z");
378  2 fileManager.saveFile("/", "y.txt", "Some content");
379  2 fileManager.saveFile("/", "b.txt", "Some content");
380   
381  2 List files = fileManager.getFiles("/");
382  2 assertEquals(4, files.size());
383   
384    // the files should be in this order
385    // - a, z, b.txt, y.txt (directories followed by files, both alphabetically)
386  2 FileMetaData file = (FileMetaData)files.get(0);
387  2 assertEquals("a", file.getName());
388  2 assertEquals("/", file.getPath());
389  2 assertTrue(file.isDirectory());
390  2 file = (FileMetaData)files.get(1);
391  2 assertEquals("z", file.getName());
392  2 assertEquals("/", file.getPath());
393  2 assertTrue(file.isDirectory());
394  2 file = (FileMetaData)files.get(2);
395  2 assertEquals("b.txt", file.getName());
396  2 assertEquals("/", file.getPath());
397  2 assertFalse(file.isDirectory());
398  2 file = (FileMetaData)files.get(3);
399  2 assertEquals("y.txt", file.getName());
400  2 assertEquals("/", file.getPath());
401  2 assertFalse(file.isDirectory());
402   
403    // and clean up
404  2 fileManager.deleteFile("/", "a");
405  2 fileManager.deleteFile("/", "z");
406  2 fileManager.deleteFile("/", "y.txt");
407  2 fileManager.deleteFile("/", "b.txt");
408    }
409   
410    /**
411    * Tests that files can be accessed recursively.
412    */
 
413  2 toggle public void testGetFilesRecursively() throws Exception {
414    // create some files and directories
415  2 fileManager.createDirectory("/", "a");
416  2 fileManager.saveFile("/a", "a.txt", "Some content");
417  2 fileManager.saveFile("/", "b.txt", "Some content");
418   
419  2 List files = fileManager.getFiles("/", true);
420  2 assertEquals(3, files.size());
421   
422    // the files should be in this order
423    // - a, a/a.txt, b.txt (directories followed by files, alphabetically and recursively)
424  2 FileMetaData file = (FileMetaData)files.get(0);
425  2 assertEquals("a", file.getName());
426  2 assertEquals("/", file.getPath());
427  2 assertTrue(file.isDirectory());
428  2 file = (FileMetaData)files.get(1);
429  2 assertEquals("a.txt", file.getName());
430  2 assertEquals("/a", file.getPath());
431  2 assertFalse(file.isDirectory());
432  2 file = (FileMetaData)files.get(2);
433  2 assertEquals("b.txt", file.getName());
434  2 assertEquals("/", file.getPath());
435  2 assertFalse(file.isDirectory());
436   
437    // and clean up
438  2 fileManager.deleteFile("/a", "a.txt");
439  2 fileManager.deleteFile("/", "a");
440  2 fileManager.deleteFile("/", "b.txt");
441    }
442   
443    /**
444    * Tests that files can be accessed when directory is empty.
445    */
 
446  2 toggle public void testGetFilesFromEmptyDirectory() throws Exception {
447  2 List files = fileManager.getFiles("/");
448  2 assertEquals(0, files.size());
449    }
450   
451    /**
452    * Tests that files can be accessed from a non-existent directory.
453    */
 
454  2 toggle public void testGetFilesFromNonExistentDirectory() throws Exception {
455    // the theme path "/some/path" doesn't exist
456  2 Theme theme = new Theme(blog, "custom", "/some/path");
457  2 blog.setEditableTheme(theme);
458  2 fileManager = new FileManager(blog, FileMetaData.THEME_FILE);
459  2 List files = fileManager.getFiles("/");
460  2 assertEquals(0, files.size());
461    }
462   
463    /**
464    * Tests that files can't be accessed outside of the root.
465    */
 
466  2 toggle public void testGetFilesThrowsExceptionWhenNewFileOutsideOfRoot() throws Exception {
467  2 try {
468  2 fileManager.getFiles("../");
469  0 fail("Accessing files outside of the root isn't allowed");
470    } catch (IllegalFileAccessException ifae) {
471    }
472    }
473   
474    /**
475    * Tests that the URLs for blog files are set correctly.
476    */
 
477  2 toggle public void testUrlForBlogFile() throws Exception {
478  2 fileManager = new FileManager(blog, FileMetaData.BLOG_FILE);
479  2 fileManager.saveFile("/", "a.txt", "Some content");
480   
481  2 List files = fileManager.getFiles("/");
482  2 FileMetaData file = (FileMetaData)files.get(0);
483  2 assertEquals("files/a.txt", file.getUrl());
484   
485    // and clean up
486  2 fileManager.deleteFile("/", "a.txt");
487    }
488   
489    /**
490    * Tests that the URLs for blog images are set correctly.
491    */
 
492  2 toggle public void testUrlForBlogImage() throws Exception {
493  2 fileManager = new FileManager(blog, FileMetaData.BLOG_IMAGE);
494  2 fileManager.saveFile("/", "a.txt", "Some content");
495   
496  2 List files = fileManager.getFiles("/");
497  2 FileMetaData file = (FileMetaData)files.get(0);
498  2 assertEquals("images/a.txt", file.getUrl());
499   
500    // and clean up
501  2 fileManager.deleteFile("/", "a.txt");
502    }
503   
504    /**
505    * Tests that the URLs for theme files are set correctly.
506    */
 
507  2 toggle public void testUrlForThemeFile() throws Exception {
508  2 Theme theme = new Theme(blog, "theme", blog.getRoot());
509  2 File themeDirectory = new File(blog.getThemeDirectory());
510  2 themeDirectory.mkdir();
511  2 blog.setEditableTheme(theme);
512  2 fileManager = new FileManager(blog, FileMetaData.THEME_FILE);
513  2 fileManager.saveFile("/", "a.txt", "Some content");
514   
515  2 List files = fileManager.getFiles("/");
516  2 FileMetaData file = (FileMetaData)files.get(0);
517  2 assertEquals("theme/a.txt", file.getUrl());
518   
519    // and clean up
520  2 fileManager.deleteFile("/", "a.txt");
521  2 themeDirectory.delete();
522    }
523   
524    }