1 7 8 package org.netbeans.modules.xml.schema.model.readwrite; 9 10 import java.io.File ; 11 import java.util.Collection ; 12 import java.util.Iterator ; 13 import javax.swing.text.Document ; 14 import junit.framework.*; 15 import org.netbeans.junit.NbTestCase; 16 import org.netbeans.modules.xml.schema.model.*; 17 import org.netbeans.modules.xml.schema.model.impl.SchemaComponentFactoryImpl; 18 import org.netbeans.modules.xml.schema.model.impl.SchemaImpl; 19 import org.netbeans.modules.xml.schema.model.impl.SchemaModelImpl; 20 import org.netbeans.modules.xml.schema.model.visitor.DefaultSchemaVisitor; 21 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent; 22 import org.netbeans.modules.xml.xam.dom.NamedComponentReference; 23 import org.openide.awt.UndoRedo.Empty; 24 import org.w3c.dom.Text ; 25 29 public class PurchaseOrderTest extends NbTestCase { 30 31 public PurchaseOrderTest(String testName) { 32 super(testName); 33 34 } 35 36 public static final String TEST_XSD = "resources/PurchaseOrder.xsd"; 37 38 protected void setUp() throws Exception { 39 } 40 41 protected void tearDown() throws Exception { 42 TestCatalogModel.getDefault().clearDocumentPool(); 43 } 44 45 public String getSchemaResourcePath() { 46 return TEST_XSD; 47 } 48 49 public void testRead() throws Exception { 50 SchemaModel model = Util.loadSchemaModel(getSchemaResourcePath()); 51 SchemaImpl si = (SchemaImpl) model.getSchema(); 52 si.accept(new SchemaTestVisitor()); 53 assertTrue(seenRef); 54 } 55 56 public void testWrite() throws Exception { 57 SchemaModel model = Util.createEmptySchemaModel(); 58 SchemaImpl si = (SchemaImpl)model.getSchema(); 59 assertNotNull(si); 60 SchemaComponentFactory factory = model.getFactory(); 61 62 model.startTransaction(); 64 si.setAttributeFormDefault(Form.UNQUALIFIED); 65 si.setElementFormDefault(Form.UNQUALIFIED); 66 si.setTargetNamespace("http://www.example.com/PO1"); 67 68 69 GlobalComplexType usAddressComplexType = factory.createGlobalComplexType(); 71 si.addComplexType(usAddressComplexType); 72 assertNull("no xsd prefix", ((AbstractDocumentComponent)si).getPeer().getPrefix()); 73 usAddressComplexType.setName("USAddress"); 74 Sequence sequence = factory.createSequence(); 75 LocalElement el = factory.createLocalElement(); 77 el.setName("name"); 78 GlobalSimpleType stringType = Util.getPrimitiveType("string"); 79 NamedComponentReference<GlobalSimpleType> ref = 80 factory.createGlobalReference(stringType, GlobalSimpleType.class, 81 el); 82 el.setType(ref); 83 assertEquals("refString", "xsd:string", ref.getRefString()); 84 sequence.addContent(el, 0); 85 86 89 el = factory.createLocalElement(); 91 el.setName("street"); 92 ref = factory.createGlobalReference(stringType, GlobalSimpleType.class, 93 el); 94 95 el.setType(ref); 96 sequence.addContent(el, 1); 97 usAddressComplexType.setDefinition(sequence); 98 99 GlobalElement commentGE = factory.createGlobalElement(); 101 si.addElement(commentGE); 102 commentGE.setName("comment"); 103 ref = factory.createGlobalReference(stringType, GlobalSimpleType.class, 104 commentGE); 105 commentGE.setType(ref); 106 107 GlobalComplexType poComplexType = factory.createGlobalComplexType(); 109 si.addComplexType(poComplexType); 110 poComplexType.setName("PurchaseOrderType"); 111 sequence = factory.createSequence(); 112 el = factory.createLocalElement(); 113 el.setName("shipTo"); 114 NamedComponentReference<GlobalComplexType>ref2 = 115 factory.createGlobalReference(usAddressComplexType, 116 GlobalComplexType.class, el); 117 118 el.setType(ref2); 119 sequence.addContent(el, 0); 120 el = factory.createLocalElement(); 121 el.setName("billTo"); 122 ref2 = factory.createGlobalReference(usAddressComplexType, 123 GlobalComplexType.class, el); 124 125 el.setType(ref); 126 sequence.addContent(el, 1); 127 ElementReference er = factory.createElementReference(); 128 NamedComponentReference<GlobalElement> refEl = 129 factory.createGlobalReference(commentGE, GlobalElement.class, el); 130 131 er.setRef(refEl); 132 er.setMinOccurs(0); 133 sequence.addContent(er, 2); 134 poComplexType.setDefinition(sequence); 135 136 GlobalElement poGE = factory.createGlobalElement(); 138 si.addElement(poGE); 139 poGE.setName("purchaseOrder"); 140 ref2 = factory.createGlobalReference(poComplexType, 141 GlobalComplexType.class, poGE); 142 143 poGE.setType(ref2); 144 145 GlobalSimpleType simpleType = factory.createGlobalSimpleType(); 147 si.addSimpleType(simpleType); 148 simpleType.setName("allNNI"); 149 Annotation ann = factory.createAnnotation(); 150 simpleType.setAnnotation(ann); 151 Documentation documentation = factory.createDocumentation(); 152 ann.addDocumentation(documentation); 153 Text txt = model.getDocument().createTextNode("documentation for simple type"); 154 org.w3c.dom.Element e = documentation.getDocumentationElement(); 155 e.appendChild(txt); 156 documentation.setDocumentationElement(e); 157 Union union = factory.createUnion(); 158 GlobalSimpleType nonNegativeInteger = Util.getPrimitiveType("nonNegativeInteger"); 159 NamedComponentReference<GlobalSimpleType> nniRef = 160 factory.createGlobalReference(nonNegativeInteger, 161 GlobalSimpleType.class, union); 162 union.addMemberType(nniRef); 163 GlobalSimpleType nonPositiveInteger = Util.getPrimitiveType("nonPositiveInteger"); 164 NamedComponentReference<GlobalSimpleType> npiRef = 165 factory.createGlobalReference(nonPositiveInteger, 166 GlobalSimpleType.class, union); 167 union.addMemberType(npiRef); 168 simpleType.setDefinition(union); 169 170 si.setVersion("1.3"); 171 model.endTransaction(); 172 Document d = (Document ) model.getModelSource().getLookup().lookup(Document .class); 175 177 assertEquals("schema's attributeFormDefault", Form.UNQUALIFIED.toString(), si.getAttributeFormDefault().toString()); 179 assertEquals("schema's elementFormDefault", Form.UNQUALIFIED.toString(), si.getElementFormDefault().toString()); 180 assertEquals("schema's targetNamespace: ", "http://www.example.com/PO1", si.getTargetNamespace()); 181 182 184 si.accept(new SchemaTestVisitor()); 185 assertTrue(seenRef); 186 assertEquals("testWrite read again", 1, countShipToVisit); 187 } 188 189 private class SchemaTestVisitor extends DefaultSchemaVisitor { 190 191 192 public SchemaTestVisitor() { 193 } 194 195 public void visit(Schema e) { 196 java.util.List <SchemaComponent> ch = e.getChildren(); 197 for (SchemaComponent c : ch) { 198 if (c instanceof GlobalComplexType) { 199 visit((GlobalComplexType)c); 200 } else if (c instanceof GlobalElement) { 201 visit((GlobalElement) c); 202 } else if (c instanceof GlobalSimpleType){ 203 visit((GlobalSimpleType)c); 204 } 205 } 206 } 207 208 public void visit(GlobalSimpleType e){ 209 Collection <SchemaComponent> ch = e.getChildren(); 210 for (SchemaComponent c : ch) { 211 if (c instanceof Union) { 212 visit((Union)c); 213 } 214 } 215 } 216 217 public void visit(Union e){ 218 Collection <NamedComponentReference<GlobalSimpleType>> mts = e.getMemberTypes(); 219 assertEquals("Number of union member types", 2, mts.size()); 220 Iterator <NamedComponentReference<GlobalSimpleType>> iterator = mts.iterator(); 221 NamedComponentReference<GlobalSimpleType> mt = iterator.next(); 222 assertFalse(mt.isBroken()); 223 assertEquals("First union member", "nonNegativeInteger", mt.get().getName()); 224 mt = iterator.next(); 225 assertFalse(mt.isBroken()); 226 assertEquals("Second union member", "nonPositiveInteger", mt.get().getName()); 227 } 228 229 public void visit(GlobalComplexType e) { 230 231 Collection <SchemaComponent> ch = e.getChildren(); 232 for (SchemaComponent c : ch) { 233 if (c instanceof Sequence) { 234 visit((Sequence)c); 235 } 236 } 237 } 238 239 public void visit(Sequence e) { 240 241 Collection <SchemaComponent> ch = e.getChildren(); 242 for (SchemaComponent c : ch) { 243 if (c instanceof LocalElement) { 244 visit((LocalElement)c); 245 } 246 if (c instanceof ElementReference) { 247 visit((ElementReference)c); 248 } 249 } 250 } 251 262 public void visit(ElementReference e) { 263 if (e.getRef() != null && e.getRef().get() != null) { 264 NamedComponentReference<GlobalElement> ge = e.getRef(); 265 assertTrue("PurchaseOrderType.ref(po:comment)", ge.getRefString().endsWith(":comment")); 266 seenRef = true; 267 } 268 } 269 270 public void visit(LocalElement e) { 271 if (e.getName().equals("shipTo")) { 272 NamedComponentReference<? extends GlobalType> t = e.getType(); 273 assertTrue("PurchaseOrderType:shipTo ref GlobalComplexType", t.get() instanceof GlobalComplexType); 274 GlobalComplexType gt = (GlobalComplexType)t.get(); 275 assertEquals("PurchaseOrderType:shipTo complexType.name", "USAddress", gt.getName()); 276 countShipToVisit++; 277 } else if (e.getName().equals("street")) { 278 NamedComponentReference<? extends GlobalType> t = e.getType(); 279 GlobalSimpleType gst = (GlobalSimpleType) t.get(); 280 assertEquals("USAddress:street type string", "string", gst.getName()); 281 } 282 } 283 284 288 public void visit(GlobalElement e) { 289 290 if (e.getName().equals("purchaseOrder")) { 291 assertTrue("purchaseOrder is of ComplexType", e.getType().get() instanceof GlobalComplexType); 292 GlobalComplexType gct = (GlobalComplexType) e.getType().get(); 293 assertEquals("purchaseOrder.type", "PurchaseOrderType", gct.getName()); 294 } else if (e.getName().equals("comment")) { 295 296 assertTrue("comment is a PrimitiveType (GlobalSimpleType)", e.getType().get() instanceof GlobalSimpleType); 297 } 298 } 299 } 300 301 boolean seenRef = false; 302 int countShipToVisit = 0; 303 304 public void testPrefixConsolidation() throws Exception { 305 TestCatalogModel.getDefault().clearDocumentPool(); 306 SchemaModelImpl model = (SchemaModelImpl) Util.createEmptySchemaModel(); 307 Schema s = model.getSchema(); 308 SchemaComponentFactory factory = model.getFactory(); 309 310 GlobalElement ge1 = factory.createGlobalElement(); 311 ge1.setName("my-auto-loan-application"); 312 LocalComplexType lct = factory.createLocalComplexType(); 313 assertNotNull(lct.getPeer().getAttribute("xmlns:xsd")); 314 ge1.setInlineType(lct); 315 assertNull(lct.getPeer().getAttribute("xmlns:xsd")); 316 ComplexContent cc = factory.createComplexContent(); 317 assertNotNull(cc.getPeer().getAttribute("xmlns:xsd")); 318 lct.setDefinition(cc); 319 assertNull(cc.getPeer().getAttribute("xmlns:xsd")); 320 321 model.startTransaction(); 322 model.getSchema().addElement(ge1); 323 model.endTransaction(); 324 325 assertNull(lct.getPeer().getAttribute("xmlns:xsd")); 326 assertNull(cc.getPeer().getAttribute("xmlns:xsd")); 327 } 328 } 329 | Popular Tags |