1 5 package org.exoplatform.services.communication.forum.test; 6 7 import java.util.List ; 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 22 abstract public class ForumServiceTestCase extends BasicTestCase { 23 static protected ForumService service_ ; 24 static protected ForumServiceContainer container_ ; 25 26 public ForumServiceTestCase(String name) { 27 super(name); 28 } 29 30 public void tearDown() throws Exception { 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 { 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()) ; topic = service_.getTopic(topic.getId()) ; 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()) ; forum = service_.getForum(forum.getId()) ; 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 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()) ; topic = service_.getTopic(topic.getId()) ; 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 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 runImportData() ; 139 Thread.sleep(1000) ; 141 } 142 143 private Category createCategory(String name, String desc) throws Exception { 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 name, String desc) throws Exception { 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 name, String desc) throws Exception { 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 subject, String message) throws Exception { 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 ; 180 abstract public void runImportData() throws Exception ; 181 182 protected String getDescription() { 183 return "Test Forum Service" ; 184 } 185 } 186 187 | Popular Tags |