1 package org.objectweb.celtix.bus.bindings.soap; 2 3 import java.io.InputStream ; 4 import java.lang.reflect.Method ; 5 import java.lang.reflect.Type ; 6 7 import javax.xml.XMLConstants ; 8 import javax.xml.bind.JAXBContext; 9 import javax.xml.namespace.QName ; 10 import javax.xml.soap.SOAPElement ; 11 import javax.xml.soap.SOAPFactory ; 12 import javax.xml.transform.stream.StreamSource ; 13 import javax.xml.validation.Schema ; 14 import javax.xml.validation.SchemaFactory ; 15 import javax.xml.ws.ProtocolException; 16 import javax.xml.ws.RequestWrapper; 17 18 import org.w3c.dom.Element ; 19 import org.w3c.dom.Node ; 20 21 import junit.framework.TestCase; 22 23 import org.objectweb.celtix.bus.jaxws.JAXBEncoderDecoder; 24 import org.objectweb.hello_world_soap_http.Greeter; 25 import org.objectweb.hello_world_soap_http.types.GreetMe; 26 import org.objectweb.hello_world_soap_http.types.StringStruct; 27 import org.objectweb.type_test.doc.TypeTestPortType; 28 29 33 public class JAXBEncoderDecoderTest extends TestCase { 34 RequestWrapper wrapperAnnotation; 35 JAXBContext context; 36 Schema schema; 37 38 public JAXBEncoderDecoderTest(String arg0) { 39 super(arg0); 40 } 41 42 public static void main(String [] args) { 43 junit.textui.TestRunner.run(JAXBEncoderDecoderTest.class); 44 } 45 46 protected void setUp() throws Exception { 47 super.setUp(); 48 49 context = JAXBEncoderDecoder.createJAXBContextForClass(Greeter.class); 50 Method method = SOAPMessageUtil.getMethod(Greeter.class, "greetMe"); 51 wrapperAnnotation = method.getAnnotation(RequestWrapper.class); 52 53 InputStream is = getClass().getResourceAsStream("resources/StringStruct.xsd"); 54 StreamSource schemaSource = new StreamSource (is); 55 assertNotNull(schemaSource); 56 SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 57 schema = factory.newSchema(schemaSource); 58 assertNotNull(schema); 59 } 60 61 public void testMarshall() throws Exception { 62 String str = new String ("Hello"); 63 QName inCorrectElName = new QName ("http://test_jaxb_marshall", "requestType"); 64 SOAPFactory soapElFactory = SOAPFactory.newInstance(); 65 Element elNode = soapElFactory.createElement(inCorrectElName); 66 assertNotNull(elNode); 67 68 Node node; 69 try { 70 JAXBEncoderDecoder.marshall(context, null, null, inCorrectElName, elNode); 71 fail("Should have thrown a ProtocolException"); 72 } catch (ProtocolException ex) { 73 } 75 76 GreetMe obj = new GreetMe(); 77 obj.setRequestType("Hello"); 78 QName elName = new QName (wrapperAnnotation.targetNamespace(), 79 wrapperAnnotation.localName()); 80 JAXBEncoderDecoder.marshall(context, null, obj, elName, elNode); 81 node = elNode.getLastChild(); 82 assertEquals(Node.ELEMENT_NODE, node.getNodeType()); 85 Node childNode = node.getFirstChild(); 86 assertEquals(Node.ELEMENT_NODE, childNode.getNodeType()); 87 childNode = childNode.getFirstChild(); 88 assertEquals(Node.TEXT_NODE, childNode.getNodeType()); 89 assertEquals(str, childNode.getNodeValue()); 90 91 StringStruct stringStruct = new StringStruct(); 93 stringStruct.setArg1("world"); 96 JAXBEncoderDecoder.marshall(context, null, stringStruct, elName, elNode); 98 try { 99 JAXBEncoderDecoder.marshall(context, schema, stringStruct, elName, elNode); 101 fail("Marshal with schema should have thrown a ProtocolException"); 102 } catch (ProtocolException ex) { 103 } 105 } 106 107 public void testMarshalRPCLit() throws Exception { 108 SOAPFactory soapElFactory = SOAPFactory.newInstance(); 109 QName elName = new QName ("http://test_jaxb_marshall", "in"); 110 SOAPElement elNode = soapElFactory.createElement(elName); 111 JAXBEncoderDecoder.marshall(context, null, new String ("TestSOAPMessage"), elName, elNode); 112 113 assertNotNull(elNode.getChildNodes()); 114 assertEquals("TestSOAPMessage", elNode.getFirstChild().getFirstChild().getNodeValue()); 115 } 116 117 public void testUnMarshall() throws Exception { 118 QName elName = new QName (wrapperAnnotation.targetNamespace(), 120 wrapperAnnotation.localName()); 121 SOAPFactory soapElFactory = SOAPFactory.newInstance(); 124 SOAPElement elNode = soapElFactory.createElement(elName); 125 elNode.addNamespaceDeclaration("", elName.getNamespaceURI()); 126 127 String str = new String ("Hello Test"); 128 elNode.addChildElement("requestType").setValue(str); 129 130 Object obj = JAXBEncoderDecoder.unmarshall(context, null, 131 elNode, elName, Class.forName(wrapperAnnotation.className())); 132 assertNotNull(obj); 133 134 assertEquals(GreetMe.class, obj.getClass()); 136 assertEquals(str, ((GreetMe)obj).getRequestType()); 137 138 try { 139 JAXBEncoderDecoder.unmarshall(context, null, null, null, String .class); 140 fail("Should have received a ProtocolException"); 141 } catch (ProtocolException pe) { 142 } catch (Exception ex) { 144 fail("Should have received a ProtocolException, not: " + ex); 145 } 146 147 elName = new QName (wrapperAnnotation.targetNamespace(), 149 "stringStruct"); 150 elNode = soapElFactory.createElement(elName); 153 elNode.addNamespaceDeclaration("", elName.getNamespaceURI()); 154 str = new String ("World"); 155 elNode.addChildElement("arg1").setValue(str); 156 obj = JAXBEncoderDecoder.unmarshall(context, null, elNode, elName, 158 Class.forName("org.objectweb.hello_world_soap_http.types.StringStruct")); 159 assertNotNull(obj); 160 assertEquals(StringStruct.class, obj.getClass()); 161 assertEquals(str, ((StringStruct)obj).getArg1()); 162 try { 163 obj = JAXBEncoderDecoder.unmarshall(context, schema, elNode, elName, 165 Class.forName("org.objectweb.hello_world_soap_http.types.StringStruct")); 166 fail("Should have thrown a ProtocolException"); 167 } catch (ProtocolException ex) { 168 } 170 } 171 172 public void testGetClassFromType() throws Exception { 173 Method testByte = SOAPMessageUtil.getMethod(TypeTestPortType.class, "testByte"); 174 Type [] genericParameterTypes = testByte.getGenericParameterTypes(); 175 Class <?>[] paramTypes = testByte.getParameterTypes(); 176 177 int idx = 0; 178 for (Type t : genericParameterTypes) { 179 Class <?> cls = JAXBEncoderDecoder.getClassFromType(t); 180 assertTrue(cls.equals(paramTypes[idx])); 181 idx++; 182 } 183 184 Method testBase64Binary = SOAPMessageUtil.getMethod(TypeTestPortType.class, "testBase64Binary"); 185 genericParameterTypes = testBase64Binary.getGenericParameterTypes(); 186 paramTypes = testBase64Binary.getParameterTypes(); 187 188 idx = 0; 189 for (Type t : genericParameterTypes) { 190 Class <?> cls = JAXBEncoderDecoder.getClassFromType(t); 191 assertTrue(cls.equals(paramTypes[idx])); 192 idx++; 193 } 194 195 196 } 197 } 198 199 | Popular Tags |