001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.business;
020:
021: import java.io.InputStream;
022: import java.util.Map;
023: import junit.framework.Test;
024: import junit.framework.TestCase;
025: import junit.framework.TestSuite;
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.apache.roller.TestUtils;
029: import org.apache.roller.business.FileManager;
030: import org.apache.roller.business.PropertiesManager;
031: import org.apache.roller.business.RollerFactory;
032: import org.apache.roller.pojos.RollerPropertyData;
033: import org.apache.roller.pojos.UserData;
034: import org.apache.roller.pojos.WeblogResource;
035: import org.apache.roller.pojos.WebsiteData;
036:
037: /**
038: * Test File Management business layer operations.
039: */
040: public class FileManagerTest extends TestCase {
041:
042: private static Log log = LogFactory.getLog(FileManagerTest.class);
043:
044: UserData testUser = null;
045: WebsiteData testWeblog = null;
046:
047: public FileManagerTest(String name) {
048: super (name);
049: }
050:
051: public static Test suite() {
052: return new TestSuite(FileManagerTest.class);
053: }
054:
055: public void setUp() throws Exception {
056:
057: try {
058: testUser = TestUtils.setupUser("FileManagerTest_userName");
059: testWeblog = TestUtils.setupWeblog(
060: "FileManagerTest_handle", testUser);
061: TestUtils.endSession(true);
062: } catch (Exception ex) {
063: log.error(ex);
064: }
065: }
066:
067: public void tearDown() throws Exception {
068:
069: try {
070: TestUtils.teardownWeblog(testWeblog.getId());
071: TestUtils.teardownUser(testUser.getId());
072: TestUtils.endSession(true);
073: } catch (Exception ex) {
074: log.error(ex);
075: }
076: }
077:
078: /**
079: * Test simple file save/delete.
080: */
081: public void testFileCRUD() throws Exception {
082:
083: // update roller properties to prepare for test
084: PropertiesManager pmgr = RollerFactory.getRoller()
085: .getPropertiesManager();
086: Map config = pmgr.getProperties();
087: ((RollerPropertyData) config.get("uploads.enabled"))
088: .setValue("true");
089: ((RollerPropertyData) config.get("uploads.types.allowed"))
090: .setValue("opml");
091: ((RollerPropertyData) config.get("uploads.dir.maxsize"))
092: .setValue("1.00");
093: pmgr.saveProperties(config);
094: TestUtils.endSession(true);
095:
096: /* NOTE: upload dir for unit tests is set in
097: roller/testdata/roller-custom.properties */
098: FileManager fmgr = RollerFactory.getRoller().getFileManager();
099:
100: // we should be starting with 0 files
101: assertEquals(0, fmgr.getFiles(testWeblog, null).length);
102:
103: // store a file
104: InputStream is = getClass().getResourceAsStream(
105: "/bookmarks.opml");
106: fmgr.saveFile(testWeblog, "bookmarks.opml", "text/plain", 1545,
107: is);
108:
109: // make sure file was stored successfully
110: assertEquals("bookmarks.opml", fmgr.getFile(testWeblog,
111: "bookmarks.opml").getName());
112: assertEquals(1, fmgr.getFiles(testWeblog, null).length);
113:
114: // delete file
115: fmgr.deleteFile(testWeblog, "bookmarks.opml");
116:
117: // make sure delete was successful
118: assertEquals(0, fmgr.getFiles(testWeblog, null).length);
119: }
120:
121: /**
122: * Test simple directory create/delete.
123: */
124: public void testDirectoryCRUD() throws Exception {
125:
126: // update roller properties to prepare for test
127: PropertiesManager pmgr = RollerFactory.getRoller()
128: .getPropertiesManager();
129: Map config = pmgr.getProperties();
130: ((RollerPropertyData) config.get("uploads.enabled"))
131: .setValue("true");
132: ((RollerPropertyData) config.get("uploads.types.allowed"))
133: .setValue("opml");
134: ((RollerPropertyData) config.get("uploads.dir.maxsize"))
135: .setValue("1.00");
136: pmgr.saveProperties(config);
137: TestUtils.endSession(true);
138:
139: FileManager fmgr = RollerFactory.getRoller().getFileManager();
140:
141: // we should be starting with 0 directories
142: assertEquals(0, fmgr.getDirectories(testWeblog).length);
143:
144: // create a directory
145: fmgr.createDirectory(testWeblog, "bucket0");
146:
147: // make sure directory was created
148: WeblogResource[] dirs = fmgr.getDirectories(testWeblog);
149: assertNotNull(dirs);
150: assertEquals(1, dirs.length);
151: assertEquals("bucket0", dirs[0].getName());
152:
153: // cleanup
154: fmgr.deleteFile(testWeblog, "bucket0");
155:
156: // make sure delete was successful
157: assertEquals(0, fmgr.getDirectories(testWeblog).length);
158: }
159:
160: /**
161: * Test save/delete of files in a directory.
162: */
163: public void testFilesInDirectoriesCRUD() throws Exception {
164:
165: // update roller properties to prepare for test
166: PropertiesManager pmgr = RollerFactory.getRoller()
167: .getPropertiesManager();
168: Map config = pmgr.getProperties();
169: ((RollerPropertyData) config.get("uploads.enabled"))
170: .setValue("true");
171: ((RollerPropertyData) config.get("uploads.types.allowed"))
172: .setValue("opml");
173: ((RollerPropertyData) config.get("uploads.dir.maxsize"))
174: .setValue("1.00");
175: pmgr.saveProperties(config);
176: TestUtils.endSession(true);
177:
178: FileManager fmgr = RollerFactory.getRoller().getFileManager();
179:
180: // we should be starting with 0 files and 0 directories
181: assertEquals(0, fmgr.getFiles(testWeblog, null).length);
182: assertEquals(0, fmgr.getDirectories(testWeblog).length);
183:
184: // create a directory
185: fmgr.createDirectory(testWeblog, "bucket1");
186:
187: // make sure directory was created
188: WeblogResource[] dirs = fmgr.getDirectories(testWeblog);
189: assertNotNull(dirs);
190: assertEquals(1, dirs.length);
191: assertEquals("bucket1", dirs[0].getName());
192:
193: // store a file into a subdirectory
194: InputStream is = getClass().getResourceAsStream(
195: "/bookmarks.opml");
196: fmgr.saveFile(testWeblog, "bucket1/bookmarks.opml",
197: "text/plain", 1545, is);
198:
199: // make sure file was stored successfully
200: assertEquals("bucket1/bookmarks.opml", fmgr.getFile(testWeblog,
201: "bucket1/bookmarks.opml").getPath());
202: assertEquals(1, fmgr.getFiles(testWeblog, "bucket1").length);
203:
204: // cleanup
205: fmgr.deleteFile(testWeblog, "bucket1/bookmarks.opml");
206: fmgr.deleteFile(testWeblog, "bucket1");
207:
208: // we should be back to 0 files and 0 directories
209: assertEquals(0, fmgr.getFiles(testWeblog, null).length);
210: assertEquals(0, fmgr.getDirectories(testWeblog).length);
211: }
212:
213: /**
214: * Test FileManager.saveFile() checks.
215: *
216: * This should test all conditions where a save should fail.
217: */
218: public void testCanSave() throws Exception {
219:
220: FileManager fmgr = RollerFactory.getRoller().getFileManager();
221: PropertiesManager pmgr = RollerFactory.getRoller()
222: .getPropertiesManager();
223: Map config = config = pmgr.getProperties();
224: ((RollerPropertyData) config.get("uploads.dir.maxsize"))
225: .setValue("1.00");
226: ((RollerPropertyData) config.get("uploads.types.forbid"))
227: .setValue("");
228: ((RollerPropertyData) config.get("uploads.types.allowed"))
229: .setValue("");
230: ((RollerPropertyData) config.get("uploads.enabled"))
231: .setValue("true");
232: pmgr.saveProperties(config);
233: TestUtils.endSession(true);
234:
235: Exception exception = null;
236: InputStream is = null;
237:
238: try {
239: // path check should fail
240: fmgr.saveFile(testWeblog, "some/path/foo.gif",
241: "text/plain", 10, is);
242: } catch (Exception ex) {
243: log.error(ex);
244: exception = ex;
245: }
246: assertNotNull(exception);
247: exception = null;
248:
249: config = pmgr.getProperties();
250: ((RollerPropertyData) config.get("uploads.dir.maxsize"))
251: .setValue("1.00");
252: pmgr.saveProperties(config);
253: TestUtils.endSession(true);
254:
255: try {
256: // quota check should fail
257: fmgr.saveFile(testWeblog, "test.gif", "text/plain",
258: 2500000, is);
259: } catch (Exception ex) {
260: log.error(ex);
261: exception = ex;
262: }
263: assertNotNull(exception);
264: exception = null;
265:
266: config = pmgr.getProperties();
267: ((RollerPropertyData) config.get("uploads.types.forbid"))
268: .setValue("gif");
269: pmgr.saveProperties(config);
270: TestUtils.endSession(true);
271:
272: try {
273: // forbidden types check should fail
274: fmgr.saveFile(testWeblog, "test.gif", "text/plain", 10, is);
275: } catch (Exception ex) {
276: log.error(ex);
277: exception = ex;
278: }
279: assertNotNull(exception);
280: exception = null;
281:
282: config = pmgr.getProperties();
283: ((RollerPropertyData) config.get("uploads.enabled"))
284: .setValue("false");
285: pmgr.saveProperties(config);
286: TestUtils.endSession(true);
287:
288: try {
289: // uploads disabled should fail
290: fmgr.saveFile(testWeblog, "test.gif", "text/plain", 10, is);
291: } catch (Exception ex) {
292: log.error(ex);
293: exception = ex;
294: }
295: assertNotNull(exception);
296: exception = null;
297: }
298:
299: }
|