1 17 package org.apache.servicemix.jsr181; 18 19 import java.io.File ; 20 import java.net.URL ; 21 22 import javax.jbi.messaging.ExchangeStatus; 23 import javax.jbi.messaging.InOut; 24 import javax.naming.InitialContext ; 25 import javax.xml.namespace.QName ; 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 { 46 container = new JBIContainer(); 47 container.setUseMBeanServer(false); 48 container.setCreateMBeanServer(false); 49 container.setMonitorInstallationDirectory(false); 50 container.setNamingContext(new InitialContext ()); 51 container.setEmbedded(true); 52 container.init(); 53 } 54 55 protected void tearDown() throws Exception { 56 if (container != null) { 57 container.shutDown(); 58 } 59 } 60 61 public void testCommonsAnnotations() throws Exception { 62 Jsr181Component component = new Jsr181Component(); 63 container.activateComponent(component, "JSR181Component"); 64 65 container.start(); 67 68 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 DefaultServiceMixClient client = new DefaultServiceMixClient(container); 78 InOut me = client.createInOutExchange(); 79 me.setInterfaceName(new QName ("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 { 97 Jsr181Component component = new Jsr181Component(); 98 container.activateComponent(component, "JSR181Component"); 99 100 container.start(); 102 103 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 DefaultServiceMixClient client = new DefaultServiceMixClient(container); 113 InOut me = client.createInOutExchange(); 114 me.setInterfaceName(new QName ("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 { 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 { 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 e) { 149 logger.info(e.getMessage()); 151 } 152 } 153 154 public void testNoEndpoints() throws Exception { 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 e) { 161 logger.info(e.getMessage()); 163 } 164 } 165 166 public void testDuplicates() throws Exception { 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 e) { 173 logger.info(e.getMessage()); 175 } 176 } 177 178 public void testNotJsr181Endpoint() throws Exception { 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 e) { 185 logger.info(e.getMessage()); 187 } 188 } 189 190 public void testUnrecognizedTypeMapping() throws Exception { 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 e) { 197 logger.info(e.getMessage()); 199 } 200 } 201 202 public void testUnrecognizedAnnotations() throws Exception { 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 e) { 209 logger.info(e.getMessage()); 211 } 212 } 213 214 protected String getServiceUnitPath(String name) { 215 URL url = getClass().getClassLoader().getResource(name + "/xbean.xml"); 216 File path = new File (url.getFile()); 217 path = path.getParentFile(); 218 return path.getAbsolutePath(); 219 } 220 221 } 222 | Popular Tags |