1 18 package org.apache.activemq.util.xstream; 19 20 import javax.jms.Connection ; 21 import javax.jms.Destination ; 22 import javax.jms.Message ; 23 import javax.jms.MessageConsumer ; 24 import javax.jms.MessageProducer ; 25 import javax.jms.ObjectMessage ; 26 import javax.jms.Session ; 27 import javax.jms.TextMessage ; 28 29 import junit.framework.TestCase; 30 31 import org.apache.activemq.ActiveMQConnectionFactory; 32 import org.apache.activemq.ActiveMQMessageConsumer; 33 34 37 public class XStreamTransformTest extends TestCase { 38 protected ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false"); 39 protected Connection connection; 40 protected long timeout = 5000; 41 42 public void testSendObjectMessageReceiveAsTextMessageAndObjectMessage() throws Exception { 43 Session objectSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 45 Destination destination = objectSession.createTopic(getClass().getName()); 46 MessageConsumer objectConsumer = objectSession.createConsumer(destination); 47 48 Session textSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 49 MessageConsumer textConsumer = textSession.createConsumer(destination); 50 ((ActiveMQMessageConsumer) textConsumer).setTransformer(null); 52 53 54 Session producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 56 MessageProducer producer = producerSession.createProducer(destination); 57 58 ObjectMessage request = producerSession.createObjectMessage(new SamplePojo("James", "London")); 59 producer.send(request); 60 61 62 Message message = objectConsumer.receive(timeout); 64 assertNotNull("Should have received a message!", message); 65 assertTrue("Should be an ObjectMessage but was: " + message, message instanceof ObjectMessage ); 66 ObjectMessage objectMessage = (ObjectMessage ) message; 67 Object 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 message = textConsumer.receive(timeout); 76 assertNotNull("Should have received a message!", message); 77 assertTrue("Should be a TextMessage but was: " + message, message instanceof TextMessage ); 78 TextMessage textMessage = (TextMessage ) message; 79 String 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 { 87 connectionFactory.setTransformer(new XStreamMessageTransformer()); 88 connection = connectionFactory.createConnection(); 89 connection.start(); 90 } 91 92 93 protected void tearDown() throws Exception { 94 if (connection != null) { 95 connection.close(); 96 } 97 } 98 } 99 | Popular Tags |