KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > communication > forum > test > ForumServiceTestCase


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.services.communication.forum.test;
6
7 import java.util.List JavaDoc;
8 import org.exoplatform.commons.utils.PageList ;
9 import org.exoplatform.container.PortalContainer;
10 import org.exoplatform.services.communication.forum.*;
11 import org.exoplatform.services.database.HibernateService;
12 import org.exoplatform.test.BasicTestCase;
13
14
15 /**
16  * Thu, May 15, 2003 @
17  * @author: Tuan Nguyen
18  * @version: $Id: ForumServiceTestCase.java,v 1.8 2004/07/29 14:09:47 tuan08 Exp $
19  * @since: 0.0
20  * @email: tuan08@yahoo.com
21  */

22 abstract public class ForumServiceTestCase extends BasicTestCase {
23   static protected ForumService service_ ;
24   static protected ForumServiceContainer container_ ;
25   
26   public ForumServiceTestCase(String JavaDoc name) {
27     super(name);
28   }
29   
30   public void tearDown() throws Exception JavaDoc {
31     PortalContainer manager = PortalContainer.getInstance();
32     HibernateService hservice =
33         (HibernateService) manager.getComponentInstanceOfType(HibernateService.class) ;
34     hservice.closeSession();
35   }
36   
37   public void testForumService() throws Exception JavaDoc {
38     Category cat = createCategory("category", "this is a test") ;
39     assertEquals("check category name: ", "category", cat.getCategoryName()) ;
40     assertEquals("check category desc: ", "this is a test", cat.getDescription()) ;
41
42     Forum forum = createForum(cat, "forum", "this is a test") ;
43     assertEquals("check forum name: ", "forum", forum.getForumName()) ;
44     assertEquals("check forum desc: ", "this is a test", forum.getDescription()) ;
45     assertEquals("check forum post count: ", 0 , forum.getPostCount()) ;
46     assertEquals("check forum topic count: ", 0 , forum.getTopicCount()) ;
47     Watcher forumWatcher = service_.createWatcher(forum) ;
48     forumWatcher.setUserName("guest") ;
49     forumWatcher.setMessageProtocol("smtp") ;
50     forumWatcher.setAddress("exo.platform@laposte.net") ;
51     service_.saveWatcher(forumWatcher) ;
52     forumWatcher = service_.getWatcher(forum, "guest") ;
53     assertTrue("expect forum watcher is found", forumWatcher != null) ;
54
55     Topic topic = createTopic(forum, "topic", "this is a test") ;
56     assertEquals("check topic : ", "topic", topic.getTopic()) ;
57     assertEquals("check topic desc: ", "this is a test", topic.getDescription()) ;
58     assertEquals("check topic post count: ", 0 , topic.getPostCount()) ;
59     Watcher topicWatcher = service_.createWatcher(topic) ;
60     topicWatcher.setUserName("guest") ;
61     topicWatcher.setMessageProtocol("smtp") ;
62     topicWatcher.setAddress("exo.platform@laposte.net") ;
63     service_.saveWatcher(topicWatcher) ;
64     topicWatcher = service_.getWatcher(topic, "guest") ;
65     assertTrue("expect topic watcher is found", topicWatcher != null) ;
66     
67     System.out.println("-------------------------------------------------------");
68     
69     Post post = createPost(topic, "post", "this is a test") ;
70     assertEquals("check post subject : ", "post", post.getSubject()) ;
71     assertEquals("check post message : ", "this is a test", post.getMessage()) ;
72     
73     forum = service_.getForum(forum.getId()) ;// with no cache forum has been updated in db
74
topic = service_.getTopic(topic.getId()) ;// with no cache topic has been updated in db
75
assertEquals("check forum topic count: ", 1 , forum.getTopicCount()) ;
76     assertEquals("check forum post count: ", 1 , forum.getPostCount()) ;
77     assertEquals("check topic post count: ", 1 , topic.getPostCount()) ;
78     
79     Post post2 = createPost(topic, "post2", "this is a test") ;
80     assertEquals("check post subject : ", "post2", post2.getSubject()) ;
81     assertEquals("check post message : ", "this is a test", post.getMessage()) ;
82
83     topic = service_.getTopic(topic.getId()) ;// with no cache topic has been updated in db
84
forum = service_.getForum(forum.getId()) ;// with no cache forum has been updated in db
85
assertEquals("check topic post count: ", 2 , topic.getPostCount()) ;
86     assertEquals("check forum topic count: ", 1 , forum.getTopicCount()) ;
87     assertEquals("check forum post count: ", 2 , forum.getPostCount()) ;
88
89     
90     cat = createCategory("2nd category", "this is a test") ;
91     List JavaDoc postList = service_.getPosts(topic.getId()).getAll() ;
92     assertEquals("check number of post in topic: ", 2 , postList.size()) ;
93     for (int i = 0; i < postList.size(); i++) {
94       Post test = (Post) postList.get(i) ;
95     }
96     
97     runExportData() ;
98     
99     service_.removePost(post2.getId()) ;
100     forum = service_.getForum(forum.getId()) ;// with no cache forum has been updated in db
101
topic = service_.getTopic(topic.getId()) ;// with no cache topic has been updated in db
102
assertEquals("check forum post count: ", 1 , forum.getPostCount()) ;
103     assertEquals("check forum topic count: ", 1 , forum.getTopicCount()) ;
104     assertEquals("check topic post count: ", 1 , topic.getPostCount()) ;
105     try {
106       service_.removeTopic(topic.getId()) ;
107       topic = service_.getTopic(topic.getId()) ;
108     } catch (ForumServiceException ex) {
109       assertEquals("check topic not found exception ", ex.TOPIC_NOT_FOUND, ex.getErrorCode()) ;
110     }
111
112     try {
113       service_.removeForum(forum.getId()) ;
114       forum = service_.getForum(forum.getId()) ;
115     } catch (ForumServiceException ex) {
116       assertEquals("check forum not found exception ", ex.FORUM_NOT_FOUND, ex.getErrorCode()) ;
117     }
118
119     forum = createForum(cat, "forum", "this is a test") ;
120     for (int i = 0; i < 25; i++) {
121       createTopic(forum, "topic" + i, "this is a test") ;
122     }
123     List JavaDoc topicList = service_.getTopics(forum.getId()).getAll() ;
124     assertEquals("check number of topics in forum: ", 25 , topicList.size()) ;
125     PageList pages = service_.getTopics(forum.getId()) ;
126     pages.setPageSize(10) ;
127     topicList = pages.getPage(1) ;
128     assertEquals("check number of topics in forum: ", 10 , topicList.size()) ;
129
130     try {
131       service_.removeCategory(cat.getId()) ;
132       cat = service_.getCategory(cat.getId()) ;
133     } catch (ForumServiceException ex) {
134       assertEquals("check category not found exception ", ForumServiceException.CATEGORY_NOT_FOUND, ex.getErrorCode()) ;
135     }
136     
137     //tearDown() ;
138
runImportData() ;
139     //make sure that the task thread is finished
140
Thread.sleep(1000) ;
141   }
142  
143   private Category createCategory(String JavaDoc name, String JavaDoc desc) throws Exception JavaDoc {
144     Category category = service_.createCategoryInstance() ;
145     category.setCategoryName(name) ;
146     category.setDescription(desc) ;
147     category = service_.addCategory(category) ;
148     return category ;
149   }
150   
151   private Forum createForum(Category cat, String JavaDoc name, String JavaDoc desc) throws Exception JavaDoc {
152     Forum forum = service_.createForumInstance() ;
153     forum.setForumName(name) ;
154     forum.setDescription(desc) ;
155     forum = service_.addForum(cat, forum) ;
156     return forum ;
157   }
158
159   private Topic createTopic(Forum forum, String JavaDoc name, String JavaDoc desc) throws Exception JavaDoc {
160     Topic topic = service_.createTopicInstance() ;
161     topic.setOwner("exo") ;
162     topic.setModifiedBy("exo") ;
163     topic.setTopic(name) ;
164     topic.setDescription(desc) ;
165     topic = service_.addTopic(forum, topic) ;
166     return topic ;
167   }
168
169   private Post createPost(Topic topic, String JavaDoc subject, String JavaDoc message) throws Exception JavaDoc {
170     Post post = service_.createPostInstance() ;
171     post.setOwner("exo") ;
172     post.setModifiedBy("exo") ;
173     post.setSubject(subject) ;
174     post.setMessage(message) ;
175     post = service_.addPost(topic, post) ;
176     return post ;
177   }
178   
179   abstract public void runExportData() throws Exception JavaDoc ;
180   abstract public void runImportData() throws Exception JavaDoc ;
181   
182   protected String JavaDoc getDescription() {
183     return "Test Forum Service" ;
184   }
185 }
186
187
Popular Tags