Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
78   188   7   11,14
0   102   0,09   7
7     1  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  ViewFilesActionTest       Line # 48 78 0% 7 0 100% 1.0
 
  (12)
 
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.web.action;
33   
34    import net.sourceforge.pebble.domain.FileManager;
35    import net.sourceforge.pebble.domain.FileMetaData;
36    import net.sourceforge.pebble.domain.Theme;
37    import net.sourceforge.pebble.web.view.ForbiddenView;
38    import net.sourceforge.pebble.web.view.View;
39    import net.sourceforge.pebble.web.view.impl.FilesView;
40   
41    import java.util.List;
42   
43    /**
44    * Tests for the ViewFilesAction class.
45    *
46    * @author Simon Brown
47    */
 
48    public class ViewFilesActionTest extends SecureActionTestCase {
49   
 
50  12 toggle protected void setUp() throws Exception {
51  12 action = new ViewFilesAction();
52   
53  12 super.setUp();
54    }
55   
56    /**
57    * Tests that files can be accessed.
58    */
 
59  2 toggle public void testViewFiles() throws Exception {
60    // create some files and directories
61  2 FileManager fileManager = new FileManager(blog, FileMetaData.BLOG_FILE);
62  2 fileManager.createDirectory("/", "a");
63  2 fileManager.createDirectory("/", "z");
64  2 fileManager.saveFile("/", "y.txt", "Some content");
65  2 fileManager.saveFile("/", "b.txt", "Some content");
66   
67  2 request.setParameter("path", "/");
68  2 request.setParameter("type", FileMetaData.BLOG_FILE);
69   
70  2 View view = action.process(request, response);
71   
72    // check file information available and the right view is returned
73  2 assertEquals(FileMetaData.BLOG_FILE, action.getModel().get("type"));
74  2 FileMetaData fileMetaData = (FileMetaData)action.getModel().get("directory");
75  2 assertEquals("/", fileMetaData.getAbsolutePath());
76  2 assertTrue(fileMetaData.isDirectory());
77  2 assertEquals("uploadFileToBlog.secureaction", action.getModel().get("uploadAction"));
78  2 assertTrue(view instanceof FilesView);
79   
80  2 List files = (List)action.getModel().get("files");
81  2 assertEquals(4, files.size());
82   
83    // the files should be in this order
84    // - a, z, b.txt, y.txt (directories followed by files, both alphabetically)
85  2 FileMetaData file = (FileMetaData)files.get(0);
86  2 assertEquals("a", file.getName());
87  2 assertEquals("/", file.getPath());
88  2 assertTrue(file.isDirectory());
89  2 file = (FileMetaData)files.get(1);
90  2 assertEquals("z", file.getName());
91  2 assertEquals("/", file.getPath());
92  2 assertTrue(file.isDirectory());
93  2 file = (FileMetaData)files.get(2);
94  2 assertEquals("b.txt", file.getName());
95  2 assertEquals("/", file.getPath());
96  2 assertFalse(file.isDirectory());
97  2 file = (FileMetaData)files.get(3);
98  2 assertEquals("y.txt", file.getName());
99  2 assertEquals("/", file.getPath());
100  2 assertFalse(file.isDirectory());
101   
102    // and clean up
103  2 fileManager.deleteFile("/", "a");
104  2 fileManager.deleteFile("/", "z");
105  2 fileManager.deleteFile("/", "y.txt");
106  2 fileManager.deleteFile("/", "b.txt");
107    }
108   
109    /**
110    * Tests that files can be accessed, even when the path isn't specified.
111    */
 
112  2 toggle public void testViewFilesWhenPathNotSpecified() throws Exception {
113    // create some files and directories
114  2 FileManager fileManager = new FileManager(blog, FileMetaData.BLOG_FILE);
115  2 fileManager.saveFile("/", "a.txt", "Some content");
116   
117  2 request.setParameter("type", FileMetaData.BLOG_FILE);
118  2 action.process(request, response);
119  2 List files = (List)action.getModel().get("files");
120  2 assertEquals(1, files.size());
121  2 FileMetaData file = (FileMetaData)files.get(0);
122  2 assertEquals("a.txt", file.getName());
123  2 assertEquals("/", file.getPath());
124  2 assertFalse(file.isDirectory());
125   
126    // and the same test with a blank path
127  2 request.setParameter("path", "");
128  2 action.process(request, response);
129  2 files = (List)action.getModel().get("files");
130  2 assertEquals(1, files.size());
131  2 file = (FileMetaData)files.get(0);
132  2 assertEquals("a.txt", file.getName());
133  2 assertEquals("/", file.getPath());
134  2 assertFalse(file.isDirectory());
135   
136    // and clean up
137  2 fileManager.deleteFile("/", "a.txt");
138    }
139   
140    /**
141    * Tests that the upload action is set correctly for blog images.
142    */
 
143  2 toggle public void testUploadActionForBlogImages() throws Exception {
144  2 request.setParameter("path", "/");
145  2 request.setParameter("type", FileMetaData.BLOG_IMAGE);
146  2 action.process(request, response);
147  2 assertEquals("uploadImageToBlog.secureaction", action.getModel().get("uploadAction"));
148    }
149   
150    /**
151    * Tests that the upload action is set correctly for theme files.
152    */
 
153  2 toggle public void testUploadActionForThemeFiles() throws Exception {
154  2 request.setParameter("path", "/");
155  2 request.setParameter("type", FileMetaData.THEME_FILE);
156  2 Theme theme = new Theme(blog, "custom", "/some/path");
157  2 blog.setEditableTheme(theme);
158  2 action.process(request, response);
159  2 assertEquals("uploadFileToTheme.secureaction", action.getModel().get("uploadAction"));
160    }
161   
162    /**
163    * Tests that files can't be accessed outside of the root.
164    */
 
165  2 toggle public void testViewFilesReturnsForbiddenWhenOutsideOfRoot() throws Exception {
166  2 request.setParameter("path", "../");
167  2 request.setParameter("type", FileMetaData.BLOG_FILE);
168   
169  2 View view = action.process(request, response);
170   
171    // check a forbidden response is returned
172  2 assertTrue(view instanceof ForbiddenView);
173    }
174   
175    /**
176    * Tests that a specific file can be selected.
177    */
 
178  2 toggle public void testSelectFile() throws Exception {
179  2 request.setParameter("path", "/");
180  2 request.setParameter("type", FileMetaData.BLOG_IMAGE);
181  2 request.setParameter("file", "afile.txt");
182  2 action.process(request, response);
183  2 FileMetaData fileMetaData = (FileMetaData)action.getModel().get("file");
184  2 assertEquals("afile.txt", fileMetaData.getName());
185  2 assertFalse(fileMetaData.isDirectory());
186    }
187   
188    }