@Test
  public void testReceivedRollbackQueue() throws Exception {
    Connection conn = createConnection();

    Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
    MessageProducer producer = sess.createProducer(queue1);

    MessageConsumer consumer = sess.createConsumer(queue1);
    conn.start();

    TextMessage mSent = sess.createTextMessage("igloo");
    producer.send(mSent);
    log.trace("sent1");

    sess.commit();

    TextMessage mRec = (TextMessage) consumer.receive(1000);
    ProxyAssertSupport.assertNotNull(mRec);
    log.trace("Got 1");
    ProxyAssertSupport.assertNotNull(mRec);
    ProxyAssertSupport.assertEquals("igloo", mRec.getText());

    sess.commit();

    mSent.setText("rollback");
    producer.send(mSent);

    sess.commit();

    log.trace("Receiving 2");
    mRec = (TextMessage) consumer.receive(1000);
    ProxyAssertSupport.assertNotNull(mRec);

    log.trace("Received 2");
    ProxyAssertSupport.assertNotNull(mRec);
    ProxyAssertSupport.assertEquals("rollback", mRec.getText());

    sess.rollback();

    TextMessage mRec2 = (TextMessage) consumer.receive(1000);
    ProxyAssertSupport.assertNotNull(mRec2);
    ProxyAssertSupport.assertEquals("rollback", mRec2.getText());

    sess.commit();

    ProxyAssertSupport.assertEquals(mRec.getText(), mRec2.getText());

    conn.close();
  }
  @Test
  public void testSimpleRollback() throws Exception {
    // send a message
    Connection conn = null;

    try {
      conn = createConnection();
      Session s = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
      s.createProducer(queue1).send(s.createTextMessage("one"));

      s.close();

      s = conn.createSession(true, Session.SESSION_TRANSACTED);
      MessageConsumer c = s.createConsumer(queue1);
      conn.start();
      Message m = c.receive(1000);
      ProxyAssertSupport.assertNotNull(m);

      ProxyAssertSupport.assertEquals("one", ((TextMessage) m).getText());
      ProxyAssertSupport.assertFalse(m.getJMSRedelivered());
      ProxyAssertSupport.assertEquals(1, m.getIntProperty("JMSXDeliveryCount"));

      s.rollback();

      // get the message again
      m = c.receive(1000);
      ProxyAssertSupport.assertNotNull(m);

      ProxyAssertSupport.assertTrue(m.getJMSRedelivered());
      ProxyAssertSupport.assertEquals(2, m.getIntProperty("JMSXDeliveryCount"));

      conn.close();

      Long i = getMessageCountForQueue("Queue1");

      ProxyAssertSupport.assertEquals(1, i.intValue());
    } finally {
      if (conn != null) {
        conn.close();
      }
      removeAllMessages(queue1.getQueueName(), true);
    }
  }
  /**
   * Make sure redelivered flag is set on redelivery via rollback, different setup: we close the
   * rolled back session and we receive the message whose acknowledgment was cancelled on a new
   * session.
   */
  @Test
  public void testRedeliveredQueue2() throws Exception {
    Connection conn = null;

    try {
      conn = createConnection();

      Session sendSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

      MessageProducer prod = sendSession.createProducer(queue1);
      prod.send(sendSession.createTextMessage("a message"));

      conn.close();

      conn = createConnection();
      Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);

      MessageConsumer cons = sess.createConsumer(queue1);

      conn.start();

      TextMessage tm = (TextMessage) cons.receive(1000);
      ProxyAssertSupport.assertNotNull(tm);

      ProxyAssertSupport.assertEquals("a message", tm.getText());

      ProxyAssertSupport.assertFalse(tm.getJMSRedelivered());
      ProxyAssertSupport.assertEquals(1, tm.getIntProperty("JMSXDeliveryCount"));

      sess.rollback();

      sess.close();

      Session sess2 = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

      cons = sess2.createConsumer(queue1);

      tm = (TextMessage) cons.receive(1000);

      ProxyAssertSupport.assertEquals("a message", tm.getText());

      ProxyAssertSupport.assertEquals(2, tm.getIntProperty("JMSXDeliveryCount"));

      ProxyAssertSupport.assertTrue(tm.getJMSRedelivered());
    } finally {
      if (conn != null) {
        conn.close();
      }
    }
  }
  @Test
  public void testReceivedRollbackTopic() throws Exception {
    Connection conn = null;

    try {
      conn = createConnection();

      Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
      MessageProducer producer = sess.createProducer(ActiveMQServerTestCase.topic1);

      MessageConsumer consumer = sess.createConsumer(ActiveMQServerTestCase.topic1);
      conn.start();

      log.info("sending message first time");
      TextMessage mSent = sess.createTextMessage("igloo");
      producer.send(mSent);
      log.info("sent message first time");

      sess.commit();

      TextMessage mRec = (TextMessage) consumer.receive(2000);
      ProxyAssertSupport.assertEquals("igloo", mRec.getText());

      sess.commit();

      log.info("sending message again");
      mSent.setText("rollback");
      producer.send(mSent);
      log.info("sent message again");

      sess.commit();

      mRec = (TextMessage) consumer.receive(2000);
      ProxyAssertSupport.assertEquals("rollback", mRec.getText());
      sess.rollback();

      TextMessage mRec2 = (TextMessage) consumer.receive(2000);

      sess.commit();

      ProxyAssertSupport.assertNotNull(mRec2);

      ProxyAssertSupport.assertEquals(mRec.getText(), mRec2.getText());
    } finally {
      if (conn != null) {
        conn.close();
      }
    }
  }
  /** Test redelivery works ok for Topic */
  @Test
  public void testRedeliveredTopic() throws Exception {
    Connection conn = null;

    try {
      conn = createConnection();

      Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
      MessageProducer producer = sess.createProducer(ActiveMQServerTestCase.topic1);

      MessageConsumer consumer = sess.createConsumer(ActiveMQServerTestCase.topic1);
      conn.start();

      Message mSent = sess.createTextMessage("igloo");
      producer.send(mSent);

      sess.commit();

      TextMessage mRec = (TextMessage) consumer.receive(2000);

      ProxyAssertSupport.assertEquals("igloo", mRec.getText());
      ProxyAssertSupport.assertFalse(mRec.getJMSRedelivered());

      sess.rollback();

      mRec = (TextMessage) consumer.receive(2000);

      ProxyAssertSupport.assertNotNull(mRec);
      ProxyAssertSupport.assertEquals("igloo", mRec.getText());
      ProxyAssertSupport.assertTrue(mRec.getJMSRedelivered());

      sess.commit();
    } finally {
      if (conn != null) {
        conn.close();
      }
    }
  }