/*
 *
 * JBoss, the OpenSource J2EE webOS
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package org.jboss.ejb3.test.mdb.unit;

//import java.rmi.*;


import junit.framework.Test;

import org.jboss.ejb3.test.mdb.Stateless;
import org.jboss.ejb3.test.mdb.TestStatus;
import org.jboss.security.SecurityAssociation;
import org.jboss.security.SimplePrincipal;
import org.jboss.test.JBossTestCase;

import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.naming.InitialContext;

/**
 * Sample client for the jboss container.
 *
 * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
 * @version $Id: MDBUnitTestCase.java,v 1.3.6.4 2005/09/14 22:19:11 bill Exp $
 */

public class MDBUnitTestCase
        extends JBossTestCase
{
   org.apache.log4j.Category log = getLog();

   static boolean deployed = false;
   static int test = 0;

   public MDBUnitTestCase(String name)
   {

      super(name);

   }

   public void testQueue() throws Exception
   {
      SecurityAssociation.setPrincipal(new SimplePrincipal("anyone"));
      SecurityAssociation.setCredential("password".toCharArray());
      
      TestStatus status = (TestStatus) getInitialContext().lookup(TestStatus.class.getName());
      status.clear();
      QueueConnection cnn = null;
      QueueSender sender = null;
      QueueSession session = null;

      Queue queue = (Queue) getInitialContext().lookup("queue/mdbtest");
      QueueConnectionFactory factory = (QueueConnectionFactory) getInitialContext().lookup("ConnectionFactory");
      cnn = factory.createQueueConnection();
      session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);

      TextMessage msg = session.createTextMessage("Hello World");

      sender = session.createSender(queue);
      sender.send(msg);

      Thread.sleep(1000);
      assertTrue(status.queueFired());
      assertTrue(status.interceptedQueue());
      assertTrue(status.postConstruct());

      //TODO: Figure out how to test preDestroy gets invoked
      //assertTrue(status.preDestroy());
      
      Stateless stateless = (Stateless)getInitialContext().lookup("Stateless");
      assertNotNull(stateless);
      String state = stateless.getState();
      assertEquals("Set", state);
   }

   public void testTopic() throws Exception
   {
      TestStatus status = (TestStatus) getInitialContext().lookup(TestStatus.class.getName());
      status.clear();
      TopicConnection cnn = null;
      MessageProducer sender = null;
      TopicSession session = null;

      Topic topic = (Topic) getInitialContext().lookup("topic/mdbtest");
      TopicConnectionFactory factory = (TopicConnectionFactory) getInitialContext().lookup("ConnectionFactory");
      cnn = factory.createTopicConnection();
      session = cnn.createTopicSession(false, QueueSession.AUTO_ACKNOWLEDGE);

      TextMessage msg = session.createTextMessage("Hello World");

      sender = session.createProducer(topic);
      sender.send(msg);

      Thread.sleep(1000);
      assertTrue(status.topicFired());
      assertTrue(status.interceptedTopic());
      assertFalse(status.postConstruct());
      assertFalse(status.preDestroy());
   } 

   public static Test suite() throws Exception
   {
      return getDeploySetup(MDBUnitTestCase.class, "mdbtest-service.xml, mdb-test.ejb3");
   }

}
