KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > util > xstream > XStreamTransformTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.util.xstream;
19
20 import javax.jms.Connection JavaDoc;
21 import javax.jms.Destination JavaDoc;
22 import javax.jms.Message JavaDoc;
23 import javax.jms.MessageConsumer JavaDoc;
24 import javax.jms.MessageProducer JavaDoc;
25 import javax.jms.ObjectMessage JavaDoc;
26 import javax.jms.Session JavaDoc;
27 import javax.jms.TextMessage JavaDoc;
28
29 import junit.framework.TestCase;
30
31 import org.apache.activemq.ActiveMQConnectionFactory;
32 import org.apache.activemq.ActiveMQMessageConsumer;
33
34 /**
35  * @version $Revision: 515936 $
36  */

37 public class XStreamTransformTest extends TestCase {
38     protected ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
39     protected Connection JavaDoc connection;
40     protected long timeout = 5000;
41
42     public void testSendObjectMessageReceiveAsTextMessageAndObjectMessage() throws Exception JavaDoc {
43         // lets create the consumers
44
Session JavaDoc objectSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
45         Destination JavaDoc destination = objectSession.createTopic(getClass().getName());
46         MessageConsumer JavaDoc objectConsumer = objectSession.createConsumer(destination);
47
48         Session JavaDoc textSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
49         MessageConsumer JavaDoc textConsumer = textSession.createConsumer(destination);
50         // lets clear the transformer on this consumer so we see the message as it really is
51
((ActiveMQMessageConsumer) textConsumer).setTransformer(null);
52
53
54         // send a message
55
Session JavaDoc producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
56         MessageProducer JavaDoc producer = producerSession.createProducer(destination);
57
58         ObjectMessage JavaDoc request = producerSession.createObjectMessage(new SamplePojo("James", "London"));
59         producer.send(request);
60
61
62         // lets consume it as an object message
63
Message JavaDoc message = objectConsumer.receive(timeout);
64         assertNotNull("Should have received a message!", message);
65         assertTrue("Should be an ObjectMessage but was: " + message, message instanceof ObjectMessage JavaDoc);
66         ObjectMessage JavaDoc objectMessage = (ObjectMessage JavaDoc) message;
67         Object JavaDoc object = objectMessage.getObject();
68         assertTrue("object payload of wrong type: " + object, object instanceof SamplePojo);
69         SamplePojo body = (SamplePojo) object;
70         assertEquals("name", "James", body.getName());
71         assertEquals("city", "London", body.getCity());
72
73
74         // lets consume it as a text message
75
message = textConsumer.receive(timeout);
76         assertNotNull("Should have received a message!", message);
77         assertTrue("Should be a TextMessage but was: " + message, message instanceof TextMessage JavaDoc);
78         TextMessage JavaDoc textMessage = (TextMessage JavaDoc) message;
79         String JavaDoc text = textMessage.getText();
80         assertTrue("Text should be non-empty!", text != null && text.length() > 0);
81         System.out.println("Received XML...");
82         System.out.println(text);
83     }
84
85
86     protected void setUp() throws Exception JavaDoc {
87         connectionFactory.setTransformer(new XStreamMessageTransformer());
88         connection = connectionFactory.createConnection();
89         connection.start();
90     }
91
92
93     protected void tearDown() throws Exception JavaDoc {
94         if (connection != null) {
95             connection.close();
96         }
97     }
98 }
99
Popular Tags