KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > bus > bindings > xml > XMLClientBindingTest


1 package org.objectweb.celtix.bus.bindings.xml;
2
3 import java.io.*;
4 import java.lang.reflect.Method JavaDoc;
5 import javax.wsdl.WSDLException;
6
7 import org.w3c.dom.*;
8
9 import junit.framework.TestCase;
10
11 import org.objectweb.celtix.Bus;
12 import org.objectweb.celtix.bindings.DataBindingCallback;
13 import org.objectweb.celtix.bus.bindings.TestClientTransport;
14 import org.objectweb.celtix.bus.bindings.TestInputStreamContext;
15 import org.objectweb.celtix.bus.bindings.TestOutputStreamContext;
16 import org.objectweb.celtix.bus.jaxws.JAXBDataBindingCallback;
17 import org.objectweb.celtix.context.GenericMessageContext;
18 import org.objectweb.celtix.context.ObjectMessageContext;
19 import org.objectweb.celtix.helpers.XMLUtils;
20 import org.objectweb.celtix.transports.ClientTransport;
21 import org.objectweb.celtix.ws.addressing.EndpointReferenceType;
22 import org.objectweb.hello_world_xml_http.wrapped.Greeter;
23
24 public class XMLClientBindingTest extends TestCase {
25     Bus bus;
26     EndpointReferenceType epr;
27     XMLUtils xmlUtils;
28     
29     public XMLClientBindingTest(String JavaDoc arg0) {
30         super(arg0);
31     }
32
33     public static void main(String JavaDoc[] args) {
34         junit.textui.TestRunner.run(XMLClientBindingTest.class);
35     }
36     
37     protected void setUp() throws Exception JavaDoc {
38         super.setUp();
39         bus = Bus.init();
40         
41         xmlUtils = new XMLUtils();
42         TestUtils testUtils = new TestUtils();
43
44         epr = testUtils.getWrappedReference();
45     }
46
47     public void testGetBinding() throws Exception JavaDoc {
48         XMLClientBinding clientBinding = new XMLClientBinding(bus, epr);
49         assertNotNull(clientBinding.getBinding());
50     }
51
52     public void testCreateObjectContext() throws Exception JavaDoc {
53         XMLClientBinding clientBinding = new XMLClientBinding(bus, epr);
54         assertNotNull(clientBinding.createObjectContext());
55     }
56
57     public void testRead() throws Exception JavaDoc {
58         TestClientBinding clientBinding = new TestClientBinding(bus, epr);
59         InputStream is = getClass().getResourceAsStream("resources/SayHiWrappedResp.xml");
60         TestInputStreamContext tisc = new TestInputStreamContext(null);
61         tisc.setInputStream(is);
62         XMLMessageContext xmlContext = new XMLMessageContextImpl(new GenericMessageContext());
63         clientBinding.getBindingImpl().read(tisc, xmlContext);
64
65         assertNotNull(xmlContext.getMessage());
66
67         is = getClass().getResourceAsStream("resources/SayHiWrappedResp.xml");
68         Document expectDOM = xmlUtils.parse(is);
69         assertNotNull(expectDOM);
70         Document resultDOM = xmlContext.getMessage().getRoot();
71         assertNotNull(resultDOM);
72         is.close();
73
74         assertTrue(expectDOM.isEqualNode(resultDOM));
75     }
76
77     public void testWrite() throws Exception JavaDoc {
78         TestClientBinding clientBinding = new TestClientBinding(bus, epr);
79         String JavaDoc resource = "resources/SayHiWrappedReq.xml";
80         InputStream is = getClass().getResourceAsStream(resource);
81         assertNotNull(resource + " is not exist.", is);
82         
83         XMLMessageFactory msgFactory = XMLMessageFactory.newInstance();
84         XMLMessage greetMeMsg = msgFactory.createMessage(is);
85         is.close();
86         
87         BufferedReader br = new BufferedReader(new InputStreamReader(getClass().
88                                                                      getResourceAsStream(resource)));
89         String JavaDoc expectString = br.readLine();
90         
91         XMLMessageContext xmlContext = new XMLMessageContextImpl(new GenericMessageContext());
92         xmlContext.setMessage(greetMeMsg);
93         
94         TestOutputStreamContext tosc = new TestOutputStreamContext(null, xmlContext);
95         clientBinding.getBindingImpl().write(xmlContext, tosc);
96
97         byte[] bArray = tosc.getOutputStreamBytes();
98
99         Document expectDOM = xmlUtils.parse(expectString);
100         Document resultDOM = xmlUtils.parse(bArray);
101
102         assertTrue(expectDOM.isEqualNode(resultDOM));
103     }
104
105     public void testInvokeOneWay() throws Exception JavaDoc {
106         TestClientBinding clientBinding = new TestClientBinding(bus, epr);
107         ObjectMessageContext objContext = clientBinding.createObjectContext();
108         assertNotNull(objContext);
109         
110         Method JavaDoc method = ClassUtils.getMethod(Greeter.class, "greetMeOneWay");
111         
112         String JavaDoc arg0 = new String JavaDoc("TestXMLInputMessage");
113         objContext.setMessageObjects(arg0);
114         
115         clientBinding.invokeOneWay(objContext,
116                                    new JAXBDataBindingCallback(method,
117                                                                DataBindingCallback.Mode.PARTS,
118                                                                null));
119     }
120     
121     class TestClientBinding extends XMLClientBinding {
122
123         public TestClientBinding(Bus b, EndpointReferenceType ref)
124             throws WSDLException, IOException {
125             super(b, ref);
126         }
127
128         protected ClientTransport createTransport(EndpointReferenceType ref)
129             throws WSDLException, IOException {
130             // REVISIT: non-null response callback
131
return new TestClientTransport(bus, ref);
132         }
133
134     }
135 }
136
Popular Tags