KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > client > ServiceMixClientTest


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.client;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.servicemix.client.ServiceMixClient;
22 import org.apache.servicemix.jbi.container.SpringJBIContainer;
23 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
24 import org.apache.servicemix.jbi.resolver.EndpointResolver;
25 import org.apache.servicemix.tck.Receiver;
26 import org.springframework.context.support.AbstractXmlApplicationContext;
27 import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
28
29 import javax.jbi.JBIException;
30 import javax.jbi.messaging.InOnly;
31 import javax.jbi.messaging.InOut;
32 import javax.jbi.messaging.NormalizedMessage;
33 import javax.xml.namespace.QName JavaDoc;
34 import javax.xml.transform.stream.StreamSource JavaDoc;
35
36 import java.io.StringReader JavaDoc;
37 import java.util.Arrays JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.Map JavaDoc;
40
41 import junit.framework.TestCase;
42
43 /**
44  * @version $Revision: 426415 $
45  */

46 public class ServiceMixClientTest extends TestCase {
47     private static final transient Log log = LogFactory.getLog(ServiceMixClientTest.class);
48
49     protected AbstractXmlApplicationContext context;
50     protected ServiceMixClient client;
51     protected Receiver receiver;
52
53     protected SourceTransformer transformer = new SourceTransformer();
54
55
56     // Send methods
57
//-------------------------------------------------------------------------
58
public void testSendUsingJbiAPIs() throws Exception JavaDoc {
59
60         InOnly exchange = client.createInOnlyExchange();
61
62         NormalizedMessage message = exchange.getInMessage();
63         message.setProperty("name", "James");
64         message.setContent(new StreamSource JavaDoc(new StringReader JavaDoc("<hello>world</hello>")));
65
66         QName JavaDoc service = new QName JavaDoc("http://servicemix.org/cheese", "receiver");
67         exchange.setService(service);
68         client.send(exchange);
69
70         receiver.getMessageList().assertMessagesReceived(1);
71     }
72
73     public void testSendUsingMapAndPOJOsByServiceName() throws Exception JavaDoc {
74
75         Map JavaDoc properties = new HashMap JavaDoc();
76         properties.put("name", "James");
77
78         QName JavaDoc service = new QName JavaDoc("http://servicemix.org/cheese", "receiver");
79         EndpointResolver resolver = client.createResolverForService(service);
80         client.send(resolver, null, properties, "<hello>world</hello>");
81
82         receiver.getMessageList().assertMessagesReceived(1);
83     }
84
85     public void testSendUsingMapAndPOJOsUsingContainerRouting() throws Exception JavaDoc {
86
87         ServiceMixClient clientNoRouting = (ServiceMixClient) context.getBean("clientWithRouting");
88
89         Map JavaDoc properties = new HashMap JavaDoc();
90         properties.put("name", "James");
91
92         clientNoRouting.send(null, null, properties, "<hello>world</hello>");
93
94         receiver.getMessageList().assertMessagesReceived(1);
95     }
96
97     public void testSendUsingMapAndPOJOsUsingContainerRoutingWithNoConfiguration() throws Exception JavaDoc {
98
99
100         try {
101             Map JavaDoc properties = new HashMap JavaDoc();
102             properties.put("name", "James");
103
104             client.send(null, null, properties, "<hello>world</hello>");
105             fail("Should have thrown an exception as we have not wired in any container routing information to this client");
106         }
107         catch (JBIException e) {
108             log.info("Caught expected exception as we have specified no endpoint resolver: " + e);
109             assertNotNull(e);
110         }
111     }
112
113
114     // Request methods
115
//-------------------------------------------------------------------------
116
public void testRequestUsingJbiAPIsByServiceName() throws Exception JavaDoc {
117         QName JavaDoc service = new QName JavaDoc("http://servicemix.org/cheese", "myService");
118         assertRequestUsingJBIAPIs(service);
119     }
120
121     public void testRequestUsingMapAndPOJOsByServiceName() throws Exception JavaDoc {
122         QName JavaDoc service = new QName JavaDoc("http://servicemix.org/cheese", "myService");
123         assertRequestUsingMapAndPOJOByServiceName(service);
124     }
125
126     public void testRequestUsingPOJOWithXStreamMarshaling() throws Exception JavaDoc {
127         QName JavaDoc service = new QName JavaDoc("http://servicemix.org/cheese", "myService");
128
129         ServiceMixClient client = (ServiceMixClient) context.getBean("clientWithXStream");
130
131         Map JavaDoc properties = new HashMap JavaDoc();
132         properties.put("name", "James");
133
134         EndpointResolver resolver = client.createResolverForService(service);
135         TestBean bean = new TestBean();
136         bean.setName("James");
137         bean.setLength(12);
138         bean.getAddresses().addAll(Arrays.asList(new String JavaDoc[] {"London", "LA"}));
139
140         Object JavaDoc response = client.request(resolver, null, properties, bean);
141
142         assertNotNull("Should have returned a non-null response!", response);
143
144         log.info("Received result: " + response);
145     }
146
147
148
149     // Implementation methods
150
//-------------------------------------------------------------------------
151
protected void assertRequestUsingJBIAPIs(QName JavaDoc service) throws Exception JavaDoc {
152         InOut exchange = client.createInOutExchange();
153
154         NormalizedMessage inMessage = exchange.getInMessage();
155         inMessage.setProperty("name", "James");
156         inMessage.setContent(new StreamSource JavaDoc(new StringReader JavaDoc("<hello>world</hello>")));
157
158         exchange.setService(service);
159         boolean answer = client.sendSync(exchange);
160         assertTrue("Should have successed", answer);
161
162         NormalizedMessage outMessage = exchange.getOutMessage();
163         assertNotNull("outMessage is null!", outMessage);
164
165         assertEquals("foo header", "hello", outMessage.getProperty("foo"));
166         log.info("Received result: " + outMessage.getContent());
167         log.info("XML is: " + transformer.toString(outMessage.getContent()));
168     }
169
170     protected void assertRequestUsingMapAndPOJOByServiceName(QName JavaDoc service) throws Exception JavaDoc {
171         Map JavaDoc properties = new HashMap JavaDoc();
172         properties.put("name", "James");
173
174         EndpointResolver resolver = client.createResolverForService(service);
175         Object JavaDoc response = client.request(resolver, null, properties, "<hello>world</hello>");
176
177         assertNotNull("Should have returned a non-null response!", response);
178         
179         log.info("Received result: " + response);
180     }
181
182     protected void setUp() throws Exception JavaDoc {
183         context = createBeanFactory();
184         //context.setXmlValidating(false);
185

186         client = getClient();
187
188         // TODO
189
//receiver = (Receiver) getBean("receiver");
190

191         SpringJBIContainer jbi = (SpringJBIContainer) getBean("jbi");
192         receiver = (Receiver) jbi.getBean("receiver");
193         assertNotNull("receiver not found in JBI container", receiver);
194     }
195     
196     protected ServiceMixClient getClient() throws Exception JavaDoc {
197         return (ServiceMixClient) getBean("client");
198     }
199
200     protected void tearDown() throws Exception JavaDoc {
201         super.tearDown();
202
203         if (context != null) {
204             context.close();
205         }
206     }
207
208     protected Object JavaDoc getBean(String JavaDoc name) {
209         Object JavaDoc answer = context.getBean(name);
210         assertNotNull("Could not find object in Spring for key: " + name, answer);
211         return answer;
212     }
213
214     protected AbstractXmlApplicationContext createBeanFactory() {
215         return new ClassPathXmlApplicationContext("org/apache/servicemix/client/example.xml");
216     }
217 }
218
Popular Tags