1 /***
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Exoffice Technologies. For written permission,
18 * please contact tma@netspace.net.au.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Exoffice Technologies. Exolab is a registered
23 * trademark of Exoffice Technologies.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2001-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: TestContextHelper.java,v 1.6 2004/01/31 13:44:24 tanderson Exp $
44 */
45 package org.exolab.jmscts.core;
46
47 import javax.jms.Connection;
48 import javax.jms.Session;
49
50
51 /***
52 * Helper to create new {@link TestContext} instances using existing contexts
53 * as prototypes
54 *
55 * @version $Revision: 1.6 $ $Date: 2004/01/31 13:44:24 $
56 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
57 */
58 public final class TestContextHelper {
59
60 /***
61 * Prevent construction of utility class
62 */
63 private TestContextHelper() {
64 }
65
66 /***
67 * Creates a new connection context with the same connection type as
68 * the supplied context.
69 *
70 * @param context the prototype context
71 * @return a new connection context
72 * @throws Exception for any error
73 */
74 public static TestContext createConnectionContext(TestContext context)
75 throws Exception {
76 if (context == null) {
77 throw new IllegalArgumentException("Argument 'context' is null");
78 }
79 if (context.getConnectionFactory() == null) {
80 throw new IllegalArgumentException(
81 "Argument 'context' is not a connection factory context");
82 }
83 Connection connection = ConnectionFactoryHelper.createConnection(
84 context, null);
85 return new TestContext(context, connection);
86 }
87
88 /***
89 * Creates a new session context with the same session type as
90 * the supplied context. A new connection will be allocated.
91 *
92 * @param context the prototype context
93 * @return a new session context
94 * @throws Exception for any error
95 */
96 public static TestContext createSessionContext(TestContext context)
97 throws Exception {
98 return createSessionContext(context, true);
99 }
100
101 /***
102 * Creates a new session context with the same session type as
103 * the supplied context.
104 *
105 * @param context the prototype context
106 * @param newConnection if <code>true</code> allocate a new connection,
107 * otherwise use the existing one
108 * @return a new session context
109 * @throws Exception for any error
110 */
111 public static TestContext createSessionContext(TestContext context,
112 boolean newConnection)
113 throws Exception {
114 if (context == null) {
115 throw new IllegalArgumentException("Argument 'context' is null");
116 }
117 if (context.getSession() == null) {
118 throw new IllegalArgumentException(
119 "Argument 'context' is not a session context");
120 }
121 TestContext connection;
122 if (newConnection) {
123 connection = createConnectionContext(context);
124 } else {
125 connection = context.getParent();
126 }
127 return createSessionContext(connection, context.getAckType());
128 }
129
130 /***
131 * Creates a new session context from a connection context
132 *
133 * @param context the connection context
134 * @param type the message acknowledgement type of the session
135 * @return a new session context
136 * @throws Exception for any error
137 */
138 public static TestContext createSessionContext(TestContext context,
139 AckType type)
140 throws Exception {
141 if (context == null) {
142 throw new IllegalArgumentException("Argument 'context' is null");
143 }
144 if (type == null) {
145 throw new IllegalArgumentException("Argument 'type' is null");
146 }
147 if (context.getConnection() == null) {
148 throw new IllegalArgumentException(
149 "Argument 'context' is not a connection context");
150 }
151 Session session = ConnectionHelper.createSession(context, type);
152 return new TestContext(context, session, type);
153 }
154
155 /***
156 * Creates a new send/receive context with the same details as
157 * the supplied context. A new connection and session will be allocated,
158 * but the message and behaviour will refer to the original.
159 *
160 * @param context the prototype context
161 * @return a new send/receive context
162 * @throws Exception for any error
163 */
164 public static TestContext createSendReceiveContext(TestContext context)
165 throws Exception {
166 return createSendReceiveContext(context, true);
167 }
168
169 /***
170 * Creates a new send/receive context with the same details as
171 * the supplied context. A session will be allocated,
172 * but the message type and behaviour will refer to the original.
173 *
174 * @param context the prototype context
175 * @param newConnection if <code>true</code> allocate a new connection,
176 * otherwise use the existing one
177 * @return a new send/receive context
178 * @throws Exception for any error
179 */
180 public static TestContext createSendReceiveContext(TestContext context,
181 boolean newConnection)
182 throws Exception {
183 if (context == null) {
184 throw new IllegalArgumentException("Argument 'context' is null");
185 }
186 if (context.getMessagingBehaviour() == null) {
187 throw new IllegalArgumentException(
188 "Argument 'context' is not a send/receive context");
189 }
190 return createSendReceiveContext(context, newConnection,
191 context.getMessagingBehaviour());
192 }
193
194 /***
195 * Creates a new send/receive context with the same details as
196 * the supplied context, but with a different messaging behaviour.
197 * A session will be allocated.
198 *
199 * @param context the prototype context
200 * @param newConnection if <code>true</code> allocate a new connection,
201 * otherwise use the existing one
202 * @param behaviour the messaging behaviour
203 * @return a new send/receive context
204 * @throws Exception for any error
205 */
206 public static TestContext createSendReceiveContext(
207 TestContext context, boolean newConnection,
208 MessagingBehaviour behaviour) throws Exception {
209 if (context == null) {
210 throw new IllegalArgumentException("Argument 'context' is null");
211 }
212 if (behaviour == null) {
213 throw new IllegalArgumentException(
214 "Argument 'behaviour' is null");
215 }
216 TestContext session = createSessionContext(context, newConnection);
217 return new TestContext(session, context.getMessageType(), behaviour);
218 }
219
220 }
This page was automatically generated by Maven