KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > broker > region > group > MessageGroupMapTest


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.broker.region.group;
19
20 import org.apache.activemq.command.ConnectionId;
21 import org.apache.activemq.command.ConsumerId;
22 import org.apache.activemq.command.SessionId;
23
24 import junit.framework.TestCase;
25
26 /**
27  *
28  * @version $Revision: 426384 $
29  */

30 public class MessageGroupMapTest extends TestCase {
31
32     protected MessageGroupMap map;
33     private ConsumerId consumer1;
34     private ConsumerId consumer2;
35     private ConsumerId consumer3;
36     private long idCounter;
37
38     public void testSingleConsumerForManyBucks() throws Exception JavaDoc {
39         assertGet("1", null);
40
41         map.put("1", consumer1);
42         assertGet("1", consumer1);
43         map.put("2", consumer1);
44         assertGet("2", consumer1);
45         map.put("3", consumer1);
46         assertGet("3", consumer1);
47
48         MessageGroupSet set = map.removeConsumer(consumer1);
49         assertContains(set, "1");
50         assertContains(set, "2");
51         assertContains(set, "3");
52         assertGet("1", null);
53         assertGet("2", null);
54         assertGet("3", null);
55     }
56
57     public void testManyConsumers() throws Exception JavaDoc {
58         assertGet("1", null);
59
60         map.put("1", consumer1);
61         assertGet("1", consumer1);
62         map.put("2", consumer2);
63         assertGet("2", consumer2);
64         map.put("3", consumer3);
65         assertGet("3", consumer3);
66
67         MessageGroupSet set = map.removeConsumer(consumer1);
68         assertContains(set, "1");
69
70         assertGet("1", null);
71         map.put("1", consumer2);
72         assertGet("1", consumer2);
73
74         set = map.removeConsumer(consumer2);
75         assertContains(set, "1");
76         assertContains(set, "2");
77     }
78
79     protected void setUp() throws Exception JavaDoc {
80         super.setUp();
81         map = createMessageGroupMap();
82         consumer1 = createConsumerId();
83         consumer2 = createConsumerId();
84         consumer3 = createConsumerId();
85     }
86
87     protected MessageGroupMap createMessageGroupMap() {
88         return new SimpleMessageGroupMap();
89     }
90
91     protected ConsumerId createConsumerId() {
92         ConnectionId connectionId = new ConnectionId("" + ++idCounter);
93         SessionId sessionId = new SessionId(connectionId, ++idCounter);
94         ConsumerId answer = new ConsumerId(sessionId, ++idCounter);
95         return answer;
96     }
97
98     protected void assertGet(String JavaDoc groupdId, ConsumerId expected) {
99         ConsumerId actual = map.get(groupdId);
100         assertEquals("Entry for groupId: " + groupdId, expected, actual);
101     }
102
103     protected void assertContains(MessageGroupSet set, String JavaDoc groupID) {
104         assertTrue("MessageGroup set: " + set + " does not contain groupID: " + groupID, set.contains(groupID));
105     }
106 }
107
Popular Tags