ServerManagement.undeployTopic("TopicSubscriptionListAsText");
   public void testListMessages() throws Exception   {      InitialContext ic = new InitialContext(ServerManagement.getJNDIEnvironment());      TopicConnectionFactory cf = (JBossConnectionFactory)ic.lookup("/ConnectionFactory");       ServerManagement.deployTopic("TopicMessageList");      Topic topic = (Topic)ic.lookup("/topic/TopicMessageList");      TopicConnection conn = cf.createTopicConnection();      conn.setClientID("Client1");      TopicSession s = conn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);      MessageProducer prod = s.createProducer(topic);      prod.setDeliveryMode(DeliveryMode.PERSISTENT);      // Create 1 durable subscription and 2 non-durable subscription      s.createDurableSubscriber(topic, "SubscriberA");            s.createSubscriber(topic);      s.createSubscriber(topic);            // Send 1 message      prod.send(s.createTextMessage("First one"));            // Start the connection for delivery      conn.start();      // There should be 3 subscriptions      ObjectName destObjectName =          new ObjectName("jboss.messaging.destination:service=Topic,name=TopicMessageList");      String strSub = (String)ServerManagement.invoke(destObjectName, "listSubscriptionsAsText", null, null);      // Each subscription will have the same message      // Durable sub      List listMsg = (List)ServerManagement.invoke(destObjectName,             "listMessagesDurableSub",            new Object[] {"SubscriberA", "Client1", null},            new String[] {"java.lang.String", "java.lang.String", "java.lang.String"});      assertEquals(1, listMsg.size());      assertTrue(listMsg.get(0) instanceof TextMessage);      assertEquals(((TextMessage)listMsg.get(0)).getText(), "First one");            log.info(strSub);            // Non-durable sub 1      int ptr1 = strSub.indexOf("Non-durable, subscriptionID=\"");      int ptr2 = strSub.indexOf("\"", ptr1 + 30);      String sub = strSub.substring(ptr1 + 29, ptr2);      long sID1 = Long.parseLong(sub);      listMsg = (List)ServerManagement.invoke(destObjectName,             "listMessagesNonDurableSub",            new Object[] {new Long(sID1), null},            new String[] {"long", "java.lang.String"});      assertEquals(1, listMsg.size());      assertTrue(listMsg.get(0) instanceof TextMessage);      assertEquals(((TextMessage)listMsg.get(0)).getText(), "First one");      // Non-durable sub 2      strSub = strSub.substring(ptr2 + 1);      ptr1 = strSub.indexOf("Non-durable, subscriptionID=\"");      ptr2 = strSub.indexOf("\"", ptr1 + 30);      sub = strSub.substring(ptr1 + 29, ptr2);      long sID2 = Long.parseLong(sub);      assertFalse(sID1 == sID2);      listMsg = (List)ServerManagement.invoke(destObjectName,             "listMessagesNonDurableSub",            new Object[] {new Long(sID2), null},            new String[] {"long", "java.lang.String"});      assertEquals(1, listMsg.size());      assertTrue(listMsg.get(0) instanceof TextMessage);      assertEquals(((TextMessage)listMsg.get(0)).getText(), "First one");            // Send another message      prod.send(s.createTextMessage("Second one"));      // The durable subscription has 2 messages      listMsg = (List)ServerManagement.invoke(destObjectName,             "listMessagesDurableSub",            new Object[] {"SubscriberA", "", null},            new String[] {"java.lang.String", "java.lang.String", "java.lang.String"});      assertEquals(2, listMsg.size());      assertTrue(listMsg.get(0) instanceof TextMessage);      assertTrue(listMsg.get(1) instanceof TextMessage);      assertEquals(((TextMessage)listMsg.get(0)).getText(), "First one");      assertEquals(((TextMessage)listMsg.get(1)).getText(), "Second one");            // Clean-up      conn.close();      ServerManagement.undeployTopic("TopicMessageList");   }