KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.roller.business;
2
3 import java.sql.Timestamp JavaDoc;
4 import java.util.Date JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Map JavaDoc;
8
9 import junit.framework.Test;
10 import junit.framework.TestSuite;
11
12 import org.roller.RollerException;
13 import org.roller.RollerPermissionsException;
14 import org.roller.model.UserManager;
15 import org.roller.model.WeblogManager;
16 import org.roller.pojos.CommentData;
17 import org.roller.pojos.UserData;
18 import org.roller.pojos.WeblogCategoryData;
19 import org.roller.pojos.WeblogEntryData;
20 import org.roller.pojos.WebsiteData;
21 import org.roller.util.Utilities;
22 import org.roller.RollerTestBase;
23
24 /**
25  * Test Roller Weblog Management.
26  */

27 public class WeblogManagerTest extends RollerTestBase
28 {
29     String JavaDoc dest_id = null;
30     String JavaDoc c1_id = null;
31     String JavaDoc c2_id = null;
32     String JavaDoc c3_id = null;
33     
34     //------------------------------------------------------------------------
35
public WeblogManagerTest(String JavaDoc name)
36     {
37         super(name);
38     }
39     
40     //------------------------------------------------------------------------
41
public static void main(String JavaDoc args[])
42     {
43         junit.textui.TestRunner.run(WeblogManagerTest.class);
44     }
45     
46     //------------------------------------------------------------------------
47
public static Test suite()
48     {
49         return new TestSuite(WeblogManagerTest.class);
50     }
51
52     //-----------------------------------------------------------------------
53
/**
54      * @see junit.framework.TestCase#setUp()
55      */

56     public void setUp() throws Exception JavaDoc
57     {
58         super.setUp();
59         setUpTestWeblogs();
60         setUpCategoryTree();
61     }
62     
63     /**
64      * Add a small category tree to the small test website (mWebsite).
65      * root/
66      * c0/
67      * c1/
68      * c2/
69      * c3/
70      */

71     public void setUpCategoryTree() throws Exception JavaDoc
72     {
73         getRoller().begin(UserData.SYSTEM_USER);
74         
75         WebsiteData wd = null;
76         WeblogCategoryData root = null;
77         WeblogManager wmgr = getRoller().getWeblogManager();
78
79         wd = getRoller().getUserManager().retrieveWebsite(mWebsite.getId());
80         root = wmgr.getRootWeblogCategory(wd);
81
82         // create empty destination folder
83
WeblogCategoryData dest = wmgr.createWeblogCategory();
84         dest.setName("c0");
85         dest.setParent(root);
86         dest.setWebsite(wd);
87         dest.save();
88         
89         // create three level src category with entry in each category
90
WeblogCategoryData c1 = wmgr.createWeblogCategory();
91         c1.setName("c1");
92         c1.setParent(root);
93         c1.setWebsite(wd);
94         c1.save();
95         
96         WeblogEntryData e1 = new WeblogEntryData(
97             null, c1, wd, "title1", null, "text", "anchor",
98             new Timestamp JavaDoc(0), new Timestamp JavaDoc(0), Boolean.FALSE);
99         e1.save();
100         
101         WeblogCategoryData c2 = wmgr.createWeblogCategory();
102         c2.setName("c2");
103         c2.setParent(c1);
104         c2.setWebsite(wd);
105         c2.save();
106       
107         WeblogEntryData e2 = new WeblogEntryData(
108             null, c2, wd, "title2", null, "text", "anchor",
109             new Timestamp JavaDoc(0), new Timestamp JavaDoc(0), Boolean.FALSE);
110         e2.save();
111         
112         WeblogCategoryData c3 = wmgr.createWeblogCategory();
113         c3.setName("c3");
114         c3.setParent(c2);
115         c3.setWebsite(wd);
116         c3.save();
117         
118         WeblogEntryData e3 = new WeblogEntryData(
119             null, c3, wd, "title3", null, "text", "anchor",
120             new Timestamp JavaDoc(0), new Timestamp JavaDoc(0), Boolean.FALSE);
121         e3.save();
122         
123         getRoller().commit();
124         
125         dest_id = dest.getId();
126         c1_id = c1.getId();
127         c2_id = c2.getId();
128         c3_id = c3.getId();
129     }
130     
131     //-----------------------------------------------------------------------
132
/**
133      * @see junit.framework.TestCase#tearDown()
134      */

135     public void tearDown() throws Exception JavaDoc
136     {
137         super.tearDown();
138         tearDownTestWeblogs();
139     }
140     
141     //-----------------------------------------------------------------------
142

143     /** Add a small category tree to the small test website (mWebsite). */
144     public void testGetRecent() throws Exception JavaDoc
145     {
146         getRoller().begin(UserData.SYSTEM_USER);
147         
148         WebsiteData wd = null;
149         WeblogCategoryData root = null;
150         WeblogManager wmgr = getRoller().getWeblogManager();
151
152         wd = getRoller().getUserManager().retrieveWebsite(mWebsite.getId());
153         root = wmgr.getRootWeblogCategory(wd);
154
155         // create top level folders
156
WeblogCategoryData t1 = wmgr.createWeblogCategory();
157         t1.setName("toplevel1");
158         t1.setParent(root);
159         t1.setWebsite(wd);
160         t1.save();
161         
162         WeblogCategoryData t2 = wmgr.createWeblogCategory();
163         t2.setName("toplevel2");
164         t2.setParent(root);
165         t2.setWebsite(wd);
166         t2.save();
167         
168         WeblogCategoryData t3 = wmgr.createWeblogCategory();
169         t3.setName("toplevel3");
170         t3.setParent(root);
171         t3.setWebsite(wd);
172         t3.save();
173         
174         getRoller().commit();
175         
176         getRoller().begin(UserData.SYSTEM_USER);
177         
178         // get persistent instances
179
t1 = wmgr.retrieveWeblogCategory(t1.getId());
180         wd = getRoller().getUserManager().retrieveWebsite(mWebsite.getId());
181         
182         // Create four entries in 1st category
183
WeblogEntryData e1 = new WeblogEntryData(
184                 null, t1, wd, "title1", null, "text1", "anchor",
185                 new Timestamp JavaDoc(0), new Timestamp JavaDoc(0), Boolean.FALSE);
186         e1.save();
187         
188         WeblogEntryData e2 = new WeblogEntryData(
189                 null, t1, wd, "title2", null, "text2", "anchor",
190                 new Timestamp JavaDoc(0), new Timestamp JavaDoc(0), Boolean.FALSE);
191         e2.save();
192         
193         WeblogEntryData e3 = new WeblogEntryData(
194                 null, t1, wd, "title3", null, "text3", "anchor",
195                 new Timestamp JavaDoc(0), new Timestamp JavaDoc(0), Boolean.FALSE);
196         e3.save();
197         
198         WeblogEntryData e4 = new WeblogEntryData(
199                 null, t1, wd, "title4", null, "text4", "anchor",
200                 new Timestamp JavaDoc(0), new Timestamp JavaDoc(0), Boolean.FALSE);
201         e4.save();
202         
203         getRoller().commit();
204         
205         getRoller().begin(UserData.SYSTEM_USER);
206         
207         //List entries = wmgr.getRecentWeblogEntriesArray(
208
//wd.getUser().getUserName(), new Date(), "toplevel1", 15, false);
209

210         List JavaDoc entries = wmgr.getWeblogEntries(
211                         wd,
212                         null, // startDate
213
new Date JavaDoc(), // endDate
214
"toplevel1", // catName
215
WeblogManager.ALL, // status
216
new Integer JavaDoc(15)); // maxEntries
217

218         assertEquals(4, entries.size());
219         
220         getRoller().rollback();
221     }
222     
223     public void testGetWeblogCategoryRoot() throws Exception JavaDoc
224     {
225         getRoller().begin(UserData.SYSTEM_USER);
226         WeblogManager wmgr = getRoller().getWeblogManager();
227         assertNotNull(wmgr.getRootWeblogCategory(mWebsite));
228         getRoller().rollback();
229     }
230     
231     public void testGetWeblogCategories() throws Exception JavaDoc
232     {
233         getRoller().begin(UserData.SYSTEM_USER);
234         WeblogManager wmgr = getRoller().getWeblogManager();
235         assertEquals(8, wmgr.getWeblogCategories(mWebsite).size());
236         getRoller().rollback();
237     }
238     
239     public void testWeblogCategoryPaths() throws Exception JavaDoc
240     {
241         WebsiteData wd = null;
242         WeblogCategoryData root = null;
243         WeblogManager wmgr = getRoller().getWeblogManager();
244         
245         getRoller().begin(UserData.SYSTEM_USER);
246         
247         wd = getRoller().getUserManager().retrieveWebsite(mWebsite.getId());
248         root = wmgr.getRootWeblogCategory(wd);
249
250         WeblogCategoryData f1 = wmgr.createWeblogCategory();
251         f1.setName("f1");
252         f1.setParent(root);
253         f1.setWebsite(wd);
254         f1.save();
255         
256         WeblogCategoryData f2 = wmgr.createWeblogCategory();
257         f2.setName("f2");
258         f2.setParent(f1);
259         f2.setWebsite(wd);
260         f2.save();
261       
262         WeblogCategoryData f3 = wmgr.createWeblogCategory();
263         f3.setName("f3");
264         f3.setParent(f2);
265         f3.setWebsite(wd);
266         f3.save();
267         
268         getRoller().commit();
269         
270         getRoller().begin(UserData.SYSTEM_USER);
271         
272         // check count of descendents and ancestors
273
f1 = wmgr.retrieveWeblogCategory(f1.getId());
274         assertEquals(2, f1.getAllDescendentAssocs().size());
275         assertEquals(1, f1.getAncestorAssocs().size());
276         
277         f2 = wmgr.retrieveWeblogCategory(f2.getId());
278         assertEquals(1, f2.getAllDescendentAssocs().size());
279         assertEquals(2, f2.getAncestorAssocs().size());
280         
281         f3 = wmgr.retrieveWeblogCategory(f3.getId());
282         assertEquals(0, f3.getAllDescendentAssocs().size());
283         assertEquals(3, f3.getAncestorAssocs().size());
284         
285         // test get by path
286
assertEquals("f1",
287             wmgr.getWeblogCategoryByPath(wd, null, "f1").getName());
288         
289         assertEquals("f1",
290             wmgr.getWeblogCategoryByPath(wd, null, "/f1").getName());
291         
292         assertEquals("f2",
293             wmgr.getWeblogCategoryByPath(wd, null, "/f1/f2").getName());
294         
295         assertEquals("f3",
296             wmgr.getWeblogCategoryByPath(wd, null, "/f1/f2/f3").getName());
297         
298         // test path creation
299
f3 = wmgr.getWeblogCategoryByPath(wd, null, "/f1/f2/f3");
300         String JavaDoc pathString = wmgr.getPath(f3);
301         String JavaDoc[] pathArray = Utilities.stringToStringArray(pathString,"/");
302         assertEquals("f1", pathArray[0]);
303         assertEquals("f2", pathArray[1]);
304         assertEquals("f3", pathArray[2]);
305         
306         getRoller().commit();
307     }
308         
309     //-----------------------------------------------------------------------
310

311     public void testGetWeblogEntryByAnchor() throws Exception JavaDoc
312     {
313         WeblogManager wmgr = getRoller().getWeblogManager();
314         
315         WeblogEntryData entry1 = (WeblogEntryData)mEntriesCreated.get(0);
316         
317         WebsiteData website = entry1.getWebsite();
318         
319         WeblogEntryData entry2 = wmgr.getWeblogEntryByAnchor(
320             website, entry1.getAnchor() );
321         
322         assertEquals( entry1.getId(), entry2.getId() );
323     }
324      
325     //-----------------------------------------------------------------------
326
/**
327      * Test latest publishTime for a User.
328      * This test passes inconsistently - I suspect Hibernate.
329      */

330     public void testGetWeblogLastUpdateTimeForUser() throws Exception JavaDoc
331     {
332         WeblogManager wmgr = getRoller().getWeblogManager();
333
334         /**
335          * really weird, but we cannot get 'entry1' directly,
336          * we have to iterate over entries. We end up getting the same one
337          * either way, but fetching it directly fails!
338          */

339         int lastEntryIndex = (mEntriesCreated.size()-1);
340         //System.out.println("# entries:" + lastEntryIndex);
341
WeblogEntryData entry1 = null;
342         //WeblogEntryData entry1 = (WeblogEntryData)mEntriesCreated.get(lastEntryIndex);
343
if (entry1 == null || !entry1.getPublishEntry().booleanValue()) // if not published, find one
344
{
345             for (int i=lastEntryIndex; i >= 0; i--)
346             {
347                 entry1 = (WeblogEntryData)
348                     mEntriesCreated.get(i); // last entry is newest
349
//System.out.println("entry " + i + "published:" + entry1.getPublishEntry());
350
if (entry1.getPublishEntry().booleanValue()) {
351                     break;
352                 }
353             }
354         }
355         
356         WebsiteData website = entry1.getWebsite();
357         UserData user = website.getUser();
358         Date JavaDoc updateTime = wmgr.getWeblogLastPublishTime(user.getUserName());
359                 
360         assertEquals("THIS FAILS RANDOMLY, TRY AGAIN", entry1.getPubTime(),updateTime);
361     }
362
363     //-----------------------------------------------------------------------
364
/**
365      * Test latest publishTime when no User is specified.
366      * This test passes inconsistently - I suspect Hibernate.
367      */

368     public void testGetWeblogLastUpdateTimeForAll() throws Exception JavaDoc
369     {
370         WeblogManager wmgr = getRoller().getWeblogManager();
371
372         /**
373          * Have to iterate and test to find the latest *enabled* entry.
374          */

375         int lastEntryIndex = (mEntriesCreated.size()-1);
376         //System.out.println("# entries:" + lastEntryIndex);
377
WeblogEntryData entry1 = null;
378         WeblogEntryData temp = null;
379         for (int i=lastEntryIndex; i >= 0; i--)
380         {
381             temp = (WeblogEntryData)
382                 mEntriesCreated.get(i); // last entry is newest
383
//System.out.println("entry " + i + "published:" + entry1.getPublishEntry());
384
if (temp.getPublishEntry().booleanValue())
385             {
386                 if (entry1 == null || entry1.getPubTime().compareTo(temp.getPubTime()) < 0)
387                 {
388                     if (entry1 != null)
389                         System.out.println("replacing " + entry1.getTitle() + " with " + temp.getTitle());
390                     else
391                         System.out.println("setting entry1 to " + temp.getTitle());
392                     entry1 = temp;
393                 }
394             }
395         }
396         
397         Date JavaDoc updateTime = wmgr.getWeblogLastPublishTime(null);
398
399         assertEquals("THIS FAILS RANDOMLY, TRY AGAIN", entry1.getPubTime(),updateTime);
400     }
401
402     /** Count weblog entries in a weblogEntry map */
403     private int countEntries( Map JavaDoc entryMap )
404     {
405         int count = 0;
406         Iterator JavaDoc days = entryMap.values().iterator();
407         while (days.hasNext())
408         {
409             List JavaDoc dayEntries = (List JavaDoc) days.next();
410             Iterator JavaDoc entries = dayEntries.iterator();
411             while (entries.hasNext())
412             {
413                 entries.next();
414                 count++;
415             }
416         }
417         return count;
418     }
419           
420     public void testIsWeblogCategoryInUse() throws Exception JavaDoc
421     {
422         WeblogManager wmgr = getRoller().getWeblogManager();
423         UserManager umgr = getRoller().getUserManager();
424                     
425         getRoller().begin(UserData.SYSTEM_USER);
426         
427         WebsiteData website = umgr.retrieveWebsite(mWebsite.getId());
428         WeblogEntryData entry = (WeblogEntryData)mEntriesCreated.get(2);
429         WeblogCategoryData rootCat = wmgr.getRootWeblogCategory(website);
430         
431         WeblogCategoryData usedCat =
432             wmgr.retrieveWeblogCategory(entry.getCategory().getId());
433                          
434         WeblogCategoryData unusedCat = wmgr.createWeblogCategory(
435             website, rootCat, "testy", "tasty", "testy.gif");
436         unusedCat.save();
437             
438         getRoller().commit();
439         
440         getRoller().begin(UserData.SYSTEM_USER);
441         usedCat = wmgr.retrieveWeblogCategory(usedCat.getId());
442         assertTrue(usedCat.isInUse());
443         
444         unusedCat = wmgr.retrieveWeblogCategory(unusedCat.getId());
445         assertFalse(unusedCat.isInUse());
446         getRoller().commit();
447     }
448
449     public void testStoreComment() throws Exception JavaDoc
450     {
451         getRoller().begin(UserData.SYSTEM_USER);
452         WeblogManager wmgr = getRoller().getWeblogManager();
453         
454         // Get entry to which comment will be added
455
WeblogEntryData entry1 = (WeblogEntryData)mEntriesCreated.get(1);
456         
457         // Ensure that entry is a persistent instance
458
entry1 = getRoller().getWeblogManager().retrieveWeblogEntry(
459             entry1.getId());
460         
461         CommentData comment = new CommentData(
462             null,
463             entry1,
464             "TestCommentUser",
465             "test@test.com",
466             "",
467             "This is a test",
468             new Timestamp JavaDoc(new Date JavaDoc().getTime()),
469             Boolean.FALSE, // spam
470
Boolean.FALSE); // notify
471

472         comment.save();
473         getRoller().commit();
474         
475         List JavaDoc comments = wmgr.getComments(entry1.getId());
476         assertTrue(comments.size() > mCommentCount);
477         
478         getRoller().begin(UserData.SYSTEM_USER);
479         wmgr.removeComment(comment.getId());
480         getRoller().commit();
481     }
482     
483     public void testMoveCategoryContents() throws RollerException
484     {
485         WeblogManager wmgr = getRoller().getWeblogManager();
486         
487         WeblogCategoryData c1 = wmgr.retrieveWeblogCategory(c1_id);
488         //WeblogCategoryData c2 = wmgr.retrieveWeblogCategory(c2_id);
489
//WeblogCategoryData c3 = wmgr.retrieveWeblogCategory(c3_id);
490
WeblogCategoryData dest = wmgr.retrieveWeblogCategory(dest_id);
491         
492         getRoller().begin(UserData.SYSTEM_USER);
493                
494         // verify number of entries in each category
495
dest = wmgr.retrieveWeblogCategory(dest.getId());
496         c1 = wmgr.retrieveWeblogCategory(c1.getId());
497         assertEquals(0, dest.retrieveWeblogEntries(true).size());
498         assertEquals(0, dest.retrieveWeblogEntries(false).size());
499         assertEquals(1, c1.retrieveWeblogEntries(false).size());
500         assertEquals(3, c1.retrieveWeblogEntries(true).size());
501         
502         // move contents of source category c1 to destination catetory dest
503
c1.moveContents(dest);
504         
505         getRoller().commit();
506         
507         getRoller().begin(UserData.SYSTEM_USER);
508         
509         // after move, verify number of entries in each category
510
dest = wmgr.retrieveWeblogCategory(dest.getId());
511         c1 = wmgr.retrieveWeblogCategory(c1.getId());
512         assertEquals(3, dest.retrieveWeblogEntries(true).size());
513         assertEquals(3, dest.retrieveWeblogEntries(false).size());
514         assertEquals(0, c1.retrieveWeblogEntries(true).size());
515         assertEquals(0, c1.retrieveWeblogEntries(false).size());
516         
517         getRoller().commit();
518     }
519     
520     public void testMoveCategoryProtection() throws RollerException
521     {
522         boolean safe = false;
523         try
524         {
525             getRoller().begin(UserData.SYSTEM_USER);
526             WeblogManager wmgr = getRoller().getWeblogManager();
527                    
528             // Move category into one of it's children
529
WeblogCategoryData c1 = wmgr.retrieveWeblogCategory(c1_id);
530             WeblogCategoryData c3 = wmgr.retrieveWeblogCategory(c3_id);
531             wmgr.moveWeblogCategoryContents(c1.getId(), c3.getId());
532             c3.save();
533             c1.save();
534             getRoller().commit();
535         }
536         catch (RollerException e)
537         {
538             safe = true;
539         }
540         assertTrue(safe);
541     }
542     
543     public void testMoveCategory() throws RollerException
544     {
545         getRoller().begin(UserData.SYSTEM_USER);
546         WeblogManager wmgr = getRoller().getWeblogManager();
547         
548         WeblogCategoryData c1 = wmgr.retrieveWeblogCategory(c1_id);
549         WeblogCategoryData c2 = wmgr.retrieveWeblogCategory(c2_id);
550         WeblogCategoryData c3 = wmgr.retrieveWeblogCategory(c3_id);
551         WeblogCategoryData dest = wmgr.retrieveWeblogCategory(dest_id);
552                
553         // verify number of entries in each category
554
dest = wmgr.retrieveWeblogCategory(dest.getId());
555         c1 = wmgr.retrieveWeblogCategory(c1.getId());
556         assertEquals(0, dest.retrieveWeblogEntries(true).size());
557         assertEquals(0, dest.retrieveWeblogEntries(false).size());
558         assertEquals(1, c1.retrieveWeblogEntries(false).size());
559         assertEquals(3, c1.retrieveWeblogEntries(true).size());
560         
561         // move contents of source category c1 to destination catetory dest
562
c1.setParent(dest);
563         c1.save();
564         
565         getRoller().commit();
566         
567         getRoller().begin(UserData.SYSTEM_USER);
568         
569         // after move, verify number of entries in each category
570
dest = wmgr.retrieveWeblogCategory(dest.getId());
571         c1 = wmgr.retrieveWeblogCategory(c1_id);
572         c2 = wmgr.retrieveWeblogCategory(c2_id);
573         c3 = wmgr.retrieveWeblogCategory(c3_id);
574                
575         assertEquals(3, dest.retrieveWeblogEntries(true).size());
576         assertEquals(0, dest.retrieveWeblogEntries(false).size());
577                 
578         assertEquals(dest, c1.getParent());
579         assertEquals(c1, c2.getParent());
580         assertEquals(c2, c3.getParent());
581
582         assertEquals(1, c1.retrieveWeblogEntries(false).size());
583         assertEquals(1, c2.retrieveWeblogEntries(false).size());
584         assertEquals(1, c3.retrieveWeblogEntries(false).size());
585         
586         List JavaDoc entries = c1.retrieveWeblogEntries(true);
587         assertEquals(3, entries.size());
588         
589         getRoller().commit();
590     }
591     
592     public void testNextPrevPost() throws RollerException
593     {
594         getRoller().begin(UserData.SYSTEM_USER);
595         WeblogManager wmgr = getRoller().getWeblogManager();
596         
597         // category: root
598
WeblogEntryData entry0 = (WeblogEntryData)mEntriesCreated.get(0);
599         WeblogEntryData entry1 = (WeblogEntryData)mEntriesCreated.get(1);
600         WeblogEntryData entry2 = (WeblogEntryData)mEntriesCreated.get(2);
601         WeblogEntryData entry3 = (WeblogEntryData)mEntriesCreated.get(3);
602         WeblogEntryData entry4 = (WeblogEntryData)mEntriesCreated.get(4);
603         WeblogEntryData entry5 = (WeblogEntryData)mEntriesCreated.get(5);
604         WeblogEntryData entry6 = (WeblogEntryData)mEntriesCreated.get(6);
605         WeblogEntryData entry7 = (WeblogEntryData)mEntriesCreated.get(7);
606         
607         // next and prev only get published entries
608

609         // entry0 is the first published entry
610
assertEquals(null, wmgr.getPreviousEntry(entry0, null));
611         
612         // next published entry is entry2
613
assertEquals(entry1, wmgr.getNextEntry(entry0, null));
614         
615         // prev to entry2 is entry 0
616
assertEquals(entry0, wmgr.getPreviousEntry(entry2, null));
617         
618         // constrain prev/next by category
619

620         WeblogCategoryData cat = (WeblogCategoryData)mCategoriesCreated.get(1);
621         
622         assertEquals(null, wmgr.getPreviousEntry(entry5, "/root-cat1/root-cat1-cat0"));
623         assertEquals(entry7, wmgr.getNextEntry(entry5, "/root-cat1/root-cat1-cat0"));
624         assertEquals(entry5, wmgr.getPreviousEntry(entry7, "/root-cat1/root-cat1-cat0"));
625         
626         getRoller().rollback();
627     }
628     
629     public void testGetComments() throws RollerException
630     {
631         getRoller().begin(UserData.SYSTEM_USER);
632         WeblogEntryData entry0 = (WeblogEntryData)mEntriesCreated.get(0);
633         WeblogManager wmgr = getRoller().getWeblogManager();
634         assertEquals(mCommentCount, wmgr.getComments(entry0.getId()).size());
635         getRoller().commit();
636     }
637     
638     public void testQueryWeblogEntries() throws RollerException
639     {
640         getRoller().begin(UserData.SYSTEM_USER);
641         WeblogManager wmgr = getRoller().getWeblogManager();
642         UserManager umgr = getRoller().getUserManager();
643         
644         WebsiteData website = (WebsiteData)mWebsites.get(0);
645         website = umgr.retrieveWebsite(website.getId());
646
647         // PUBLISHISHED ONLY
648
List JavaDoc publishedEntries = wmgr.getWeblogEntries(
649                         website, // userName
650
null, // startDate
651
new Date JavaDoc(), // endDate
652
null, // catName
653
WeblogManager.PUB_ONLY, // status
654
null); // maxEntries
655
assertEquals(mExpectedPublishedEntryCount, publishedEntries.size());
656                     
657         // DRAFT ONLY
658
List JavaDoc draftEntries = wmgr.getWeblogEntries(
659                         website, // userName
660
null, // startDate
661
new Date JavaDoc(), // endDate
662
null, // catName
663
WeblogManager.DRAFT_ONLY, // status
664
null); // maxEntries
665
assertEquals(mExpectedEntryCount-mExpectedPublishedEntryCount, draftEntries.size());
666                           
667         // PUBLISHED and DRAFT
668
List JavaDoc allEntries = wmgr.getWeblogEntries(
669                         website, // userName
670
null, // startDate
671
new Date JavaDoc(), // endDate
672
null, // catName
673
WeblogManager.ALL, // status
674
null); // maxEntries
675
assertEquals(mExpectedEntryCount, allEntries.size());
676
677         // no status specified
678
List JavaDoc allEntries2 = wmgr.getWeblogEntries(
679                         website, // userName
680
null, // startDate
681
new Date JavaDoc(), // endDate
682
null, // catName
683
null, // status
684
null); // maxEntries
685
assertEquals(mExpectedEntryCount, allEntries2.size());
686                                             
687         getRoller().commit();
688     }
689     
690     public void testGetRecentComments() throws Exception JavaDoc
691     {
692         getRoller().begin(UserData.SYSTEM_USER);
693         WeblogManager wmgr = getRoller().getWeblogManager();
694         UserManager umgr = getRoller().getUserManager();
695         
696         WebsiteData website = (WebsiteData)mWebsites.get(0);
697         website = umgr.retrieveWebsite(website.getId());
698         List JavaDoc comments = wmgr.getRecentComments(website, 2);
699         assertTrue(comments.size() > 1);
700         // Comment 0 should be named 'name1' and Comment 1 'name0'
701
/*
702         System.out.println(((CommentData)comments.get(0)).getName());
703         System.out.println(((CommentData)comments.get(1)).getName());
704         */

705         assertTrue("name1".compareTo("name0") >0);
706         assertTrue(
707             ((CommentData)comments.get(0)).getName().compareTo(
708                 ((CommentData)comments.get(1)).getName()) > 0);
709        /*
710         System.out.println(((CommentData)comments.get(0)).getPostTime());
711         System.out.println(((CommentData)comments.get(1)).getPostTime());
712         */

713         assertTrue(
714                ((CommentData)comments.get(0)).getPostTime().compareTo(
715                    ((CommentData)comments.get(1)).getPostTime()) > 0);
716     }
717     
718     public void testGetLastPublishTime() throws Exception JavaDoc
719     {
720         getRoller().begin(UserData.SYSTEM_USER);
721         WeblogManager wmgr = getRoller().getWeblogManager();
722         WebsiteData website = (WebsiteData)mWebsites.get(0);
723         
724         Date JavaDoc lastPub = wmgr.getWeblogLastPublishTime(
725                            website.getUser().getUserName());
726         //System.out.println(lastPub);
727
assertTrue(lastPub.compareTo(new Date JavaDoc()) <= 0);
728     }
729     
730     public void testEntryAttributes() throws Exception JavaDoc {
731         getRoller().begin(UserData.SYSTEM_USER);
732         WeblogManager wmgr = getRoller().getWeblogManager();
733         WebsiteData website = (WebsiteData)mWebsites.get(0);
734         
735         WeblogCategoryData cat = wmgr.getRootWeblogCategory(website);
736
737         WeblogEntryData entry = new WeblogEntryData(
738                 null, cat, website, "title2", null, "text2", "attributetest",
739                 new Timestamp JavaDoc(0), new Timestamp JavaDoc(0), Boolean.FALSE);
740         entry.save();
741         assertNotNull(entry.getId());
742                 
743         entry.putEntryAttribute("testname", "testvalue");
744         entry.putEntryAttribute("testname2", "testvalue2");
745         entry.save();
746         
747         getRoller().commit();
748        
749         WeblogEntryData fetchedEntry = wmgr.retrieveWeblogEntry(entry.getId());
750         assertNotNull(fetchedEntry.getEntryAttributes());
751         assertEquals(2, fetchedEntry.getEntryAttributes().size());
752         
753         assertEquals(fetchedEntry.findEntryAttribute("testname"), "testvalue");
754         assertEquals(fetchedEntry.findEntryAttribute("testname2"), "testvalue2");
755     }
756
757
758     public void testPermissions() throws Exception JavaDoc
759     {
760         getRoller().begin(UserData.ANONYMOUS_USER);
761         
762         // evil testuser
763
UserData testuser = getRoller().getUserManager().getUser("testuser");
764         assertNotNull(testuser);
765         
766         // gets hold of testuser0's entry
767
WebsiteData website0 = getRoller().getUserManager().getWebsite("testuser0");
768         assertNotNull(website0);
769         List JavaDoc entries = getRoller().getWeblogManager().getWeblogEntries(
770                 website0,
771                 null, // start
772
new Date JavaDoc(), // end
773
null, // cat
774
WeblogManager.ALL,
775                 new Integer JavaDoc(1));
776         WeblogEntryData entry = (WeblogEntryData)entries.get(0);
777         assertNotNull(entry);
778         
779         // and tries to save it
780
getRoller().setUser(testuser);
781         boolean denied = false;
782         try
783         {
784             entry.save();
785         }
786         catch (RollerPermissionsException e)
787         {
788             // permission denied!
789
denied = true;
790         }
791         assertTrue(denied);
792         getRoller().rollback();
793     }
794 }
795
796
Popular Tags