/*
* Copyright 2009 Red Hat, Inc.
* Red Hat licenses this file to you under the Apache License, version
* 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package org.hornetq.tests.integration.jms.client;
import javax.jms.Connection;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageEOFException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.StreamMessage;
import junit.framework.Assert;
import org.hornetq.core.logging.Logger;
import org.hornetq.tests.util.JMSTestBase;
/**
*
* A MessageTest
*
* @author <a href="mailto:tim.fox@jboss.com">Tim Fox</a>
*
*
*/
public class MessageTest extends JMSTestBase
{
// Constants -----------------------------------------------------
private static final Logger log = Logger.getLogger(MessageTest.class);
private static final long TIMEOUT = 1000;
private static final String propName1 = "myprop1";
private static final String propName2 = "myprop2";
private static final String propName3 = "myprop3";
// Attributes ----------------------------------------------------
// Static --------------------------------------------------------
// Constructors --------------------------------------------------
// Public --------------------------------------------------------
//https://jira.jboss.org/jira/browse/HORNETQ-242
public void testStreamMessageReadsNull() throws Exception
{
Connection conn = cf.createConnection();
Queue queue = createQueue("testQueue");
try
{
Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer prod = sess.createProducer(queue);
MessageConsumer cons = sess.createConsumer(queue);
conn.start();
StreamMessage msg = sess.createStreamMessage();
msg.writeInt(1);
msg.writeInt(2);
msg.writeInt(3);
StreamMessage received = (StreamMessage)sendAndConsumeMessage(msg, prod, cons);
Assert.assertNotNull(received);
assertEquals(1, received.readObject());
assertEquals(2, received.readObject());
assertEquals(3, received.readObject());
try
{
received.readObject();
fail("Should throw exception");
}
catch (MessageEOFException e)
{
//Ok
}
try
{
received.readBoolean();
fail("Should throw exception");
}
catch (MessageEOFException e)
{
//Ok
}
try
{
received.readByte();
fail("Should throw exception");
}
catch (MessageEOFException e)
{
//Ok
}
try
{
received.readChar();
fail("Should throw exception");
}
catch (MessageEOFException e)
{
//Ok
}
try
{
received.readDouble();
fail("Should throw exception");
}
catch (MessageEOFException e)
{
//Ok
}
try
{
received.readFloat();
fail("Should throw exception");
}
catch (MessageEOFException e)
{
//Ok
}
try
{
received.readInt();
fail("Should throw exception");
}
catch (MessageEOFException e)
{
//Ok
}
try
{
received.readLong();
fail("Should throw exception");
}
catch (MessageEOFException e)
{
//Ok
}
try
{
received.readShort();
fail("Should throw exception");
}
catch (MessageEOFException e)
{
//Ok
}
try
{
received.readString();
fail("Should throw exception");
}
catch (MessageEOFException e)
{
//Ok
}
}
finally
{
try
{
conn.close();
}
catch (Throwable igonred)
{
}
}
}
public void testNullProperties() throws Exception
{
Connection conn = cf.createConnection();
Queue queue = createQueue("testQueue");
try
{
Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer prod = sess.createProducer(queue);
MessageConsumer cons = sess.createConsumer(queue);
conn.start();
Message msg = sess.createMessage();
msg.setObjectProperty(MessageTest.propName1, null);
msg.setStringProperty(MessageTest.propName2, null);
checkProperties(msg);
Message received = sendAndConsumeMessage(msg, prod, cons);
Assert.assertNotNull(received);
checkProperties(received);
}
finally
{
try
{
conn.close();
}
catch (Throwable igonred)
{
}
}
}
// Package protected ---------------------------------------------
// Protected -----------------------------------------------------
// Private -------------------------------------------------------
private void checkProperties(final Message message) throws Exception
{
Assert.assertNull(message.getObjectProperty(MessageTest.propName1));
Assert.assertNull(message.getStringProperty(MessageTest.propName1));
Assert.assertNull(message.getStringProperty(MessageTest.propName2));
Assert.assertNull(message.getObjectProperty(MessageTest.propName2));
Assert.assertNull(message.getStringProperty(MessageTest.propName3));
Assert.assertNull(message.getObjectProperty(MessageTest.propName3));
try
{
MessageTest.log.info(message.getIntProperty(MessageTest.propName1));
Assert.fail("Should throw exception");
}
catch (NumberFormatException e)
{
// Ok
}
try
{
MessageTest.log.info(message.getShortProperty(MessageTest.propName1));
}
catch (NumberFormatException e)
{
// Ok
}
try
{
MessageTest.log.info(message.getByteProperty(MessageTest.propName1));
}
catch (NumberFormatException e)
{
// Ok
}
Assert.assertEquals(false, message.getBooleanProperty(MessageTest.propName1));
try
{
MessageTest.log.info(message.getLongProperty(MessageTest.propName1));
}
catch (NumberFormatException e)
{
// Ok
}
try
{
MessageTest.log.info(message.getFloatProperty(MessageTest.propName1));
}
catch (NullPointerException e)
{
// Ok
}
try
{
MessageTest.log.info(message.getDoubleProperty(MessageTest.propName1));
}
catch (NullPointerException e)
{
// Ok
}
}
private Message sendAndConsumeMessage(final Message msg, final MessageProducer prod, final MessageConsumer cons) throws Exception
{
prod.send(msg);
Message received = cons.receive(MessageTest.TIMEOUT);
return received;
}
// Inner classes -------------------------------------------------
}