KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > eip > ContentBasedRouterTest


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.eip;
18
19 import javax.jbi.messaging.ExchangeStatus;
20 import javax.jbi.messaging.InOnly;
21 import javax.jbi.messaging.InOut;
22 import javax.xml.namespace.QName JavaDoc;
23
24 import org.apache.servicemix.eip.patterns.ContentBasedRouter;
25 import org.apache.servicemix.eip.support.RoutingRule;
26 import org.apache.servicemix.eip.support.XPathPredicate;
27 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
28 import org.apache.servicemix.tck.ReceiverComponent;
29 import org.w3c.dom.Node JavaDoc;
30
31 public class ContentBasedRouterTest extends AbstractEIPTest {
32
33     private ContentBasedRouter router;
34
35     protected void setUp() throws Exception JavaDoc {
36         super.setUp();
37
38         router = new ContentBasedRouter();
39         router.setRules(new RoutingRule[] {
40                 new RoutingRule(
41                         new XPathPredicate("/hello/@id = '1'"),
42                         createServiceExchangeTarget(new QName JavaDoc("target1"))),
43                 new RoutingRule(
44                         new XPathPredicate("/hello/@id = '2'"),
45                         createServiceExchangeTarget(new QName JavaDoc("target2"))),
46                 new RoutingRule(
47                         null,
48                         createServiceExchangeTarget(new QName JavaDoc("target3")))
49         });
50         configurePattern(router);
51         activateComponent(router, "router");
52     }
53     
54     public void testInOnly() throws Exception JavaDoc {
55         ReceiverComponent rec1 = activateReceiver("target1");
56         ReceiverComponent rec2 = activateReceiver("target2");
57         ReceiverComponent rec3 = activateReceiver("target3");
58         
59         InOnly me = client.createInOnlyExchange();
60         me.setService(new QName JavaDoc("router"));
61         me.getInMessage().setContent(createSource("<hello id='1' />"));
62         client.sendSync(me);
63         assertEquals(ExchangeStatus.DONE, me.getStatus());
64         
65         rec1.getMessageList().assertMessagesReceived(1);
66         rec2.getMessageList().assertMessagesReceived(0);
67         rec3.getMessageList().assertMessagesReceived(0);
68
69         rec1.getMessageList().flushMessages();
70         rec2.getMessageList().flushMessages();
71         rec3.getMessageList().flushMessages();
72
73         me = client.createInOnlyExchange();
74         me.setService(new QName JavaDoc("router"));
75         me.getInMessage().setContent(createSource("<hello id='2' />"));
76         client.sendSync(me);
77         assertEquals(ExchangeStatus.DONE, me.getStatus());
78         
79         rec1.getMessageList().assertMessagesReceived(0);
80         rec2.getMessageList().assertMessagesReceived(1);
81         rec3.getMessageList().assertMessagesReceived(0);
82
83         rec1.getMessageList().flushMessages();
84         rec2.getMessageList().flushMessages();
85         rec3.getMessageList().flushMessages();
86
87         me = client.createInOnlyExchange();
88         me.setService(new QName JavaDoc("router"));
89         me.getInMessage().setContent(createSource("<hello id='3' />"));
90         client.sendSync(me);
91         assertEquals(ExchangeStatus.DONE, me.getStatus());
92         
93         rec1.getMessageList().assertMessagesReceived(0);
94         rec2.getMessageList().assertMessagesReceived(0);
95         rec3.getMessageList().assertMessagesReceived(1);
96     }
97
98     public void testInOut() throws Exception JavaDoc {
99         activateComponent(new ReturnMockComponent("<from1/>"), "target1");
100         activateComponent(new ReturnMockComponent("<from2/>"), "target2");
101         activateComponent(new ReturnMockComponent("<from3/>"), "target3");
102         
103         InOut me = client.createInOutExchange();
104         me.setService(new QName JavaDoc("router"));
105         me.getInMessage().setContent(createSource("<hello id='1' />"));
106         client.sendSync(me);
107         assertEquals(ExchangeStatus.ACTIVE, me.getStatus());
108         Node JavaDoc node = new SourceTransformer().toDOMNode(me.getOutMessage());
109         assertEquals("from1", node.getFirstChild().getNodeName());
110         client.done(me);
111         
112         me = client.createInOutExchange();
113         me.setService(new QName JavaDoc("router"));
114         me.getInMessage().setContent(createSource("<hello id='2' />"));
115         client.sendSync(me);
116         assertEquals(ExchangeStatus.ACTIVE, me.getStatus());
117         node = new SourceTransformer().toDOMNode(me.getOutMessage());
118         assertEquals("from2", node.getFirstChild().getNodeName());
119         client.done(me);
120         
121         me = client.createInOutExchange();
122         me.setService(new QName JavaDoc("router"));
123         me.getInMessage().setContent(createSource("<hello id='3' />"));
124         client.sendSync(me);
125         assertEquals(ExchangeStatus.ACTIVE, me.getStatus());
126         node = new SourceTransformer().toDOMNode(me.getOutMessage());
127         assertEquals("from3", node.getFirstChild().getNodeName());
128         client.done(me);
129     }
130
131 }
132
Popular Tags