KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.servicemix.jbi.container.ActivationSpec;
20 import org.apache.servicemix.jbi.container.JBIContainer;
21 import org.apache.servicemix.jbi.container.SubscriptionSpec;
22 import org.apache.servicemix.jbi.nmr.flow.Flow;
23 import org.apache.servicemix.jbi.nmr.flow.FlowProvider;
24 import org.apache.servicemix.tck.ReceiverComponent;
25 import org.apache.servicemix.tck.Sender;
26 import org.apache.servicemix.tck.SenderComponent;
27
28 import javax.jbi.messaging.MessageExchange;
29 import javax.jbi.messaging.MessagingException;
30 import javax.jbi.messaging.NormalizedMessage;
31
32 import junit.framework.TestCase;
33
34 public class SubscriptionPropertyCopyTest extends TestCase {
35
36     public void testStNull() throws Exception JavaDoc {
37         runTest("st", null);
38     }
39     
40     public void testStSt() throws Exception JavaDoc {
41         runTest("st", "st");
42     }
43     
44     public void testStSeda() throws Exception JavaDoc {
45         runTest("st", "seda");
46     }
47     
48     public void testSedaNull() throws Exception JavaDoc {
49         runTest("seda", null);
50     }
51     
52     public void testSedaSt() throws Exception JavaDoc {
53         runTest("seda", "st");
54     }
55     
56     public void testSedaSeda() throws Exception JavaDoc {
57         runTest("seda", "seda");
58     }
59     
60     private void runTest(String JavaDoc flowName, String JavaDoc subscriptionFlowName) throws Exception JavaDoc {
61         JBIContainer container = new JBIContainer();
62         try {
63             if (subscriptionFlowName != null && !subscriptionFlowName.equals(flowName)) {
64                 container.getDefaultBroker().setFlows(new Flow[] { FlowProvider.getFlow(flowName),
65                                                                    FlowProvider.getFlow(subscriptionFlowName)});
66             } else {
67                 container.getDefaultBroker().setFlows(new Flow[] { FlowProvider.getFlow(flowName) });
68             }
69             if (subscriptionFlowName != null) {
70                 container.getDefaultBroker().getSubscriptionManager().setFlowName(subscriptionFlowName);
71             }
72             container.setEmbedded(true);
73             container.init();
74             container.start();
75             
76             Sender sender = new SenderComponent();
77             ActivationSpec senderActivationSpec = new ActivationSpec("sender", sender);
78             senderActivationSpec.setFailIfNoDestinationEndpoint(false);
79             container.activateComponent(senderActivationSpec);
80             
81             ReceiverListener receiver1 = new ReceiverListener();
82             container.activateComponent(createReceiverAS("receiver1", receiver1));
83     
84             ReceiverListener receiver2 = new ReceiverListener();
85             container.activateComponent(createReceiverAS("receiver2", receiver2));
86             
87             sender.sendMessages(1);
88             
89             Thread.sleep(100);
90             
91             assertFalse(receiver1.isPropertySetOnExchange());
92             assertFalse(receiver2.isPropertySetOnExchange());
93             assertFalse(receiver1.isPropertySetOnMessage());
94             assertFalse(receiver2.isPropertySetOnMessage());
95         } finally {
96             container.shutDown();
97         }
98     }
99     
100     private ActivationSpec createReceiverAS(String JavaDoc id, Object JavaDoc component) {
101         ActivationSpec as = new ActivationSpec(id, component);
102         SubscriptionSpec ss = new SubscriptionSpec();
103         ss.setService(SenderComponent.SERVICE);
104         as.setEndpoint(id);
105         as.setSubscriptions(new SubscriptionSpec[] { ss });
106         return as;
107     }
108     
109     public static class ReceiverListener extends ReceiverComponent {
110         private boolean propertySetOnExchange;
111         private boolean propertySetOnMessage;
112         
113         public void onMessageExchange(MessageExchange exchange) throws MessagingException {
114             propertySetOnExchange = (exchange.getProperty("testProperty") != null);
115             exchange.setProperty("testProperty", "foo");
116             NormalizedMessage in = exchange.getMessage("in");
117             propertySetOnMessage = (in.getProperty("testProperty") != null);
118             in.setProperty("testProperty", "foo");
119         }
120         
121         public boolean isPropertySetOnExchange() {
122             return propertySetOnExchange;
123         }
124         
125         public boolean isPropertySetOnMessage() {
126             return propertySetOnMessage;
127         }
128     }
129 }
130
Popular Tags