001: package org.objectweb.celtix.bus.bindings.soap;
002:
003: import java.io.InputStream;
004: import java.lang.reflect.Method;
005: import java.lang.reflect.Type;
006:
007: import javax.xml.XMLConstants;
008: import javax.xml.bind.JAXBContext;
009: import javax.xml.namespace.QName;
010: import javax.xml.soap.SOAPElement;
011: import javax.xml.soap.SOAPFactory;
012: import javax.xml.transform.stream.StreamSource;
013: import javax.xml.validation.Schema;
014: import javax.xml.validation.SchemaFactory;
015: import javax.xml.ws.ProtocolException;
016: import javax.xml.ws.RequestWrapper;
017:
018: import org.w3c.dom.Element;
019: import org.w3c.dom.Node;
020:
021: import junit.framework.TestCase;
022:
023: import org.objectweb.celtix.bus.jaxws.JAXBEncoderDecoder;
024: import org.objectweb.hello_world_soap_http.Greeter;
025: import org.objectweb.hello_world_soap_http.types.GreetMe;
026: import org.objectweb.hello_world_soap_http.types.StringStruct;
027: import org.objectweb.type_test.doc.TypeTestPortType;
028:
029: /**
030: * JAXBEncoderDecoderTest
031: * @author apaibir
032: */
033: public class JAXBEncoderDecoderTest extends TestCase {
034: RequestWrapper wrapperAnnotation;
035: JAXBContext context;
036: Schema schema;
037:
038: public JAXBEncoderDecoderTest(String arg0) {
039: super (arg0);
040: }
041:
042: public static void main(String[] args) {
043: junit.textui.TestRunner.run(JAXBEncoderDecoderTest.class);
044: }
045:
046: protected void setUp() throws Exception {
047: super .setUp();
048:
049: context = JAXBEncoderDecoder
050: .createJAXBContextForClass(Greeter.class);
051: Method method = SOAPMessageUtil.getMethod(Greeter.class,
052: "greetMe");
053: wrapperAnnotation = method.getAnnotation(RequestWrapper.class);
054:
055: InputStream is = getClass().getResourceAsStream(
056: "resources/StringStruct.xsd");
057: StreamSource schemaSource = new StreamSource(is);
058: assertNotNull(schemaSource);
059: SchemaFactory factory = SchemaFactory
060: .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
061: schema = factory.newSchema(schemaSource);
062: assertNotNull(schema);
063: }
064:
065: public void testMarshall() throws Exception {
066: String str = new String("Hello");
067: QName inCorrectElName = new QName("http://test_jaxb_marshall",
068: "requestType");
069: SOAPFactory soapElFactory = SOAPFactory.newInstance();
070: Element elNode = soapElFactory.createElement(inCorrectElName);
071: assertNotNull(elNode);
072:
073: Node node;
074: try {
075: JAXBEncoderDecoder.marshall(context, null, null,
076: inCorrectElName, elNode);
077: fail("Should have thrown a ProtocolException");
078: } catch (ProtocolException ex) {
079: //expected - not a valid object
080: }
081:
082: GreetMe obj = new GreetMe();
083: obj.setRequestType("Hello");
084: QName elName = new QName(wrapperAnnotation.targetNamespace(),
085: wrapperAnnotation.localName());
086: JAXBEncoderDecoder.marshall(context, null, obj, elName, elNode);
087: node = elNode.getLastChild();
088: //The XML Tree Looks like
089: //<GreetMe><requestType>Hello</requestType></GreetMe>
090: assertEquals(Node.ELEMENT_NODE, node.getNodeType());
091: Node childNode = node.getFirstChild();
092: assertEquals(Node.ELEMENT_NODE, childNode.getNodeType());
093: childNode = childNode.getFirstChild();
094: assertEquals(Node.TEXT_NODE, childNode.getNodeType());
095: assertEquals(str, childNode.getNodeValue());
096:
097: // Now test schema validation during marshaling
098: StringStruct stringStruct = new StringStruct();
099: // Don't initialize one of the structure members.
100: //stringStruct.setArg0("hello");
101: stringStruct.setArg1("world");
102: // Marshal without the schema should work.
103: JAXBEncoderDecoder.marshall(context, null, stringStruct,
104: elName, elNode);
105: try {
106: // Marshal with the schema should get an exception.
107: JAXBEncoderDecoder.marshall(context, schema, stringStruct,
108: elName, elNode);
109: fail("Marshal with schema should have thrown a ProtocolException");
110: } catch (ProtocolException ex) {
111: //expected - not a valid object
112: }
113: }
114:
115: public void testMarshalRPCLit() throws Exception {
116: SOAPFactory soapElFactory = SOAPFactory.newInstance();
117: QName elName = new QName("http://test_jaxb_marshall", "in");
118: SOAPElement elNode = soapElFactory.createElement(elName);
119: JAXBEncoderDecoder.marshall(context, null, new String(
120: "TestSOAPMessage"), elName, elNode);
121:
122: assertNotNull(elNode.getChildNodes());
123: assertEquals("TestSOAPMessage", elNode.getFirstChild()
124: .getFirstChild().getNodeValue());
125: }
126:
127: public void testUnMarshall() throws Exception {
128: //Hello World Wsdl generated namespace
129: QName elName = new QName(wrapperAnnotation.targetNamespace(),
130: wrapperAnnotation.localName());
131: //Create a XML Tree of
132: //<GreetMe><requestType>Hello</requestType></GreetMe>
133: SOAPFactory soapElFactory = SOAPFactory.newInstance();
134: SOAPElement elNode = soapElFactory.createElement(elName);
135: elNode.addNamespaceDeclaration("", elName.getNamespaceURI());
136:
137: String str = new String("Hello Test");
138: elNode.addChildElement("requestType").setValue(str);
139:
140: Object obj = JAXBEncoderDecoder.unmarshall(context, null,
141: elNode, elName, Class.forName(wrapperAnnotation
142: .className()));
143: assertNotNull(obj);
144:
145: //Add a Node and then test
146: assertEquals(GreetMe.class, obj.getClass());
147: assertEquals(str, ((GreetMe) obj).getRequestType());
148:
149: try {
150: JAXBEncoderDecoder.unmarshall(context, null, null, null,
151: String.class);
152: fail("Should have received a ProtocolException");
153: } catch (ProtocolException pe) {
154: //Expected Exception
155: } catch (Exception ex) {
156: fail("Should have received a ProtocolException, not: " + ex);
157: }
158:
159: // Now test schema validation during unmarshaling
160: elName = new QName(wrapperAnnotation.targetNamespace(),
161: "stringStruct");
162: // Create an XML Tree of
163: // <StringStruct><arg1>World</arg1></StringStruct>
164: elNode = soapElFactory.createElement(elName);
165: elNode.addNamespaceDeclaration("", elName.getNamespaceURI());
166: str = new String("World");
167: elNode.addChildElement("arg1").setValue(str);
168: // Should unmarshal without problems when no schema used.
169: obj = JAXBEncoderDecoder
170: .unmarshall(
171: context,
172: null,
173: elNode,
174: elName,
175: Class
176: .forName("org.objectweb.hello_world_soap_http.types.StringStruct"));
177: assertNotNull(obj);
178: assertEquals(StringStruct.class, obj.getClass());
179: assertEquals(str, ((StringStruct) obj).getArg1());
180: try {
181: // unmarshal with schema should raise exception.
182: obj = JAXBEncoderDecoder
183: .unmarshall(
184: context,
185: schema,
186: elNode,
187: elName,
188: Class
189: .forName("org.objectweb.hello_world_soap_http.types.StringStruct"));
190: fail("Should have thrown a ProtocolException");
191: } catch (ProtocolException ex) {
192: // expected - schema validation should fail.
193: }
194: }
195:
196: public void testGetClassFromType() throws Exception {
197: Method testByte = SOAPMessageUtil.getMethod(
198: TypeTestPortType.class, "testByte");
199: Type[] genericParameterTypes = testByte
200: .getGenericParameterTypes();
201: Class<?>[] paramTypes = testByte.getParameterTypes();
202:
203: int idx = 0;
204: for (Type t : genericParameterTypes) {
205: Class<?> cls = JAXBEncoderDecoder.getClassFromType(t);
206: assertTrue(cls.equals(paramTypes[idx]));
207: idx++;
208: }
209:
210: Method testBase64Binary = SOAPMessageUtil.getMethod(
211: TypeTestPortType.class, "testBase64Binary");
212: genericParameterTypes = testBase64Binary
213: .getGenericParameterTypes();
214: paramTypes = testBase64Binary.getParameterTypes();
215:
216: idx = 0;
217: for (Type t : genericParameterTypes) {
218: Class<?> cls = JAXBEncoderDecoder.getClassFromType(t);
219: assertTrue(cls.equals(paramTypes[idx]));
220: idx++;
221: }
222:
223: }
224: }
|