summaryrefslogtreecommitdiff
path: root/qpid/java/systests/src/main/java/org/apache/qpid/test/unit/client/message/ObjectMessageTest.java
blob: 728ef85bd2a13b9dcd09cc46c26db940dca1c246 (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
/*
 *
 * 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.message;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.qpid.client.AMQConnection;
import org.apache.qpid.client.AMQDestination;
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.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

public class ObjectMessageTest extends QpidBrokerTestCase implements MessageListener
{
    private static final Logger _logger = LoggerFactory.getLogger(ObjectMessageTest.class);

    private AMQConnection connection;
    private AMQDestination destination;
    private AMQSession session;
    private MessageProducer producer;
    private Serializable[] data;
    private volatile boolean waiting;
    private int received;
    private final ArrayList items = new ArrayList();

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

    protected void setUp() throws Exception
    {
        super.setUp();
        connection =  (AMQConnection) getConnection("guest", "guest");
        destination = new AMQQueue(connection, randomize("LatencyTest"), true);
        session = (AMQSession) connection.createSession(false, AMQSession.NO_ACKNOWLEDGE);

        // set up a consumer
        session.createConsumer(destination).setMessageListener(this);
        connection.start();

        // create a publisher
        producer = session.createProducer(destination, false, false);
        A a1 = new A(1, "A");
        A a2 = new A(2, "a");
        B b = new B(1, "B");
        C c = new C();
        c.put("A1", a1);
        c.put("a2", a2);
        c.put("B", b);
        c.put("String", "String");

        data = new Serializable[] { a1, a2, b, c, "Hello World!", new Integer(1001) };
    }

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

    public ObjectMessageTest()
    { }

    ObjectMessageTest(String broker) throws Exception
    {
        _broker = broker;
    }

    public void testSendAndReceive() throws Exception
    {
        try
        {
            send();
            waitUntilReceived(data.length);
            check();
            _logger.info("All " + data.length + " items matched.");
        }
        catch (Exception e)
        {
            e.printStackTrace();
            fail("This Test should succeed but failed due to: " + e);
        }
    }

    public void testSetObjectPropertyForString() throws Exception
    {
        String testStringProperty = "TestStringProperty";
        ObjectMessage msg = session.createObjectMessage(data[0]);
        msg.setObjectProperty("TestStringProperty", testStringProperty);
        assertEquals(testStringProperty, msg.getObjectProperty("TestStringProperty"));
    }

    public void testSetObjectPropertyForBoolean() throws Exception
    {
        ObjectMessage msg = session.createObjectMessage(data[0]);
        msg.setObjectProperty("TestBooleanProperty", Boolean.TRUE);
        assertEquals(Boolean.TRUE, msg.getObjectProperty("TestBooleanProperty"));
    }

    public void testSetObjectPropertyForByte() throws Exception
    {
        ObjectMessage msg = session.createObjectMessage(data[0]);
        msg.setObjectProperty("TestByteProperty", Byte.MAX_VALUE);
        assertEquals(Byte.MAX_VALUE, msg.getObjectProperty("TestByteProperty"));
    }

    public void testSetObjectPropertyForShort() throws Exception
    {
        ObjectMessage msg = session.createObjectMessage(data[0]);
        msg.setObjectProperty("TestShortProperty", Short.MAX_VALUE);
        assertEquals(Short.MAX_VALUE, msg.getObjectProperty("TestShortProperty"));
    }

    public void testSetObjectPropertyForInteger() throws Exception
    {
        ObjectMessage msg = session.createObjectMessage(data[0]);
        msg.setObjectProperty("TestIntegerProperty", Integer.MAX_VALUE);
        assertEquals(Integer.MAX_VALUE, msg.getObjectProperty("TestIntegerProperty"));
    }

    public void testSetObjectPropertyForDouble() throws Exception
    {
        ObjectMessage msg = session.createObjectMessage(data[0]);
        msg.setObjectProperty("TestDoubleProperty", Double.MAX_VALUE);
        assertEquals(Double.MAX_VALUE, msg.getObjectProperty("TestDoubleProperty"));
    }

    public void testSetObjectPropertyForFloat() throws Exception
    {
        ObjectMessage msg = session.createObjectMessage(data[0]);
        msg.setObjectProperty("TestFloatProperty", Float.MAX_VALUE);
        assertEquals(Float.MAX_VALUE, msg.getObjectProperty("TestFloatProperty"));
    }

    public void testSetObjectPropertyForByteArray() throws Exception
    {
        byte[] array = { 1, 2, 3, 4, 5 };
        ObjectMessage msg = session.createObjectMessage(data[0]);
        msg.setObjectProperty("TestByteArrayProperty", array);
        assertTrue(Arrays.equals(array, (byte[]) msg.getObjectProperty("TestByteArrayProperty")));
    }

    public void testSetObjectForNull() throws Exception
    {
        ObjectMessage msg = session.createObjectMessage();
        msg.setObject(null);
        assertNull(msg.getObject());
    }

    private void send() throws Exception
    {
        for (int i = 0; i < data.length; i++)
        {
            ObjectMessage msg;
            if ((i % 2) == 0)
            {
                msg = session.createObjectMessage(data[i]);
            }
            else
            {
                msg = session.createObjectMessage();
                msg.setObject(data[i]);
            }

            producer.send(msg);
        }
    }

    public void check() throws Exception
    {
        Object[] actual = (Object[]) items.toArray();
        if (actual.length != data.length)
        {
            throw new Exception("Expected " + data.length + " objects, got " + actual.length);
        }

        for (int i = 0; i < data.length; i++)
        {
            if (actual[i] instanceof Exception)
            {
                throw new Exception("Error on receive of " + data[i], ((Exception) actual[i]));
            }

            if (actual[i] == null)
            {
                throw new Exception("Expected " + data[i] + " got null");
            }

            if (!data[i].equals(actual[i]))
            {
                throw new Exception("Expected " + data[i] + " got " + actual[i]);
            }
        }
    }

    private void close() throws Exception
    {
        session.close();
        connection.close();
    }

    private synchronized void waitUntilReceived(int count) throws InterruptedException
    {
        waiting = true;
        while (received < count)
        {
            wait();
        }

        waiting = false;
    }

    public void onMessage(Message message)
    {

        try
        {
            if (message instanceof ObjectMessage)
            {
                items.add(((ObjectMessage) message).getObject());
            }
            else
            {
                _logger.error("ERROR: Got " + message.getClass().getName() + " not ObjectMessage");
                items.add(message);
            }
        }
        catch (JMSException e)
        {
            e.printStackTrace();
            items.add(e);
        }

        synchronized (this)
        {
            received++;
            notify();
        }
    }

    public static void main(String[] argv) throws Exception
    {
        String broker = (argv.length > 0) ? argv[0] : "vm://:1";
        if ("-help".equals(broker))
        {
            System.out.println("Usage: <broker>");
        }

        new ObjectMessageTest(broker).testSendAndReceive();
    }

    private static class A implements Serializable
    {
        private String sValue;
        private int iValue;

        A(int i, String s)
        {
            sValue = s;
            iValue = i;
        }

        public int hashCode()
        {
            return iValue;
        }

        public boolean equals(Object o)
        {
            return (o instanceof A) && equals((A) o);
        }

        protected boolean equals(A a)
        {
            return areEqual(a.sValue, sValue) && (a.iValue == iValue);
        }
    }

    private static class B extends A
    {
        private long time;

        B(int i, String s)
        {
            super(i, s);
            time = System.currentTimeMillis();
        }

        protected boolean equals(A a)
        {
            return super.equals(a) && (a instanceof B) && (time == ((B) a).time);
        }
    }

    private static class C extends HashMap implements Serializable
    { }

    private static boolean areEqual(Object a, Object b)
    {
        return (a == null) ? (b == null) : a.equals(b);
    }

    private static String randomize(String in)
    {
        return in + System.currentTimeMillis();
    }
}