KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > test > message > NestedMapAndListPropertyTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.test.message;
19
20 import org.apache.activemq.test.JmsTopicSendReceiveWithTwoConnectionsAndEmbeddedBrokerTest;
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 import javax.jms.JMSException JavaDoc;
25 import javax.jms.Message JavaDoc;
26
27 import java.util.Arrays JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31
32 /**
33  * Tests that a Message can have nested Map and List properties attached.
34  *
35  * @version $Revision: 426366 $
36  */

37 public class NestedMapAndListPropertyTest extends JmsTopicSendReceiveWithTwoConnectionsAndEmbeddedBrokerTest {
38
39     private static final Log log = LogFactory.getLog(NestedMapAndListPropertyTest.class);
40
41     protected void assertMessageValid(int index, Message JavaDoc message) throws JMSException JavaDoc {
42         Object JavaDoc value = message.getObjectProperty("textField");
43         assertEquals("textField", data[index], value);
44
45         Map JavaDoc map = (Map JavaDoc) message.getObjectProperty("mapField");
46         assertNotNull(map);
47         assertEquals("mapField.a", "foo", map.get("a"));
48         assertEquals("mapField.b", new Integer JavaDoc(23), map.get("b"));
49         assertEquals("mapField.c", new Long JavaDoc(45), map.get("c"));
50
51         value = map.get("d");
52         assertTrue("mapField.d should be a Map", value instanceof Map JavaDoc);
53         map = (Map JavaDoc) value;
54
55         assertEquals("mapField.d.x", "abc", map.get("x"));
56         value = map.get("y");
57         assertTrue("mapField.d.y is a List", value instanceof List JavaDoc);
58         List JavaDoc list = (List JavaDoc) value;
59         log.debug("mapField.d.y: " + list);
60         assertEquals("listField.size", 3, list.size());
61
62         log.debug("Found map: " + map);
63
64         list = (List JavaDoc) message.getObjectProperty("listField");
65         log.debug("listField: " + list);
66         assertEquals("listField.size", 3, list.size());
67         assertEquals("listField[0]", "a", list.get(0));
68         assertEquals("listField[1]", "b", list.get(1));
69         assertEquals("listField[2]", "c", list.get(2));
70     }
71
72     protected Message JavaDoc createMessage(int index) throws JMSException JavaDoc {
73         Message JavaDoc answer = session.createMessage();
74
75         answer.setStringProperty("textField", data[index]);
76
77         Map JavaDoc grandChildMap = new HashMap JavaDoc();
78         grandChildMap.put("x", "abc");
79         grandChildMap.put("y", Arrays.asList(new Object JavaDoc[] { "a", "b", "c" }));
80
81         Map JavaDoc nestedMap = new HashMap JavaDoc();
82         nestedMap.put("a", "foo");
83         nestedMap.put("b", new Integer JavaDoc(23));
84         nestedMap.put("c", new Long JavaDoc(45));
85         nestedMap.put("d", grandChildMap);
86
87         answer.setObjectProperty("mapField", nestedMap);
88         answer.setObjectProperty("listField", Arrays.asList(new Object JavaDoc[] { "a", "b", "c" }));
89
90         return answer;
91     }
92
93 }
94
Popular Tags