1 package org.objectweb.celtix.routing; 2 3 import java.io.File ; 4 import java.lang.reflect.InvocationHandler ; 5 import java.lang.reflect.Proxy ; 6 import java.net.URL ; 7 import java.net.URLClassLoader ; 8 import java.util.HashMap ; 9 import java.util.List ; 10 import java.util.Map ; 11 12 import javax.wsdl.Definition; 13 import javax.wsdl.Port; 14 import javax.wsdl.Service; 15 import javax.xml.namespace.QName ; 16 import javax.xml.ws.Endpoint; 17 import javax.xml.ws.Provider; 18 import javax.xml.ws.WebServiceException; 19 20 import junit.framework.TestCase; 21 import org.objectweb.celtix.Bus; 22 import org.objectweb.celtix.bus.configuration.wsdl.WsdlPortProvider; 23 import org.objectweb.celtix.routing.configuration.RouteType; 24 25 public class RouterTest extends TestCase { 26 27 private Map <String , Object > properties; 28 private String javaClasspath; 29 private File srcDir; 30 31 public void setUp() { 32 properties = new HashMap <String , Object >(); 33 javaClasspath = System.getProperty("java.class.path"); 34 srcDir = new File (getClass().getResource(".").getFile(), "/temp"); 35 } 36 37 public void tearDown() throws Exception { 38 System.setProperty("java.class.path", javaClasspath); 39 RouteTypeUtil.deleteDir(srcDir); 40 41 Bus bus = Bus.getCurrent(); 42 bus.shutdown(true); 43 Bus.setCurrent(null); 44 } 45 46 public void testRouterCreation() throws Exception { 47 properties.put("org.objectweb.celtix.BusId", "RT1"); 48 Bus bus = Bus.init(null, properties); 49 Bus.setCurrent(bus); 50 51 Definition def = bus.getWSDLManager().getDefinition(getClass().getResource("resources/router.wsdl")); 52 53 QName sourceSrv = new QName ("http://objectweb.org/HWRouter", "HTTPSoapServiceSource"); 54 String sourcePort = new String ("HTTPSoapPortSource"); 55 QName destSrv = new QName ("http://objectweb.org/HWRouter", "HTTPSoapServiceDestination"); 56 String destPort = new String ("HTTPSoapPortDestination"); 57 58 testRouterSourceAndDestination(def, 59 sourceSrv, sourcePort, 60 destSrv, destPort, 61 true); 62 63 sourceSrv = new QName ("http://objectweb.org/HWRouter", "HTTPXMLServiceSource"); 64 sourcePort = new String ("HTTPXMLPortSource"); 65 destSrv = new QName ("http://objectweb.org/HWRouter", "HTTPSoapServiceDestination"); 66 destPort = new String ("HTTPSoapPortDestination"); 67 68 testRouterSourceAndDestination(def, 69 sourceSrv, sourcePort, 70 destSrv, destPort, 71 false); 72 } 73 74 public void testPassThroughRouterInit() throws Exception { 75 properties.put("org.objectweb.celtix.BusId", "RT2"); 76 Bus bus = Bus.init(null, properties); 77 Bus.setCurrent(bus); 78 79 Definition def = bus.getWSDLManager().getDefinition(getClass().getResource("resources/router.wsdl")); 80 81 QName sourceSrv = new QName ("http://objectweb.org/HWRouter", "HTTPSoapServiceSource"); 82 String sourcePort = new String ("HTTPSoapPortSource"); 83 QName destSrv = new QName ("http://objectweb.org/HWRouter", "HTTPSoapServiceDestination"); 84 String destPort = new String ("HTTPSoapPortDestination"); 85 86 RouteType rt = 87 RouteTypeUtil.createRouteType("route_0", 88 sourceSrv, sourcePort, 89 destSrv, destPort); 90 91 TestRouter router = new TestRouter(null, def, rt); 92 router.init(); 93 assertEquals(1, router.epList.size()); 94 Endpoint ep = router.epList.get(0); 95 assertNotNull("Should have a Endpoint for Source Service", ep); 96 assertNotNull("Should have a wsdl model", ep.getMetadata()); 97 Map <String , Object > props = ep.getProperties(); 98 assertNotNull("Should have a wsdl model", props); 99 assertEquals(sourceSrv, props.get(Endpoint.WSDL_SERVICE)); 100 QName portName = (QName ) props.get(Endpoint.WSDL_PORT); 101 assertEquals(sourceSrv.getNamespaceURI(), portName.getNamespaceURI()); 102 assertEquals(sourcePort, portName.getLocalPart()); 103 Object impl = ep.getImplementor(); 104 assertTrue("Should be instance of Provider<Source>", 105 impl instanceof Provider); 106 StreamSourceMessageProvider ssmp = (StreamSourceMessageProvider) impl; 107 assertNull("WebServiceContext is not set as endpoint is not published", 108 ssmp.getContext()); 109 } 110 111 public void testNormalRouterInit() throws Exception { 112 properties.put("org.objectweb.celtix.BusId", "RT3"); 113 Bus bus = Bus.init(null, properties); 114 Bus.setCurrent(bus); 115 116 URL wsdlURl = getClass().getResource("resources/router.wsdl"); 117 Definition def = bus.getWSDLManager().getDefinition(wsdlURl); 118 119 QName sourceSrv = new QName ("http://objectweb.org/HWRouter", "HTTPXMLServiceSource"); 120 String sourcePort = new String ("HTTPXMLPortSource"); 121 QName destSrv = new QName ("http://objectweb.org/HWRouter", "HTTPSoapServiceDestination"); 122 String destPort = new String ("HTTPSoapPortDestination"); 123 124 RouteType rt = 125 RouteTypeUtil.createRouteType("route_1", 126 sourceSrv, sourcePort, 127 destSrv, destPort); 128 129 ClassLoader loader = new URLClassLoader (new URL [] {srcDir.toURL()}, null); 130 TestRouter router = new TestRouter(loader, def, rt); 132 try { 133 router.init(); 134 fail("Should throw a WebServiceException with cause of ClassNotFoundError"); 135 } catch (WebServiceException ex) { 136 if (ex.getCause() instanceof ClassNotFoundException ) { 137 } 139 } 140 141 loader = doCodeGeneration(wsdlURl.getFile(), srcDir); 143 144 router = new TestRouter(loader, def, rt); 145 router.init(); 146 assertEquals(1, router.epList.size()); 147 Endpoint ep = router.epList.get(0); 148 assertNotNull("Should have a Endpoint for Source Service", ep); 149 assertNotNull("Should have a wsdl model", ep.getMetadata()); 150 Map <String , Object > props = ep.getProperties(); 151 assertNotNull("Should have a wsdl model", props); 152 assertEquals(sourceSrv, props.get(Endpoint.WSDL_SERVICE)); 153 QName portName = (QName ) props.get(Endpoint.WSDL_PORT); 154 assertEquals(sourceSrv.getNamespaceURI(), portName.getNamespaceURI()); 155 assertEquals(sourcePort, portName.getLocalPart()); 156 Object impl = ep.getImplementor(); 157 158 assertTrue("Implemetor Should be a proxy Class", 160 Proxy.isProxyClass(impl.getClass())); 161 162 InvocationHandler implHandler = Proxy.getInvocationHandler(impl); 163 assertTrue("Invocation Handler should be instance of SEIImplHandler", 164 implHandler instanceof SEIImplHandler); 165 SEIImplHandler seiHandler = (SEIImplHandler)implHandler; 166 assertNull("Should have a WebServiceContext set", 167 seiHandler.getContext()); 168 } 169 170 private ClassLoader doCodeGeneration(String wsdlUrl, File opDir) throws Exception { 171 boolean isClassPathSet = javaClasspath != null 173 && (javaClasspath.indexOf("JAXWS") >= 0); 174 if (!isClassPathSet) { 175 System.setProperty("java.class.path", 176 RouteTypeUtil.getClassPath(getClass().getClassLoader())); 177 } 178 179 File classDir = new File (opDir, "/classes"); 180 classDir.mkdirs(); 181 182 RouteTypeUtil.invokeWSDLToJava(wsdlUrl, opDir, classDir); 183 184 URLClassLoader loader = 185 URLClassLoader.newInstance(new URL [] {classDir.toURL()}, 186 getClass().getClassLoader()); 187 return loader; 188 } 189 190 private void testRouterSourceAndDestination(Definition def, 191 QName sourceSrv, String sourcePort, 192 QName destSrv, String destPort, 193 boolean isSameBinding) { 194 RouteType rt = 195 RouteTypeUtil.createRouteType("route_0", 196 sourceSrv, sourcePort, 197 destSrv, destPort); 198 199 TestRouter router = new TestRouter(null, def, rt); 200 assertNotNull("WSDL Model should be set for the router", router.getWSDLModel()); 201 assertNotNull("RouteType should be set for the router", router.getRoute()); 202 203 Service s = def.getService(destSrv); 204 Port p = router.getDestinationPorts(s); 205 206 assertNotNull("Should have a wsdl port", p); 207 assertEquals(destPort, p.getName()); 208 209 s = def.getService(sourceSrv); 210 p = router.getSourcePort(s); 211 212 assertNotNull("Should have a wsdl port", p); 213 assertEquals(sourcePort, p.getName()); 214 215 assertEquals(isSameBinding, 217 router.testIsSameBindingId(p)); 218 } 219 220 public static void main(String [] args) { 221 junit.textui.TestRunner.run(RouterTest.class); 222 } 223 224 225 class TestRouter extends RouterImpl { 226 227 public TestRouter(ClassLoader loader, Definition model, RouteType rt) { 228 super(loader, model, rt); 229 } 230 231 public boolean testIsSameBindingId(Port p) { 232 WsdlPortProvider provider = new WsdlPortProvider(p); 233 return super.isSameBindingId((String ) provider.getObject("bindingId")); 234 } 235 236 public Port getSourcePort(Service service) { 237 return (Port) super.sourcePortMap.get(service.getQName()); 238 } 239 240 public Port getDestinationPorts(Service service) { 241 return (Port) super.destPortMap.get(service.getQName()); 242 } 243 244 public List <Endpoint> getEndpoints() { 245 return super.epList; 246 } 247 } 248 } 249 | Popular Tags |