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: AbstractSelectorTestCase.java,v 1.7 2004/02/03 21:52:11 tanderson Exp $
44 */
45 package org.exolab.jmscts.test.selector;
46
47 import java.util.List;
48 import java.util.Map;
49
50 import javax.jms.Destination;
51 import javax.jms.InvalidSelectorException;
52 import javax.jms.Message;
53
54 import org.exolab.jmscts.core.AbstractSendReceiveTestCase;
55 import org.exolab.jmscts.core.MessagingHelper;
56 import org.exolab.jmscts.core.MessagePopulator;
57 import org.exolab.jmscts.core.MessageReceiver;
58 import org.exolab.jmscts.core.PropertyPopulator;
59 import org.exolab.jmscts.core.SessionHelper;
60 import org.exolab.jmscts.core.TestContext;
61
62
63 /***
64 * This class enables selector test cases to be run for each JMS message,
65 * session and synchronous/asynchronous processing type.
66 *
67 * @version $Revision: 1.7 $ $Date: 2004/02/03 21:52:11 $
68 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
69 * @see AbstractSendReceiveTestCase
70 */
71 public abstract class AbstractSelectorTestCase
72 extends AbstractSendReceiveTestCase {
73
74 /***
75 * The destination used by this test case
76 */
77 private static final String DESTINATION = "SelectorTest";
78
79
80 /***
81 * Construct an instance of this class for a specific test case
82 *
83 * @param name the name of test case
84 */
85 public AbstractSelectorTestCase(String name) {
86 super(name);
87 }
88
89 /***
90 * Get the message populator. This implementation always returns null
91 *
92 * @return null
93 */
94 public MessagePopulator getMessagePopulator() {
95 return null;
96 }
97
98 /***
99 * Returns the list of destination names used by this test case. These
100 * are used to pre-create destinations prior to running the test case.
101 *
102 * @return the list of destinations used by this test case
103 */
104 public String[] getDestinations() {
105 return new String[] {DESTINATION};
106 }
107
108 /***
109 * Verifies that the specified selector correctly selects messages
110 *
111 * @param selector the selector to test
112 * @param selectsAll if <code>true</code>, the selector is expected to
113 * select all messages; if <code>false</code>, the selector is expected
114 * to select no messages
115 * @throws Exception for any error
116 */
117 protected void checkSelector(String selector, boolean selectsAll)
118 throws Exception {
119 checkSelector(selector, selectsAll, null);
120 }
121
122 /***
123 * Verifies that the specified selector correctly selects messages
124 *
125 * @param selector the selector to test
126 * @param selectsAll if <code>true</code>, the selector is expected to
127 * select all messages; if <code>false</code>, the selector is expected
128 * to select no messages
129 * @param properties a map of property names to property values, to be
130 * populated on sent messages. May be <code>null</code>
131 * @throws Exception for any error
132 */
133 protected void checkSelector(String selector, boolean selectsAll,
134 Map properties)
135 throws Exception {
136 final int count = 5; // the number of messages to send
137 TestContext context = getContext();
138 Message message = context.getMessage();
139 Destination destination = getDestination(DESTINATION);
140 MessageReceiver receiver = null;
141
142 try {
143 receiver = SessionHelper.createReceiver(
144 context, destination, selector, false);
145 if (properties != null) {
146 PropertyPopulator populator = new PropertyPopulator(
147 properties);
148 populator.populate(message);
149 }
150 MessagingHelper.send(context, message, destination, count);
151 int expected = (selectsAll) ? count : 0;
152 long timeout = context.getMessagingBehaviour().getTimeout();
153 List result = receiver.receive(expected, timeout);
154 int received = (result == null) ? 0 : result.size();
155 if (received != expected) {
156 fail("Expected " + expected + " messages for selector=\""
157 + selector + "\" but got " + received);
158 }
159 } catch (InvalidSelectorException exception) {
160 fail("InvalidSelectorException thrown for valid selector=\""
161 + selector + "\"");
162 } finally {
163 if (receiver != null) {
164 receiver.remove();
165 }
166 }
167 }
168
169 /***
170 * Verifies that attempting to construct a receiver for the supplied
171 * selector throws <code>InvalidSelectorException</code>
172 *
173 * @param selector the selector to test
174 * @throws Exception for any error
175 */
176 protected void checkInvalidSelector(String selector) throws Exception {
177 TestContext context = getContext();
178 Destination destination = getDestination(DESTINATION);
179 MessageReceiver receiver = null;
180 try {
181 receiver = SessionHelper.createReceiver(
182 context, destination, selector, false);
183 fail("Expected InvalidSelectorException to be thrown for"
184 + " selector=\"" + selector + "\"");
185 } catch (InvalidSelectorException expected) {
186 // the expected behaviour
187 } catch (Exception exception) {
188 String message =
189 "Expected InvalidSelectorException to be thrown for selector="
190 + "\"" + selector + "\", but threw "
191 + exception.getClass().getName() + ", message="
192 + exception.getMessage() + " instead";
193 fail(message);
194 } finally {
195 if (receiver != null) {
196 receiver.remove();
197 }
198 }
199 }
200
201 }
This page was automatically generated by Maven