KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > sca > ScaComponentTest


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.sca;
18
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.File JavaDoc;
21 import java.net.URL JavaDoc;
22
23 import javax.naming.InitialContext JavaDoc;
24 import javax.xml.bind.JAXBContext;
25 import javax.xml.namespace.QName JavaDoc;
26 import javax.xml.transform.Source JavaDoc;
27 import javax.xml.transform.dom.DOMSource JavaDoc;
28
29 import junit.framework.TestCase;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.apache.servicemix.client.DefaultServiceMixClient;
34 import org.apache.servicemix.client.ServiceMixClient;
35 import org.apache.servicemix.components.util.MockServiceComponent;
36 import org.apache.servicemix.jbi.container.ActivationSpec;
37 import org.apache.servicemix.jbi.container.JBIContainer;
38 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
39 import org.apache.servicemix.jbi.jaxp.StringSource;
40 import org.apache.servicemix.jbi.resolver.ServiceNameEndpointResolver;
41 import org.apache.servicemix.sca.bigbank.stockquote.StockQuoteResponse;
42 import org.w3c.dom.Node JavaDoc;
43
44 public class ScaComponentTest extends TestCase {
45
46     private static Log log = LogFactory.getLog(ScaComponentTest.class);
47     
48     protected JBIContainer container;
49     
50     protected void setUp() throws Exception JavaDoc {
51         container = new JBIContainer();
52         container.setUseMBeanServer(false);
53         container.setCreateMBeanServer(false);
54         container.setMonitorInstallationDirectory(false);
55         container.setNamingContext(new InitialContext JavaDoc());
56         container.setEmbedded(true);
57         container.init();
58     }
59     
60     protected void tearDown() throws Exception JavaDoc {
61         if (container != null) {
62             container.shutDown();
63         }
64     }
65     
66     public void testDeploy() throws Exception JavaDoc {
67         ScaComponent component = new ScaComponent();
68         container.activateComponent(component, "JSR181Component");
69
70         MockServiceComponent mock = new MockServiceComponent();
71         mock.setService(new QName JavaDoc("http://www.quickstockquote.com", "StockQuoteService"));
72         mock.setEndpoint("StockQuoteServiceJBI");
73         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
74         StockQuoteResponse r = new StockQuoteResponse();
75         r.setResult(8.23f);
76         JAXBContext.newInstance(StockQuoteResponse.class).createMarshaller().marshal(r, baos);
77         mock.setResponseXml(baos.toString());
78         ActivationSpec as = new ActivationSpec();
79         as.setComponent(mock);
80         container.activateComponent(as);
81         
82         // Start container
83
container.start();
84         
85         // Deploy SU
86
component.getServiceUnitManager().deploy("su", getServiceUnitPath("org/apache/servicemix/sca/bigbank"));
87         component.getServiceUnitManager().init("su", getServiceUnitPath("org/apache/servicemix/sca/bigbank"));
88         component.getServiceUnitManager().start("su");
89         
90         ServiceMixClient client = new DefaultServiceMixClient(container);
91         Source JavaDoc req = new StringSource("<AccountReportRequest><CustomerID>id</CustomerID></AccountReportRequest>");
92         Object JavaDoc rep = client.request(new ServiceNameEndpointResolver(
93                                                 new QName JavaDoc("http://sca.servicemix.apache.org/Bigbank/Account", "AccountService")),
94                                              null, null, req);
95         if (rep instanceof Node JavaDoc) {
96             rep = new DOMSource JavaDoc((Node JavaDoc) rep);
97         }
98         log.info(new SourceTransformer().toString((Source JavaDoc) rep));
99     }
100      
101     protected String JavaDoc getServiceUnitPath(String JavaDoc name) {
102         URL JavaDoc url = getClass().getClassLoader().getResource(name + "/sca.module");
103         File JavaDoc path = new File JavaDoc(url.getFile());
104         path = path.getParentFile();
105         return path.getAbsolutePath();
106     }
107     
108 }
109
Popular Tags