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: TestContext.java,v 1.7 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.ConnectionFactory;
49 import javax.jms.JMSException;
50 import javax.jms.Message;
51 import javax.jms.QueueConnection;
52 import javax.jms.QueueConnectionFactory;
53 import javax.jms.QueueSession;
54 import javax.jms.Session;
55 import javax.jms.TopicConnection;
56 import javax.jms.TopicConnectionFactory;
57 import javax.jms.TopicSession;
58 import javax.jms.XAQueueConnection;
59 import javax.jms.XAQueueConnectionFactory;
60 import javax.jms.XAQueueSession;
61 import javax.jms.XATopicConnection;
62 import javax.jms.XATopicConnectionFactory;
63 import javax.jms.XATopicSession;
64
65 import org.exolab.jmscts.provider.Administrator;
66
67
68 /***
69 * Instances of this class maintain the context of the current test case.
70 *
71 * @version $Revision: 1.7 $ $Date: 2004/01/31 13:44:24 $
72 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
73 * @see JMSTestCase
74 * @see JMSTestRunner
75 * @see ProviderTestRunner
76 * @see ConnectionTestRunner
77 * @see SessionTestRunner
78 * @see MessageTestRunner
79 * @see SendReceiveTestRunner
80 * @see TestCoverage
81 */
82 public final class TestContext {
83
84 /***
85 * The parent context
86 */
87 private TestContext _parent = null;
88
89 /***
90 * The test coverage of requirements
91 */
92 private TestCoverage _coverage = null;
93
94 /***
95 * The test statistics
96 */
97 private TestStatistics _statistics = null;
98
99 /***
100 * The JMS provider administration interface
101 */
102 private Administrator _administrator = null;
103
104 /***
105 * The current connection factory
106 */
107 private ConnectionFactory _factory = null;
108
109 /***
110 * The connection factory interface class. This is required to identify
111 * the connection factory implementation as some vendors
112 * (eg. Sun) implement queue and topic connection factories using the
113 * same class, but fail to create connections if the factory was intended
114 * to create a different connection type.
115 */
116 private Class _factoryType = null;
117
118 /***
119 * The current connection
120 */
121 private Connection _connection = null;
122
123 /***
124 * The current session
125 */
126 private Session _session = null;
127
128 /***
129 * The current session's message acknowledgement type
130 */
131 private AckType _type = null;
132
133 /***
134 * The message type
135 */
136 private Class _messageType;
137
138 /***
139 * The current message
140 */
141 private Message _message = null;
142
143 /***
144 * The messaging behaviour
145 */
146 private MessagingBehaviour _behaviour = null;
147
148 /***
149 * Indicates if the context is invalid
150 */
151 private boolean _invalid = false;
152
153
154 /***
155 * Construct a requirements test root context
156 *
157 * @param coverage the test coverage of requirements
158 */
159 public TestContext(TestCoverage coverage) {
160 if (coverage == null) {
161 throw new IllegalArgumentException("Argument 'coverage' is null");
162 }
163 _coverage = coverage;
164 }
165
166 /***
167 * Construct a stress test root context
168 *
169 * @param statistics the stress test statistics
170 */
171 public TestContext(TestStatistics statistics) {
172 if (statistics == null) {
173 throw new IllegalArgumentException(
174 "Argument 'statistics' is null");
175 }
176 _statistics = statistics;
177 }
178
179 /***
180 * Construct an administrator context. The parent must be a root context.
181 *
182 * @param parent a root context
183 * @param administrator the JMS provider administration interface
184 */
185 public TestContext(TestContext parent, Administrator administrator) {
186 if (parent == null) {
187 throw new IllegalArgumentException("Argument 'parent' is null");
188 }
189 if (administrator == null) {
190 throw new IllegalArgumentException(
191 "Argument 'administrator' is null");
192 }
193 _parent = parent;
194 _administrator = administrator;
195 }
196
197 /***
198 * Construct a connection factory context. The parent context must be
199 * an administrator context.
200 *
201 * @param parent an administrator context
202 * @param factory the connection factory
203 * @param type the connection factory interface class. This is required to
204 * identify the connection factory implementation as some vendors
205 * (eg. Sun) implement queue and topic connection factories using the
206 * same class, but fail to create connections if the factory was intended
207 * to create a different connection type.
208 */
209 public TestContext(TestContext parent, ConnectionFactory factory,
210 Class type) {
211 if (parent == null) {
212 throw new IllegalArgumentException("Argument 'parent' is null");
213 }
214 if (factory == null) {
215 throw new IllegalArgumentException("Argument 'factory' is null");
216 }
217 if (type == null) {
218 throw new IllegalArgumentException("Argument 'type' is null");
219 }
220 if (!type.equals(QueueConnectionFactory.class)
221 && !type.equals(TopicConnectionFactory.class)
222 && !type.equals(XAQueueConnectionFactory.class)
223 && !type.equals(XATopicConnectionFactory.class)) {
224 throw new IllegalArgumentException(
225 "Argument 'type' is not a valid connection factory interface");
226 }
227 _parent = parent;
228 _factory = factory;
229 _factoryType = type;
230 }
231
232 /***
233 * Construct a connection context. The parent context must be a connection
234 * factory context.
235 *
236 * @param parent a connection factory context
237 * @param connection the connection
238 */
239 public TestContext(TestContext parent, Connection connection) {
240 if (parent == null) {
241 throw new IllegalArgumentException("Argument 'parent' is null");
242 }
243 if (connection == null) {
244 throw new IllegalArgumentException(
245 "Argument 'connection' is null");
246 }
247 _parent = parent;
248 _connection = connection;
249 }
250
251 /***
252 * Construct a session context. The parent context must be a connection
253 * context.
254 *
255 * @param parent a connection context
256 * @param session the session
257 * @param type the message acknowledgement type of the session
258 */
259 public TestContext(TestContext parent, Session session, AckType type) {
260 if (parent == null) {
261 throw new IllegalArgumentException("Argument 'parent' is null");
262 }
263 if (session == null) {
264 throw new IllegalArgumentException("Argument 'session' is null");
265 }
266 if (type == null) {
267 throw new IllegalArgumentException("Argument 'type' is null");
268 }
269 _parent = parent;
270 _session = session;
271 _type = type;
272 }
273
274 /***
275 * Construct a message context. The parent context must be a session
276 * context.
277 *
278 * @param parent a session context
279 * @param messageType the message type
280 */
281 public TestContext(TestContext parent, Class messageType) {
282 if (parent == null) {
283 throw new IllegalArgumentException("Argument 'parent' is null");
284 }
285 if (messageType == null) {
286 throw new IllegalArgumentException(
287 "Argument 'messageType' is null");
288 }
289 _parent = parent;
290 _messageType = messageType;
291 }
292
293 /***
294 * Construct a message context. The parent context must be a session
295 * context.
296 *
297 * @param parent a session context
298 * @param message the message
299 */
300 public TestContext(TestContext parent, Message message) {
301 if (parent == null) {
302 throw new IllegalArgumentException("Argument 'parent' is null");
303 }
304 if (message == null) {
305 throw new IllegalArgumentException("Argument 'message' is null");
306 }
307 _parent = parent;
308 _message = message;
309 _messageType = MessageTypes.getType(message);
310 }
311
312 /***
313 * Construct a send/receive context. The parent context must be a session
314 * context.
315 *
316 * @param parent a session context
317 * @param messageType the message type
318 * @param behaviour the messaging behaviour
319 */
320 public TestContext(TestContext parent, Class messageType,
321 MessagingBehaviour behaviour) {
322 this(parent, messageType);
323 if (behaviour == null) {
324 throw new IllegalArgumentException("Argument 'behaviour' is null");
325 }
326 _behaviour = behaviour;
327 }
328
329 /***
330 * Construct a send/receive context. The parent context must be a session
331 * context.
332 *
333 * @param parent a session context
334 * @param message the message
335 * @param behaviour the messaging behaviour
336 */
337 public TestContext(TestContext parent, Message message,
338 MessagingBehaviour behaviour) {
339 this(parent, message);
340 if (behaviour == null) {
341 throw new IllegalArgumentException("Argument 'behaviour' is null");
342 }
343 _behaviour = behaviour;
344 }
345
346 /***
347 * Returns the test coverage of requirements
348 *
349 * @return the test coverage of requirements
350 */
351 public TestCoverage getCoverage() {
352 TestCoverage coverage = _coverage;
353 if (coverage == null && _parent != null) {
354 coverage = _parent.getCoverage();
355 }
356 return coverage;
357 }
358
359 /***
360 * Returns the test statistics
361 *
362 * @return the test statistics
363 */
364 public TestStatistics getStatistics() {
365 TestStatistics statistics = _statistics;
366 if (statistics == null && _parent != null) {
367 statistics = _parent.getStatistics();
368 }
369 return statistics;
370 }
371
372 /***
373 * Returns the JMS provider's administration interface
374 *
375 * @return the administration interface
376 */
377 public Administrator getAdministrator() {
378 Administrator administrator = _administrator;
379 if (administrator == null && _parent != null) {
380 administrator = _parent.getAdministrator();
381 }
382 return administrator;
383 }
384
385 /***
386 * Returns the current connection factory
387 *
388 * @return the current connection factory
389 */
390 public ConnectionFactory getConnectionFactory() {
391 ConnectionFactory factory = _factory;
392 if (factory == null && _parent != null) {
393 factory = _parent.getConnectionFactory();
394 }
395 return factory;
396 }
397
398 /***
399 * Returns the connection factory type
400 *
401 * @return the connection factory type
402 */
403 public Class getConnectionFactoryType() {
404 Class type = _factoryType;
405 if (type == null && _parent != null) {
406 type = _parent.getConnectionFactoryType();
407 }
408 return type;
409 }
410
411 /***
412 * Determines if the connection factory is a QueueConnectionFactory
413 *
414 * @return <code>true</code> if the connection factory is a
415 * <code>QueueConnectionFactory</code>
416 */
417 public boolean isQueueConnectionFactory() {
418 Class type = getConnectionFactoryType();
419 return (type != null && type.equals(QueueConnectionFactory.class));
420 }
421
422 /***
423 * Determines if the connection factory is a TopicConnectionFactory
424 *
425 * @return <code>true</code> if the connection factory is a
426 * <code>TopicConnectionFactory</code>
427 */
428 public boolean isTopicConnectionFactory() {
429 Class type = getConnectionFactoryType();
430 return (type != null && type.equals(TopicConnectionFactory.class));
431 }
432
433 /***
434 * Determines if the connection factory is an XAQueueConnectionFactory
435 *
436 * @return <code>true</code> if the connection factory is an
437 * <code>XAQueueConnectionFactory</code>
438 */
439 public boolean isXAQueueConnectionFactory() {
440 Class type = getConnectionFactoryType();
441 return (type != null && type.equals(XAQueueConnectionFactory.class));
442 }
443
444 /***
445 * Determines if the connection factory is an XATopicConnectionFactory
446 *
447 * @return <code>true</code> if the connection factory is an
448 * <code>XATopicConnectionFactory</code>
449 */
450 public boolean isXATopicConnectionFactory() {
451 Class type = getConnectionFactoryType();
452 return (type != null && type.equals(XATopicConnectionFactory.class));
453 }
454
455 /***
456 * Determines if the context is a queue context, i.e., point-to-point
457 *
458 * @return <code>true</code> if this is a topic context
459 */
460 public boolean isQueue() {
461 return isQueueConnectionFactory() || isXAQueueConnectionFactory();
462 }
463
464 /***
465 * Determines if the context is a topic context, i.e.,
466 * publish-and-subscribe
467 *
468 * @return <code>true</code> if this is a topic context
469 */
470 public boolean isTopic() {
471 return !isQueue();
472 }
473
474 /***
475 * Returns the current connection
476 *
477 * @return the current connection, or null, if this not a child of a
478 * connection factory context
479 */
480 public Connection getConnection() {
481 Connection connection = _connection;
482 if (connection == null && _parent != null) {
483 connection = _parent.getConnection();
484 }
485 return connection;
486 }
487
488 /***
489 * Returns the current connection type
490 *
491 * @return the current connection type, or null, if this not a child of a
492 * connection factory context
493 */
494 public Class getConnectionType() {
495 Class result = null;
496 Connection connection = getConnection();
497 if (connection != null) {
498 Class type = getConnectionFactoryType();
499 if (type.equals(QueueConnectionFactory.class)) {
500 result = QueueConnection.class;
501 } else if (type.equals(TopicConnectionFactory.class)) {
502 result = TopicConnection.class;
503 } else if (type.equals(XAQueueConnectionFactory.class)) {
504 result = XAQueueConnection.class;
505 } else {
506 result = XATopicConnection.class;
507 }
508 }
509 return result;
510 }
511
512 /***
513 * Returns the current session
514 *
515 * @return the current session, or null, if this not a child of a
516 * connection context
517 */
518 public Session getSession() {
519 Session session = _session;
520 if (session == null && _parent != null) {
521 session = _parent.getSession();
522 }
523 return session;
524 }
525
526 /***
527 * Returns the current session type
528 *
529 * @return the current session type, or null, if this not a child of a
530 * connection context
531 */
532 public Class getSessionType() {
533 Class result = null;
534 Session session = getSession();
535 if (session != null) {
536 Class type = getConnectionFactoryType();
537 if (type.equals(QueueConnectionFactory.class)) {
538 result = QueueSession.class;
539 } else if (type.equals(TopicConnectionFactory.class)) {
540 result = TopicSession.class;
541 } else if (type.equals(XAQueueConnectionFactory.class)) {
542 result = XAQueueSession.class;
543 } else {
544 result = XATopicSession.class;
545 }
546 }
547 return result;
548 }
549
550 /***
551 * Returns the current session's message acknowledgement type
552 *
553 * @return the message acknowledgement type of the session, or null,
554 * if this is not a child of a connection context
555 */
556 public AckType getAckType() {
557 AckType type = _type;
558 if (type == null && _parent != null) {
559 type = _parent.getAckType();
560 }
561 return type;
562 }
563
564 /***
565 * Returns the current message
566 *
567 * @return the current message, or null, if this not a child of a message
568 * context
569 */
570 public Message getMessage() {
571 Message message = _message;
572 if (message == null && _parent != null) {
573 message = _parent.getMessage();
574 }
575 return message;
576 }
577
578 /***
579 * Returns the current message type
580 *
581 * @return the current message type, or null, if this not a child of a
582 * message context
583 */
584 public Class getMessageType() {
585 Class type = _messageType;
586 if (type == null && _parent != null) {
587 type = _parent.getMessageType();
588 }
589 return type;
590 }
591
592 /***
593 * Returns the current messaging behaviour
594 *
595 * @return the current messaging behaviour, or null, if this not a child of
596 * a send/receive context
597 */
598 public MessagingBehaviour getMessagingBehaviour() {
599 MessagingBehaviour behaviour = _behaviour;
600 if (behaviour == null && _parent != null) {
601 behaviour = _parent.getMessagingBehaviour();
602 }
603 return behaviour;
604 }
605
606 /***
607 * Invalidate the connection and/or session managed by this context
608 */
609 public void invalidate() {
610 TestContext context = this;
611 while (context._session == null && context._parent != null) {
612 context = context._parent;
613 }
614 context._session = null;
615 context._invalid = true;
616
617 context = this;
618 while (context._connection == null && context._parent != null) {
619 context = context._parent;
620 }
621 context._connection = null;
622 context._invalid = true;
623 }
624
625 /***
626 * Determines if this context is invalid
627 *
628 * @return <code>true</code> if this context is invalid
629 */
630 public boolean isInvalid() {
631 TestContext context = this;
632 while (!context._invalid && context._parent != null) {
633 context = context._parent;
634 }
635 return context._invalid;
636 }
637
638 /***
639 * Returns the parent context
640 *
641 * @return the parent context, or <code>null</code> if there is no parent
642 * context
643 */
644 public TestContext getParent() {
645 return _parent;
646 }
647
648 /***
649 * Close this context
650 *
651 * @throws JMSException if a JMS error is encountered
652 */
653 public void close() throws JMSException {
654 if (_session != null) {
655 _session.close();
656 }
657
658 if (_connection != null) {
659 _connection.close();
660 }
661 }
662
663 /***
664 * Returns a string representation of the context
665 *
666 * @return a string representation of the context
667 */
668 public String toString() {
669 StringBuffer result = new StringBuffer();
670 String type = null;
671
672 if (getConnectionFactoryType() != null) {
673 if (isQueueConnectionFactory()) {
674 type = "queue";
675 } else if (isTopicConnectionFactory()) {
676 type = "topic";
677 } else if (isXAQueueConnectionFactory()) {
678 type = "XA queue";
679 } else {
680 type = "XA topic";
681 }
682 }
683
684 ConnectionFactory factory = getConnectionFactory();
685 Connection connection = getConnection();
686 Session session = getSession();
687 Message message = getMessage();
688 MessagingBehaviour behaviour = getMessagingBehaviour();
689
690 if (factory != null && connection == null) {
691 result.append(type);
692 result.append(" connection factory");
693 } else if (connection != null && session == null) {
694 result.append(type);
695 result.append(" connection");
696 } else if (session != null) {
697 result.append(type);
698 result.append(" session, mode=");
699 result.append(getAckType().toString());
700 if (message != null) {
701 result.append(", message type=");
702 result.append(MessageTypes.getType(message).getName());
703 }
704 if (behaviour != null) {
705 result.append(", ");
706 result.append(behaviour.toString());
707 }
708 }
709 return result.toString();
710 }
711
712 }
This page was automatically generated by Maven