1 17 package org.apache.servicemix.sca; 18 19 import java.io.ByteArrayOutputStream ; 20 import java.io.File ; 21 import java.net.URL ; 22 23 import javax.naming.InitialContext ; 24 import javax.xml.bind.JAXBContext; 25 import javax.xml.namespace.QName ; 26 import javax.xml.transform.Source ; 27 import javax.xml.transform.dom.DOMSource ; 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 ; 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 { 51 container = new JBIContainer(); 52 container.setUseMBeanServer(false); 53 container.setCreateMBeanServer(false); 54 container.setMonitorInstallationDirectory(false); 55 container.setNamingContext(new InitialContext ()); 56 container.setEmbedded(true); 57 container.init(); 58 } 59 60 protected void tearDown() throws Exception { 61 if (container != null) { 62 container.shutDown(); 63 } 64 } 65 66 public void testDeploy() throws Exception { 67 ScaComponent component = new ScaComponent(); 68 container.activateComponent(component, "JSR181Component"); 69 70 MockServiceComponent mock = new MockServiceComponent(); 71 mock.setService(new QName ("http://www.quickstockquote.com", "StockQuoteService")); 72 mock.setEndpoint("StockQuoteServiceJBI"); 73 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 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 container.start(); 84 85 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 req = new StringSource("<AccountReportRequest><CustomerID>id</CustomerID></AccountReportRequest>"); 92 Object rep = client.request(new ServiceNameEndpointResolver( 93 new QName ("http://sca.servicemix.apache.org/Bigbank/Account", "AccountService")), 94 null, null, req); 95 if (rep instanceof Node ) { 96 rep = new DOMSource ((Node ) rep); 97 } 98 log.info(new SourceTransformer().toString((Source ) rep)); 99 } 100 101 protected String getServiceUnitPath(String name) { 102 URL url = getClass().getClassLoader().getResource(name + "/sca.module"); 103 File path = new File (url.getFile()); 104 path = path.getParentFile(); 105 return path.getAbsolutePath(); 106 } 107 108 } 109 | Popular Tags |