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: SendReceiveTestInvoker.java,v 1.4 2004/01/31 13:44:24 tanderson Exp $
44 */
45 package org.exolab.jmscts.core;
46
47 import java.util.HashMap;
48 import java.util.Iterator;
49 import java.util.Map;
50
51 import javax.jms.Destination;
52 import javax.jms.IllegalStateException;
53 import javax.jms.Message;
54 import javax.jms.Session;
55
56 import junit.framework.Test;
57 import junit.framework.TestResult;
58
59 import org.apache.log4j.Category;
60
61 import org.exolab.jmscts.provider.Administrator;
62
63
64 /***
65 * Helper class to run a test case, for the given message type,
66 * delivery mode, messaging mode, and destination type.
67 * <p>
68 * Prior to running, it creates a new message of
69 * the specified type, and creates any destinations required by the test.
70 *
71 * @version $Revision: 1.4 $ $Date: 2004/01/31 13:44:24 $
72 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
73 * @see SendReceiveTestCase
74 */
75 public class SendReceiveTestInvoker extends MessageTestInvoker {
76
77 /***
78 * The messaging behaviour
79 */
80 private final MessagingBehaviour _behaviour;
81
82 /***
83 * The destinations used by the test
84 */
85 private Map _destinations = null;
86
87 /***
88 * Seed for generating destination names
89 */
90 private static int _seed = 0;
91
92 /***
93 * The logger
94 */
95 private static final Category _log =
96 Category.getInstance(SendReceiveTestInvoker.class);
97
98
99 /***
100 * Construct a new <code>SendReceiveTestInvoker</code>
101 *
102 * @param test the test to run. Must be an instance of
103 * <code>SendReceiveTestCase</code>
104 * @param result the result of the test
105 * @param context the test context
106 * @param filter the test filter. May be <code>null</code>
107 * @param messageType the message type to run the test against
108 * @param behaviour the messaging behaviour
109 */
110 public SendReceiveTestInvoker(
111 Test test, TestResult result, TestContext context, TestFilter filter,
112 Class messageType, MessagingBehaviour behaviour) {
113 super(test, result, context, filter, messageType);
114
115 if (!(test instanceof SendReceiveTestCase)) {
116 throw new IllegalArgumentException(
117 "Argument 'test' must implement SendReceiveTestCase");
118 }
119 if (behaviour == null) {
120 throw new IllegalArgumentException("Argument 'behaviour' is null");
121 }
122
123 _behaviour = behaviour;
124 }
125
126 /***
127 * Setup the test
128 *
129 * @param test the test
130 * @param context the test context
131 * @throws Exception for any error
132 */
133 protected void setUp(JMSTest test, TestContext context) throws Exception {
134 super.setUp(test, context);
135
136 if (_log.isDebugEnabled()) {
137 String type = context.getSessionType().getName();
138 String msg = "test=" + test + " using session type=" + type
139 + ", message type=" + getMessageType().getName() + ", "
140 + _behaviour;
141 _log.debug("running " + msg);
142 }
143
144 SendReceiveTestCase sendReceive = (SendReceiveTestCase) test;
145 TestContext child;
146 if (sendReceive.shouldCreateMessage()) {
147 // set up the message for the test
148 Message message = create();
149 child = new TestContext(context, message, _behaviour);
150 } else {
151 child = new TestContext(context, getMessageType(), _behaviour);
152 }
153 test.setContext(child);
154 String[] names = sendReceive.getDestinations();
155 _destinations = createDestinations(child, names);
156 sendReceive.setDestinations(_destinations);
157 }
158
159 /***
160 * Tear down the test
161 *
162 * @param test the test
163 * @param context the test context
164 * @throws Exception for any error
165 */
166 protected void tearDown(JMSTest test, TestContext context)
167 throws Exception {
168
169 Session session = context.getSession();
170
171 if (_log.isDebugEnabled()) {
172 String type = context.getSessionType().getName();
173 String msg = "test=" + test + " using session type=" + type
174 + ", message type=" + getMessageType().getName() + ", "
175 + _behaviour;
176 _log.debug("completed " + msg);
177 }
178
179 if (!context.isInvalid()) {
180 try {
181 if (session.getTransacted()) {
182 // flush any messages to help ensure that destroying the
183 // destination doesn't cause too many problems
184 session.commit();
185 }
186 } catch (IllegalStateException ignore) {
187 // session has been closed
188 }
189 destroyDestinations(_destinations);
190 }
191 }
192
193 /***
194 * Create any destinations required by the test case
195 *
196 * @param context the test context
197 * @param names the destination names to create <code>Destination</code>
198 * instances for
199 * @return a map of Destination instances, keyed on name.
200 * @throws Exception for any error
201 */
202 private Map createDestinations(TestContext context, String[] names)
203 throws Exception {
204 HashMap destinations = new HashMap();
205 Administrator admin = context.getAdministrator();
206 if (names != null) {
207 Destination destination = null;
208 for (int i = 0; i < names.length; ++i) {
209 String key = names[i];
210 String name = key + (++_seed);
211 try {
212 DestinationHelper.destroy(name, admin);
213 destination = DestinationHelper.create(context, name);
214 } catch (Exception exception) {
215 _log.error(exception, exception);
216 throw exception;
217 }
218 destinations.put(key, destination);
219 }
220 }
221 return destinations;
222 }
223
224 /***
225 * Destroy any destinations set up for the test case
226 *
227 * @param destinations a map of <code>Destination</code> instances, keyed
228 * on name
229 * @throws Exception for any error
230 */
231 protected void destroyDestinations(Map destinations) throws Exception {
232 TestContext context = getContext();
233 Administrator admin = context.getAdministrator();
234
235 Iterator iter = destinations.values().iterator();
236 while (iter.hasNext()) {
237 try {
238 Destination destination = (Destination) iter.next();
239 DestinationHelper.destroy(destination, admin);
240 } catch (Exception exception) {
241 _log.error(exception, exception);
242 throw exception;
243 }
244 }
245 }
246
247 }
This page was automatically generated by Maven