KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jsr181 > Jsr181ComponentTest


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.jsr181;
18
19 import java.io.File JavaDoc;
20 import java.net.URL JavaDoc;
21
22 import javax.jbi.messaging.ExchangeStatus;
23 import javax.jbi.messaging.InOut;
24 import javax.naming.InitialContext JavaDoc;
25 import javax.xml.namespace.QName 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.client.DefaultServiceMixClient;
32 import org.apache.servicemix.jbi.container.JBIContainer;
33 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
34 import org.apache.servicemix.jbi.jaxp.StringSource;
35 import org.apache.servicemix.jsr181.Jsr181Component;
36
37 import test.EchoService;
38
39 public class Jsr181ComponentTest extends TestCase {
40
41     private static Log logger = LogFactory.getLog(Jsr181ComponentTest.class);
42     
43     protected JBIContainer container;
44     
45     protected void setUp() throws Exception JavaDoc {
46         container = new JBIContainer();
47         container.setUseMBeanServer(false);
48         container.setCreateMBeanServer(false);
49         container.setMonitorInstallationDirectory(false);
50         container.setNamingContext(new InitialContext JavaDoc());
51         container.setEmbedded(true);
52         container.init();
53     }
54     
55     protected void tearDown() throws Exception JavaDoc {
56         if (container != null) {
57             container.shutDown();
58         }
59     }
60     
61     public void testCommonsAnnotations() throws Exception JavaDoc {
62         Jsr181Component component = new Jsr181Component();
63         container.activateComponent(component, "JSR181Component");
64
65         // Start container
66
container.start();
67         
68         // Deploy SU
69
component.getServiceUnitManager().deploy("su", getServiceUnitPath("good1"));
70         component.getServiceUnitManager().init("su", getServiceUnitPath("good1"));
71         component.getServiceUnitManager().start("su");
72         
73         assertNotNull(EchoService.instance);
74         assertNotNull(EchoService.instance.getContext());
75         
76         // Call it
77
DefaultServiceMixClient client = new DefaultServiceMixClient(container);
78         InOut me = client.createInOutExchange();
79         me.setInterfaceName(new QName JavaDoc("http://test", "EchoServicePortType"));
80         me.getInMessage().setContent(new StringSource("<echo xmlns='http://test'><in0>world</in0></echo>"));
81         client.sendSync(me);
82         if (me.getStatus() == ExchangeStatus.ERROR) {
83             if (me.getFault() != null) {
84                 fail("Received fault: " + new SourceTransformer().toString(me.getFault().getContent()));
85             } else if (me.getError() != null) {
86                 throw me.getError();
87             } else {
88                 fail("Received ERROR status");
89             }
90         } else {
91             logger.info(new SourceTransformer().toString(me.getOutMessage().getContent()));
92         }
93     }
94     
95     
96     public void testWithoutAnnotations() throws Exception JavaDoc {
97         Jsr181Component component = new Jsr181Component();
98         container.activateComponent(component, "JSR181Component");
99
100         // Start container
101
container.start();
102         
103         // Deploy SU
104
component.getServiceUnitManager().deploy("good2", getServiceUnitPath("good2"));
105         component.getServiceUnitManager().init("good2", getServiceUnitPath("good2"));
106         component.getServiceUnitManager().start("good2");
107         
108         assertNotNull(EchoService.instance);
109         assertNotNull(EchoService.instance.getContext());
110         
111         // Call it
112
DefaultServiceMixClient client = new DefaultServiceMixClient(container);
113         InOut me = client.createInOutExchange();
114         me.setInterfaceName(new QName JavaDoc("http://test", "EchoService2PortType"));
115         me.getInMessage().setContent(new StringSource("<echo xmlns='http://test'><in0>world</in0></echo>"));
116         client.sendSync(me);
117         if (me.getStatus() == ExchangeStatus.ERROR) {
118             if (me.getFault() != null) {
119                 fail("Received fault: " + new SourceTransformer().toString(me.getFault().getContent()));
120             } else if (me.getError() != null) {
121                 throw me.getError();
122             } else {
123                 fail("Received ERROR status");
124             }
125         } else {
126             logger.info(new SourceTransformer().toString(me.getOutMessage().getContent()));
127         }
128     }
129     
130     public void testDeployUndeploy() throws Exception JavaDoc {
131         Jsr181Component component = new Jsr181Component();
132         container.activateComponent(component, "JSR181Component");
133         component.getServiceUnitManager().deploy("d/u", getServiceUnitPath("good1"));
134         component.getServiceUnitManager().shutDown("d/u");
135         component.getServiceUnitManager().undeploy("d/u", getServiceUnitPath("good1"));
136         component.getServiceUnitManager().deploy("d/u", getServiceUnitPath("good1"));
137         component.getServiceUnitManager().start("d/u");
138         component.getServiceUnitManager().stop("d/u");
139         component.getServiceUnitManager().shutDown("d/u");
140     }
141     
142     public void testNoPojoNoClass() throws Exception JavaDoc {
143         Jsr181Component component = new Jsr181Component();
144         container.activateComponent(component, "JSR181Component");
145         try {
146             component.getServiceUnitManager().deploy("bad1", getServiceUnitPath("bad1"));
147             fail("Expected an exception");
148         } catch (Exception JavaDoc e) {
149             // ok
150
logger.info(e.getMessage());
151         }
152     }
153     
154     public void testNoEndpoints() throws Exception JavaDoc {
155         Jsr181Component component = new Jsr181Component();
156         container.activateComponent(component, "JSR181Component");
157         try {
158             component.getServiceUnitManager().deploy("bad2", getServiceUnitPath("bad2"));
159             fail("Expected an exception");
160         } catch (Exception JavaDoc e) {
161             // ok
162
logger.info(e.getMessage());
163         }
164     }
165     
166     public void testDuplicates() throws Exception JavaDoc {
167         Jsr181Component component = new Jsr181Component();
168         container.activateComponent(component, "JSR181Component");
169         try {
170             component.getServiceUnitManager().deploy("bad3", getServiceUnitPath("bad3"));
171             fail("Expected an exception");
172         } catch (Exception JavaDoc e) {
173             // ok
174
logger.info(e.getMessage());
175         }
176     }
177     
178     public void testNotJsr181Endpoint() throws Exception JavaDoc {
179         Jsr181Component component = new Jsr181Component();
180         container.activateComponent(component, "JSR181Component");
181         try {
182             component.getServiceUnitManager().deploy("bad4", getServiceUnitPath("bad4"));
183             fail("Expected an exception");
184         } catch (Exception JavaDoc e) {
185             // ok
186
logger.info(e.getMessage());
187         }
188     }
189     
190     public void testUnrecognizedTypeMapping() throws Exception JavaDoc {
191         Jsr181Component component = new Jsr181Component();
192         container.activateComponent(component, "JSR181Component");
193         try {
194             component.getServiceUnitManager().deploy("bad5", getServiceUnitPath("bad5"));
195             fail("Expected an exception");
196         } catch (Exception JavaDoc e) {
197             // ok
198
logger.info(e.getMessage());
199         }
200     }
201     
202     public void testUnrecognizedAnnotations() throws Exception JavaDoc {
203         Jsr181Component component = new Jsr181Component();
204         container.activateComponent(component, "JSR181Component");
205         try {
206             component.getServiceUnitManager().deploy("bad6", getServiceUnitPath("bad6"));
207             fail("Expected an exception");
208         } catch (Exception JavaDoc e) {
209             // ok
210
logger.info(e.getMessage());
211         }
212     }
213     
214     protected String JavaDoc getServiceUnitPath(String JavaDoc name) {
215         URL JavaDoc url = getClass().getClassLoader().getResource(name + "/xbean.xml");
216         File JavaDoc path = new File JavaDoc(url.getFile());
217         path = path.getParentFile();
218         return path.getAbsolutePath();
219     }
220     
221 }
222
Popular Tags