KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jbossmq > test > TemporarySessionConnectionUnitTestCase


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.test.jbossmq.test;
23
24 import javax.jms.JMSException JavaDoc;
25 import javax.jms.QueueConnection JavaDoc;
26 import javax.jms.QueueConnectionFactory JavaDoc;
27 import javax.jms.QueueSession JavaDoc;
28 import javax.jms.Session JavaDoc;
29 import javax.jms.TemporaryQueue JavaDoc;
30 import javax.naming.Context JavaDoc;
31
32 import org.jboss.test.JBossTestCase;
33
34 /**
35  * Tests for temporaries and session/connection consumer construction
36  *
37  * @author <a HREF="mailto:adrian@jboss.org>Adrian Brock</a>
38  * @version <tt>$Revision: 37406 $</tt>
39  */

40 public class TemporarySessionConnectionUnitTestCase extends JBossTestCase
41 {
42    static String JavaDoc QUEUE_FACTORY = "ConnectionFactory";
43
44    QueueConnection JavaDoc queueConnection;
45
46    public TemporarySessionConnectionUnitTestCase(String JavaDoc name) throws Exception JavaDoc
47    {
48       super(name);
49    }
50
51    public void testTemporaryDifferentSession() throws Exception JavaDoc
52    {
53       connect();
54       try
55       {
56          QueueSession JavaDoc session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
57          TemporaryQueue JavaDoc temp = session.createTemporaryQueue();
58          session.createConsumer(temp);
59          session.close();
60          session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
61          session.createConsumer(temp).close();
62       }
63       finally
64       {
65          disconnect();
66       }
67    }
68
69    public void testTemporaryDifferentConnection() throws Exception JavaDoc
70    {
71       connect();
72       try
73       {
74          QueueSession JavaDoc session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
75          TemporaryQueue JavaDoc temp = session.createTemporaryQueue();
76          session.createConsumer(temp);
77          disconnect();
78          connect();
79          session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
80          try
81          {
82             session.createConsumer(temp).close();
83             fail("Should not be able to consume a temporary on different connection");
84          }
85          catch (JMSException JavaDoc expected)
86          {
87          }
88       }
89       finally
90       {
91          disconnect();
92       }
93    }
94
95    protected void connect() throws Exception JavaDoc
96    {
97       Context JavaDoc context = getInitialContext();
98       QueueConnectionFactory JavaDoc queueFactory = (QueueConnectionFactory JavaDoc) context.lookup(QUEUE_FACTORY);
99       queueConnection = queueFactory.createQueueConnection();
100       queueConnection.start();
101
102       getLog().debug("Connection established.");
103    }
104
105    protected void disconnect()
106    {
107       try
108       {
109          if (queueConnection != null)
110             queueConnection.close();
111       }
112       catch (Exception JavaDoc ignored)
113       {
114       }
115
116       getLog().debug("Connection closed.");
117    }
118 }
119
Popular Tags