KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > broker > policy > SimpleDispatchPolicyTest


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.policy;
19
20 import org.apache.activemq.broker.QueueSubscriptionTest;
21 import org.apache.activemq.broker.BrokerService;
22 import org.apache.activemq.broker.region.policy.FixedCountSubscriptionRecoveryPolicy;
23 import org.apache.activemq.broker.region.policy.PolicyEntry;
24 import org.apache.activemq.broker.region.policy.PolicyMap;
25 import org.apache.activemq.broker.region.policy.SimpleDispatchPolicy;
26 import org.apache.activemq.util.MessageIdList;
27
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30
31 public class SimpleDispatchPolicyTest extends QueueSubscriptionTest {
32
33     protected BrokerService createBroker() throws Exception JavaDoc {
34         BrokerService broker = super.createBroker();
35
36         PolicyEntry policy = new PolicyEntry();
37         policy.setDispatchPolicy(new SimpleDispatchPolicy());
38         policy.setSubscriptionRecoveryPolicy(new FixedCountSubscriptionRecoveryPolicy());
39         PolicyMap pMap = new PolicyMap();
40         pMap.setDefaultEntry(policy);
41
42         broker.setDestinationPolicy(pMap);
43
44         return broker;
45     }
46
47     public void testOneProducerTwoConsumersSmallMessagesLargePrefetch() throws Exception JavaDoc {
48         super.testOneProducerTwoConsumersSmallMessagesLargePrefetch();
49
50         // One consumer should have received all messages, and the rest none
51
assertOneConsumerReceivedAllMessages(messageCount);
52     }
53
54     public void testOneProducerTwoConsumersLargeMessagesLargePrefetch() throws Exception JavaDoc {
55         super.testOneProducerTwoConsumersLargeMessagesLargePrefetch();
56
57         // One consumer should have received all messages, and the rest none
58
assertOneConsumerReceivedAllMessages(messageCount);
59     }
60
61     public void assertOneConsumerReceivedAllMessages(int messageCount) throws Exception JavaDoc {
62         boolean found = false;
63         for (Iterator JavaDoc i=consumers.keySet().iterator(); i.hasNext();) {
64             MessageIdList messageIdList = (MessageIdList)consumers.get(i.next());
65             int count = messageIdList.getMessageCount();
66             if (count > 0) {
67                 if (found) {
68                     fail("No other consumers should have received any messages");
69                 } else {
70                     assertEquals("Consumer should have received all messages.", messageCount, count);
71                     found = true;
72                 }
73             }
74         }
75
76         if (!found) {
77             fail("At least one consumer should have received all messages");
78         }
79     }
80 }
81
Popular Tags