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: SessionTestRunner.java,v 1.3 2004/01/31 13:44:24 tanderson Exp $
44 */
45 package org.exolab.jmscts.core;
46
47 import javax.jms.JMSException;
48 import javax.jms.Session;
49
50 import junit.framework.Protectable;
51 import junit.framework.Test;
52 import junit.framework.TestResult;
53
54 import org.apache.log4j.Category;
55
56
57 /***
58 * This class enables generic session test cases to be run for each JMS
59 * session type.
60 * <p>
61 * Test cases must implement the {@link SessionTestCase} interface.
62 *
63 * @version $Revision: 1.3 $ $Date: 2004/01/31 13:44:24 $
64 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
65 * @see SessionTestCase
66 * @see ConnectionTestCase
67 * @see ConnectionTestRunner
68 */
69 public class SessionTestRunner extends TestRunner
70 implements ConnectionTestCase {
71
72 /***
73 * The logger
74 */
75 private static final Category _log =
76 Category.getInstance(SessionTestRunner.class);
77
78
79 /***
80 * Construct an instance with the test case to run.
81 *
82 * @param test the test case to run
83 */
84 public SessionTestRunner(SessionTestCase test) {
85 super(test);
86 }
87
88 /***
89 * Get the connection factory types to test against
90 *
91 * @return the connection factory types to test against
92 */
93 public ConnectionFactoryTypes getConnectionFactoryTypes() {
94 return ((SessionTestCase) getTest()).getConnectionFactoryTypes();
95 }
96
97 /***
98 * Returns true if the connection should be started prior to running the
99 * test.
100 *
101 * @return true if the connection should be started, false if it should be
102 * stopped
103 */
104 public boolean startConnection() {
105 return ((SessionTestCase) getTest()).startConnection();
106 }
107
108 /***
109 * Returns true if the client identifier should be set on the connection.
110 * This is only applicable for TopicConnection instances, and will be
111 * ignored for QueueConnection instances.
112 *
113 * @return true if an identifier should be allocated to the connection
114 */
115 public boolean setClientID() {
116 return ((SessionTestCase) getTest()).setClientID();
117 }
118
119 /***
120 * Runs the test case against sessions for each of the acknowledgement
121 * types specified by the test.
122 *
123 * @param test the test case to run
124 * @param result the instance to collect results in
125 */
126 protected void runTest(Test test, TestResult result) {
127 SessionTestCase sessionTest = (SessionTestCase) test;
128 AckType[] types = sessionTest.getAckTypes().getTypes();
129 TestContext context = getContext();
130 TestFilter filter = getFilter();
131
132 if (sessionTest instanceof TestRunner) {
133 ((TestRunner) sessionTest).setFilter(filter);
134 }
135
136 for (int i = 0; i < types.length && !result.shouldStop(); ++i) {
137 if (filter == null || filter.includes(context, types[i], test)) {
138 result.runProtected(test, new Tester(test, result, types[i]));
139 }
140 }
141 }
142
143 /***
144 * Creates the session, prior to running the test case
145 *
146 * @throws Exception if the session cannot be created, or the test case
147 * does not implement the SessionTestCase interface
148 */
149 protected void setUp() throws Exception {
150 checkImplements(SessionTestCase.class);
151 }
152
153 /***
154 * Helper class to run a test case
155 */
156 private class Tester implements Protectable {
157
158 /***
159 * The test case to run
160 */
161 private Test _test = null;
162
163 /***
164 * The instance to collect test results in
165 */
166 private TestResult _result = null;
167
168 /***
169 * The message acknowledgement type
170 */
171 private AckType _type = null;
172
173 /***
174 * Construct a new <code>Tester</code>
175 *
176 * @param test the test
177 * @param result the test result
178 * @param type the session acknowledgement type
179 */
180 public Tester(Test test, TestResult result, AckType type) {
181 _test = test;
182 _result = result;
183 _type = type;
184 }
185
186 /***
187 * Run the test
188 *
189 * @throws Exception for any error
190 */
191 public void protect() throws Exception {
192 SessionTestCase test = (SessionTestCase) _test;
193 TestContext context = getContext();
194 TestContext parent = context;
195
196 if (!test.share()) {
197 // test case cannot share resources, so allocate a new
198 // connection
199 parent = TestContextHelper.createConnectionContext(context);
200 }
201 Session session = null;
202
203 try {
204 session = ConnectionHelper.createSession(parent, _type);
205 } catch (Exception exception) {
206 _log.error(exception, exception);
207 throw exception;
208 }
209
210 String msg = null;
211 if (_log.isDebugEnabled()) {
212 msg = "test=" + test + " using session type="
213 + session.getClass().getName() + ", behaviour=" + _type;
214 _log.debug("running " + msg);
215 }
216 TestContext child = new TestContext(parent, session, _type);
217 test.setContext(child);
218
219 if (test.startConnection()) {
220 child.getConnection().start();
221 } else {
222 child.getConnection().stop();
223 }
224 _test.run(_result);
225
226 if (_log.isDebugEnabled()) {
227 _log.debug("completed " + msg);
228 }
229
230 try {
231 child.close();
232 } catch (JMSException exception) {
233 _log.error(exception, exception);
234 throw exception;
235 }
236
237 if (parent != context) {
238 // if a new connection was allocated, close it
239 try {
240 parent.close();
241 } catch (Exception exception) {
242 _log.error(exception, exception);
243 throw exception;
244 }
245 }
246 }
247
248 }
249
250 }
This page was automatically generated by Maven