KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > model > SchemaModelTest


1 package org.netbeans.modules.xml.schema.model;
2 import java.beans.PropertyChangeEvent JavaDoc;
3 import junit.framework.*;
4 import java.beans.PropertyChangeListener JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.Collection JavaDoc;
7 import javax.swing.text.Document JavaDoc;
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 /*
12  * SchemaModelTest.java
13  * JUnit based test
14  *
15  * Created on October 3, 2005, 3:51 PM
16  */

17
18 /**
19  *
20  * @author nn136682
21  */

22 public class SchemaModelTest extends TestCase {
23     
24     private static String JavaDoc TEST_XSD = "resources/testInclude.xsd";
25     private static String JavaDoc TEST_BAD_XSD = "resources/testBad.xsd";
26     private static String JavaDoc TEST_BAD_INCLUDE_XSD = "resources/testBadInclude.xsd";
27     private static String JavaDoc TEST_TYPES_XSD = "resources/testTypes.xsd";
28     private static String JavaDoc TEST_FAKE_XSD = "resources/fakeSchema.xsd";
29     
30     public SchemaModelTest(String JavaDoc testName) {
31         super(testName);
32     }
33     protected void setUp() throws Exception JavaDoc {
34     }
35
36     protected void tearDown() throws Exception JavaDoc {
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     /**
47      * Test of flush method, of class org.netbeans.modules.xml.schema.model.api.SchemaModel.
48      */

49     public void testFlushDumpThenReload() throws Exception JavaDoc {
50         SchemaModel sm = Util.loadSchemaModel(TEST_XSD);
51         assertNotNull(sm);
52         Document JavaDoc doc = AbstractDocumentModel.class.cast(sm).getBaseDocument();
53         assertNotNull(doc);
54         Schema schema = sm.getSchema();
55         assertNotNull(schema);
56         String JavaDoc current = schema.getVersion();
57         sm.startTransaction();
58         schema.setVersion("1.3");
59         sm.endTransaction();
60     assertEquals("1.3", sm.getSchema().getVersion());
61     Document JavaDoc d = (Document JavaDoc) sm.getModelSource().getLookup().lookup(Document JavaDoc.class);
62     assertSame(doc,d);
63     
64     //System.out.println(d.getText(0, d.getLength()));
65
SchemaModel sm2 = Util.dumpAndReloadModel(sm);
66     d = (Document JavaDoc) sm2.getModelSource().getLookup().lookup(Document JavaDoc.class);
67         //System.out.println(d.getText(0, d.getLength()));
68

69         this.assertFalse("testSync", sm2.getSchema().getVersion().equals(current));
70         assertEquals("1.3", sm2.getSchema().getVersion());
71     }
72
73     /**
74      * Test of getSchema method, of class org.netbeans.modules.xmlschema.api.SchemaModel.
75      */

76     public void testGetSchema() throws Exception JavaDoc {
77         SchemaModel sm = Util.loadSchemaModel(TEST_XSD);
78         assertNotNull(sm);
79         String JavaDoc uri = sm.getSchema().getTargetNamespace();
80         String JavaDoc expectUri = "http://www.example.com/testInclude";
81         assertEquals(expectUri, uri);
82     }
83
84     /**
85      * Test of getState method, of class org.netbeans.modules.xmlschema.api.SchemaModel.
86      */

87     public void testGetState() throws Exception JavaDoc {
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     /**
98      * Test of getState method, of class org.netbeans.modules.xmlschema.api.SchemaModel.
99      */

100     public void testBadInclude() throws Exception JavaDoc {
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 JavaDoc {
110         private String JavaDoc expectedEvent;
111         private boolean gotIt = false;
112         
113         public Listener(String JavaDoc expected) {
114             expectedEvent = expected;
115             
116         }
117         public void propertyChange(PropertyChangeEvent JavaDoc 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     /**
128      * Test of addPropertyChangeListener method, of class org.netbeans.modules.xmlschema.api.SchemaModel.
129      */

130     public void testVersionChangedListener() throws Exception JavaDoc {
131         SchemaModel sm = Util.loadSchemaModel(TEST_XSD);
132         Listener JavaDoc pcl = new Listener JavaDoc(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 JavaDoc {
148             SchemaModel sm = TestCatalogModel.getDefault().getSchemaModel(NamespaceLocation.TEST_INCLUDE);
149             if (! NamespaceLocation.SOMEFILE.getResourceFile().exists()) {
150                 NamespaceLocation.SOMEFILE.refreshResourceFile();
151             }
152             Collection JavaDoc<Schema> schemas = sm.findSchemas("http://www.example.com/testInclude");
153             ArrayList JavaDoc<Schema> list = new ArrayList JavaDoc<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 JavaDoc {
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 JavaDoc {
172         SchemaModel sm = Util.loadSchemaModel(TEST_FAKE_XSD);
173         assert(sm.getState() == State.NOT_WELL_FORMED);
174 }
175 }
176
Popular Tags