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

import static org.junit.Assert.assertNotNull;
import io.hawtjms.test.support.AmqpTestSupport;

import javax.jms.Connection;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;

/**
 * Test basic Session functionality.
 */
public class JmsSessionTest extends AmqpTestSupport {

    @Rule public TestName name = new TestName();

    @Test(timeout = 60000)
    public void testCreateSession() throws Exception {
        JmsConnectionFactory factory = new JmsConnectionFactory(getBrokerAmqpConnectionURI());
        JmsConnection connection = (JmsConnection) factory.createConnection();
        assertNotNull(connection);
        connection.start();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        assertNotNull(session);

        session.close();
        connection.close();
    }

    @Test(timeout=30000)
    public void testSessionCreateProducer() throws Exception {
        JmsConnectionFactory factory = new JmsConnectionFactory(getBrokerAmqpConnectionURI());
        JmsConnection connection = (JmsConnection) factory.createConnection();
        assertNotNull(connection);
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        assertNotNull(session);

        Queue queue = session.createQueue("test.queue");
        MessageProducer producer = session.createProducer(queue);

        producer.close();
        session.close();
        connection.close();
    }

    @Test(timeout=30000)
    public void testSessionCreateConsumer() throws Exception {
        JmsConnectionFactory factory = new JmsConnectionFactory(getBrokerAmqpConnectionURI());
        JmsConnection connection = (JmsConnection) factory.createConnection();
        assertNotNull(connection);
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        assertNotNull(session);

        Queue queue = session.createQueue("test.queue");
        MessageConsumer consumer = session.createConsumer(queue);

        consumer.close();
        session.close();
        connection.close();
    }

    @Test(timeout=30000)
    public void testSessionDoubleCloseWithoutException() throws Exception {
        Connection connection = createAmqpConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        session.close();
        session.close();
        connection.close();
    }
}

@@ -921,37 +921,8 @@
 t;%0A%0A
-import javax.jms.Connection;%0A
 impo
@@ -1057,68 +1057,12 @@
 nit.
-Rule;%0Aimport org.junit.Test;%0Aimport org.junit.rules.TestName
+Test
 ;%0A%0A/
@@ -1164,58 +1164,8 @@
  %7B%0A%0A
-    @Rule public TestName name = new TestName();%0A%0A
     
@@ -1242,36 +1242,33 @@
 ption %7B%0A        
-JmsC
+c
 onnectionFactory
@@ -1264,147 +1264,21 @@
 tion
-Factory factory = new JmsConnectionFactory(getBrokerAmqpConnectionURI());%0A        JmsConnection connection = (JmsConnection) factory.create
+ = createAmqp
 Conn
@@ -1498,36 +1498,8 @@
 ();%0A
-        connection.close();%0A
     
@@ -1589,36 +1589,33 @@
 ption %7B%0A        
-JmsC
+c
 onnectionFactory
@@ -1611,147 +1611,21 @@
 tion
-Factory factory = new JmsConnectionFactory(getBrokerAmqpConnectionURI());%0A        JmsConnection connection = (JmsConnection) factory.create
+ = createAmqp
 Conn
@@ -1966,36 +1966,8 @@
 ();%0A
-        connection.close();%0A
     
@@ -2057,36 +2057,33 @@
 ption %7B%0A        
-JmsC
+c
 onnectionFactory
@@ -2079,147 +2079,21 @@
 tion
-Factory factory = new JmsConnectionFactory(getBrokerAmqpConnectionURI());%0A        JmsConnection connection = (JmsConnection) factory.create
+ = createAmqp
 Conn
@@ -2434,36 +2434,8 @@
 ();%0A
-        connection.close();%0A
     
@@ -2550,19 +2550,8 @@
     
-Connection 
 conn
@@ -2750,36 +2750,8 @@
 ();%0A
-        connection.close();%0A
     

simplify the test using the improved AmqpTestSupport methods.