KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > nmr > SubscriptionTest


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

17 package org.apache.servicemix.jbi.nmr;
18
19 import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList;
20
21 import org.apache.servicemix.MessageExchangeListener;
22 import org.apache.servicemix.jbi.container.ActivationSpec;
23 import org.apache.servicemix.jbi.container.JBIContainer;
24 import org.apache.servicemix.jbi.container.SubscriptionSpec;
25 import org.apache.servicemix.jbi.nmr.flow.Flow;
26 import org.apache.servicemix.jbi.nmr.flow.FlowProvider;
27 import org.apache.servicemix.tck.Receiver;
28 import org.apache.servicemix.tck.ReceiverComponent;
29 import org.apache.servicemix.tck.SenderComponent;
30
31 import javax.jbi.messaging.MessageExchange;
32 import javax.jbi.messaging.MessagingException;
33
34 import java.util.List JavaDoc;
35
36 import junit.framework.TestCase;
37
38 public class SubscriptionTest extends TestCase {
39
40     public void testStNullAsync() throws Exception JavaDoc {
41         runTest("st", null, false);
42     }
43     
44     public void testStStAsync() throws Exception JavaDoc {
45         runTest("st", "st", false);
46     }
47     
48     public void testStSedaAsync() throws Exception JavaDoc {
49         runTest("st", "seda", false);
50     }
51     
52     public void testSedaNullAsync() throws Exception JavaDoc {
53         runTest("seda", null, false);
54     }
55     
56     public void testSedaStAsync() throws Exception JavaDoc {
57         runTest("seda", "st", false);
58     }
59     
60     public void testSedaSedaAsync() throws Exception JavaDoc {
61         runTest("seda", "seda", false);
62     }
63     
64     public void testStNullSync() throws Exception JavaDoc {
65         runTest("st", null, true);
66     }
67     
68     public void testStStSync() throws Exception JavaDoc {
69         runTest("st", "st", true);
70     }
71     
72     public void testStSedaSync() throws Exception JavaDoc {
73         runTest("st", "seda", true);
74     }
75     
76     public void testSedaNullSync() throws Exception JavaDoc {
77         runTest("seda", null, true);
78     }
79     
80     public void testSedaStSync() throws Exception JavaDoc {
81         runTest("seda", "st", true);
82     }
83     
84     public void testSedaSedaSync() throws Exception JavaDoc {
85         runTest("seda", "seda", true);
86     }
87     
88     private void runTest(String JavaDoc flowName, String JavaDoc subscriptionFlowName, boolean sync) throws Exception JavaDoc {
89         JBIContainer container = new JBIContainer();
90         try {
91             container.setEmbedded(true);
92             if (subscriptionFlowName != null && !subscriptionFlowName.equals(flowName)) {
93                 container.getDefaultBroker().setFlows(new Flow[] { FlowProvider.getFlow(flowName),
94                                                             FlowProvider.getFlow(subscriptionFlowName)});
95             } else {
96                 container.getDefaultBroker().setFlows(new Flow[] { FlowProvider.getFlow(flowName) });
97             }
98             if (subscriptionFlowName != null) {
99                 container.getDefaultBroker().getSubscriptionManager().setFlowName(subscriptionFlowName);
100             }
101             // TODO: check why the following line is enabled, there is
102
// a 5 seconds pause when Management stuff is initialized
103
//container.setCreateMBeanServer(true);
104
container.init();
105             container.start();
106             
107             SenderListener sender = new SenderListener();
108             ActivationSpec senderActivationSpec = new ActivationSpec("sender", sender);
109             senderActivationSpec.setFailIfNoDestinationEndpoint(false);
110             container.activateComponent(senderActivationSpec);
111             
112             Receiver receiver1 = new ReceiverComponent();
113             container.activateComponent(createReceiverAS("receiver1", receiver1));
114     
115             Receiver receiver2 = new ReceiverComponent();
116             container.activateComponent(createReceiverAS("receiver2", receiver2));
117             
118             sender.sendMessages(1, sync);
119             
120             Thread.sleep(100);
121             
122             assertEquals(1, receiver1.getMessageList().getMessageCount());
123             assertEquals(1, receiver2.getMessageList().getMessageCount());
124             assertEquals(0, sender.responses.size());
125         } finally {
126             container.shutDown();
127         }
128     }
129     
130     private ActivationSpec createReceiverAS(String JavaDoc id, Object JavaDoc component) {
131         ActivationSpec as = new ActivationSpec(id, component);
132         SubscriptionSpec ss = new SubscriptionSpec();
133         ss.setService(SenderComponent.SERVICE);
134         as.setEndpoint(id);
135         as.setSubscriptions(new SubscriptionSpec[] { ss });
136         return as;
137     }
138     
139     public static class SenderListener extends SenderComponent implements MessageExchangeListener {
140
141         public List responses = new CopyOnWriteArrayList();
142         
143         public void onMessageExchange(MessageExchange exchange) throws MessagingException {
144             responses.add(exchange);
145         }
146         
147     }
148     
149 }
150
Popular Tags