KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > standalone > jms > unit > MDBUnitTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ejb3.test.standalone.jms.unit;
23
24 import java.util.Hashtable JavaDoc;
25 import javax.jms.Queue JavaDoc;
26 import javax.jms.QueueConnection JavaDoc;
27 import javax.jms.QueueConnectionFactory JavaDoc;
28 import javax.jms.QueueSender JavaDoc;
29 import javax.jms.QueueSession JavaDoc;
30 import javax.jms.TextMessage JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32 import org.apache.log4j.BasicConfigurator;
33 import org.apache.log4j.ConsoleAppender;
34 import org.apache.log4j.FileAppender;
35 import org.apache.log4j.PatternLayout;
36 import org.jboss.ejb3.embedded.EJB3StandaloneBootstrap;
37 import org.jboss.ejb3.test.standalone.jms.TestStatus;
38 import org.jboss.test.JBossTestCase;
39
40 /**
41  * POJO Environment tests
42  *
43  * @author <a HREF="bill@jboss.org">Bill Burke</a>
44  * @version $Revision: 40696 $
45  */

46 public class MDBUnitTestCase extends JBossTestCase
47 {
48    private static boolean booted = false;
49
50    public MDBUnitTestCase(String JavaDoc name)
51    {
52       super(name);
53    }
54
55    protected void setUp() throws Exception JavaDoc
56    {
57       // set bad properties to make sure that we're injecting InitialContext correct
58
// System.setProperty("java.naming.factory.initial", "ERROR");
59
// System.setProperty("java.naming.factory.url.pkgs", "ERROR");
60

61       super.setUp();
62       BasicConfigurator.resetConfiguration();
63       PatternLayout layout = new PatternLayout("%r %-5p [%c{1}] %m%n");
64       ConsoleAppender appender = new ConsoleAppender(layout);
65       BasicConfigurator.configure(appender);
66       String JavaDoc file = System.getProperty("org.jboss.test.logfile");
67       if (file != null)
68       {
69          FileAppender fileAppender = new FileAppender(layout, file);
70          BasicConfigurator.configure(fileAppender);
71       }
72       long start = System.currentTimeMillis();
73       log.info("Starting");
74       try
75       {
76          if (!booted)
77          {
78             booted = true;
79             EJB3StandaloneBootstrap.boot("");
80             EJB3StandaloneBootstrap.scanClasspath("jms.jar");
81          }
82       }
83       catch (Exception JavaDoc e)
84       {
85          throw e;
86       }
87       catch (Throwable JavaDoc t)
88       {
89          throw new RuntimeException JavaDoc(t);
90       }
91       log.info("setup took: " + (System.currentTimeMillis() - start));
92    }
93
94    @Override JavaDoc
95    protected void tearDown() throws Exception JavaDoc
96    {
97       super.tearDown();
98       EJB3StandaloneBootstrap.shutdown();
99    }
100    
101
102    protected InitialContext JavaDoc getLocalInitialContext() throws Exception JavaDoc
103    {
104       return new InitialContext JavaDoc(getLocalInitialContextProperties());
105    }
106
107    protected Hashtable JavaDoc getLocalInitialContextProperties()
108    {
109       return EJB3StandaloneBootstrap.getInitialContextProperties();
110    }
111
112    public void testQueue() throws Throwable JavaDoc
113    {
114       InitialContext JavaDoc ctx = getLocalInitialContext();
115
116       executeEJBs(ctx);
117    }
118
119    private void executeEJBs(InitialContext JavaDoc ctx)
120            throws Exception JavaDoc
121    {
122       TestStatus status = (TestStatus) getLocalInitialContext().lookup("TestStatusBean/remote");
123       status.clear();
124       QueueConnection JavaDoc cnn = null;
125       QueueSender JavaDoc sender = null;
126       QueueSession JavaDoc session = null;
127
128       Queue JavaDoc queue = (Queue JavaDoc) getInitialContext().lookup("queue/mdbtest");
129       QueueConnectionFactory JavaDoc factory = (QueueConnectionFactory JavaDoc) getInitialContext().lookup("ConnectionFactory");
130       cnn = factory.createQueueConnection();
131       session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
132
133       TextMessage JavaDoc msg = session.createTextMessage("Hello World");
134
135       sender = session.createSender(queue);
136       sender.send(msg);
137
138       Thread.sleep(1000);
139       assertTrue(status.queueFired());
140    }
141
142    protected void configureLoggingAfterBootstrap()
143    {
144       //enableTrace("org.jboss.tm");
145
}
146  /*
147    public static Test suite() throws Exception
148    {
149       return getDeploySetup(MDBUnitTestCase.class, "mdbtest-service.xml");
150    }
151    */

152 }
Popular Tags