KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.roller.business;
2
3 import java.io.InputStream JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Map JavaDoc;
6
7 import junit.framework.Test;
8 import junit.framework.TestCase;
9 import junit.framework.TestSuite;
10
11 import org.roller.model.FileManager;
12 import org.roller.model.PropertiesManager;
13 import org.roller.model.Roller;
14 import org.roller.model.RollerFactory;
15 import org.roller.model.UserManager;
16 import org.roller.pojos.RollerPropertyData;
17 import org.roller.pojos.UserData;
18 import org.roller.pojos.WebsiteData;
19 import org.roller.util.RollerMessages;
20
21 /**
22  * @author David M Johnson
23  */

24 public class FileManagerTest extends TestCase
25 {
26     //private Roller mRoller = null;
27
//private WebsiteData mWebsite = null;
28

29     public void setUp() throws Exception JavaDoc
30     {
31         
32     }
33     
34     public void tearDown() throws Exception JavaDoc
35     {
36         // No need to clean up, we are using mocks
37
}
38     
39     public void testCanSave()
40     {
41         try
42         {
43             // do some setup for our test
44
Roller mRoller = RollerFactory.getRoller();
45             
46             UserManager umgr = mRoller.getUserManager();
47             WebsiteData mWebsite = umgr.getWebsite("FileManagerTest_userName");
48             
49             if(mWebsite == null)
50                 mWebsite = this.createTestUser();
51             
52             // update roller properties to prepare for test
53
PropertiesManager pmgr = mRoller.getPropertiesManager();
54             Map JavaDoc config = pmgr.getProperties();
55             ((RollerPropertyData)config.get("uploads.enabled")).setValue("false");
56             ((RollerPropertyData)config.get("uploads.types.forbid")).setValue("gif");
57             ((RollerPropertyData)config.get("uploads.dir.maxsize")).setValue("1.00");
58             pmgr.store(config);
59             mRoller.commit();
60             
61             FileManager fmgr = new FileManagerImpl();
62             RollerMessages msgs = new RollerMessages();
63             assertFalse(fmgr.canSave(mWebsite, "test.gif", 2500000, msgs));
64             //assertEquals(4, msgs.getErrorCount());
65
}
66         catch (Exception JavaDoc e)
67         {
68             e.printStackTrace();
69         }
70     }
71     
72     public void testSave()
73     {
74         try
75         {
76             // do some setup for our test
77
Roller mRoller = RollerFactory.getRoller();
78             
79             UserManager umgr = mRoller.getUserManager();
80             WebsiteData mWebsite = umgr.getWebsite("FileManagerTest_userName");
81             
82             if(mWebsite == null)
83                 mWebsite = this.createTestUser();
84             
85             // update roller properties to prepare for test
86
PropertiesManager pmgr = mRoller.getPropertiesManager();
87             Map JavaDoc config = pmgr.getProperties();
88             ((RollerPropertyData)config.get("uploads.enabled")).setValue("true");
89             ((RollerPropertyData)config.get("uploads.types.allowed")).setValue("opml");
90             ((RollerPropertyData)config.get("uploads.dir.maxsize")).setValue("1.00");
91             pmgr.store(config);
92             mRoller.commit();
93             
94             /* NOTE: upload dir for unit tests is set in
95                roller/personal/testing/roller-custom.properties */

96             FileManager fmgr = new FileManagerImpl();
97             RollerMessages msgs = new RollerMessages();
98             InputStream JavaDoc is = getClass().getResourceAsStream("/bookmarks.opml");
99             fmgr.saveFile(mWebsite, "bookmarks.opml", 1545, is);
100             
101             assertEquals(1, fmgr.getFiles(mWebsite).length);
102             
103             fmgr.deleteFile(mWebsite, "bookmarks.opml");
104             Thread.sleep(2000);
105             assertEquals(0, fmgr.getFiles(mWebsite).length);
106         }
107         catch (Exception JavaDoc e)
108         {
109             e.printStackTrace();
110         }
111     }
112     
113     private WebsiteData createTestUser() throws Exception JavaDoc {
114         // do some setup for our test
115
Roller mRoller = RollerFactory.getRoller();
116         
117         UserManager umgr = mRoller.getUserManager();
118         mRoller.begin(UserData.SYSTEM_USER);
119         UserData user = new UserData(null,
120                 "FileManagerTest_userName",
121                 "FileManagerTest_password",
122                 "FileManagerTest_description",
123                 "FileManagerTest@example.com",
124                 new java.util.Date JavaDoc());
125         Map JavaDoc pages = new HashMap JavaDoc();
126         pages.put("Weblog","Weblog page content");
127         pages.put("_day","Day page content");
128         pages.put("css","CSS page content");
129         umgr.addUser(user, pages, "basic", "en_US_WIN", "America/Los_Angeles");
130         mRoller.commit();
131         WebsiteData mWebsite = umgr.getWebsite(user.getUserName());
132         
133         return mWebsite;
134     }
135
136     public static Test suite()
137     {
138         return new TestSuite(FileManagerTest.class);
139     }
140 }
141
Popular Tags