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 2003-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: ReportHelper.java,v 1.4 2004/02/02 03:50:24 tanderson Exp $
44 */
45 package org.exolab.jmscts.report;
46
47 import java.io.ByteArrayOutputStream;
48 import java.io.PrintStream;
49
50 import javax.jms.BytesMessage;
51 import javax.jms.DeliveryMode;
52 import javax.jms.Session;
53 import javax.jms.MapMessage;
54 import javax.jms.Message;
55 import javax.jms.ObjectMessage;
56 import javax.jms.StreamMessage;
57 import javax.jms.TextMessage;
58
59 import org.exolab.jmscts.core.AckType;
60 import org.exolab.jmscts.core.JMSTestCase;
61 import org.exolab.jmscts.core.MessagingBehaviour;
62 import org.exolab.jmscts.core.ReceiptType;
63 import org.exolab.jmscts.core.TestContext;
64 import org.exolab.jmscts.core.types.DeliveryModeType;
65 import org.exolab.jmscts.core.types.DestinationType;
66 import org.exolab.jmscts.core.types.FactoryType;
67 import org.exolab.jmscts.core.types.MessageType;
68 import org.exolab.jmscts.core.types.ReceiverType;
69 import org.exolab.jmscts.core.types.SessionType;
70
71
72 /***
73 * Helper class to map between types
74 *
75 * @version $Revision: 1.4 $ $Date: 2004/02/02 03:50:24 $
76 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
77 */
78 public final class ReportHelper {
79
80 /***
81 * Prevent construction of utility class
82 */
83 private ReportHelper() {
84 }
85
86 /***
87 * Maps a {@link JMSTestCase} instance to a {@link TestRuns}
88 *
89 * @param test the test case
90 * @return a new <code>TestRuns</code> instance
91 */
92 public static TestRuns getTestRuns(JMSTestCase test) {
93 TestRuns result = new TestRuns();
94 result.setTest(getName(test));
95 return result;
96 }
97
98 /***
99 * Maps a {@link JMSTestCase} instance to a {@link TestRun}
100 *
101 * @param test the test case
102 * @return a new <code>TestRun</code> instance
103 */
104 public static TestRun getTestRun(JMSTestCase test) {
105 return getTestRun(test, test.getContext());
106 }
107
108 /***
109 * Maps a {@link JMSTestCase} instance to a {@link TestRun},
110 * using the specified context
111 *
112 * @param test the test case
113 * @param context the test context. May be null.
114 * @return a new <code>TestRun</code> instance
115 */
116 public static TestRun getTestRun(JMSTestCase test, TestContext context) {
117 TestRun result = new TestRun();
118 if (context != null) {
119 result.setContext(getContext(context));
120 }
121 return result;
122 }
123
124 /***
125 * Maps exceptions to a {@link Failure}
126 *
127 * @param cause the exception to map
128 * @param rootCause the root cause of the exception.
129 * May be <code>null</code>
130 * @return the mapped exceptions
131 */
132 public static Failure getFailure(Throwable cause, Throwable rootCause) {
133 String description = cause.getMessage();
134 if (description == null) {
135 description = cause.getClass().getName();
136 }
137 return getFailure(description, cause, rootCause);
138 }
139
140 /***
141 * Maps exceptions to a {@link Failure}
142 *
143 * @param description a description of the failure
144 * @param cause the exception to map
145 * @param rootCause the root cause of the exception.
146 * May be <code>null</code>
147 * @return the mapped exceptions
148 */
149 public static Failure getFailure(String description, Throwable cause,
150 Throwable rootCause) {
151 Failure failure = new Failure();
152 failure.setDescription(description);
153 Cause reason = new Cause();
154 reason.addAnyObject(getStackTrace(cause));
155 failure.setCause(reason);
156 if (rootCause != null) {
157 RootCause rootReason = new RootCause();
158 rootReason.addAnyObject(getStackTrace(rootCause));
159 failure.setRootCause(rootReason);
160 }
161 return failure;
162 }
163
164 /***
165 * Return a fully qualified name for a test case
166 *
167 * @param test the test case
168 * @return the fully qualified name of the test case
169 */
170 public static String getName(JMSTestCase test) {
171 return test.getClass().getName() + "." + test.getName();
172 }
173
174 /***
175 * Maps a {@link TestContext} instance to a {@link Context}
176 *
177 * @param context the test context
178 * @return the mapped context
179 */
180 public static Context getContext(TestContext context) {
181 Context result = new Context();
182 result.setFactory(getFactory(context));
183 result.setSession(getSession(context));
184 result.setMessage(getMessage(context));
185 result.setBehaviour(getBehaviour(context));
186 return result;
187 }
188
189 /***
190 * Maps the factory in a {@link TestContext} to a {@link Factory}
191 *
192 * @param context the text context
193 * @return the mapped factory
194 */
195 public static Factory getFactory(TestContext context) {
196 Factory result = new Factory();
197
198 if (context.isQueueConnectionFactory()) {
199 result.setType(FactoryType.QUEUECONNECTIONFACTORY);
200 } else if (context.isTopicConnectionFactory()) {
201 result.setType(FactoryType.TOPICCONNECTIONFACTORY);
202 } else if (context.isXAQueueConnectionFactory()) {
203 result.setType(FactoryType.XAQUEUECONNECTIONFACTORY);
204 } else {
205 result.setType(FactoryType.XATOPICCONNECTIONFACTORY);
206 }
207 return result;
208 }
209
210 /***
211 * Maps the session in a {@link TestContext} to a {@link SessionType}
212 *
213 * @param context the text context
214 * @return the mapped session type, or null, if the context doesn't contain
215 * a session
216 */
217 public static SessionType getSession(TestContext context) {
218 SessionType result = null;
219 AckType ack = context.getAckType();
220 if (ack != null) {
221 if (ack.getTransacted()) {
222 result = SessionType.TRANSACTED;
223 } else {
224 switch (ack.getAcknowledgeMode()) {
225 case Session.AUTO_ACKNOWLEDGE:
226 result = SessionType.AUTO_ACKNOWLEDGE;
227 break;
228 case Session.CLIENT_ACKNOWLEDGE:
229 result = SessionType.CLIENT_ACKNOWLEDGE;
230 break;
231 default:
232 result = SessionType.DUPS_OK_ACKNOWLEDGE;
233 }
234 }
235 }
236 return result;
237 }
238
239 /***
240 * Maps the message in a {@link TestContext} to a {@link MessageType}
241 *
242 * @param context the text context
243 * @return the mapped message type, or null, if the context doesn't contain
244 * a message
245 */
246 public static MessageType getMessage(TestContext context) {
247 MessageType result = null;
248 Class type = context.getMessageType();
249 if (type != null) {
250 if (type == BytesMessage.class) {
251 result = MessageType.BYTESMESSAGE;
252 } else if (type == MapMessage.class) {
253 result = MessageType.MAPMESSAGE;
254 } else if (type == ObjectMessage.class) {
255 result = MessageType.OBJECTMESSAGE;
256 } else if (type == StreamMessage.class) {
257 result = MessageType.STREAMMESSAGE;
258 } else if (type == TextMessage.class) {
259 result = MessageType.TEXTMESSAGE;
260 } else if (type == Message.class) {
261 result = MessageType.MESSAGE;
262 }
263 }
264 return result;
265 }
266
267 /***
268 * Maps the messaging behaviour in a {@link TestContext} to a
269 * {@link Behaviour}
270 *
271 * @param context the text context
272 * @return the mapped behaviour, or null, if the context doesn't contain
273 * a {@link MessagingBehaviour} instance
274 */
275 public static Behaviour getBehaviour(TestContext context) {
276 Behaviour result = null;
277 MessagingBehaviour behaviour = context.getMessagingBehaviour();
278 if (behaviour != null) {
279 result = new Behaviour();
280 if (behaviour.getDeliveryMode() == DeliveryMode.PERSISTENT) {
281 result.setDeliveryMode(DeliveryModeType.PERSISTENT);
282 } else {
283 result.setDeliveryMode(DeliveryModeType.NON_PERSISTENT);
284 }
285
286 ReceiptType receipt = behaviour.getReceiptType();
287 if (ReceiptType.SYNCHRONOUS.equals(receipt)) {
288 if (behaviour.getDurable()) {
289 result.setReceiver(ReceiverType.DURABLE_SYNCHRONOUS);
290 } else {
291 result.setReceiver(ReceiverType.SYNCHRONOUS);
292 }
293 } else if (ReceiptType.ASYNCHRONOUS.equals(receipt)) {
294 if (behaviour.getDurable()) {
295 result.setReceiver(ReceiverType.DURABLE_ASYNCHRONOUS);
296 } else {
297 result.setReceiver(ReceiverType.ASYNCHRONOUS);
298 }
299 } else if (ReceiptType.BROWSER.equals(receipt)) {
300 result.setReceiver(ReceiverType.BROWSER);
301 }
302
303 if (behaviour.getAdministered()) {
304 result.setDestination(DestinationType.ADMINISTERED);
305 } else {
306 result.setDestination(DestinationType.TEMPORARY);
307 }
308 }
309 return result;
310 }
311
312 /***
313 * Returns a stack trace as a string
314 *
315 * @param error the exception to get the stack trace from
316 * @return the stack trace of <code>error</code>
317 */
318 private static String getStackTrace(Throwable error) {
319 ByteArrayOutputStream stream = new ByteArrayOutputStream();
320 PrintStream print = new PrintStream(stream);
321 error.printStackTrace(print);
322 print.flush();
323 String result = stream.toString();
324 return result;
325 }
326
327 }
This page was automatically generated by Maven