KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > business > FileManagerTest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * 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. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.business;
20
21 import java.io.InputStream JavaDoc;
22 import java.util.Map JavaDoc;
23 import junit.framework.Test;
24 import junit.framework.TestCase;
25 import junit.framework.TestSuite;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.roller.TestUtils;
29 import org.apache.roller.model.FileManager;
30 import org.apache.roller.model.PropertiesManager;
31 import org.apache.roller.model.Roller;
32 import org.apache.roller.model.RollerFactory;
33 import org.apache.roller.model.UserManager;
34 import org.apache.roller.pojos.RollerPropertyData;
35 import org.apache.roller.pojos.UserData;
36 import org.apache.roller.pojos.WebsiteData;
37 import org.apache.roller.util.RollerMessages;
38
39
40 /**
41  * Test File Management business layer operations.
42  */

43 public class FileManagerTest extends TestCase {
44     
45     public static Log log = LogFactory.getLog(FileManagerTest.class);
46     
47     UserData testUser = null;
48     WebsiteData testWeblog = null;
49     
50     
51     public FileManagerTest(String JavaDoc name) {
52         super(name);
53     }
54     
55     
56     public static Test suite() {
57         return new TestSuite(FileManagerTest.class);
58     }
59     
60     
61     public void setUp() throws Exception JavaDoc {
62         
63         try {
64             testUser = TestUtils.setupUser("FileManagerTest_userName");
65             testWeblog = TestUtils.setupWeblog("FileManagerTest_handle", testUser);
66             TestUtils.endSession(true);
67         } catch (Exception JavaDoc ex) {
68             log.error(ex);
69         }
70     }
71     
72     public void tearDown() throws Exception JavaDoc {
73         
74         try {
75             TestUtils.teardownWeblog(testWeblog.getId());
76             TestUtils.teardownUser(testUser.getId());
77             TestUtils.endSession(true);
78         } catch (Exception JavaDoc ex) {
79             log.error(ex);
80         }
81     }
82     
83     
84     public void testCanSave() throws Exception JavaDoc {
85         
86         // update roller properties to prepare for test
87
PropertiesManager pmgr = RollerFactory.getRoller().getPropertiesManager();
88         Map JavaDoc config = pmgr.getProperties();
89         ((RollerPropertyData)config.get("uploads.enabled")).setValue("false");
90         ((RollerPropertyData)config.get("uploads.types.forbid")).setValue("gif");
91         ((RollerPropertyData)config.get("uploads.dir.maxsize")).setValue("1.00");
92         pmgr.saveProperties(config);
93         TestUtils.endSession(true);
94         
95         // test quota functionality
96
FileManager fmgr = RollerFactory.getRoller().getFileManager();
97         RollerMessages msgs = new RollerMessages();
98         assertFalse(fmgr.canSave(testWeblog.getHandle(), "test.gif", "text/plain", 2500000, msgs));
99     }
100     
101     
102     public void testSave() throws Exception JavaDoc {
103         
104         // update roller properties to prepare for test
105
PropertiesManager pmgr = RollerFactory.getRoller().getPropertiesManager();
106         Map JavaDoc config = pmgr.getProperties();
107         ((RollerPropertyData)config.get("uploads.enabled")).setValue("true");
108         ((RollerPropertyData)config.get("uploads.types.allowed")).setValue("opml");
109         ((RollerPropertyData)config.get("uploads.dir.maxsize")).setValue("1.00");
110         pmgr.saveProperties(config);
111         TestUtils.endSession(true);
112         
113         /* NOTE: upload dir for unit tests is set in
114                roller/personal/testing/roller-custom.properties */

115         FileManager fmgr = RollerFactory.getRoller().getFileManager();
116         RollerMessages msgs = new RollerMessages();
117         
118         // store a file
119
InputStream JavaDoc is = getClass().getResourceAsStream("/bookmarks.opml");
120         fmgr.saveFile(testWeblog.getHandle(), "bookmarks.opml", "text/plain", 1545, is);
121         
122         // make sure file was stored successfully
123
assertEquals(1, fmgr.getFiles(testWeblog.getHandle()).length);
124         
125         // delete a file
126
fmgr.deleteFile(testWeblog.getHandle(), "bookmarks.opml");
127         
128         // make sure delete was successful
129
Thread.sleep(2000);
130         assertEquals(0, fmgr.getFiles(testWeblog.getHandle()).length);
131     }
132     
133 }
134
Popular Tags