1 7 8 package org.netbeans.modules.xml.schema.model; 9 import java.io.File ; 10 import java.io.IOException ; 11 import java.util.ArrayList ; 12 import java.util.Set ; 13 import junit.framework.*; 14 import org.netbeans.modules.xml.schema.model.Schema.Block; 15 import org.netbeans.modules.xml.schema.model.Schema.Final; 16 import org.netbeans.modules.xml.schema.model.impl.SchemaModelImpl; 17 import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel; 18 import org.netbeans.modules.xml.xam.dom.NamedComponentReference; 19 23 public class SchemaTest extends TestCase { 24 25 public SchemaTest(String testName) { 26 super(testName); 27 } 28 29 public static final String TEST_XSD = "resources/PurchaseOrder.xsd"; 30 Schema schema = null; 31 SchemaModel model; 32 protected void setUp() throws Exception { 33 model = Util.loadSchemaModel(TEST_XSD); 34 schema = model.getSchema(); 35 } 36 37 protected void tearDown() throws Exception { 38 TestCatalogModel.getDefault().clearDocumentPool(); 39 } 40 41 public static Test suite() { 42 TestSuite suite = new TestSuite(SchemaTest.class); 43 return suite; 44 } 45 46 47 public void testReadElements() throws Exception { 48 ArrayList <GlobalElement> elements = new ArrayList (schema.getElements()); 49 assertEquals("Schema.getElements", 2, elements.size()); 50 assertEquals("Schema.getElements(1)", "comment", elements.get(1).getName()); 51 } 52 53 56 public void testGetAttributeFormDefault() { 57 Form f = schema.getAttributeFormDefault(); 58 assertEquals("getAttributeFormDefault", Form.UNQUALIFIED, f); 59 } 60 61 64 public void testSetAttributeFormDefault() throws Exception { 65 SchemaModel model = Util.loadSchemaModel(TEST_XSD); 66 Schema myschema = model.getSchema(); 67 model.startTransaction(); 68 myschema.setAttributeFormDefault(Form.QUALIFIED); 69 model.endTransaction(); 70 71 myschema = Util.dumpAndReloadModel(model).getSchema(); 72 assertEquals("setAttributeFormDefault", Form.QUALIFIED, myschema.getAttributeFormDefault()); 73 } 74 75 78 public void testGetBlockDefault() { 79 Set <Block> result = schema.getBlockDefault(); 80 assertNull("testGetBlockDefault", result); 81 assertEquals("testGetBlockDefaultDefault", "", schema.getBlockDefaultDefault().toString()); 82 } 83 84 public void testGetBlockDefaultEffective() throws IOException { 85 Set <Block> d1 = schema.getBlockDefaultDefault(); 86 d1.clear(); d1.add(Block.ALL); 87 model.startTransaction(); 88 schema.setBlockDefault(d1); 89 model.endTransaction(); 90 91 assertEquals("testGetBlockDefaultEffective.0", "#all", schema.getBlockDefaultEffective().toString()); 92 93 ArrayList <GlobalComplexType> types = new ArrayList (schema.getComplexTypes()); 94 GlobalComplexType typePurchaseOrder = null; 95 if (types.get(0).getName().equals("PurchaseOrderType")) { 96 typePurchaseOrder = types.get(0); 97 } 98 99 Set <GlobalComplexType.Block> d2 = typePurchaseOrder.getBlock(); 100 assertNull("testGetBlockEffective.2", d2); 101 d2 = typePurchaseOrder.getBlockEffective(); 102 assertEquals("testGetBlockDefaultEffective.3", "#all", d2.toString()); 103 } 104 105 108 public void testSetBlockDefault() throws IOException { 109 Set <Block> d = schema.getBlockDefaultDefault(); 110 d.add(Block.EXTENSION); 111 model.startTransaction(); 112 schema.setBlockDefault(d); 113 model.endTransaction(); 114 assertEquals("testSetBlockDefault.1", "extension", schema.getBlockDefault().toString()); 115 assertEquals("testSetBlockDefault.2", d, schema.getBlockDefault()); 116 } 117 118 121 public void testGetElementFormDefault() { 122 Form result = schema.getElementFormDefault(); 123 assertEquals("getElementFormDefault", Form.UNQUALIFIED, result); 124 } 125 126 129 public void testSetElementFormDefault() throws Exception { 130 SchemaModel model = Util.loadSchemaModel(TEST_XSD); 131 Schema myschema = model.getSchema(); 132 Form old = myschema.getElementFormDefault(); 133 assertEquals("getElementFormDefault", Form.UNQUALIFIED, old); 134 model.startTransaction(); 135 myschema.setElementFormDefault(Form.QUALIFIED); 136 model.endTransaction(); 137 Form now = myschema.getElementFormDefault(); 138 assertEquals("setElementFormDefault: notchanged", Form.QUALIFIED, now); 139 140 myschema = Util.dumpAndReloadModel(model).getSchema(); 141 assertEquals("setElementFormDefault", Form.QUALIFIED, myschema.getElementFormDefault()); 142 } 143 144 147 public void testGetFinalDefault() { 148 Set <Final> result = schema.getFinalDefault(); 149 assertNull("getFinalDefault", result); 150 Set <Final> def = schema.getFinalDefaultDefault(); 151 assertEquals("getFinalDefaultDefault", def.toString(), ""); 152 result = schema.getFinalDefaultEffective(); 153 assertEquals("getFinalDefaultEffective", def.toString(), result.toString()); 154 } 155 156 159 public void testSetFinalDefault() throws Exception { 160 SchemaModel model = Util.loadSchemaModel(TEST_XSD); 161 Schema myschema = model.getSchema(); 162 Set <Final> f = myschema.getFinalDefaultDefault(); 163 f.add(Final.ALL); 164 model.startTransaction(); 165 myschema.setFinalDefault(f); 166 model.endTransaction(); 167 168 myschema = Util.dumpAndReloadModel(model).getSchema(); 169 assertTrue("setFinalDefault flush failed", f.equals(myschema.getFinalDefault())); 170 } 171 172 175 public void testGetTargetNamespace() throws Exception { 176 String url = schema.getTargetNamespace(); 177 assertEquals("getTargetNamespace", "http://www.example.com/PO1", url); 178 } 179 180 public void testGetComplexTypes() { 181 ArrayList <GlobalComplexType> types = new ArrayList (schema.getComplexTypes()); 182 GlobalComplexType typePurchaseOrder = null; 183 if (types.get(0).getName().equals("PurchaseOrderType")) { 184 typePurchaseOrder = types.get(0); 185 } 186 assertNotNull("getComplexTypes", typePurchaseOrder); 187 188 ComplexTypeDefinition ctd = typePurchaseOrder.getDefinition(); 189 assertTrue("getComplexTypes:sequence", ctd instanceof Sequence); 190 191 Sequence seq = (Sequence) ctd; 192 assertEquals("getComplexTypes:PurchaseOrder:sequence count", 3, seq.getChildren().size()); 193 ArrayList <SchemaComponent> elements = new ArrayList (seq.getChildren()); 194 assertTrue("getComplexTypes:PurchaseOrder:sequence.element(0).name", elements.get(0) instanceof LocalElement); 195 LocalElement e = (LocalElement) elements.get(1); 196 assertNotNull("getComplexTypes:PurchaseOrder:billTo type null", e.getType()); 197 assertTrue("getComplexTypes:PurchaseOrder:billTo type", e.getType() instanceof NamedComponentReference); 198 NamedComponentReference<? extends GlobalType> ref = e.getType(); 199 GlobalComplexType gct = (GlobalComplexType) ref.get(); 200 assertEquals("getComplexTypes:PurchaseOrder:billTo type", "USAddress", gct.getName()); 201 } 202 203 public void testAddSchemaReferences() throws Exception { 204 Import extref = schema.getModel().getFactory().createImport(); 205 extref.setNamespace("foor"); 206 extref.setSchemaLocation("foor"); 207 model.startTransaction(); 208 schema.addExternalReference(extref); 209 model.endTransaction(); 210 int index = ((AbstractDocumentModel)model).getAccess().getElementIndexOf(schema.getPeer(), extref.getPeer()); 211 assertEquals(0, index); 212 213 GlobalElement ge = schema.getModel().getFactory().createGlobalElement(); 215 ge.setName("newElement"); 216 model.startTransaction(); 217 schema.addElement(ge); 218 model.endTransaction(); 219 index = ((AbstractDocumentModel)model).getAccess().getElementIndexOf(schema.getPeer(), extref.getPeer()); 220 assertEquals("import should still be the first child component", 0, index); 221 index = ((AbstractDocumentModel)model).getAccess().getElementIndexOf(schema.getPeer(), ge.getPeer()); 222 assertEquals("globalelement should be appended as last component", schema.getChildren().size()-1, index); 223 } 224 225 } 226 | Popular Tags |