summaryrefslogtreecommitdiff
path: root/qpid/java/systests/src/test/java/org/apache/qpid/test/unit/client/connection/ConnectionStartTest.java
blob: 6ea1582bb86bb0066ddee54bc220cb95fbfb3d34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 *
 * 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.test.unit.client.connection;

import org.apache.qpid.client.AMQConnection;
import org.apache.qpid.client.AMQQueue;
import org.apache.qpid.client.AMQSession;
import org.apache.qpid.test.utils.QpidBrokerTestCase;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class ConnectionStartTest extends QpidBrokerTestCase
{

    private String _broker = "vm://:1";

    private AMQConnection _connection;
    private Session _consumerSess;
    private MessageConsumer _consumer;

    protected void setUp() throws Exception
    {
        super.setUp();
        try
        {


            AMQConnection pubCon = (AMQConnection) getConnection("guest", "guest");

            AMQQueue queue = new AMQQueue(pubCon,"ConnectionStartTest");

            Session pubSess = pubCon.createSession(false, AMQSession.AUTO_ACKNOWLEDGE);

            MessageProducer pub = pubSess.createProducer(queue);

            _connection = (AMQConnection) getConnection("guest", "guest");

            _consumerSess = _connection.createSession(false, AMQSession.AUTO_ACKNOWLEDGE);

            _consumer = _consumerSess.createConsumer(queue);

            //publish after queue is created to ensure it can be routed as expected
            pub.send(pubSess.createTextMessage("Initial Message"));

            pubCon.close();

        }
        catch (Exception e)
        {
            _logger.error("Connection to " + _broker + " should succeed.", e);
            fail("Connection to " + _broker + " should succeed. Reason: " + e);
        }
    }

    protected void tearDown() throws Exception
    {
        _connection.close();
        super.tearDown();
    }

    public void testSimpleReceiveConnection()
    {
        try
        {
            assertTrue("Connection should not be started", !_connection.started());
            //Note that this next line will start the dispatcher in the session
            // should really not be called before _connection start
            //assertTrue("There should not be messages waiting for the consumer", _consumer.receiveNoWait() == null);
            _connection.start();
            assertTrue("There should be messages waiting for the consumer", _consumer.receive(10*1000) != null);
            assertTrue("Connection should be started", _connection.started());

        }
        catch (JMSException e)
        {
            fail("An error occured during test because:" + e);
        }

    }

    public void testMessageListenerConnection()
    {
        final CountDownLatch _gotMessage = new CountDownLatch(1);

        try
        {
            assertTrue("Connection should not be started", !_connection.started());
            _consumer.setMessageListener(new MessageListener()
            {
                public void onMessage(Message message)
                {
                    try
                    {
                        assertTrue("Connection should be started", _connection.started());
                        assertEquals("Mesage Received", "Initial Message", ((TextMessage) message).getText());
                        _gotMessage.countDown();
                    }
                    catch (JMSException e)
                    {
                        fail("Couldn't get message text because:" + e.getCause());
                    }
                }
            });

            assertTrue("Connection should not be started", !_connection.started());
            _connection.start();
            assertTrue("Connection should be started", _connection.started());

            try
            {
                assertTrue("Listener was never called", _gotMessage.await(10 * 1000, TimeUnit.MILLISECONDS));
            }
            catch (InterruptedException e)
            {
                fail("Timed out awaiting message via onMessage");
            }

        }
        catch (JMSException e)
        {
            fail("Failed because:" + e.getCause());
        }

    }


    public static junit.framework.Test suite()
    {
        return new junit.framework.TestSuite(ConnectionStartTest.class);
    }
}