KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > model > spi > GenericExtensibilityElementTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.wsdl.model.spi;
21
22 import java.util.List JavaDoc;
23 import javax.xml.namespace.QName JavaDoc;
24 import junit.framework.*;
25 import org.netbeans.modules.xml.wsdl.model.BindingOutput;
26 import org.netbeans.modules.xml.wsdl.model.Definitions;
27 import org.netbeans.modules.xml.wsdl.model.ExtensibilityElement;
28 import org.netbeans.modules.xml.wsdl.model.TestCatalogModel;
29 import org.netbeans.modules.xml.wsdl.model.Util;
30 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
31 import org.netbeans.modules.xml.wsdl.model.extensions.soap.SOAPBody;
32 import org.netbeans.modules.xml.wsdl.model.extensions.soap.SOAPHeader;
33 import org.netbeans.modules.xml.wsdl.model.visitor.FindWSDLComponent;
34 import org.w3c.dom.Element JavaDoc;
35
36 /**
37  *
38  * @author Nam Nguyen
39  */

40 public class GenericExtensibilityElementTest extends TestCase {
41     
42     public GenericExtensibilityElementTest(String JavaDoc testName) {
43         super(testName);
44     }
45
46     protected void setUp() throws Exception JavaDoc {
47     }
48
49     protected void tearDown() throws Exception JavaDoc {
50         TestCatalogModel.getDefault().clearDocumentPool();
51     }
52
53     public static Test suite() {
54         TestSuite suite = new TestSuite(GenericExtensibilityElementTest.class);
55         
56         return suite;
57     }
58
59     public void testAnyElement() throws Exception JavaDoc {
60         WSDLModel model = Util.loadWSDLModel("resources/echo.wsdl");
61         Definitions definitions = model.getDefinitions();
62         String JavaDoc xpath = "/definitions/binding/operation[@name='operation_0']/output";
63         BindingOutput output = FindWSDLComponent.findComponent(BindingOutput.class, definitions, xpath);
64         assertEquals("output", output.getName());
65         List JavaDoc<ExtensibilityElement> allEEs = output.getExtensibilityElements();
66         assertEquals(1, allEEs.size());
67         SOAPBody body = output.getExtensibilityElements(SOAPBody.class).get(0);
68         assertEquals("someNS", body.getAnyElements().get(0).getQName().getNamespaceURI());
69         
70         SOAPBody innerBody = model.getFactory().createSOAPBody();
71         model.startTransaction();
72         body.addExtensibilityElement(innerBody);
73         model.endTransaction();
74         
75         String JavaDoc localName = "element0";
76         Element e0 = model.getDocument().createElementNS("someNS", localName);
77         GenericExtensibilityElement gee = new GenericExtensibilityElement(model, e0);
78         model.startTransaction();
79         body.addAnyElement(gee, 0);
80         model.endTransaction();
81         assertEquals(localName, body.getAnyElements().get(0).getPeer().getLocalName());
82
83         localName = "element2";
84         Element e2 = model.getDocument().createElementNS("someNS", localName);
85         GenericExtensibilityElement gee2 = new GenericExtensibilityElement(model, e2);
86         model.startTransaction();
87         body.addAnyElement(gee2, 2);
88         model.endTransaction();
89         assertEquals(localName, body.getAnyElements().get(2).getPeer().getLocalName());
90         assertTrue(body.getExtensibilityElements().get(3) instanceof SOAPBody);
91
92         model.startTransaction();
93         body.removeAnyElement(gee);
94         localName = "element1";
95         Element e1 = model.getDocument().createElementNS("someNS", localName);
96         GenericExtensibilityElement gee1 = new GenericExtensibilityElement(model, e1);
97         body.addAnyElement(gee1, 1);
98         model.endTransaction();
99         assertEquals(localName, body.getAnyElements().get(1).getPeer().getLocalName());
100         assertTrue(body.getExtensibilityElements().get(3) instanceof SOAPBody);
101     }
102     
103     public void testAddAnyElementToEmptyWsdl() throws Exception JavaDoc {
104         WSDLModel model = Util.loadWSDLModel("resources/empty.wsdl");
105         Definitions definitions = model.getDefinitions();
106         
107         model.startTransaction();
108         QName JavaDoc qname = new QName JavaDoc("fooNS", "fooParent", "fo");
109         ExtensibilityElement parentEE = new GenericExtensibilityElement(model, qname);
110         definitions.addExtensibilityElement(parentEE);
111         model.endTransaction();
112         
113         model.startTransaction();
114         qname = new QName JavaDoc("fooNS", "foo", "fo");
115         ExtensibilityElement element = new GenericExtensibilityElement(model, qname);
116         String JavaDoc text = "asdfasdfsdf";
117         element.setContentFragment(text);
118         parentEE.addAnyElement(element, 0);
119         model.endTransaction();
120
121         model = Util.dumpAndReloadModel(model.getBaseDocument());
122         definitions = model.getDefinitions();
123         parentEE = definitions.getExtensibilityElements().get(0);
124         element = parentEE.getAnyElements().get(0);
125         assertEquals(text, element.getContentFragment());
126     }
127
128     public void testAddAnyElementToKnownEEwithKnownEEChildren() throws Exception JavaDoc {
129         WSDLModel model = Util.loadWSDLModel("resources/stockquote_headerFault.xml");
130         String JavaDoc xpath = "/definitions/binding/operation/input/soap:header";
131         SOAPHeader header = Util.find(SOAPHeader.class, model, xpath);
132         assertEquals(2, header.getSOAPHeaderFaults().size());
133         
134         model.startTransaction();
135         QName JavaDoc qname = new QName JavaDoc("fooNS", "foo", "fo");
136         ExtensibilityElement any = new GenericExtensibilityElement(model, qname);
137         header.addAnyElement(any, 1);
138         header.addAnyElement((ExtensibilityElement)any.copy(header), 3);
139         model.endTransaction();
140         
141         model = Util.dumpAndReloadModel(model.getBaseDocument());
142         header = Util.find(SOAPHeader.class, model, xpath);
143         any = (ExtensibilityElement)header.getChildren().get(1);
144         assertEquals(qname, any.getQName());
145         any = (ExtensibilityElement)header.getChildren().get(3);
146         assertEquals(qname, any.getQName());
147     }
148 }
149
Popular Tags