/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF 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 io.hawtjms.tests.discovery;

import static org.junit.Assert.assertTrue;
import io.hawtjms.jms.JmsConnection;
import io.hawtjms.jms.JmsConnectionFactory;
import io.hawtjms.jms.JmsConnectionListener;
import io.hawtjms.jms.message.JmsInboundMessageDispatch;
import io.hawtjms.test.util.AmqpTestSupport;
import io.hawtjms.test.util.Wait;

import javax.jms.Connection;

import org.junit.Test;

/**
 * Test that a Broker using AMQP can be discovered and JMS operations can be performed.
 */
public class JmsAmqpDiscoveryTest extends AmqpTestSupport implements JmsConnectionListener {

    @Test(timeout=60000)
    public void testRunningBrokerIsDiscovered() throws Exception {
        connection = createConnection();
        final JmsConnection jmsConnection = (JmsConnection) connection;
        connection.start();

        assertTrue("connection never connected.", Wait.waitFor(new Wait.Condition() {

            @Override
            public boolean isSatisified() throws Exception {
                return jmsConnection.isConnected();
            }
        }));
    }

    @Override
    protected boolean isAmqpDiscovery() {
        return true;
    }

    protected Connection createConnection() throws Exception {
        JmsConnectionFactory factory = new JmsConnectionFactory(
            "discovery:(multicast://default)?maxReconnectDelay=500");
        return factory.createConnection();
    }

    @Override
    public void onConnectionFailure(Throwable error) {
    }

    @Override
    public void onConnectionInterrupted() {
    }

    @Override
    public void onConnectionRestored() {
    }

    @Override
    public void onMessage(JmsInboundMessageDispatch envelope) {
    }
}

@@ -1155,26 +1155,134 @@
 java
-x.jms.Connection;%0A
+.util.concurrent.CountDownLatch;%0Aimport java.util.concurrent.TimeUnit;%0A%0Aimport javax.jms.Connection;%0A%0Aimport org.junit.Before;
 %0Aimp
@@ -1488,24 +1488,330 @@
 Listener %7B%0A%0A
+    private CountDownLatch interrupted;%0A    private CountDownLatch restored;%0A    private JmsConnection jmsConnection;%0A%0A    @Override%0A    @Before%0A    public void setUp() throws Exception %7B%0A        super.setUp();%0A%0A        interrupted = new CountDownLatch(1);%0A        restored = new CountDownLatch(1);%0A    %7D%0A%0A
     @Test(ti
@@ -1935,80 +1935,8 @@
 ();%0A
-        final JmsConnection jmsConnection = (JmsConnection) connection;%0A
     
@@ -2208,32 +2208,1160 @@
     %7D));%0A    %7D%0A%0A
+    @Test(timeout=60000)%0A    public void testConnectionFailsWhenBrokerGoesDown() throws Exception %7B%0A        connection = createConnection();%0A        connection.start();%0A%0A        assertTrue(%22connection never connected.%22, Wait.waitFor(new Wait.Condition() %7B%0A%0A            @Override%0A            public boolean isSatisified() throws Exception %7B%0A                return jmsConnection.isConnected();%0A            %7D%0A        %7D));%0A%0A        stopPrimaryBroker();%0A%0A        assertTrue(interrupted.await(20, TimeUnit.SECONDS));%0A    %7D%0A%0A    @Test(timeout=60000)%0A    public void testConnectionRestoresAfterBrokerRestarted() throws Exception %7B%0A        connection = createConnection();%0A        connection.start();%0A%0A        assertTrue(%22connection never connected.%22, Wait.waitFor(new Wait.Condition() %7B%0A%0A            @Override%0A            public boolean isSatisified() throws Exception %7B%0A                return jmsConnection.isConnected();%0A            %7D%0A        %7D));%0A%0A        stopPrimaryBroker();%0A        assertTrue(interrupted.await(20, TimeUnit.SECONDS));%0A        startPrimaryBroker();%0A        assertTrue(restored.await(20, TimeUnit.SECONDS));%0A    %7D%0A%0A
     @Override%0A  
@@ -3634,22 +3634,28 @@
         
-return
+connection =
  factory
@@ -3671,24 +3671,154 @@
 nnection();%0A
+        jmsConnection = (JmsConnection) connection;%0A        jmsConnection.addConnectionListener(this);%0A        return connection;%0A
     %7D%0A%0A    @
@@ -3942,24 +3942,57 @@
 rrupted() %7B%0A
+        interrupted.countDown();%0A
     %7D%0A%0A    @
@@ -4037,24 +4037,54 @@
 estored() %7B%0A
+        restored.countDown();%0A
     %7D%0A%0A    @

Add more discovery tests.