PostModelTestCase.java :  » JBoss » jboss-forums » org » jboss » portlet » test » forums » Java Open Source

Java Open Source » JBoss » jboss forums 
jboss forums » org » jboss » portlet » test » forums » PostModelTestCase.java
/*****************************************
 *                                       *
 *  JBoss Portal: The OpenSource Portal  *
 *                                       *
 *       Forums JBoss Portlet            *
 *                                       *
 *   Distributable under LGPL license.   *
 *   See terms of license at gnu.org.    *
 *                                       *
 *****************************************/
package org.jboss.portlet.test.forums;

import org.jboss.portlet.forums.impl.TopicImpl;
import org.jboss.portlet.forums.impl.PostImpl;
import org.jboss.portlet.forums.impl.MessageImpl;
import org.jboss.portlet.forums.impl.ForumImpl;
import org.jboss.portlet.forums.impl.CategoryImpl;
import org.jboss.portlet.forums.model.Post;
import org.jboss.portlet.forums.model.Topic;
import org.jboss.portlet.forums.model.Message;
import org.jboss.portlet.forums.model.Forum;
import org.jboss.portlet.forums.model.Category;
import org.jboss.portal.core.modules.ModuleException;


import java.util.Date;
import java.util.Set;
import java.util.HashSet;
import java.util.List;
import java.util.Iterator;

/**
 * @author <a href="mailto:boleslaw.dawidowicz@jboss.comboleslaw.dawidowicz@jboss.com">Boleslaw Dawidowicz</a>
 * @version $Revision: 2408 $
 */

public class PostModelTestCase extends ForumsModelTestCase
{
   public PostModelTestCase(String name)
   {
      super(name);
   }

   public void testWholeTreeSavedTestCase()
   {
      Set namesSet = new HashSet();
      try
      {
         List categories = forumsModule.findCategories();
         for (Iterator ci = categories.iterator(); ci.hasNext();)
         {
            Category category = (Category)ci.next();
            namesSet.add(category.getTitle());
            List forums = category.getForums();
            for (Iterator fi = forums.iterator(); fi.hasNext();)
            {
               Forum forum = (Forum)fi.next();
               namesSet.add(forum.getName());
               System.out.println("There are : " + forums.size() + " forums found");
               List topics = forum.getTopics();
               System.out.println("There are : " + topics.size() + " topics found");
               for (Iterator ti = topics.iterator(); ti.hasNext();)
               {
                  Topic topic = (Topic)ti.next();
                  System.out.println("Processing topic subject: " + topic.getSubject());
                  Iterator pi = topic.getPosts().iterator();
                  System.out.println("Number of posts in topic: " + topic.getPosts().size());
                  for (; pi.hasNext();)
                  {
                     System.out.println("Processing post");

                     Post post = (Post)pi.next();
                     namesSet.add(post.getMessage().getSubject());
                  }
               }
            }
         }

         Set expectedSubjectSet = new HashSet();
         expectedSubjectSet.add("Sample Category");
         expectedSubjectSet.add("Sample Forum");
         expectedSubjectSet.add("subject1");
         expectedSubjectSet.add("subject2");
         assertEquals(expectedSubjectSet, namesSet);
      }
      catch (ModuleException e)
      {
         e.printStackTrace();
      }

   }


   public void testOnlyPostDeleteTestCase()
   {

      try
      {
         List posts = forumsModule.findPosts();
         System.out.println("There are : " + posts.size() + " posts found");
         for (Iterator ti = posts.iterator(); ti.hasNext();)
         {
            Post post = (Post)ti.next();
            forumsModule.removePost(post);
         }
         posts = forumsModule.findPosts();
         assertEquals(posts.size(), 0);
         //clean up
         List categories = forumsModule.findCategories();
         for (Iterator ci = categories.iterator(); ci.hasNext();)
         {
            Category category = (Category)ci.next();
            forumsModule.removeCategory(category);
         }

      }
      catch (ModuleException e)
      {
         e.printStackTrace();
      }

   }

   protected void populate() throws Exception
   {
      Topic topic1 = new TopicImpl();
      //topic1.setPosts(new ArrayList());
      topic1.setSubject("sample subject");

      //topic1.set
      Post post1 = new PostImpl();
      post1.setCreateDate(new Date());
      post1.setMessage(new MessageImpl());
      Message message1 = new MessageImpl();
      message1.setSubject("subject1");
      post1.setMessage(message1);
      Post post2 = new PostImpl();
      post2.setCreateDate(new Date());
      Message message2 = new MessageImpl();
      message2.setSubject("subject2");
      post2.setMessage(message2);

      topic1.addPost(post1);
      topic1.addPost(post2);

      Forum forum1 = new ForumImpl();

      forum1.addTopic(topic1);
      forum1.setName("Sample Forum");

      Category category1 = new CategoryImpl();
      category1.addForum(forum1);
      category1.setTitle("Sample Category");


      session.save(category1);
   }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.