KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > model > readwrite > SimpleTest


1 package org.netbeans.modules.xml.wsdl.model.readwrite;
2
3 import java.io.File JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Arrays JavaDoc;
6 import java.util.Collection JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.List JavaDoc;
9 import junit.framework.*;
10 import org.netbeans.junit.NbTestCase;
11 import org.netbeans.modules.xml.schema.model.GlobalType;
12 import org.netbeans.modules.xml.wsdl.model.Binding;
13 import org.netbeans.modules.xml.wsdl.model.BindingFault;
14 import org.netbeans.modules.xml.wsdl.model.BindingInput;
15 import org.netbeans.modules.xml.wsdl.model.BindingOperation;
16 import org.netbeans.modules.xml.wsdl.model.BindingOutput;
17 import org.netbeans.modules.xml.wsdl.model.Definitions;
18 import org.netbeans.modules.xml.wsdl.model.Input;
19 import org.netbeans.modules.xml.wsdl.model.Message;
20 import org.netbeans.modules.xml.wsdl.model.Operation;
21 import org.netbeans.modules.xml.wsdl.model.Output;
22 import org.netbeans.modules.xml.wsdl.model.Part;
23 import org.netbeans.modules.xml.wsdl.model.Port;
24 import org.netbeans.modules.xml.wsdl.model.PortType;
25 import org.netbeans.modules.xml.wsdl.model.RequestResponseOperation;
26 import org.netbeans.modules.xml.wsdl.model.Service;
27 import org.netbeans.modules.xml.wsdl.model.TestCatalogModel;
28 import org.netbeans.modules.xml.wsdl.model.Types;
29 import org.netbeans.modules.xml.wsdl.model.Util;
30 import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
31 import org.netbeans.modules.xml.wsdl.model.WSDLComponentFactory;
32 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
33 import org.netbeans.modules.xml.wsdl.model.extensions.soap.SOAPAddress;
34 import org.netbeans.modules.xml.wsdl.model.extensions.soap.SOAPBinding;
35 import org.netbeans.modules.xml.wsdl.model.extensions.soap.SOAPBody;
36 import org.netbeans.modules.xml.wsdl.model.extensions.soap.SOAPFault;
37 import org.netbeans.modules.xml.wsdl.model.extensions.soap.SOAPMessageBase;
38 import org.netbeans.modules.xml.wsdl.model.extensions.soap.SOAPOperation;
39 import org.netbeans.modules.xml.wsdl.model.extensions.xsd.WSDLSchema;
40 import org.netbeans.modules.xml.wsdl.model.visitor.FindWSDLComponent;
41 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
42 import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
43
44 /**
45  *
46  * @author Nam Nguyen
47  */

48 public class SimpleTest extends NbTestCase {
49     
50     public SimpleTest(String JavaDoc testName) {
51         super(testName);
52     }
53
54     protected void setUp() throws Exception JavaDoc {
55     }
56
57     protected void tearDown() throws Exception JavaDoc {
58         TestCatalogModel.getDefault().clearDocumentPool();
59     }
60
61     public void testWrite() throws Exception JavaDoc {
62         WSDLModel model = Util.loadWSDLModel("resources/empty.wsdl");
63         WSDLComponentFactory fact = model.getFactory();
64         
65         model.startTransaction();
66         Definitions d = model.getDefinitions();
67         d.setName("HelloService");
68         d.setTargetNamespace("urn:HelloService/wsdl");
69         Types types = fact.createTypes();
70         d.setTypes(types);
71         WSDLSchema wsdlSchema = fact.createWSDLSchema();
72         types.addExtensibilityElement(wsdlSchema);
73         wsdlSchema.getSchemaModel().getSchema().setTargetNamespace("urn:HelloService/wsdl");
74         
75         Message m1 = fact.createMessage();
76         Message m2 = fact.createMessage();
77         d.addMessage(m1); d.addMessage(m2);
78         m1.setName("HelloServiceSEI_sayHello");
79         m2.setName("HelloServiceSEI_sayHelloResponse");
80         Part p = fact.createPart();
81         p.setName("String_1"); //TODO test setType
82
p.setType(p.createSchemaReference(Util.getPrimitiveType("string"), GlobalType.class));
83         m1.addPart(p);
84         p = fact.createPart();
85         p.setName("String_2");
86         p.setType(p.createSchemaReference(Util.getPrimitiveType("string"), GlobalType.class));
87         m1.addPart(p);
88         p = fact.createPart();
89         p.setName("result");
90         p.setType(p.createSchemaReference(Util.getPrimitiveType("string"), GlobalType.class));
91         m2.addPart(p);
92
93         PortType pt = fact.createPortType();
94         d.addPortType(pt);
95         pt.setName("HelloServiceSEI");
96         RequestResponseOperation op = fact.createRequestResponseOperation();
97         pt.addOperation(op);
98         op.setName("sayHello");
99         op.setParameterOrder(Arrays.asList(new String JavaDoc[] {"String_1", "String_2"}));
100         Input in = fact.createInput();
101         op.setInput(in);
102         in.setMessage(in.createReferenceTo(m1, Message.class));
103         NamedComponentReference<Message> msgRef = in.getMessage();
104               
105         assertEquals("setTargetNamespace should declare prefix", "tns:HelloServiceSEI_sayHello", msgRef.getRefString());
106         
107         Output out = fact.createOutput();
108         op.setOutput(out);
109         out.setMessage(out.createReferenceTo(m2, Message.class));
110         
111         Binding b = fact.createBinding();
112         d.addBinding(b);
113         b.setName("HelloServiceSEIBinding");
114         b.setType(b.createReferenceTo(pt, PortType.class));
115         BindingOperation bo = fact.createBindingOperation();
116         b.addBindingOperation(bo);
117         bo.setName("sayHello");
118         SOAPBinding sb = fact.createSOAPBinding();
119         sb.setTransportURI("http://schemas.xmlsoap.org/soap/http");
120         sb.setStyle(SOAPBinding.Style.RPC);
121         b.addExtensibilityElement(sb);
122         SOAPOperation soo = fact.createSOAPOperation();
123         bo.addExtensibilityElement(soo);
124         soo.setSoapAction("");
125         
126         assertTrue(b.getChildren().get(0) == sb);
127         assertTrue(b.getChildren().get(1) == bo);
128         
129         BindingInput bin = fact.createBindingInput();
130         bo.setBindingInput(bin);
131         SOAPBody body = fact.createSOAPBody();
132         bin.addExtensibilityElement(body);
133         body.setUse(SOAPMessageBase.Use.LITERAL);
134         body.setNamespace("urn:HelloService/wsdl");
135         
136         BindingOutput bout = fact.createBindingOutput();
137         bo.setBindingOutput(bout);
138         body = fact.createSOAPBody();
139         bout.addExtensibilityElement(body);
140         body.setUse(SOAPMessageBase.Use.LITERAL);
141         body.setNamespace("urn:HelloService/wsdl");
142         
143         
144         BindingFault bfault = fact.createBindingFault();
145         bo.addBindingFault(bfault);
146         SOAPFault sFault = fact.createSOAPFault();
147         bfault.addExtensibilityElement(sFault);
148         sFault.setUse(SOAPMessageBase.Use.LITERAL);
149         sFault.setNamespace("urn:HelloService/wsdl");
150         
151         //test the order in which children were added in binding operation
152
List JavaDoc children = bo.getChildren();
153         Iterator JavaDoc it = children.iterator();
154         assertEquals("binding.operation.soapOperation", soo, it.next());
155         assertEquals("binding.operation.input", bin, it.next());
156         assertEquals("binding.operation.output", bout, it.next());
157         assertEquals("binding.operation.fault", bfault, it.next());
158         
159         Service service = fact.createService();
160         d.addService(service);
161         service.setName("HelloService");
162         Port port = fact.createPort();
163         service.addPort(port);
164         port.setName("HelloServiceSEIPort");
165         port.setBinding(port.createReferenceTo(b, Binding.class));
166         SOAPAddress sad = fact.createSOAPAddress();
167         port.addExtensibilityElement(sad);
168         sad.setLocation("REPLACE_WITH_ACTUAL_URL");
169         
170         model.endTransaction();
171
172         //Util.dumpToFile(model.getBaseDocument(), new File("C:\\temp\\HelloService.wsdl"));
173
readAndCheck(model);
174     }
175
176     public void testRead() throws Exception JavaDoc {
177         WSDLModel model = Util.loadWSDLModel(getTestResourcePath());
178         readAndCheck(model);
179     }
180     
181     private void readAndCheck(WSDLModel model) {
182         Definitions d = model.getDefinitions();
183         Collection JavaDoc<Message> messages = d.getMessages();
184         assertEquals("read.message.count", 2, messages.size());
185         Iterator JavaDoc<Message> it = messages.iterator(); it.next();
186         Message m = it.next();
187         AbstractDocumentComponent acm = (AbstractDocumentComponent) m;
188         String JavaDoc prefix = acm.getPeer().getPrefix();
189         assertTrue("wsdl prefix is not null or empty: "+prefix, prefix == null || prefix.equals(""));
190         assertEquals("read.message.name", "HelloServiceSEI_sayHelloResponse", m.getName());
191         List JavaDoc<Part> parts = new ArrayList JavaDoc(m.getParts());
192         assertEquals("read.message.part.name", "result", parts.iterator().next().getName());
193         assertEquals("string", parts.get(0).getType().get().getName());
194         
195         Collection JavaDoc<PortType> porttypes = d.getPortTypes();
196         assertEquals("read.portType", 1, porttypes.size());
197         PortType pt = porttypes.iterator().next();
198         assertEquals("read.portType.name", "HelloServiceSEI", pt.getName());
199         Operation op = pt.getOperations().iterator().next();
200         assertTrue("read.portType.operation", op instanceof RequestResponseOperation);
201         assertEquals("read.portType.operation.parameterOrder", "[String_1, String_2]", op.getParameterOrder().toString());
202         
203         Message m1 = d.getMessages().iterator().next();
204         assertEquals("message[1].name", "HelloServiceSEI_sayHello", m1.getName());
205         assertEquals("message[1].parts.count=2", 2, m1.getParts().size());
206
207         String JavaDoc xpath0 = "/definitions/message[1]";
208         WSDLComponent found = (WSDLComponent) new FindWSDLComponent().findComponent(model.getRootComponent(), xpath0);
209         assertTrue(xpath0, found instanceof Message);
210         Message m1x = (Message) found;
211         assertEquals("write.xpath", m1, m1x);
212
213         RequestResponseOperation rro = (RequestResponseOperation) op;
214         assertEquals("operation.name", "sayHello", rro.getName());
215         Input in = rro.getInput();
216         assertEquals("portType.operation.input.message", messages.iterator().next(), in.getMessage().get());
217
218         Output out = rro.getOutput();
219         assertEquals("portType.operation.output.message", m.getName(), out.getMessage().get().getName());
220         
221         Binding b = d.getBindings().iterator().next();
222         Collection JavaDoc<SOAPBinding> soapB = b.getExtensibilityElements(SOAPBinding.class);
223         SOAPBinding sb = soapB.iterator().next();
224         assertEquals("binding.soap.style", SOAPBinding.Style.RPC, sb.getStyle());
225         assertEquals("binding.soap.uri", "http://schemas.xmlsoap.org/soap/http", sb.getTransportURI());
226
227         BindingOperation bo = b.getBindingOperations().iterator().next();
228         Collection JavaDoc<SOAPOperation> soapOps = bo.getExtensibilityElements(SOAPOperation.class);
229         assertEquals("binding.soap.style", SOAPBinding.Style.RPC, soapOps.iterator().next().getStyle());
230         assertEquals("binding.soap.uri", "", soapOps.iterator().next().getSoapAction());
231
232         assertEquals("binding.type", pt, b.getType().get());
233         assertEquals("binding.name", "HelloServiceSEIBinding", b.getName());
234         assertEquals("sayHelloRequest", bo.getBindingInput().getInput().get().getName());
235         SOAPBody body = (SOAPBody) bo.getBindingInput().getExtensibilityElements().iterator().next();
236         assertEquals("binding.operation.input", SOAPBody.Use.LITERAL, body.getUse());
237         SOAPBody body2 = (SOAPBody) bo.getBindingOutput().getExtensibilityElements().iterator().next();
238         assertEquals("binding.operation.output", "urn:HelloService/wsdl", body2.getNamespace());
239         
240         Collection JavaDoc<BindingFault> bFaults = bo.getBindingFaults();
241         assertEquals("binding.operation.faults", 1, bFaults.size());
242         BindingFault fault = bFaults.iterator().next();
243         SOAPFault sfault = (SOAPFault)fault.getExtensibilityElements().iterator().next();
244         assertEquals("binding.operation.fault", "urn:HelloService/wsdl", sfault.getNamespace());
245         
246         Service s = d.getServices().iterator().next();
247         assertEquals("serice.name", "HelloService", s.getName());
248         Port p = s.getPorts().iterator().next();
249         assertEquals("service.port.name", "HelloServiceSEIPort", p.getName());
250         Binding binding = p.getBinding().get();
251         assertEquals("service.port.binding", b, binding);
252         SOAPAddress soapAddress = p.getExtensibilityElements(SOAPAddress.class).iterator().next();
253         assertEquals("service.port.soapAddress", "REPLACE_WITH_ACTUAL_URL", soapAddress.getLocation());
254         
255     }
256
257     public String JavaDoc getTestResourcePath() {
258         return "resources/HelloService.wsdl";
259     }
260     
261     public void testPrefixPreference() throws Exception JavaDoc {
262         WSDLModel model = Util.loadWSDLModel("resources/empty.wsdl");
263         model.startTransaction();
264         model.getDefinitions().setTypes(model.getFactory().createTypes());
265         model.endTransaction();
266         assertNull(model.getDefinitions().getTypes().getPeer().getPrefix());
267         assertNull(model.getDefinitions().getTypes().getPeer().getAttribute("xmlns:wsdl"));
268         assertNull(model.getDefinitions().getTypes().getPeer().getAttribute("xmlns"));
269         //Util.dumpToFile(model.getBaseDocument(), new File(getWorkDir(), "test.wsdl"));
270
}
271 }
272
Popular Tags