summaryrefslogtreecommitdiff
path: root/java/systests/src/main/java/org/apache/qpid/test/client/message/JMSDestinationTest.java
blob: a7efe4922bc78676200a5e3fae92ae6505ff23e4 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
 *
 * 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.client.message;

import org.apache.qpid.configuration.ClientProperties;
import org.apache.qpid.client.AMQAnyDestination;
import org.apache.qpid.client.AMQDestination;
import org.apache.qpid.client.AMQTopic;
import org.apache.qpid.client.CustomJMSXProperty;
import org.apache.qpid.framing.AMQShortString;
import org.apache.qpid.management.common.mbeans.ManagedQueue;
import org.apache.qpid.test.utils.JMXTestUtils;
import org.apache.qpid.test.utils.QpidBrokerTestCase;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.Topic;
import javax.management.openmbean.CompositeDataSupport;
import javax.management.openmbean.TabularData;
import java.nio.BufferOverflowException;
import java.util.Iterator;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * From the API Docs getJMSDestination:
 *
 * When a message is received, its JMSDestination value must be equivalent to
 * the value assigned when it was sent.
 */
public class JMSDestinationTest extends QpidBrokerTestCase
{

    private Connection _connection;
    private Session _session;

    private static final String USER = "admin";
    private CountDownLatch _receiveMessage;
    private Message _message;

    public void setUp() throws Exception
    {
        //Ensure JMX management is enabled for MovedToQueue test 
        setConfigurationProperty("management.enabled", "true");

        super.setUp();

        _connection = getConnection();

        _session = _connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    }

    /**
     * Test a message sent to a queue comes back with JMSDestination queue
     *
     * @throws Exception
     */
    public void testQueue() throws Exception
    {

        Queue queue = _session.createQueue(getTestQueueName());

        MessageConsumer consumer = _session.createConsumer(queue);

        sendMessage(_session, queue, 1);

        _connection.start();

        Message message = consumer.receive(10000);

        assertNotNull("Message should not be null", message);

        Destination destination = message.getJMSDestination();

        assertNotNull("JMSDestination should not be null", destination);

        assertEquals("Incorrect Destination type", queue.getClass(), destination.getClass());
    }

    /**
     * Test a message sent to a topic comes back with JMSDestination topic
     *
     * @throws Exception
     */
    public void testTopic() throws Exception
    {

        Topic topic = _session.createTopic(getTestQueueName() + "Topic");

        MessageConsumer consumer = _session.createConsumer(topic);

        sendMessage(_session, topic, 1);

        _connection.start();

        Message message = consumer.receive(10000);

        assertNotNull("Message should not be null", message);

        Destination destination = message.getJMSDestination();

        assertNotNull("JMSDestination should not be null", destination);

        assertEquals("Incorrect Destination type", topic.getClass(), destination.getClass());
    }

    /**
     * Test a message sent to a topic then moved on the broker
     * comes back with JMSDestination queue.
     *
     * i.e. The client is not just setting the value to be the same as the
     * current consumer destination.
     *
     * This test can only be run against the Java broker as it uses JMX to move
     * messages between queues.
     *
     * @throws Exception
     */
    public void testMovedToQueue() throws Exception
    {
        // Setup JMXUtils
        JMXTestUtils jmxUtils = new JMXTestUtils(this, USER, USER);
        jmxUtils.setUp();
        // Open the JMX Connection
        jmxUtils.open();
        try
        {

            Queue queue = _session.createQueue(getTestQueueName());

            _session.createConsumer(queue).close();

            sendMessage(_session, queue, 1);

            Topic topic = _session.createTopic(getTestQueueName() + "Topic");

            MessageConsumer consumer = _session.createConsumer(topic);

            // Use Management to move message.

            ManagedQueue managedQueue = jmxUtils.
                    getManagedObject(ManagedQueue.class,
                                     jmxUtils.getQueueObjectName(getConnectionFactory().getVirtualPath().substring(1),
                                                                 getTestQueueName()));

            // Find the first message on the queue
            TabularData data = managedQueue.viewMessages(1L, 2L);

            Iterator values = data.values().iterator();
            assertTrue("No Messages found via JMX", values.hasNext());

            // Get its message ID
            Long msgID = (Long) ((CompositeDataSupport) values.next()).get("AMQ MessageId");

            // Start the connection and consume message that has been moved to the
            // queue
            _connection.start();

            Message message = consumer.receive(1000);

            //Validate we don't have a message on the queue before we start
            assertNull("Message should be null", message);

            // Move it to from the topic to the queue
            managedQueue.moveMessages(msgID, msgID, ((AMQTopic) topic).getQueueName());

            // Retrieve the newly moved message
            message = consumer.receive(1000);

            assertNotNull("Message should not be null", message);

            Destination destination = message.getJMSDestination();

            assertNotNull("JMSDestination should not be null", destination);

            assertEquals("Incorrect Destination type", queue.getClass(), destination.getClass());

        }
        finally
        {
            jmxUtils.close();
        }

    }

    /**
     * Test a message sent to a queue comes back with JMSDestination queue
     * when received via a message listener
     *
     * @throws Exception
     */
    public void testQueueAsync() throws Exception
    {

        Queue queue = _session.createQueue(getTestQueueName());

        MessageConsumer consumer = _session.createConsumer(queue);

        sendMessage(_session, queue, 1);

        _connection.start();

        _message = null;
        _receiveMessage = new CountDownLatch(1);

        consumer.setMessageListener(new MessageListener()
        {
            public void onMessage(Message message)
            {
                _message = message;
                _receiveMessage.countDown();
            }
        });

        assertTrue("Timed out waiting for message to be received ", _receiveMessage.await(1, TimeUnit.SECONDS));

        assertNotNull("Message should not be null", _message);

        Destination destination = _message.getJMSDestination();

        assertNotNull("JMSDestination should not be null", destination);

        assertEquals("Incorrect Destination type", queue.getClass(), destination.getClass());
    }

    /**
     * Test a message received without the JMS_QPID_DESTTYPE can be resent
     * and correctly have the property set.
     *
     * To do this we need to create a 0-10 connection and send a message
     * which is then received by a 0-8/9 client.
     *
     * @throws Exception
     */
    public void testReceiveResend() throws Exception
    {
        // Create a 0-10 Connection and send message
        setSystemProperty(ClientProperties.AMQP_VERSION, "0-10");

        Connection connection010 = getConnection();

        Session session010 = connection010.createSession(true, Session.SESSION_TRANSACTED);

        // Create queue for testing
        Queue queue = session010.createQueue(getTestQueueName());

        // Ensure queue exists
        session010.createConsumer(queue).close();

        sendMessage(session010, queue, 1);

        // Close the 010 connection
        connection010.close();

        // Create a 0-8 Connection and receive message
        setSystemProperty(ClientProperties.AMQP_VERSION, "0-8");

        Connection connection08 = getConnection();

        Session session08 = connection08.createSession(false, Session.AUTO_ACKNOWLEDGE);

        MessageConsumer consumer = session08.createConsumer(queue);

        connection08.start();

        Message message = consumer.receive(1000);

        assertNotNull("Didn't receive 0-10 message.", message);

        // Validate that JMS_QPID_DESTTYPE is not set
        try
        {
            message.getIntProperty(CustomJMSXProperty.JMS_QPID_DESTTYPE.toString());
            fail("JMS_QPID_DESTTYPE should not be set, so should throw NumberFormatException");
        }
        catch (NumberFormatException nfe)
        {

        }

        // Resend message back to queue and validate that
        // a) getJMSDestination works without the JMS_QPID_DESTTYPE
        // b) we can actually send without a BufferOverFlow.

        MessageProducer producer = session08.createProducer(queue);

        try
        {
            producer.send(message);
        }
        catch (BufferOverflowException bofe)
        {
            // Print the stack trace so we can validate where the execption occured.
            bofe.printStackTrace();
            fail("BufferOverflowException thrown during send");
        }

        message = consumer.receive(1000);

        assertNotNull("Didn't receive recent 0-8 message.", message);

        // Validate that JMS_QPID_DESTTYPE is not set
        assertEquals("JMS_QPID_DESTTYPE should be set to a Queue", AMQDestination.QUEUE_TYPE,
                     message.getIntProperty(CustomJMSXProperty.JMS_QPID_DESTTYPE.toString()));

    }

    /**
     * Send a message to a custom exchange and then verify
     * the message received has the proper destination set
     *
     * @throws Exception
     */
    public void testGetDestinationWithCustomExchange() throws Exception
    {

        AMQDestination dest = new AMQAnyDestination(new AMQShortString("my-exchange"),
                                                    new AMQShortString("direct"),
                                                    new AMQShortString("test"),
                                                    false,
                                                    false,
                                                    new AMQShortString("test"),
                                                    false,
                                                    new AMQShortString[]{new AMQShortString("test")});
        
        // to force the creation of my-exchange.
        sendMessage(_session, dest, 1);
        
        MessageProducer prod = _session.createProducer(dest);
        
        MessageConsumer consumer = _session.createConsumer(dest);
        
        _connection.start();

        sendMessage(_session, dest, 1);
        
        Message message = consumer.receive(10000);

        assertNotNull("Message should not be null", message);

        Destination destination = message.getJMSDestination();

        assertNotNull("JMSDestination should not be null", destination);

        assertEquals("Incorrect Destination name", "my-exchange", dest.getExchangeName().asString());
        assertEquals("Incorrect Destination type", "direct", dest.getExchangeClass().asString());
        assertEquals("Incorrect Routing Key", "test", dest.getRoutingKey().asString());
    }
}