/*
* Created on Nov 12, 2004
*
*/
package org.mom4j.jms;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.MessageNotWriteableException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.StreamMessage;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import junit.framework.Assert;
/**
* @author denninger
*
*/
public class TestMessageTypes extends JmsTestCase {
public static final String TEXT = "abcdefghijklmnopqrstuvwxyz���<>-_.;*+#?/&%$�!\"@";
public TestMessageTypes(String name) {
super(name);
}
public void testTextMessage() throws Exception {
/* Create Naming-Context */
Context ctx = new InitialContext(namingProps);
/* Lookup 'administerable' Objects */
QueueConnectionFactory qcf = (QueueConnectionFactory) ctx
.lookup("QueueConnectionFactory");
Queue q = (Queue) ctx.lookup("testQueue");
QueueConnection qc = null;
QueueSession s_send = null;
QueueSession s_rec = null;
try {
/* Create a connection to the queue */
qc = qcf.createQueueConnection("system", "system");
/*
* Create a session on top of the connection which will be used only
* for sending messages, transacted and with auto-acknowledge-mode
*/
s_send = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
/*
* Create a session on top of the connection which will be used only
* for receiving messages, transacted and with auto-acknowledge-mode
*/
s_rec = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
/* start the connection */
qc.start();
/* Create a receiver */
QueueReceiver qr = s_rec.createReceiver(q);
/* Create a sender for sending messages */
QueueSender qsender = s_send.createSender(q);
qsender.setDisableMessageTimestamp(false);
/* create a text message for sending */
TextMessage msg = s_send.createTextMessage();
/* and put some nasty text into it */
msg.setText(TEXT);
/* Send the message */
qsender.send(msg);
/* Commit the sending session */
s_send.commit();
/* Receive the message */
msg = (TextMessage) qr.receiveNoWait();
Assert.assertNotNull("message recieved", msg);
/* Commit the session to clear the queue */
s_rec.commit();
/* Check, that the text of the message is still the same */
Assert.assertEquals("text is not the same " + msg.getText(), msg
.getText(), TEXT);
} finally {
try {
s_rec.close();
} catch (Exception ex) {
}
try {
s_send.close();
} catch (Exception ex) {
}
try {
qc.close();
} catch (Exception ex) {
}
}
}
public void testMapMessage() throws Exception {
/* Create Naming-Context */
Context ctx = new InitialContext(namingProps);
/* Lookup 'administerable' Objects */
QueueConnectionFactory qcf = (QueueConnectionFactory) ctx
.lookup("QueueConnectionFactory");
Queue q = (Queue) ctx.lookup("testQueue");
QueueConnection qc = null;
QueueSession s_send = null;
QueueSession s_rec = null;
try {
/* Create a connection to the queue */
qc = qcf.createQueueConnection("system", "system");
/*
* Create a session on top of the connection which will be used only
* for sending messages, transacted and with auto-acknowledge-mode
*/
s_send = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
/*
* Create a session on top of the connection which will be used only
* for receiving messages, transacted and with auto-acknowledge-mode
*/
s_rec = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
/* start the connection */
qc.start();
/* Create a receiver */
QueueReceiver qr = s_rec.createReceiver(q);
/* Create a sender for sending messages */
QueueSender qsender = s_send.createSender(q);
qsender.setDisableMessageTimestamp(false);
/* create a map message for sending */
MapMessage msg = s_send.createMapMessage();
/* put some values in it */
msg.setByte("byte", (byte) 0);
msg.setBoolean("boolean", true);
msg.setChar("char", 'c');
msg.setDouble("double", 1.11d);
msg.setFloat("float", 1.1f);
msg.setInt("int", 1);
msg.setShort("short", (short) 1);
msg.setString("string", TEXT);
/* Send the message */
qsender.send(msg);
/* Commit the sending session */
s_send.commit();
/* Receive the message */
msg = (MapMessage) qr.receiveNoWait();
Assert.assertNotNull("message recieved", msg);
/* Commit the session to clear the queue */
s_rec.commit();
/* Check, that the text of the message is still the same */
Assert.assertEquals("byte", msg.getByte("byte"), (byte) 0);
Assert.assertEquals("boolean", msg.getBoolean("boolean"), true);
Assert.assertEquals("char", msg.getChar("char"), 'c');
Assert.assertEquals("double", msg.getDouble("double"), 1.11d, 0);
Assert.assertEquals("float", msg.getFloat("float"), 1.1f, 0);
Assert.assertEquals("int", msg.getInt("int"), 1);
Assert.assertEquals("short", msg.getShort("short"), (short) 1);
Assert.assertEquals("string", msg.getString("string"), TEXT);
try {
msg.setBoolean("boolean", false);
Assert.fail("Expected MessageNotWriteableException");
} catch (MessageNotWriteableException ex) {
//as expected
}
Assert.assertNotNull(msg.getString("byte"));
Assert.assertNotNull(msg.getString("boolean"));
Assert.assertNotNull(msg.getString("char"));
Assert.assertNotNull(msg.getString("double"));
Assert.assertNotNull(msg.getString("float"));
Assert.assertNotNull(msg.getString("int"));
Assert.assertNotNull(msg.getString("short"));
Assert.assertNotNull(msg.getString("string"));
try {
msg.getBoolean("byte");
Assert.fail("Expected JMSException");
} catch (JMSException ex) {
//as expected
}
msg.getLong("short");
try {
msg.getByte("short");
Assert.fail("Expected JMSException");
} catch (JMSException ex) {
//as expected
}
try {
msg.getFloat("boolean");
Assert.fail("Expected JMSException");
} catch (JMSException ex) {
//as expected
}
try {
msg.getDouble("short");
Assert.fail("Expected JMSException");
} catch (JMSException ex) {
//as expected
}
} finally {
try {
s_rec.close();
} catch (Exception ex) {
}
try {
s_send.close();
} catch (Exception ex) {
}
try {
qc.close();
} catch (Exception ex) {
}
}
}
public void testStreamMessage() throws Exception {
/* Create Naming-Context */
Context ctx = new InitialContext(namingProps);
/* Lookup 'administerable' Objects */
QueueConnectionFactory qcf = (QueueConnectionFactory) ctx
.lookup("QueueConnectionFactory");
Queue q = (Queue) ctx.lookup("testQueue");
QueueConnection qc = null;
QueueSession s_send = null;
QueueSession s_rec = null;
try {
/* Create a connection to the queue */
qc = qcf.createQueueConnection("system", "system");
/*
* Create a session on top of the connection which will be used only
* for sending messages, transacted and with auto-acknowledge-mode
*/
s_send = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
/*
* Create a session on top of the connection which will be used only
* for receiving messages, transacted and with auto-acknowledge-mode
*/
s_rec = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
/* start the connection */
qc.start();
/* Create a receiver */
QueueReceiver qr = s_rec.createReceiver(q);
/* Create a sender for sending messages */
QueueSender qsender = s_send.createSender(q);
qsender.setDisableMessageTimestamp(false);
/* create a text message for sending */
StreamMessage msg = s_send.createStreamMessage();
/* and write values into it */
msg.writeByte((byte)0);
msg.writeBoolean(true);
msg.writeChar('c');
msg.writeDouble(1.11d);
msg.writeFloat(1.1f);
msg.writeInt(1);
msg.writeShort((short)1);
msg.writeString(TEXT);
/* Send the message */
qsender.send(msg);
/* Commit the sending session */
s_send.commit();
/* Receive the message */
msg = (StreamMessage) qr.receiveNoWait();
Assert.assertNotNull("message recieved", msg);
/* Commit the session to clear the queue */
s_rec.commit();
/* Check, that the text of the message is still the same */
Assert.assertEquals("byte", msg.readByte(), (byte) 0);
Assert.assertEquals("boolean", msg.readBoolean(), true);
Assert.assertEquals("char", msg.readChar(), 'c');
Assert.assertEquals("double", msg.readDouble(), 1.11d, 0);
Assert.assertEquals("float", msg.readFloat(), 1.1f, 0);
Assert.assertEquals("int", msg.readInt(), 1);
Assert.assertEquals("short", msg.readShort(), (short) 1);
Assert.assertEquals("string", msg.readString(), TEXT);
} finally {
try {
s_rec.close();
} catch (Exception ex) {
}
try {
s_send.close();
} catch (Exception ex) {
}
try {
qc.close();
} catch (Exception ex) {
}
}
}
}