KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > bus > bindings > soap > JAXBEncoderDecoderTest


1 package org.objectweb.celtix.bus.bindings.soap;
2
3 import java.io.InputStream JavaDoc;
4 import java.lang.reflect.Method JavaDoc;
5 import java.lang.reflect.Type JavaDoc;
6
7 import javax.xml.XMLConstants JavaDoc;
8 import javax.xml.bind.JAXBContext;
9 import javax.xml.namespace.QName JavaDoc;
10 import javax.xml.soap.SOAPElement JavaDoc;
11 import javax.xml.soap.SOAPFactory JavaDoc;
12 import javax.xml.transform.stream.StreamSource JavaDoc;
13 import javax.xml.validation.Schema JavaDoc;
14 import javax.xml.validation.SchemaFactory JavaDoc;
15 import javax.xml.ws.ProtocolException;
16 import javax.xml.ws.RequestWrapper;
17
18 import org.w3c.dom.Element JavaDoc;
19 import org.w3c.dom.Node JavaDoc;
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 /**
30  * JAXBEncoderDecoderTest
31  * @author apaibir
32  */

33 public class JAXBEncoderDecoderTest extends TestCase {
34     RequestWrapper wrapperAnnotation;
35     JAXBContext context;
36     Schema JavaDoc schema;
37     
38     public JAXBEncoderDecoderTest(String JavaDoc arg0) {
39         super(arg0);
40     }
41
42     public static void main(String JavaDoc[] args) {
43         junit.textui.TestRunner.run(JAXBEncoderDecoderTest.class);
44     }
45
46     protected void setUp() throws Exception JavaDoc {
47         super.setUp();
48
49         context = JAXBEncoderDecoder.createJAXBContextForClass(Greeter.class);
50         Method JavaDoc method = SOAPMessageUtil.getMethod(Greeter.class, "greetMe");
51         wrapperAnnotation = method.getAnnotation(RequestWrapper.class);
52         
53         InputStream JavaDoc is = getClass().getResourceAsStream("resources/StringStruct.xsd");
54         StreamSource JavaDoc schemaSource = new StreamSource JavaDoc(is);
55         assertNotNull(schemaSource);
56         SchemaFactory JavaDoc 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 JavaDoc {
62         String JavaDoc str = new String JavaDoc("Hello");
63         QName JavaDoc inCorrectElName = new QName JavaDoc("http://test_jaxb_marshall", "requestType");
64         SOAPFactory JavaDoc soapElFactory = SOAPFactory.newInstance();
65         Element JavaDoc elNode = soapElFactory.createElement(inCorrectElName);
66         assertNotNull(elNode);
67
68         Node JavaDoc node;
69         try {
70             JAXBEncoderDecoder.marshall(context, null, null, inCorrectElName, elNode);
71             fail("Should have thrown a ProtocolException");
72         } catch (ProtocolException ex) {
73             //expected - not a valid object
74
}
75
76         GreetMe obj = new GreetMe();
77         obj.setRequestType("Hello");
78         QName JavaDoc elName = new QName JavaDoc(wrapperAnnotation.targetNamespace(),
79                                  wrapperAnnotation.localName());
80         JAXBEncoderDecoder.marshall(context, null, obj, elName, elNode);
81         node = elNode.getLastChild();
82         //The XML Tree Looks like
83
//<GreetMe><requestType>Hello</requestType></GreetMe>
84
assertEquals(Node.ELEMENT_NODE, node.getNodeType());
85         Node JavaDoc 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         // Now test schema validation during marshaling
92
StringStruct stringStruct = new StringStruct();
93         // Don't initialize one of the structure members.
94
//stringStruct.setArg0("hello");
95
stringStruct.setArg1("world");
96         // Marshal without the schema should work.
97
JAXBEncoderDecoder.marshall(context, null, stringStruct, elName, elNode);
98         try {
99             // Marshal with the schema should get an exception.
100
JAXBEncoderDecoder.marshall(context, schema, stringStruct, elName, elNode);
101             fail("Marshal with schema should have thrown a ProtocolException");
102         } catch (ProtocolException ex) {
103             //expected - not a valid object
104
}
105     }
106     
107     public void testMarshalRPCLit() throws Exception JavaDoc {
108         SOAPFactory JavaDoc soapElFactory = SOAPFactory.newInstance();
109         QName JavaDoc elName = new QName JavaDoc("http://test_jaxb_marshall", "in");
110         SOAPElement JavaDoc elNode = soapElFactory.createElement(elName);
111         JAXBEncoderDecoder.marshall(context, null, new String JavaDoc("TestSOAPMessage"), elName, elNode);
112         
113         assertNotNull(elNode.getChildNodes());
114         assertEquals("TestSOAPMessage", elNode.getFirstChild().getFirstChild().getNodeValue());
115     }
116
117     public void testUnMarshall() throws Exception JavaDoc {
118         //Hello World Wsdl generated namespace
119
QName JavaDoc elName = new QName JavaDoc(wrapperAnnotation.targetNamespace(),
120                                  wrapperAnnotation.localName());
121         //Create a XML Tree of
122
//<GreetMe><requestType>Hello</requestType></GreetMe>
123
SOAPFactory JavaDoc soapElFactory = SOAPFactory.newInstance();
124         SOAPElement JavaDoc elNode = soapElFactory.createElement(elName);
125         elNode.addNamespaceDeclaration("", elName.getNamespaceURI());
126
127         String JavaDoc str = new String JavaDoc("Hello Test");
128         elNode.addChildElement("requestType").setValue(str);
129
130         Object JavaDoc obj = JAXBEncoderDecoder.unmarshall(context, null,
131                          elNode, elName, Class.forName(wrapperAnnotation.className()));
132         assertNotNull(obj);
133
134         //Add a Node and then test
135
assertEquals(GreetMe.class, obj.getClass());
136         assertEquals(str, ((GreetMe)obj).getRequestType());
137         
138         try {
139             JAXBEncoderDecoder.unmarshall(context, null, null, null, String JavaDoc.class);
140             fail("Should have received a ProtocolException");
141         } catch (ProtocolException pe) {
142             //Expected Exception
143
} catch (Exception JavaDoc ex) {
144             fail("Should have received a ProtocolException, not: " + ex);
145         }
146         
147         // Now test schema validation during unmarshaling
148
elName = new QName JavaDoc(wrapperAnnotation.targetNamespace(),
149                            "stringStruct");
150         // Create an XML Tree of
151
// <StringStruct><arg1>World</arg1></StringStruct>
152
elNode = soapElFactory.createElement(elName);
153         elNode.addNamespaceDeclaration("", elName.getNamespaceURI());
154         str = new String JavaDoc("World");
155         elNode.addChildElement("arg1").setValue(str);
156         // Should unmarshal without problems when no schema used.
157
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             // unmarshal with schema should raise exception.
164
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             // expected - schema validation should fail.
169
}
170     }
171     
172     public void testGetClassFromType() throws Exception JavaDoc {
173         Method JavaDoc testByte = SOAPMessageUtil.getMethod(TypeTestPortType.class, "testByte");
174         Type JavaDoc[] genericParameterTypes = testByte.getGenericParameterTypes();
175         Class JavaDoc<?>[] paramTypes = testByte.getParameterTypes();
176  
177         int idx = 0;
178         for (Type JavaDoc t : genericParameterTypes) {
179             Class JavaDoc<?> cls = JAXBEncoderDecoder.getClassFromType(t);
180             assertTrue(cls.equals(paramTypes[idx]));
181             idx++;
182         }
183         
184         Method JavaDoc testBase64Binary = SOAPMessageUtil.getMethod(TypeTestPortType.class, "testBase64Binary");
185         genericParameterTypes = testBase64Binary.getGenericParameterTypes();
186         paramTypes = testBase64Binary.getParameterTypes();
187  
188         idx = 0;
189         for (Type JavaDoc t : genericParameterTypes) {
190             Class JavaDoc<?> cls = JAXBEncoderDecoder.getClassFromType(t);
191             assertTrue(cls.equals(paramTypes[idx]));
192             idx++;
193         }
194         
195         
196     }
197 }
198
199
Popular Tags