001: /*
002: * SchemaTest.java
003: *
004: * Created on December 9, 2005, 11:53 AM
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009:
010: package org.netbeans.modules.xml.wsdl.model.readwrite;
011:
012: import java.util.Collection;
013: import java.util.Iterator;
014: import junit.framework.TestCase;
015: import org.netbeans.modules.xml.schema.model.GlobalElement;
016: import org.netbeans.modules.xml.schema.model.Schema;
017: import org.netbeans.modules.xml.schema.model.SchemaModel;
018: import org.netbeans.modules.xml.wsdl.model.Binding;
019: import org.netbeans.modules.xml.wsdl.model.BindingInput;
020: import org.netbeans.modules.xml.wsdl.model.BindingOperation;
021: import org.netbeans.modules.xml.wsdl.model.Definitions;
022: import org.netbeans.modules.xml.wsdl.model.ExtensibilityElement;
023: import org.netbeans.modules.xml.wsdl.model.Message;
024: import org.netbeans.modules.xml.wsdl.model.Part;
025: import org.netbeans.modules.xml.wsdl.model.TestCatalogModel;
026: import org.netbeans.modules.xml.wsdl.model.Types;
027: import org.netbeans.modules.xml.wsdl.model.Util;
028: import org.netbeans.modules.xml.wsdl.model.WSDLComponentFactory;
029: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
030: import org.netbeans.modules.xml.wsdl.model.extensions.soap.SOAPHeader;
031: import org.netbeans.modules.xml.wsdl.model.extensions.xsd.WSDLSchema;
032: import org.netbeans.modules.xml.wsdl.model.extensions.xsd.impl.WSDLSchemaImpl;
033: import org.netbeans.modules.xml.wsdl.model.impl.WSDLComponentFactoryImpl;
034: import org.netbeans.modules.xml.wsdl.model.impl.WSDLModelImpl;
035: import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
036:
037: /**
038: *
039: * @author rico
040: */
041: public class SchemaReadWriteTest extends TestCase implements
042: TestReadWrite {
043:
044: /** Creates a new instance of SchemaTest */
045: public SchemaReadWriteTest(String testName) {
046: super (testName);
047: }
048:
049: protected void setUp() throws Exception {
050: }
051:
052: protected void tearDown() throws Exception {
053: TestCatalogModel.getDefault().clearDocumentPool();
054: }
055:
056: /**
057: * Reconstruct the schema pointed by #getSchemaResourcePath from empty schema.
058: */
059: public void testWrite() throws Exception {
060:
061: WSDLModel model = Util.loadWSDLModel(getTestResourcePath2());
062: WSDLComponentFactory fact = model.getFactory();
063: this .assertNotNull(model);
064: Definitions d = model.getDefinitions();
065: this .assertNotNull(d);
066:
067: model.startTransaction();
068: d.setName("StockQuote");
069: d.setTargetNamespace("http://example.com/stockquote.wsdl");
070: Types types = fact.createTypes();
071: WSDLComponentFactoryImpl factory = new WSDLComponentFactoryImpl(
072: (WSDLModelImpl) model);
073: WSDLSchema wsdlSchema = factory.createWSDLSchema();
074: types.addExtensibilityElement(wsdlSchema);
075: model.endTransaction();
076: Collection<ExtensibilityElement> ees = types
077: .getExtensibilityElements();
078: assertEquals("number of EE", 1, ees.size());
079: SchemaModel schemaModel = wsdlSchema.getSchemaModel();
080: assertNotNull("schemaModel not null", schemaModel);
081: Schema schema = schemaModel.getSchema();
082: assertNotNull("schema not null", schema);
083: GlobalElement ge = schemaModel.getFactory()
084: .createGlobalElement();
085: model.startTransaction();
086: schema.addElement(ge);
087: model.endTransaction();
088: //File dumpFile = Util.dumpToTempFile(((WSDLModelImpl)model).getBaseDocument());
089: //System.out.println("dumpFile; " + dumpFile.getCanonicalPath());
090: Collection<Schema> schemas = types.getSchemas();
091: assertEquals("number of schemas ", 1, schemas.size());
092: }
093:
094: /**
095: * Test reading in the schema specified by #getSchemaResourcePath.
096: * Verifying the resulted model using a visitor or
097: * FindSchemaComponentFromDOM#findComponent method.
098: */
099: public void testRead() throws Exception {
100: WSDLModel model = Util.loadWSDLModel(getTestResourcePath());
101: this .assertNotNull(model);
102: Definitions d = model.getDefinitions();
103: this .assertNotNull(d);
104: Types types = d.getTypes();
105: this .assertNotNull(types);
106: Collection<ExtensibilityElement> ee = types
107: .getExtensibilityElements();
108: System.out.println("number of EE: " + ee.size());
109: assertTrue("ExtensibilityElement instanceof WSDLSchemaImpl", ee
110: .iterator().next() instanceof WSDLSchemaImpl);
111: WSDLSchemaImpl wsdlSchema = (WSDLSchemaImpl) ee.iterator()
112: .next();
113: SchemaModel schemaModel = wsdlSchema.getSchemaModel();
114: assertNotNull("schema model is not null", schemaModel);
115: Schema schema = schemaModel.getSchema();
116: assertNotNull("schema not null", schema);
117: Collection<GlobalElement> gElements = schema.getElements();
118: assertEquals("number of global elements", 2, gElements.size());
119:
120: Binding binding = d.getBindings().iterator().next();
121: BindingOperation bo = binding.getBindingOperations().iterator()
122: .next();
123: BindingInput bi = bo.getBindingInput();
124: SOAPHeader sh = (SOAPHeader) bi.getExtensibilityElements(
125: SOAPHeader.class).iterator().next();
126: assertNotNull(sh.getMessage().get());
127: }
128:
129: public void testReadSchemaReference() throws Exception {
130: WSDLModel model = Util.loadWSDLModel(getTestResourcePath());
131: this .assertNotNull(model);
132: Definitions d = model.getDefinitions();
133: this .assertNotNull(d);
134: Types types = d.getTypes();
135: this .assertNotNull(types);
136: Collection<Message> messages = d.getMessages();
137: assertEquals("number of messages", 2, messages.size());
138: //get the first message
139: Message message = messages.iterator().next();
140: Collection<Part> parts = message.getParts();
141: assertEquals("number of parts", 1, parts.size());
142: //get the part
143: Part part = parts.iterator().next();
144: //retrieve the referenced element
145: NamedComponentReference<GlobalElement> ref = part.getElement();
146: assertEquals("namespace of reference",
147: "http://example.com/stockquote.xsd", ref
148: .getEffectiveNamespace());
149: GlobalElement ge = ref.get();
150: assertEquals("name of global element", "TradePriceRequest", ge
151: .getName());
152: }
153:
154: public void testWriteSchemaReference() throws Exception {
155: WSDLModel model = Util.loadWSDLModel(getTestResourcePath());
156: this .assertNotNull(model);
157: Definitions d = model.getDefinitions();
158: this .assertNotNull(d);
159: Types types = d.getTypes();
160: Collection<Schema> schemas = types.getSchemas();
161: Schema schema = schemas.iterator().next();
162: Collection<GlobalElement> elements = schema.getElements();
163: GlobalElement ge = elements.iterator().next();
164:
165: this .assertNotNull(types);
166: WSDLComponentFactory factory = model.getFactory();
167: Message message = factory.createMessage();
168: model.startTransaction();
169: message.setName("BogusMessage");
170: Part part = factory.createPart();
171: part.setName("BogusPart");
172: NamedComponentReference<GlobalElement> ref = part
173: .createSchemaReference(ge, GlobalElement.class);
174: part.setElement(ref);
175: message.addPart(part);
176: d.addMessage(message);
177: model.endTransaction();
178:
179: //read back the message
180: Collection<Message> messages = d.getMessages();
181: assertEquals("number of messages", 3, messages.size());
182: Iterator<Message> mIterator = messages.iterator();
183: mIterator.next();
184: mIterator.next();
185: //get the third message
186: Message m = mIterator.next();
187: Collection<Part> parts = m.getParts();
188: part = parts.iterator().next();
189: NamedComponentReference<GlobalElement> gRef = part.getElement();
190: assertNotNull("global reference to part element is not null",
191: gRef);
192: GlobalElement rsc = gRef.get();
193: assertNotNull(
194: "ReferenceableSchemaComponent should not be null", rsc);
195: //File dumpFile = Util.dumpToTempFile(((WSDLModelImpl)model).getBaseDocument());
196: //System.out.println("dumpFile; " + dumpFile.getCanonicalPath());
197: }
198:
199: public String getTestResourcePath() {
200: return "resources/stockquote.xml";
201: }
202:
203: public String getTestResourcePath2() {
204: return "resources/emptyStockquote.xml";
205: }
206: }
|