package org.csstudio.platform.utility.jms;

import static org.junit.Assert.*;

import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.Topic;

import org.junit.Test;

/** Unit test of the basic JMS connection.
 *  <B>Will not work at other sites without adjusting the URL!</B>
 *  @author Kay Kasemir
 */
@SuppressWarnings("nls")
public class JMSConnectionFactoryTest
{
    /** JMS server URL. <B>Adjust for your site!</B> */
    final private static String URL = "tcp://ics-srv02.sns.ornl.gov:61616";
    
    /** JMS topic that should be available at CSS sites. */
    final private static String TOPIC = "LOG";

    @Test
    public void testJMSConnection() throws Exception
    {
        // Connect
        System.out.println("Trying to connect to JMS");
        System.out.println("Server     : " + URL);
        System.out.println("Topic      : " + TOPIC);
        final Connection connection = JMSConnectionFactory.connect(URL);
        connection.start();
        
        System.out.println("Connected  : " + connection.getClientID());

        // Create (unused) producer and consumer
        final Session session = connection.createSession(
                /* transacted */false, Session.AUTO_ACKNOWLEDGE);
        final Topic topic = session.createTopic(TOPIC);
        final MessageProducer producer = session.createProducer(topic);
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        
        final MessageConsumer consumer = session.createConsumer(topic);
        
        // Unclear what to check other than "no exception"
        System.out.println("Destination: " + producer.getDestination());
        assertTrue("LOG used in destination",
                producer.getDestination().toString().indexOf("LOG") >= 0);
        
        // Shutdown
        consumer.close();
        producer.close();
        session.close();
        connection.close();
    }
}

@@ -475,17 +475,50 @@
 toryTest
+ implements JMSConnectionListener
 %0A
-
 %7B%0A    /*
@@ -754,16 +754,311 @@
 = %22LOG%22;
+%0A    %0A    private boolean connected = false;%0A%0A    public void linkDown()%0A    %7B%0A        connected = false;%0A        System.out.println(%22JMS link down%22);%0A    %7D%0A%0A    public void linkUp(final String server)%0A    %7B%0A        connected = true;%0A        System.out.println(%22JMS link up to %22 + server);%0A    %7D
 %0A%0A    @T
@@ -1368,24 +1368,84 @@
 nnect(URL);%0A
+        JMSConnectionFactory.addListener(connection, this);%0A
         conn
@@ -1988,32 +1988,32 @@
 opic);%0A        %0A
-
         // Uncle
@@ -2055,16 +2055,174 @@
 eption%22%0A
+        // Don't want to read/write in here, it's only a connection test%0A        // that should otherwise have no side effect.%0A        assertTrue(connected);%0A
         

check listener