1 package org.netbeans.modules.xml.schema.model; 2 import java.beans.PropertyChangeEvent ; 3 import junit.framework.*; 4 import java.beans.PropertyChangeListener ; 5 import java.util.ArrayList ; 6 import java.util.Collection ; 7 import javax.swing.text.Document ; 8 import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel; 9 import org.netbeans.modules.xml.xam.Model.State; 10 import org.netbeans.modules.xml.xam.dom.NamedComponentReference; 11 17 18 22 public class SchemaModelTest extends TestCase { 23 24 private static String TEST_XSD = "resources/testInclude.xsd"; 25 private static String TEST_BAD_XSD = "resources/testBad.xsd"; 26 private static String TEST_BAD_INCLUDE_XSD = "resources/testBadInclude.xsd"; 27 private static String TEST_TYPES_XSD = "resources/testTypes.xsd"; 28 private static String TEST_FAKE_XSD = "resources/fakeSchema.xsd"; 29 30 public SchemaModelTest(String testName) { 31 super(testName); 32 } 33 protected void setUp() throws Exception { 34 } 35 36 protected void tearDown() throws Exception { 37 TestCatalogModel.getDefault().clearDocumentPool(); 38 } 39 40 public static Test suite() { 41 TestSuite suite = new TestSuite(SchemaModelTest.class); 42 43 return suite; 44 } 45 46 49 public void testFlushDumpThenReload() throws Exception { 50 SchemaModel sm = Util.loadSchemaModel(TEST_XSD); 51 assertNotNull(sm); 52 Document doc = AbstractDocumentModel.class.cast(sm).getBaseDocument(); 53 assertNotNull(doc); 54 Schema schema = sm.getSchema(); 55 assertNotNull(schema); 56 String current = schema.getVersion(); 57 sm.startTransaction(); 58 schema.setVersion("1.3"); 59 sm.endTransaction(); 60 assertEquals("1.3", sm.getSchema().getVersion()); 61 Document d = (Document ) sm.getModelSource().getLookup().lookup(Document .class); 62 assertSame(doc,d); 63 64 SchemaModel sm2 = Util.dumpAndReloadModel(sm); 66 d = (Document ) sm2.getModelSource().getLookup().lookup(Document .class); 67 69 this.assertFalse("testSync", sm2.getSchema().getVersion().equals(current)); 70 assertEquals("1.3", sm2.getSchema().getVersion()); 71 } 72 73 76 public void testGetSchema() throws Exception { 77 SchemaModel sm = Util.loadSchemaModel(TEST_XSD); 78 assertNotNull(sm); 79 String uri = sm.getSchema().getTargetNamespace(); 80 String expectUri = "http://www.example.com/testInclude"; 81 assertEquals(expectUri, uri); 82 } 83 84 87 public void testGetState() throws Exception { 88 SchemaModel sm = Util.loadSchemaModel(TEST_XSD); 89 State s1 = sm.getState(); 90 State expResult1 = State.VALID; 91 assertEquals(expResult1, s1); 92 93 SchemaModel sm2 = Util.loadSchemaModel(TEST_BAD_XSD); 94 assertEquals("Expect not well-formed source", State.NOT_WELL_FORMED, sm2.getState()); 95 } 96 97 100 public void testBadInclude() throws Exception { 101 SchemaModel sm = Util.loadSchemaModel(TEST_XSD); 102 State s1 = sm.getState(); 103 State expResult1 = State.VALID; 104 assertEquals(expResult1, s1); 105 SchemaModel sm2 = Util.loadSchemaModel(TEST_BAD_INCLUDE_XSD); 106 assertEquals("Expect not well-formed source", State.NOT_WELL_FORMED, sm2.getState()); 107 } 108 109 private class Listener implements PropertyChangeListener { 110 private String expectedEvent; 111 private boolean gotIt = false; 112 113 public Listener(String expected) { 114 expectedEvent = expected; 115 116 } 117 public void propertyChange(PropertyChangeEvent evt) { 118 assertNotNull(evt); 119 System.out.println("Got " + evt.getPropertyName()); 120 assertEquals(expectedEvent, evt.getPropertyName()); 121 gotIt = true; 122 } 123 public boolean gotIt() { return gotIt; } 124 public void resetGotIt() { gotIt = false; } 125 } 126 127 130 public void testVersionChangedListener() throws Exception { 131 SchemaModel sm = Util.loadSchemaModel(TEST_XSD); 132 Listener pcl = new Listener (Schema.VERSION_PROPERTY); 133 sm.addPropertyChangeListener(pcl); 134 sm.startTransaction(); 135 sm.getSchema().setVersion("1.0"); 136 sm.endTransaction(); 137 assertTrue("test VersionChanged event", pcl.gotIt()); 138 139 pcl.resetGotIt(); 140 sm.removePropertyChangeListener(pcl); 141 sm.startTransaction(); 142 sm.getSchema().setVersion("1.1"); 143 sm.endTransaction(); 144 assertFalse("test remove listener, no more events", pcl.gotIt()); 145 } 146 147 public void testCircularInclude() throws Exception { 148 SchemaModel sm = TestCatalogModel.getDefault().getSchemaModel(NamespaceLocation.TEST_INCLUDE); 149 if (! NamespaceLocation.SOMEFILE.getResourceFile().exists()) { 150 NamespaceLocation.SOMEFILE.refreshResourceFile(); 151 } 152 Collection <Schema> schemas = sm.findSchemas("http://www.example.com/testInclude"); 153 ArrayList <Schema> list = new ArrayList <Schema>(schemas); 154 assertEquals("circular include is ok", 2, schemas.size()); 155 assertTrue("2 distinct schema", list.get(1) != list.get(0)); 156 assertEquals("http://www.example.com/testInclude", list.get(0).getTargetNamespace()); 157 } 158 159 public void testTypes() throws Exception { 160 SchemaModel sm = Util.loadSchemaModel(TEST_TYPES_XSD); 161 GlobalComplexType gct = (GlobalComplexType)sm.getSchema().getChildren().get(0); 162 ComplexTypeDefinition ctd = gct.getDefinition(); 163 ComplexExtension ce = (ComplexExtension)ctd.getChildren().get(0); 164 NamedComponentReference<GlobalType> ncr = ce.getBase(); 165 GlobalType type = ncr.get(); 166 assert(type != null && type instanceof GlobalSimpleType); 167 GlobalSimpleType gst = (GlobalSimpleType)type; 168 assert(gst.getName() != null && gst.getName().equals("anyType")); 169 } 170 171 public void testFakeSchema() throws Exception { 172 SchemaModel sm = Util.loadSchemaModel(TEST_FAKE_XSD); 173 assert(sm.getState() == State.NOT_WELL_FORMED); 174 } 175 } 176 | Popular Tags |