KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * SchemaTest.java
3  * JUnit based test
4  *
5  * Created on October 5, 2005, 12:49 PM
6  */

7
8 package org.netbeans.modules.xml.schema.model;
9 import java.io.File JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.Set JavaDoc;
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 /**
20  *
21  * @author rico
22  */

23 public class SchemaTest extends TestCase {
24     
25     public SchemaTest(String JavaDoc testName) {
26         super(testName);
27     }
28
29     public static final String JavaDoc TEST_XSD = "resources/PurchaseOrder.xsd";
30     Schema schema = null;
31     SchemaModel model;
32     protected void setUp() throws Exception JavaDoc {
33         model = Util.loadSchemaModel(TEST_XSD);
34         schema = model.getSchema();
35     }
36
37     protected void tearDown() throws Exception JavaDoc {
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 JavaDoc {
48         ArrayList JavaDoc<GlobalElement> elements = new ArrayList JavaDoc(schema.getElements());
49         assertEquals("Schema.getElements", 2, elements.size());
50         assertEquals("Schema.getElements(1)", "comment", elements.get(1).getName());
51     }
52
53     /**
54      * Test of getAttributeFormDefault method, of class org.netbeans.modules.xml.schema.model.api.Schema.
55      */

56     public void testGetAttributeFormDefault() {
57         Form f = schema.getAttributeFormDefault();
58         assertEquals("getAttributeFormDefault", Form.UNQUALIFIED, f);
59     }
60
61     /**
62      * Test of setAttributeFormDefault method, of class org.netbeans.modules.xml.schema.model.api.Schema.
63      */

64     public void testSetAttributeFormDefault() throws Exception JavaDoc {
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     /**
76      * Test of getBlockDefault method, of class org.netbeans.modules.xml.schema.model.api.Schema.
77      */

78     public void testGetBlockDefault() {
79         Set JavaDoc<Block> result = schema.getBlockDefault();
80         assertNull("testGetBlockDefault", result);
81         assertEquals("testGetBlockDefaultDefault", "", schema.getBlockDefaultDefault().toString());
82     }
83     
84     public void testGetBlockDefaultEffective() throws IOException JavaDoc{
85         Set JavaDoc<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 JavaDoc<GlobalComplexType> types = new ArrayList JavaDoc(schema.getComplexTypes());
94         GlobalComplexType typePurchaseOrder = null;
95         if (types.get(0).getName().equals("PurchaseOrderType")) {
96             typePurchaseOrder = types.get(0);
97         }
98         
99         Set JavaDoc<GlobalComplexType.Block> d2 = typePurchaseOrder.getBlock();
100         assertNull("testGetBlockEffective.2", d2);
101         d2 = typePurchaseOrder.getBlockEffective();
102         assertEquals("testGetBlockDefaultEffective.3", "#all", d2.toString());
103     }
104     
105     /**
106      * Test of setBlockDefault method, of class org.netbeans.modules.xml.schema.model.api.Schema.
107      */

108     public void testSetBlockDefault() throws IOException JavaDoc{
109         Set JavaDoc<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     /**
119      * Test of getElementFormDefault method, of class org.netbeans.modules.xml.schema.model.api.Schema.
120      */

121     public void testGetElementFormDefault() {
122         Form result = schema.getElementFormDefault();
123         assertEquals("getElementFormDefault", Form.UNQUALIFIED, result);
124     }
125
126     /**
127      * Test of setElementFormDefault method, of class org.netbeans.modules.xml.schema.model.api.Schema.
128      */

129     public void testSetElementFormDefault() throws Exception JavaDoc {
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     /**
145      * Test of getFinalDefault method, of class org.netbeans.modules.xml.schema.model.api.Schema.
146      */

147     public void testGetFinalDefault() {
148         Set JavaDoc<Final> result = schema.getFinalDefault();
149         assertNull("getFinalDefault", result);
150         Set JavaDoc<Final> def = schema.getFinalDefaultDefault();
151         assertEquals("getFinalDefaultDefault", def.toString(), "");
152         result = schema.getFinalDefaultEffective();
153         assertEquals("getFinalDefaultEffective", def.toString(), result.toString());
154     }
155
156     /**
157      * Test of setFinalDefault method, of class org.netbeans.modules.xml.schema.model.api.Schema.
158      */

159      public void testSetFinalDefault() throws Exception JavaDoc {
160         SchemaModel model = Util.loadSchemaModel(TEST_XSD);
161         Schema myschema = model.getSchema();
162         Set JavaDoc<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     /**
173      * Test of getTargetNamespace method, of class org.netbeans.modules.xml.schema.model.api.Schema.
174      */

175     public void testGetTargetNamespace() throws Exception JavaDoc {
176         String JavaDoc url = schema.getTargetNamespace();
177         assertEquals("getTargetNamespace", "http://www.example.com/PO1", url);
178     }
179
180     public void testGetComplexTypes() {
181         ArrayList JavaDoc<GlobalComplexType> types = new ArrayList JavaDoc(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 JavaDoc<SchemaComponent> elements = new ArrayList JavaDoc(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 JavaDoc {
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         // now add new element it should be added after import
214
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