KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jbossmq > test > MessageBodyUnitTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.jbossmq.test;
23
24 import java.util.Arrays JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import javax.jms.JMSException JavaDoc;
28 import javax.jms.Message JavaDoc;
29 import javax.jms.ObjectMessage JavaDoc;
30 import javax.jms.Queue JavaDoc;
31 import javax.jms.QueueConnection JavaDoc;
32 import javax.jms.QueueConnectionFactory JavaDoc;
33 import javax.jms.QueueReceiver JavaDoc;
34 import javax.jms.QueueSender JavaDoc;
35 import javax.jms.QueueSession JavaDoc;
36 import javax.jms.Session JavaDoc;
37 import javax.jms.TextMessage JavaDoc;
38 import javax.naming.Context JavaDoc;
39
40 import org.jboss.logging.Logger;
41 import org.jboss.test.JBossTestCase;
42
43 /**
44  * Tests message bodies.
45  *
46  * @author Loren Rosen (submitted patch)
47  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
48  * @version $Revision: 58115 $
49  */

50 public class MessageBodyUnitTestCase extends JBossTestCase
51 {
52    // Provider specific
53
public static final String JavaDoc QUEUE_FACTORY = "ConnectionFactory";
54    public static final String JavaDoc TEST_QUEUE = "queue/testQueue";
55
56    Context JavaDoc context;
57    QueueConnection JavaDoc queueConnection;
58    QueueSession JavaDoc session;
59    Queue JavaDoc queue;
60
61    QueueReceiver JavaDoc receiver;
62    QueueSender JavaDoc sender;
63
64    Logger log;
65
66    public MessageBodyUnitTestCase(String JavaDoc name) throws Exception JavaDoc
67    {
68       super(name);
69       log = getLog();
70    }
71
72    protected void setUp() throws Exception JavaDoc
73    {
74       connect();
75    }
76
77    protected void tearDown() throws Exception JavaDoc
78    {
79       disconnect();
80    }
81
82    protected void connect() throws Exception JavaDoc
83    {
84       log.debug("connecting");
85       if (context == null)
86       {
87          context = getInitialContext();
88       }
89
90       QueueConnectionFactory JavaDoc queueFactory = (QueueConnectionFactory JavaDoc) context.lookup(QUEUE_FACTORY);
91       queueConnection = queueFactory.createQueueConnection();
92       log.debug("connected");
93
94       queueConnection.start();
95       session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
96       log.debug("session established");
97
98       queue = (Queue JavaDoc) context.lookup(TEST_QUEUE);
99
100       receiver = session.createReceiver(queue);
101       sender = session.createSender(queue);
102       log.debug("sender established");
103
104       drainQueue();
105       log.debug("end of connect call");
106    }
107
108    protected void disconnect() throws Exception JavaDoc
109    {
110       queueConnection.close();
111    }
112
113    private void drainQueue() throws Exception JavaDoc
114    {
115       log.debug("draining queue");
116
117       Message JavaDoc message = receiver.receive(2000);
118       int c = 0;
119       while (message != null)
120       {
121          message = receiver.receive(2000);
122          c++;
123       }
124
125       if (c != 0)
126          log.debug("Drained " + c + " messages from the queue");
127
128       log.debug("drained queue");
129
130    }
131
132    protected void validate(String JavaDoc payload) throws Exception JavaDoc
133    {
134       log.debug("validating text |" + payload + "|");
135
136       TextMessage JavaDoc outMessage = session.createTextMessage();
137       outMessage.setText(payload);
138       log.debug("sending |" + payload + "|");
139       sender.send(outMessage);
140
141       log.debug("receiving |" + payload + "|");
142       TextMessage JavaDoc inMessage = (TextMessage JavaDoc) receiver.receive();
143       log.debug("received |" + payload + "|");
144       String JavaDoc inPayload = inMessage.getText();
145
146       assertEquals("Message body text test", payload, inPayload);
147       log.debug("validated text " + payload);
148    }
149
150    public void testTextMessageBody() throws Exception JavaDoc
151    {
152       log.debug("testing text");
153
154       validate("ordinary text");
155       validate(" ");
156       validate("");
157       // very long strings, non-printable ASCII strings
158
char c[] = new char[1024 * 32];
159       Arrays.fill(c, 'x');
160       validate(new String JavaDoc(c));
161       Arrays.fill(c, '\u0130'); // I with dot
162
validate(new String JavaDoc(c));
163       Arrays.fill(c, '\u0008');
164       validate(new String JavaDoc(c));
165       log.debug("tested text");
166    }
167
168    protected void validate(java.io.Serializable JavaDoc payload) throws Exception JavaDoc
169    {
170       ObjectMessage JavaDoc outMessage = session.createObjectMessage();
171       outMessage.setObject(payload);
172       sender.send(outMessage);
173
174       ObjectMessage JavaDoc inMessage = (ObjectMessage JavaDoc) receiver.receive();
175       Object JavaDoc inPayload = inMessage.getObject();
176
177       assertEquals("Message body object test", payload, inPayload);
178    }
179
180    public void testObjectMessageBody() throws Exception JavaDoc
181    {
182       log.debug("testing object");
183       validate(new Integer JavaDoc(0));
184       validate(new Integer JavaDoc(1));
185       validate(new Integer JavaDoc(-1));
186       validate(new Integer JavaDoc(Integer.MAX_VALUE));
187       validate(new Integer JavaDoc(Integer.MIN_VALUE));
188       validate(new Integer JavaDoc(-1));
189       validate(new Float JavaDoc(1.0));
190       validate(new Float JavaDoc(0.0));
191       validate(new Float JavaDoc(-1.0));
192       validate(new Float JavaDoc(Float.MAX_VALUE));
193       validate(new Float JavaDoc(Float.MIN_VALUE));
194       validate(new Float JavaDoc(Float.NaN));
195       validate(new Float JavaDoc(Float.POSITIVE_INFINITY));
196       validate(new Float JavaDoc(Float.NEGATIVE_INFINITY));
197       validate(new Float JavaDoc(1.0));
198       HashMap JavaDoc m = new HashMap JavaDoc(); // Fill with serializable stuff
199
m.put("file", new java.io.File JavaDoc("somefile.txt"));
200       m.put("url", new java.net.URL JavaDoc("http://example.net"));
201       validate(m);
202       validate((java.io.Serializable JavaDoc)Collections.nCopies(10000, "Repeat"));
203    }
204
205    /**
206     * Test null properties.
207     */

208    public void testNullProperties() throws Exception JavaDoc
209    {
210       TextMessage JavaDoc message = session.createTextMessage();
211
212       message.setStringProperty("THE_PROP", null);
213       message.setObjectProperty("THE_PROP2", null);
214
215       try
216       {
217         message.setStringProperty("", null);
218         fail("empty string property");
219       }
220       catch (IllegalArgumentException JavaDoc e) {}
221
222       try
223       {
224         message.setStringProperty(null, null);
225         fail("null property");
226       }
227       catch (IllegalArgumentException JavaDoc e) {}
228    }
229
230    public void testInvalidPropertyName() throws Exception JavaDoc
231    {
232       Message JavaDoc message = session.createMessage();
233
234       String JavaDoc[] invalid = new String JavaDoc[]
235       {
236          "invalid-hyphen",
237          "1digitfirst",
238          "NULL",
239          "TRUE",
240          "FALSE",
241          "NOT",
242          "AND",
243          "OR",
244          "BETWEEN",
245          "LIKE",
246          "IN",
247          "IS",
248          "ESCAPE"
249       };
250
251       for (int i = 0; i < invalid.length; ++i)
252       {
253          try
254          {
255             message.setStringProperty(invalid[i], "whatever");
256             fail("expected error for invalid property name " + invalid[i]);
257          }
258          catch (IllegalArgumentException JavaDoc expected)
259          {
260          }
261       }
262
263       String JavaDoc[] valid = new String JavaDoc[]
264       {
265          "identifier",
266          "_",
267          "$",
268          "_xSx",
269          "$x_x",
270          "A1",
271          "null",
272          "true",
273          "false",
274          "not",
275          "and",
276          "or",
277          "between",
278          "like",
279          "in",
280          "is",
281          "escape"
282       };
283
284       for (int i = 0; i < invalid.length; ++i)
285          message.setStringProperty(valid[i], "whatever");
286    }
287
288    /**
289     * Test vendor properties.
290     */

291    public void testVendorProperties() throws Exception JavaDoc
292    {
293       TextMessage JavaDoc message = session.createTextMessage();
294
295       try
296       {
297         message.setStringProperty("JMS_JBOSS_SCHEDULED_DELIVERY", "whenever");
298         fail("invalid type");
299       }
300       catch (JMSException JavaDoc e) {}
301       try
302       {
303         message.setObjectProperty("JMS_JBOSS_SCHEDULED_DELIVERY", "10234");
304         fail("invalid type");
305       }
306       catch (JMSException JavaDoc e) {}
307       try
308       {
309         message.setStringProperty("JMS_JBOSS_REDELIVERY_COUNT", "fruity");
310         fail("invalid type");
311       }
312       catch (JMSException JavaDoc e) {}
313       try
314       {
315         message.setStringProperty("JMS_JBOSS_REDELIVERY_LIMIT", "fruity");
316         fail("invalid type");
317       }
318       catch (JMSException JavaDoc e) {}
319
320       message.setLongProperty("JMS_JBOSS_SCHEDULED_DELIVERY", 10234);
321       message.setIntProperty("JMS_JBOSS_REDELIVERY_COUNT", 123);
322       message.setShortProperty("JMS_JBOSS_REDELIVERY_LIMIT", (short)1);
323    }
324
325    public static junit.framework.Test suite() throws Exception JavaDoc
326    {
327        ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
328        return getDeploySetup(MessageBodyUnitTestCase.class,
329                loader.getResource("messaging/test-destinations-service.xml").toString());
330    }
331
332 }
333
Popular Tags