package org.objectweb.jtests.jms.conform.connection;
import org.objectweb.jtests.jms.framework.*;
import javax.jms.*;
import junit.framework.*;
public class ConnectionTest extends PTPTestCase {
public void testAcknowledge() {
try {
receiverConnection.stop();
receiverSession = receiverConnection.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
receiver = receiverSession.createReceiver(receiverQueue);
receiverConnection.start();
Message message = senderSession.createMessage();
sender.send(message);
Message m = receiver.receive();
receiverConnection.close();
m.acknowledge();
fail("§4.3.5 Invoking the acknowledge method of a received message from a closed "+
"connection's session must throw a [javax.jms.]IllegalStateException.\n");
} catch (javax.jms.IllegalStateException e) {
} catch (JMSException e) {
fail("§4.3.5 Invoking the acknowledge method of a received message from a closed "+
"connection's session must throw a [javax.jms.]IllegalStateException, not a "+ e);
} catch (java.lang.IllegalStateException e) {
fail("§4.3.5 Invoking the acknowledge method of a received message from a closed "+
"connection's session must throw an [javax.jms.]IllegalStateException "+
"[not a java.lang.IllegalStateException]");
}
}
public void testUseClosedConnection() {
try {
senderConnection.close();
senderConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
fail("Should raise a javax.jms.IllegalStateException");
} catch (javax.jms.IllegalStateException e) {
} catch (JMSException e) {
fail("Should raise a javax.jms.IllegalStateException, not a "+ e);
} catch (java.lang.IllegalStateException e) {
fail("Should raise a javax.jms.IllegalStateException, not a java.lang.IllegalStateException");
}
}
public void testMessageSentWhenConnectionClosed() {
try {
senderConnection.stop();
Message message = senderSession.createTextMessage();
sender.send(message);
Message m = receiver.receive();
} catch (JMSException e) {
fail(e);
}
}
public void testCloseClosedConnection() {
try {
senderConnection.close();
senderConnection.close();
} catch (Exception e) {
fail("§4.3.5 Closing a closed connection must not throw an exception.\n");
}
}
public void testStartStartedConnection() {
try {
senderConnection.start();
} catch (JMSException e) {
fail(e);
}
}
public void testStopStoppedConnection() {
try {
senderConnection.stop();
senderConnection.stop();
} catch (JMSException e) {
fail(e);
}
}
public void testStopConsumerConnection() {
try {
receiverConnection.stop();
receiver.setMessageListener(new MessageListener() {
public void onMessage(Message m) {
try {
fail("The message must not be received, the consumer connection is stopped");
assertEquals("test", ((TextMessage)m).getText());
} catch (JMSException e) {
fail(e);
}
}
});
TextMessage message = senderSession.createTextMessage();
message.setText("test");
sender.send(message);
synchronized(this) {
try {
wait(1000);
} catch (Exception e) {
fail (e);
}
}
} catch (JMSException e) {
fail(e);
}
}
public static Test suite() {
return new TestSuite(ConnectionTest.class);
}
public ConnectionTest(String name) {
super(name);
}
}