KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * SchemaTest.java
3  *
4  * Created on December 9, 2005, 11:53 AM
5  *
6  * To change this template, choose Tools | Template Manager
7  * and open the template in the editor.
8  */

9
10 package org.netbeans.modules.xml.wsdl.model.readwrite;
11
12 import java.util.Collection JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import junit.framework.TestCase;
15 import org.netbeans.modules.xml.schema.model.GlobalElement;
16 import org.netbeans.modules.xml.schema.model.Schema;
17 import org.netbeans.modules.xml.schema.model.SchemaModel;
18 import org.netbeans.modules.xml.wsdl.model.Binding;
19 import org.netbeans.modules.xml.wsdl.model.BindingInput;
20 import org.netbeans.modules.xml.wsdl.model.BindingOperation;
21 import org.netbeans.modules.xml.wsdl.model.Definitions;
22 import org.netbeans.modules.xml.wsdl.model.ExtensibilityElement;
23 import org.netbeans.modules.xml.wsdl.model.Message;
24 import org.netbeans.modules.xml.wsdl.model.Part;
25 import org.netbeans.modules.xml.wsdl.model.TestCatalogModel;
26 import org.netbeans.modules.xml.wsdl.model.Types;
27 import org.netbeans.modules.xml.wsdl.model.Util;
28 import org.netbeans.modules.xml.wsdl.model.WSDLComponentFactory;
29 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
30 import org.netbeans.modules.xml.wsdl.model.extensions.soap.SOAPHeader;
31 import org.netbeans.modules.xml.wsdl.model.extensions.xsd.WSDLSchema;
32 import org.netbeans.modules.xml.wsdl.model.extensions.xsd.impl.WSDLSchemaImpl;
33 import org.netbeans.modules.xml.wsdl.model.impl.WSDLComponentFactoryImpl;
34 import org.netbeans.modules.xml.wsdl.model.impl.WSDLModelImpl;
35 import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
36
37 /**
38  *
39  * @author rico
40  */

41 public class SchemaReadWriteTest extends TestCase implements TestReadWrite{
42     
43     /** Creates a new instance of SchemaTest */
44     public SchemaReadWriteTest(String JavaDoc testName) {
45         super(testName);
46     }
47     
48     protected void setUp() throws Exception JavaDoc {
49     }
50
51     protected void tearDown() throws Exception JavaDoc {
52         TestCatalogModel.getDefault().clearDocumentPool();
53     }
54     
55     /**
56      * Reconstruct the schema pointed by #getSchemaResourcePath from empty schema.
57      */

58     public void testWrite() throws Exception JavaDoc {
59   
60         WSDLModel model = Util.loadWSDLModel(getTestResourcePath2());
61         WSDLComponentFactory fact = model.getFactory();
62         this.assertNotNull(model);
63         Definitions d = model.getDefinitions();
64         this.assertNotNull(d);
65         
66         model.startTransaction();
67         d.setName("StockQuote");
68         d.setTargetNamespace("http://example.com/stockquote.wsdl");
69         Types types = fact.createTypes();
70         WSDLComponentFactoryImpl factory = new WSDLComponentFactoryImpl((WSDLModelImpl)model);
71         WSDLSchema wsdlSchema = factory.createWSDLSchema();
72         types.addExtensibilityElement(wsdlSchema);
73         model.endTransaction();
74         Collection JavaDoc<ExtensibilityElement> ees = types.getExtensibilityElements();
75         assertEquals("number of EE", 1, ees.size());
76         SchemaModel schemaModel = wsdlSchema.getSchemaModel();
77         assertNotNull("schemaModel not null", schemaModel);
78         Schema schema = schemaModel.getSchema();
79         assertNotNull("schema not null", schema);
80         GlobalElement ge = schemaModel.getFactory().createGlobalElement();
81         model.startTransaction();
82         schema.addElement(ge);
83         model.endTransaction();
84         //File dumpFile = Util.dumpToTempFile(((WSDLModelImpl)model).getBaseDocument());
85
//System.out.println("dumpFile; " + dumpFile.getCanonicalPath());
86
Collection JavaDoc<Schema> schemas = types.getSchemas();
87         assertEquals("number of schemas " , 1 , schemas.size());
88     }
89     
90
91     /**
92      * Test reading in the schema specified by #getSchemaResourcePath.
93      * Verifying the resulted model using a visitor or
94      * FindSchemaComponentFromDOM#findComponent method.
95      */

96     public void testRead() throws Exception JavaDoc {
97         WSDLModel model = Util.loadWSDLModel(getTestResourcePath());
98         this.assertNotNull(model);
99         Definitions d = model.getDefinitions();
100         this.assertNotNull(d);
101         Types types = d.getTypes();
102         this.assertNotNull(types);
103         Collection JavaDoc<ExtensibilityElement> ee = types.getExtensibilityElements();
104         System.out.println("number of EE: " + ee.size());
105         assertTrue("ExtensibilityElement instanceof WSDLSchemaImpl", ee.iterator().next() instanceof WSDLSchemaImpl);
106         WSDLSchemaImpl wsdlSchema = (WSDLSchemaImpl)ee.iterator().next();
107         SchemaModel schemaModel = wsdlSchema.getSchemaModel();
108         assertNotNull("schema model is not null", schemaModel);
109         Schema schema = schemaModel.getSchema();
110         assertNotNull("schema not null", schema);
111         Collection JavaDoc<GlobalElement> gElements = schema.getElements();
112         assertEquals("number of global elements", 2, gElements.size());
113         
114         Binding binding = d.getBindings().iterator().next();
115         BindingOperation bo = binding.getBindingOperations().iterator().next();
116         BindingInput bi = bo.getBindingInput();
117         SOAPHeader sh = (SOAPHeader) bi.getExtensibilityElements(SOAPHeader.class).iterator().next();
118         assertNotNull(sh.getMessage().get());
119     }
120
121     public void testReadSchemaReference() throws Exception JavaDoc {
122         WSDLModel model = Util.loadWSDLModel(getTestResourcePath());
123         this.assertNotNull(model);
124         Definitions d = model.getDefinitions();
125         this.assertNotNull(d);
126         Types types = d.getTypes();
127         this.assertNotNull(types);
128         Collection JavaDoc<Message> messages = d.getMessages();
129         assertEquals("number of messages", 2, messages.size());
130         //get the first message
131
Message message = messages.iterator().next();
132         Collection JavaDoc<Part> parts = message.getParts();
133         assertEquals("number of parts", 1, parts.size());
134         //get the part
135
Part part = parts.iterator().next();
136         //retrieve the referenced element
137
NamedComponentReference<GlobalElement> ref = part.getElement();
138         assertEquals("namespace of reference", "http://example.com/stockquote.xsd", ref.getEffectiveNamespace());
139         GlobalElement ge = ref.get();
140         assertEquals("name of global element", "TradePriceRequest", ge.getName());
141     }
142     
143     public void testWriteSchemaReference() throws Exception JavaDoc {
144         WSDLModel model = Util.loadWSDLModel(getTestResourcePath());
145         this.assertNotNull(model);
146         Definitions d = model.getDefinitions();
147         this.assertNotNull(d);
148         Types types = d.getTypes();
149         Collection JavaDoc<Schema> schemas = types.getSchemas();
150         Schema schema = schemas.iterator().next();
151         Collection JavaDoc<GlobalElement> elements = schema.getElements();
152         GlobalElement ge = elements.iterator().next();
153         
154         this.assertNotNull(types);
155         WSDLComponentFactory factory = model.getFactory();
156         Message message = factory.createMessage();
157         model.startTransaction();
158         message.setName("BogusMessage");
159         Part part = factory.createPart();
160         part.setName("BogusPart");
161         NamedComponentReference<GlobalElement> ref = part.createSchemaReference(ge, GlobalElement.class);
162         part.setElement(ref);
163         message.addPart(part);
164         d.addMessage(message);
165         model.endTransaction();
166         
167         
168         //read back the message
169
Collection JavaDoc<Message> messages = d.getMessages();
170         assertEquals("number of messages", 3 , messages.size());
171         Iterator JavaDoc<Message> mIterator = messages.iterator();
172         mIterator.next();
173         mIterator.next();
174         //get the third message
175
Message m = mIterator.next();
176         Collection JavaDoc<Part> parts = m.getParts();
177         part = parts.iterator().next();
178         NamedComponentReference<GlobalElement> gRef = part.getElement();
179         assertNotNull("global reference to part element is not null", gRef);
180         GlobalElement rsc = gRef.get();
181         assertNotNull("ReferenceableSchemaComponent should not be null", rsc);
182         //File dumpFile = Util.dumpToTempFile(((WSDLModelImpl)model).getBaseDocument());
183
//System.out.println("dumpFile; " + dumpFile.getCanonicalPath());
184
}
185     
186     public String JavaDoc getTestResourcePath() {
187         return "resources/stockquote.xml";
188     }
189     
190     public String JavaDoc getTestResourcePath2(){
191        return "resources/emptyStockquote.xml";
192     }
193 }
194
Popular Tags