KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > RollerTestBase


1
2 package org.roller;
3
4 import java.sql.Timestamp JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.Calendar JavaDoc;
7 import java.util.Date JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.LinkedList JavaDoc;
11 import java.util.List JavaDoc;
12 import java.util.Map JavaDoc;
13
14 import junit.framework.TestCase;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18 import org.roller.business.BookmarkManagerTest;
19 import org.roller.model.Roller;
20 import org.roller.model.RollerFactory;
21 import org.roller.model.UserManager;
22 import org.roller.model.WeblogManager;
23 import org.roller.pojos.CommentData;
24 import org.roller.pojos.UserData;
25 import org.roller.pojos.WeblogCategoryData;
26 import org.roller.pojos.WeblogEntryData;
27 import org.roller.pojos.WebsiteData;
28
29 /**
30  * Base class for Roller backend unit test.
31  * Responsible for creating the appropriate Roller implementation.
32  * Provides setup methods to create website, user, blog entries, bookmarks, etc.
33  */

34 public abstract class RollerTestBase extends TestCase
35 {
36     public static Log mLogger =
37         LogFactory.getFactory().getInstance(BookmarkManagerTest.class);
38
39     private Roller mRoller = null;
40
41     /** Simple website created by addUser(), no frills. */
42     protected WebsiteData mWebsite = null;
43     protected String JavaDoc testUsername = "testuser";
44
45     /** Collection of websites created, each with category tree. */
46     protected List JavaDoc mWebsites = new LinkedList JavaDoc();
47
48     /** Number of website/users to create */
49     protected int mBlogCount = 1;
50
51     /** Number of categories to create in each category of tree. */
52     protected int mCatCount = 2;
53     /** Depth of created category tree. */
54     protected int mCatDepth = 2;
55     /** Number of weblog entries per category, half will be published status */
56     protected int mEntriesPerCatCount = 6; // 3 of 6 published
57
/** Number of comments to creaate per weblog entry */
58     protected int mCommentCount = 2;
59     
60     protected int mExpectedEntryCount = mEntriesPerCatCount +
61                                         mEntriesPerCatCount*mCatCount +
62                                         mEntriesPerCatCount*(mCatCount*mCatDepth);
63     protected int mExpectedPublishedEntryCount = (int)(mEntriesPerCatCount*0.5) +
64                                         (int)(mEntriesPerCatCount*0.5)*mCatCount +
65                                         (int)(mEntriesPerCatCount*0.5)*(mCatCount*mCatDepth);
66
67     /** Store users to make teardown easy. */
68     protected List JavaDoc mUsersCreated = new ArrayList JavaDoc();
69     /** Store categories for use in asserts. */
70     protected List JavaDoc mCategoriesCreated = new ArrayList JavaDoc();
71     /** Store entries for use in asserts. */
72     protected List JavaDoc mEntriesCreated = new ArrayList JavaDoc();
73     /** Store comments for use in asserts. */
74     protected List JavaDoc mCommentsCreated = new ArrayList JavaDoc();
75
76     /** Used to walk back through time as entries are created, one per day. */
77     protected Calendar JavaDoc mCalendar = null;
78     
79     //------------------------------------------------------------------------
80
public RollerTestBase()
81     {
82         super();
83     }
84
85     //------------------------------------------------------------------------
86
public RollerTestBase(String JavaDoc name)
87     {
88         super(name);
89     }
90
91     //------------------------------------------------------------------------
92
/**
93      * @see TestCase#setUp()
94      */

95     protected void setUp() throws Exception JavaDoc
96     {
97         super.setUp();
98         UserManager umgr = getRoller().getUserManager();
99
100         getRoller().begin(UserData.SYSTEM_USER);
101
102         // create User
103
UserData user = createUser(umgr,
104                    testUsername,
105                    "password",
106                    "TestUser",
107                    "testuser@example.com");
108
109         // get website
110
mWebsite = umgr.getWebsite(user.getUserName());
111         getRoller().commit();
112     }
113
114     //-----------------------------------------------------------------------
115
protected UserData createUser(
116                     UserManager umgr,
117                     String JavaDoc username,
118                     String JavaDoc password,
119                     String JavaDoc fullName,
120                     String JavaDoc email) throws RollerException
121     {
122         UserData ud = new UserData(null,
123             username, // userName
124
password, // password
125
fullName, // fullName
126
email, // emailAddress
127
new java.util.Date JavaDoc() // dateCreated
128
);
129         Map JavaDoc pages = new HashMap JavaDoc();
130         pages.put("Weblog","Weblog page content");
131         pages.put("_day","Day page content");
132         pages.put("css","CSS page content");
133         umgr.addUser(ud, pages, "basic", "en_US_WIN", "America/Los_Angeles");
134         return ud;
135     }
136
137     //-----------------------------------------------------------------------
138
/** If you use this, call tearDownTestWeblogs() */
139     public void setUpTestWeblogs() throws Exception JavaDoc
140     {
141         UserManager umgr = getRoller().getUserManager();
142         WeblogManager wmgr = getRoller().getWeblogManager();
143
144         // Loop to create weblogs
145
for (int i=0; i<mBlogCount; i++)
146         {
147             getRoller().begin(UserData.SYSTEM_USER);
148
149             UserData ud = createUser(umgr,
150                 "testuser"+i, // userName
151
"password", // password
152
"Test User #"+i, // fullName
153
"test"+i+"@test.com" // emailAddress
154
);
155             WebsiteData website = umgr.getWebsite(ud.getUserName());
156             mWebsites.add(website);
157             mUsersCreated.add(ud);
158
159             getRoller().commit();
160
161             mLogger.debug("Created user "+ud.getUserName());
162
163             // ensure that the first weblog entry created is the newest
164
mCalendar = Calendar.getInstance();
165             mCalendar.setTime(new Date JavaDoc());
166
167             // create categories
168
getRoller().begin(UserData.SYSTEM_USER);
169             website = umgr.retrieveWebsite(website.getId());
170             WeblogCategoryData rootCat = wmgr.getRootWeblogCategory(website);
171             createCategoryPostsAndComments(0, wmgr, website, rootCat);
172             getRoller().commit();
173         }
174
175         // commit all the objects
176
}
177
178     private void createCategoryPostsAndComments(
179             int depth,
180             WeblogManager wmgr,
181             WebsiteData website,
182             WeblogCategoryData rootCat) throws RollerException
183     {
184         Calendar JavaDoc commentCalendar = Calendar.getInstance();
185
186         Timestamp JavaDoc day;
187         WeblogEntryData wd;
188         if (depth == 0)
189         {
190             // roll calendar forward one day
191
mCalendar.roll(Calendar.DATE, true);
192             day = new Timestamp JavaDoc(mCalendar.getTime().getTime());
193             day.setNanos(0); // kludge
194
wd = new WeblogEntryData(
195                  null, // id
196
rootCat, // category
197
website, // websiteId
198
"Future Blog", // title
199
null,
200                  "Blog to the Future", // text
201
null, // anchor
202
day, // pubTime
203
day, // updateTime
204
Boolean.TRUE ); // publishEntry
205
wd.save();
206             
207             // roll calendar back to today
208
mCalendar.roll(Calendar.DATE, false);
209         }
210         
211         // create entries under the category passed in
212
for ( int k=0; k<mEntriesPerCatCount; k++ )
213         {
214             day = new Timestamp JavaDoc(mCalendar.getTime().getTime());
215             day.setNanos(0); // kludge
216

217             boolean published = k%2==0 ? true : false;
218
219             wd = new WeblogEntryData(
220                     null, // id
221
rootCat, // category
222
website, // websiteId
223
rootCat.getName() + ":entry"+k, // title
224
null,
225                     rootCat.getName() + ":entry"+k, // text
226
null, // anchor
227
day, // pubTime
228
day, // updateTime
229
new Boolean JavaDoc(published) ); // publishEntry
230
wd.save();
231
232             // add at beginning of list
233
mEntriesCreated.add(0, wd);
234
235             Timestamp JavaDoc now = wd.getPubTime();
236             for ( int l=0; l<mCommentCount; l++ )
237             {
238                 // need to seperate comments in time
239
// it took alot of trial & error to get this working!
240
commentCalendar.setTime(now);
241                 commentCalendar.add(Calendar.HOUR, l);
242                 now = new Timestamp JavaDoc(commentCalendar.getTime().getTime());
243                 CommentData comment = new CommentData(null,
244                         wd, // entry
245
"name"+l, // name
246
"test"+l+"@test.com", // email
247
"url"+l, // url
248
"This is my comment", // content
249
now, // postTime
250
Boolean.FALSE, // spam
251
Boolean.FALSE); // notify
252
comment.save();
253                 mCommentsCreated.add(comment);
254                 mLogger.debug(" Created comment ["
255                         +comment.getId()+"]"+ comment.getName());
256             }
257
258             mCalendar.add(Calendar.DATE, -1);
259         }
260
261         // create categories under the category passed in
262
for ( int j=0; j<mCatCount; j++ )
263         {
264             WeblogCategoryData cat = wmgr.createWeblogCategory(
265                 website, // website
266
rootCat, // parent
267
rootCat.getName()+"-cat"+j, // name
268
"desc", // description
269
null ); // image
270
cat.save();
271             mCategoriesCreated.add(cat);
272             mLogger.debug(" Created cat ["+cat.getId()+"]"+cat.getName());
273
274             if (depth < mCatDepth)
275             {
276                 createCategoryPostsAndComments(depth+1, wmgr, website, cat);
277             }
278         }
279     }
280
281     //-----------------------------------------------------------------------
282
/** Tear down weblogs created in setupTestWeblogs() */
283     public void tearDownTestWeblogs() throws Exception JavaDoc
284     {
285         getRoller().begin(UserData.SYSTEM_USER);
286         UserManager umgr = getRoller().getUserManager();
287         for (Iterator JavaDoc iter = mUsersCreated.iterator(); iter.hasNext();)
288         {
289             UserData element = (UserData) iter.next();
290             element = umgr.retrieveUser(element.getId());
291             element.remove();
292         }
293         getRoller().commit();
294     }
295
296     //------------------------------------------------------------------------
297

298     /**
299      * Child TestCases should take care to tearDown() their own resources
300      * (including their own implementation). RollerTestBase will clean up the
301      * getRoller() instance.
302      *
303      * @see TestCase#tearDown()
304      */

305     public void tearDown() throws Exception JavaDoc
306     {
307         try
308         {
309             deleteWebsite(testUsername);
310             //getRoller().release();
311
}
312         catch (RollerException e)
313         {
314             mLogger.error("Tearing down",e);
315         }
316         super.tearDown();
317     }
318
319     /**
320      * Delete the website created for this test.
321      *
322      * @throws RollerException
323      */

324     private void deleteWebsite(String JavaDoc deleteMe) throws RollerException
325     {
326         mLogger.debug("try to delete " + deleteMe);
327         getRoller().begin(UserData.SYSTEM_USER);
328         mWebsite = getRoller().getUserManager().getWebsite(deleteMe);
329         if (mWebsite != null)
330         {
331             mWebsite.getUser().remove();
332         }
333         getRoller().commit();
334     }
335
336     //------------------------------------------------------------------------
337
/**
338      * Get Roller implementation to be used in tests.
339      */

340     public Roller getRoller() throws RollerException
341     {
342         if ( mRoller == null )
343         {
344             mRoller = RollerFactory.getRoller();
345         }
346         return mRoller;
347     }
348 }
349
Popular Tags