1 9 10 package org.netbeans.modules.xml.wsdl.model.readwrite; 11 12 import java.util.Collection ; 13 import java.util.Iterator ; 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 41 public class SchemaReadWriteTest extends TestCase implements TestReadWrite{ 42 43 44 public SchemaReadWriteTest(String testName) { 45 super(testName); 46 } 47 48 protected void setUp() throws Exception { 49 } 50 51 protected void tearDown() throws Exception { 52 TestCatalogModel.getDefault().clearDocumentPool(); 53 } 54 55 58 public void testWrite() throws Exception { 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 <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 Collection <Schema> schemas = types.getSchemas(); 87 assertEquals("number of schemas " , 1 , schemas.size()); 88 } 89 90 91 96 public void testRead() throws Exception { 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 <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 <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 { 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 <Message> messages = d.getMessages(); 129 assertEquals("number of messages", 2, messages.size()); 130 Message message = messages.iterator().next(); 132 Collection <Part> parts = message.getParts(); 133 assertEquals("number of parts", 1, parts.size()); 134 Part part = parts.iterator().next(); 136 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 { 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 <Schema> schemas = types.getSchemas(); 150 Schema schema = schemas.iterator().next(); 151 Collection <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 Collection <Message> messages = d.getMessages(); 170 assertEquals("number of messages", 3 , messages.size()); 171 Iterator <Message> mIterator = messages.iterator(); 172 mIterator.next(); 173 mIterator.next(); 174 Message m = mIterator.next(); 176 Collection <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 } 185 186 public String getTestResourcePath() { 187 return "resources/stockquote.xml"; 188 } 189 190 public String getTestResourcePath2(){ 191 return "resources/emptyStockquote.xml"; 192 } 193 } 194 | Popular Tags |