KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jbossmessaging > 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.jbossmessaging.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.jbossmessaging.JMSTestCase;
33
34 /**
35  * Tests for temporaries and session/connection consumer construction
36  *
37  * @author <a HREF="mailto:richard.achmatowicz@jboss.com">Richard Achmatowicz</a>
38  * @author <a HREF="mailto:adrian@jboss.org>Adrian Brock</a>
39  * @version <tt>$Revision: 37406 $</tt>
40  */

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