1 /***
2 *
3 * Copyright 2004 Protique Ltd
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.codehaus.activesoap.handler.xmlbeans;
19
20 import org.apache.xmlbeans.XmlObject;
21 import org.codehaus.activesoap.TestSupport;
22 import org.codehaus.activesoap.examples.snowboard.SnowboardService;
23 import org.codehaus.activesoap.examples.snowboard.SnowboardServiceImpl;
24 import org.codehaus.activesoap.schema.addressing.AttributedURI;
25 import org.codehaus.activesoap.schema.eventing.DeliveryType;
26 import org.codehaus.activesoap.schema.eventing.SubscribeDocument;
27 import org.codehaus.activesoap.schema.snowboard.GetEndorsingBoarderDocument;
28 import org.codehaus.activesoap.schema.snowboard.GetEndorsingBoarderResponseDocument;
29 import org.w3c.dom.Node;
30
31 import javax.xml.stream.XMLStreamReader;
32 import javax.xml.stream.XMLStreamWriter;
33 import java.io.StringWriter;
34 import java.net.URL;
35
36 /***
37 * @version $Revision: 1.4 $
38 */
39 public class XMLBeansHelperTest extends TestSupport {
40
41 public void testSave() throws Exception {
42 GetEndorsingBoarderResponseDocument answer = GetEndorsingBoarderResponseDocument.Factory.newInstance();
43 GetEndorsingBoarderResponseDocument.GetEndorsingBoarderResponse response = GetEndorsingBoarderResponseDocument.GetEndorsingBoarderResponse.Factory.newInstance();
44 response.setEndorsingBoarder("James");
45 answer.setGetEndorsingBoarderResponse(response);
46
47 System.out.println("About to stream: " + answer);
48
49 StringWriter buffer = new StringWriter();
50 XMLStreamWriter out = createXMLStreamWriter(buffer);
51 XMLBeansHelper.save(answer, out, repairing);
52
53 System.out.println("Created: " + prettyPrintXml(buffer.toString()));
54 }
55
56 public void testSave2() throws Exception {
57 GetEndorsingBoarderDocument answer = GetEndorsingBoarderDocument.Factory.newInstance();
58 GetEndorsingBoarderDocument.GetEndorsingBoarder border = answer.addNewGetEndorsingBoarder();
59 border.setManufacturer("Burton");
60 border.setModel("Custom, 167");
61
62 System.out.println("About to stream: " + answer);
63
64 StringWriter buffer = new StringWriter();
65 XMLStreamWriter out = createXMLStreamWriter(buffer);
66 XMLBeansHelper.save(answer, out, repairing);
67
68 System.out.println("Created: " + prettyPrintXml(buffer.toString()));
69 }
70
71 public void testXMLBeansParse() throws Exception {
72 XMLStreamReader in = createXMLStreamReader(SnowboardService.class.getResource("sampleRestRequest.xml"));
73 XmlObject request = XmlObject.Factory.parse(in);
74
75
76 assertTrue("Incorrect type: " + request, request instanceof GetEndorsingBoarderDocument);
77
78 GetEndorsingBoarderDocument borderDoc = (GetEndorsingBoarderDocument) request;
79 System.out.println("Got document: " + borderDoc);
80
81 SnowboardService service = new SnowboardServiceImpl();
82 service.invoke(borderDoc);
83 }
84
85 public void testXMLBeansParseWithoutStAX() throws Exception {
86 URL resource = SnowboardService.class.getResource("sampleRestRequest2.xml");
87 XmlObject request = XmlObject.Factory.parse(resource);
88
89
90 assertTrue("Incorrect type: " + request, request instanceof GetEndorsingBoarderDocument);
91
92 GetEndorsingBoarderDocument borderDoc = (GetEndorsingBoarderDocument) request;
93 System.out.println("Got document: " + borderDoc);
94
95 SnowboardService service = new SnowboardServiceImpl();
96 service.invoke(borderDoc);
97 }
98
99 public void testXMLBeansParseEventingWithoutStAX() throws Exception {
100 URL resource = getClass().getResource("subscribeRequest.xml");
101 XmlObject request = XmlObject.Factory.parse(resource);
102
103
104 assertTrue("Incorrect type: " + request, request instanceof SubscribeDocument);
105
106 SubscribeDocument document = (SubscribeDocument) request;
107 System.out.println("Got document: " + document);
108
109 SubscribeDocument.Subscribe subscribe = document.getSubscribe();
110 DeliveryType delivery = subscribe.getDelivery();
111
112 assertTrue("Should have a delivery", delivery != null);
113
114 System.out.println("got delivery: " + delivery);
115
116 String mode = delivery.getMode();
117
118 System.out.println("Mode is: " + mode);
119 assertEquals("Cheese", mode);
120
121 XmlObject[] xmlObjects = delivery.selectPath("declare namespace wsa=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\" */wsa:Address");
122
123 assertTrue("Should have found an object: " + xmlObjects, xmlObjects != null);
124 assertEquals("Number of objects found", 1, xmlObjects.length);
125
126 XmlObject addressObject = xmlObjects[0];
127
128 assertTrue("Incorrect type: " + addressObject, addressObject instanceof AttributedURI);
129
130 AttributedURI address = (AttributedURI) addressObject;
131 System.out.println("Got address: " + address);
132
133 String addressText = address.getStringValue();
134 System.out.println("Found address: " + addressText);
135 assertEquals("http://www.example.com/MyEventSink/OnStormWarning", addressText);
136 }
137
138
139 public void testLoad() throws Exception {
140 XMLStreamReader in = createXMLStreamReader(SnowboardService.class.getResource("sampleRequest.xml"));
141 XmlObject xmlObject = XmlObject.Factory.parse(in);
142
143 System.out.println("Created: " + xmlObject);
144
145 XmlObject[] xmlObjects = xmlObject.selectPath("declare namespace es=\"http://namespaces.snowboard-info.com\" */*/es:GetEndorsingBoarder");
146
147 assertTrue("Should have found an object: " + xmlObjects, xmlObjects != null);
148 assertEquals("Number of objects found", 1, xmlObjects.length);
149
150 XmlObject request = xmlObjects[0];
151
152
153 System.out.println("Class: " + request.getClass());
154
155 Node domNode = request.getDomNode();
156 System.out.println("DOM node: " + domNode + " type: " + domNode.getClass());
157
158
159
160
161
162
163
164
165
166
167
168 }
169 }