/**
 * 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 org.apache.qpid.jms;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.net.URI;

import javax.jms.JMSException;

import org.apache.qpid.jms.message.JmsInboundMessageDispatch;
import org.apache.qpid.jms.provider.Provider;
import org.apache.qpid.jms.util.IdGenerator;
import org.junit.Test;
import org.mockito.Mockito;

/**
 * Test basic functionality around JmsConnection
 */
public class JmsConnectionTest {

    private final Provider provider = Mockito.mock(Provider.class);
    private final IdGenerator clientIdGenerator = new IdGenerator();

    @Test(expected=JMSException.class)
    public void testJmsConnectionThrowsJMSExceptionProviderStartFails() throws JMSException, IllegalStateException, IOException {
        Mockito.doThrow(IOException.class).when(provider).start();
        new JmsConnection("ID:TEST:1", provider, clientIdGenerator);
    }

    @Test
    public void testGetProvider() throws JMSException {
        JmsConnection connection = new JmsConnection("ID:TEST:1", provider, clientIdGenerator);
        assertSame(provider, connection.getProvider());
    }

    @Test(expected=javax.jms.IllegalStateException.class)
    public void testSetClientIdWithNull() throws JMSException {
        JmsConnection connection = new JmsConnection("ID:TEST:1", provider, clientIdGenerator);
        connection.setClientID(null);
    }

    @Test
    public void testGetConnectionId() throws JMSException {
        JmsConnection connection = new JmsConnection("ID:TEST:1", provider, clientIdGenerator);
        assertEquals("ID:TEST:1", connection.getConnectionId().toString());
    }

    @Test
    public void testAddConnectionListener() throws JMSException {
        JmsConnection connection = new JmsConnection("ID:TEST:1", provider, clientIdGenerator);
        JmsConnectionListener listener = new JmsConnectionListener() {

            @Override
            public void onMessage(JmsInboundMessageDispatch envelope) {
            }

            @Override
            public void onConnectionRestored(URI remoteURI) {
            }

            @Override
            public void onConnectionInterrupted(URI remoteURI) {
            }

            @Override
            public void onConnectionFailure(Throwable error) {
            }

            @Override
            public void onConnectionEstablished(URI remoteURI) {
            }
        };

        assertFalse(connection.removeConnectionListener(listener));
        connection.addConnectionListener(listener);
        assertTrue(connection.removeConnectionListener(listener));
    }
}

@@ -1178,121 +1178,410 @@
 jms.
-provider.Provider;%0Aimport org.apache.qpid.jms.util.IdGenerator;%0Aimport org.junit.Test;%0Aimport org.mockito.Mockito
+meta.JmsConnectionInfo;%0Aimport org.apache.qpid.jms.meta.JmsResource;%0Aimport org.apache.qpid.jms.provider.Provider;%0Aimport org.apache.qpid.jms.provider.ProviderFuture;%0Aimport org.apache.qpid.jms.util.IdGenerator;%0Aimport org.junit.Test;%0Aimport org.mockito.Mockito;%0Aimport org.mockito.invocation.InvocationOnMock;%0Aimport org.mockito.stubbing.Answer;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory
 ;%0A%0A/
@@ -1666,24 +1666,113 @@
 tionTest %7B%0A%0A
+    private static final Logger LOG = LoggerFactory.getLogger(JmsConnectionTest.class);%0A%0A
     private 
@@ -2201,32 +2201,343 @@
 erator);%0A    %7D%0A%0A
+    @Test%0A    public void testStateAfterCreate() throws JMSException %7B%0A        JmsConnection connection = new JmsConnection(%22ID:TEST:1%22, provider, clientIdGenerator);%0A%0A        assertFalse(connection.isStarted());%0A        assertFalse(connection.isClosed());%0A        assertFalse(connection.isConnected());%0A    %7D%0A%0A
     @Test%0A    pu
@@ -4216,11 +4216,924 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testConnectionStart() throws JMSException, IOException %7B%0A%0A        Mockito.doAnswer(new Answer%3CObject%3E() %7B%0A            @Override%0A            public Object answer(InvocationOnMock invocation) throws Throwable %7B%0A                Object%5B%5D args = invocation.getArguments();%0A                LOG.debug(%22Handling provider create call%22);%0A                if (args%5B0%5D instanceof JmsConnectionInfo) %7B%0A                    ProviderFuture request = (ProviderFuture) args%5B1%5D;%0A                    request.onSuccess();%0A                %7D%0A                return null;%0A            %7D%0A        %7D).when(provider).create(Mockito.any(JmsResource.class), Mockito.any(ProviderFuture.class));%0A%0A        JmsConnection connection = new JmsConnection(%22ID:TEST:1%22, provider, clientIdGenerator);%0A        assertFalse(connection.isConnected());%0A        connection.start();%0A        assertTrue(connection.isConnected());%0A    %7D
 %0A%7D%0A

Add a couple more tests for connection state handling.