KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.StringReader JavaDoc;
20 import java.util.Arrays JavaDoc;
21
22 import javax.jbi.messaging.InOnly;
23 import javax.jbi.messaging.InOut;
24 import javax.jbi.messaging.NormalizedMessage;
25 import javax.xml.transform.stream.StreamSource JavaDoc;
26
27 import junit.framework.TestCase;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.servicemix.jbi.container.SpringJBIContainer;
32 import org.apache.servicemix.tck.MessageList;
33 import org.apache.servicemix.tck.Receiver;
34 import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
35 import org.springframework.context.support.AbstractXmlApplicationContext;
36
37 /**
38  * @version $Revision: 429658 $
39  */

40 public class ClientDestinationTest extends TestCase {
41     private static final transient Log log = LogFactory.getLog(ClientDestinationTest.class);
42
43     protected AbstractXmlApplicationContext context;
44     protected ServiceMixClient client;
45     protected MessageList messageList = new MessageList();
46
47     public void testInOnlyMessage() throws Exception JavaDoc {
48         // START SNIPPET: message
49
Destination destination = client.createDestination("service:http://servicemix.org/cheese/receiver");
50         Message message = destination.createInOnlyMessage();
51         message.setProperty("name", "James");
52         message.setBody("<hello>world</hello>");
53         
54         client.send(message);
55         // END SNIPPET: message
56
messageList.assertMessagesReceived(1);
57     }
58     
59     public void testInOnlyExchange() throws Exception JavaDoc {
60         // START SNIPPET: inOnly
61
Destination destination = client.createDestination("service:http://servicemix.org/cheese/receiver");
62         InOnly exchange = destination.createInOnlyExchange();
63
64         NormalizedMessage message = exchange.getInMessage();
65         message.setProperty("name", "James");
66         message.setContent(new StreamSource JavaDoc(new StringReader JavaDoc("<hello>world</hello>")));
67
68         client.send(exchange);
69         // END SNIPPET: inOnly
70

71         messageList.assertMessagesReceived(1);
72     }
73     
74     public void testInOutExchange() throws Exception JavaDoc {
75         // START SNIPPET: inOut
76
Destination destination = client.createDestination("service:http://servicemix.org/cheese/myService");
77         InOut exchange = destination.createInOutExchange();
78         
79         NormalizedMessage request = exchange.getInMessage();
80         request.setProperty("name", "James");
81         request.setContent(new StreamSource JavaDoc(new StringReader JavaDoc("<hello>world</hello>")));
82
83         client.sendSync(exchange);
84         
85         NormalizedMessage response = exchange.getOutMessage();
86         // END SNIPPET: inOut
87

88         assertNotNull("Should have returned a non-null response!", response);
89
90         log.info("Received result: " + response);
91     }
92
93
94     public void testInOnlyMessageUsingPOJOWithXStreamMarshaling() throws Exception JavaDoc {
95         TestBean bean = new TestBean();
96         bean.setName("James");
97         bean.setLength(12);
98         bean.getAddresses().addAll(Arrays.asList(new String JavaDoc[] { "London", "LA" }));
99
100         Destination destination = client.createDestination("service:http://servicemix.org/cheese/receiver");
101         Message message = destination.createInOnlyMessage();
102         message.setProperty("name", "James");
103         message.setBody(bean);
104
105         client.send(message);
106
107         messageList.assertMessagesReceived(1);
108     }
109
110     protected void setUp() throws Exception JavaDoc {
111         context = createBeanFactory();
112         client = (ServiceMixClient) getBean("client");
113         messageList = createMessageList();
114     }
115
116     /*
117      * protected MessageList createMessageList() throws Exception { MessageList
118      * answer = new MessageList(); Destination consumer =
119      * client.createEndpoint(destinationUri, messageList); return answer; }
120      */

121
122     protected MessageList createMessageList() throws Exception JavaDoc {
123         SpringJBIContainer jbi = (SpringJBIContainer) getBean("jbi");
124         Receiver receiver = (Receiver) jbi.getBean("receiver");
125         assertNotNull("receiver not found in JBI container", receiver);
126         return receiver.getMessageList();
127     }
128
129     protected void tearDown() throws Exception JavaDoc {
130         super.tearDown();
131
132         if (context != null) {
133             context.close();
134         }
135     }
136
137     protected Object JavaDoc getBean(String JavaDoc name) {
138         Object JavaDoc answer = context.getBean(name);
139         assertNotNull("Could not find object in Spring for key: " + name, answer);
140         return answer;
141     }
142
143     protected AbstractXmlApplicationContext createBeanFactory() {
144         return new ClassPathXmlApplicationContext("org/apache/servicemix/client/example.xml");
145     }
146 }
147
Popular Tags