KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jbossmessaging > 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.jbossmessaging.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.jbossmessaging.JMSTestCase;
42
43 /**
44  * Tests message bodies.
45  *
46  * @author <a HREF="mailto:richard.achmatowicz@jboss.com">Richard Achmatowicz</a>
47  * @author Loren Rosen (submitted patch)
48  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
49  * @version $Revision: 37406 $
50  */

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

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

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