KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > model > impl > SchemaComponentImplTest


1 /*
2  * CommonSchemaComponentImplTest.java
3  * JUnit based test
4  *
5  * Created on October 14, 2005, 7:19 AM
6  */

7
8 package org.netbeans.modules.xml.schema.model.impl;
9
10 import java.io.IOException JavaDoc;
11 import javax.xml.XMLConstants JavaDoc;
12 import junit.framework.*;
13 import java.util.Collection JavaDoc;
14 import org.netbeans.modules.xml.schema.model.Annotation;
15 import org.netbeans.modules.xml.schema.model.AttributeGroupReference;
16 import org.netbeans.modules.xml.schema.model.Documentation;
17 import org.netbeans.modules.xml.schema.model.GlobalAttributeGroup;
18 import org.netbeans.modules.xml.schema.model.SchemaComponent;
19 import org.netbeans.modules.xml.schema.model.GlobalComplexType;
20 import org.netbeans.modules.xml.schema.model.GlobalElement;
21 import org.netbeans.modules.xml.schema.model.GlobalSimpleType;
22 import org.netbeans.modules.xml.schema.model.LocalAttribute;
23 import org.netbeans.modules.xml.schema.model.LocalElement;
24 import org.netbeans.modules.xml.schema.model.Redefine;
25 import org.netbeans.modules.xml.schema.model.Schema;
26 import org.netbeans.modules.xml.schema.model.SchemaComponentFactory;
27 import org.netbeans.modules.xml.schema.model.SchemaModel;
28 import org.netbeans.modules.xml.schema.model.Sequence;
29 import org.netbeans.modules.xml.schema.model.TestCatalogModel;
30 import org.netbeans.modules.xml.schema.model.Util;
31 import org.netbeans.modules.xml.schema.model.visitor.FindSchemaComponentFromDOM;
32 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
33 import org.w3c.dom.Document JavaDoc;
34 import org.w3c.dom.Element JavaDoc;
35 import org.w3c.dom.Node JavaDoc;
36 import org.w3c.dom.NodeList JavaDoc;
37 /**
38  *
39  * @author Vidhya Narayanan
40  * @author Nam Nguyen
41  */

42 public class SchemaComponentImplTest extends TestCase {
43     
44     public SchemaComponentImplTest(String JavaDoc testName) {
45         super(testName);
46     }
47     
48     public static final String JavaDoc TEST_XSD = "resources/PurchaseOrder.xsd";
49     private static final String JavaDoc TEST_XSD2 = "resources/KeyRef.xsd";
50     
51     Schema schema = null;
52     SchemaModelImpl model = null;
53     
54     protected void setUp() throws Exception JavaDoc {
55         SchemaModel model2 = Util.loadSchemaModel(TEST_XSD);
56         schema = model2.getSchema();
57     }
58
59     protected void tearDown() throws Exception JavaDoc {
60         TestCatalogModel.getDefault().clearDocumentPool();
61     }
62     
63     public void testFromSameModel() throws Exception JavaDoc {
64         SchemaModel cur_model = ((SchemaComponentImpl)schema).getModel();
65         SchemaModel new_model = Util.loadSchemaModel(TEST_XSD2);
66         boolean modelcheck = ((SchemaComponentImpl)schema).fromSameModel(new_model.getSchema());
67         assertEquals("model is different", false, modelcheck);
68         Collection JavaDoc<SchemaComponent> comps = schema.getChildren();
69         modelcheck = ((SchemaComponentImpl)schema).fromSameModel(comps.iterator().next());
70         assertEquals("model is the same", true, modelcheck);
71     }
72
73     public void testSetAnnotation() throws IOException JavaDoc {
74         SchemaModel model = ((SchemaComponentImpl)schema).getModel();
75         Collection JavaDoc<SchemaComponent> comps = schema.getChildren();
76         assertEquals("# children for schema", 5, comps.size());
77         Annotation a = schema.getModel().getFactory().createAnnotation();
78         model.startTransaction();
79         ((SchemaComponentImpl)schema).setAnnotation(a);
80         model.endTransaction();
81         comps = schema.getChildren();
82         assertEquals("# children for schema", 6, comps.size());
83         java.util.Iterator JavaDoc i = comps.iterator();
84         Annotation a1 = (Annotation)i.next();
85         int index = ((java.util.List JavaDoc)comps).indexOf(a1);
86         assertEquals("added annotation as first child", 0, index);
87         assertEquals("added annotation is same as newly created", a, a1);
88         
89         //Check if annotation has also been added in the DOM
90
//SchemaModel model = ((SchemaComponentImpl)schema).getModel();
91
Document JavaDoc doc = ((SchemaModelImpl)model).getDocument();
92         Node JavaDoc root = doc.getFirstChild();
93         NodeList JavaDoc nl = root.getChildNodes();
94         Node JavaDoc ann = nl.item(0);
95         for (int j=0; j<nl.getLength(); j++) {
96             if (nl.item(j) instanceof Element) {
97                 ann = nl.item(j);
98                 break;
99             }
100         }
101         assertEquals("#1 child for schema DOM is annotation", "annotation", ann.getLocalName());
102     }
103     
104     public void testGetAnnotations() throws IOException JavaDoc {
105         SchemaModel model = ((SchemaComponentImpl)schema).getModel();
106         Annotation a = model.getFactory().createAnnotation();
107         model.startTransaction();
108         ((SchemaComponentImpl)schema).setAnnotation(a);
109         model.endTransaction();
110         Annotation ann = schema.getAnnotation();
111         assertNotNull("only one annotation should be present", ann);
112     }
113     
114     public void testSetGlobalReference() throws Exception JavaDoc {
115         SchemaModel mod = Util.loadSchemaModel("resources/ipo.xsd");
116         Schema schema = mod.getSchema();
117         SchemaComponentFactory fact = mod.getFactory();
118         
119         mod.startTransaction();
120         GlobalAttributeGroup gap = fact.createGlobalAttributeGroup();
121         schema.addAttributeGroup(gap);
122         gap.setName("myAttrGroup2");
123         LocalAttribute ga = fact.createLocalAttribute();
124         gap.addLocalAttribute(ga);
125         ga.setName("ga");
126         GlobalSimpleType gst = FindSchemaComponentFromDOM.find(
127                 GlobalSimpleType.class, schema, "/schema/simpleType[@name='Sku']");
128         ga.setType(ga.createReferenceTo(gst, GlobalSimpleType.class));
129
130         mod.endTransaction();
131         
132         String JavaDoc v = ((AbstractDocumentComponent)ga).getPeer().getAttribute("type");
133         assertEquals("ref should have prefix", "ipo:Sku", v);
134         
135         mod.startTransaction();
136         /*
137         <complexType name="myCT">
138             <sequence>
139                 <simpleType name="productName" type="xsd:string"/>
140             <attributeGroup ref="ipo:myAttrGroup2"/>
141         </complexType>
142          */

143         GlobalComplexType gct = fact.createGlobalComplexType();
144         schema.addComplexType(gct);
145         gct.setName("myCT");
146         Sequence seq = Util.createSequence(mod, gct);
147         LocalElement le = Util.createLocalElement(mod, seq, "productName", 0);
148         le.setType(le.createReferenceTo(Util.getPrimitiveType("string"), GlobalSimpleType.class));
149         
150         AttributeGroupReference agr = fact.createAttributeGroupReference();
151         gct.addAttributeGroupReference(agr);
152         agr.setGroup(agr.createReferenceTo(gap, GlobalAttributeGroup.class));
153
154         mod.endTransaction();
155         
156         v = ((AbstractDocumentComponent)agr).getPeer().getAttribute("ref");
157         assertEquals("ref should have prefix", "ipo:myAttrGroup2", v);
158     }
159     
160     public void testSetAndGetID() throws Exception JavaDoc {
161         assertNull("id attribute is optional", schema.getId());
162         schema.getModel().startTransaction();
163         String JavaDoc v = "testSEtAndGetID";
164         schema.setId(v);
165         schema.getModel().endTransaction();
166         assertEquals("testSetAndGetID.setID", v, schema.getId());
167     }
168     
169     public void testCanPaste() throws Exception JavaDoc {
170         SchemaModel mod = Util.loadSchemaModel("resources/PurchaseOrder.xsd");
171         Schema schema = mod.getSchema();
172         GlobalComplexType gct = schema.getChildren(GlobalComplexType.class).get(0);
173         Sequence seq = (Sequence) gct.getDefinition();
174         LocalElement le = seq.getChildren(LocalElement.class).get(0);
175         assertFalse(gct.canPaste(seq)); // already have sequence
176
assertFalse(gct.canPaste(le));
177     }
178
179     public void testCanPasteRedefine() throws Exception JavaDoc {
180         SchemaModel model1 = Util.loadSchemaModel("resources/PurchaseOrder_redefine.xsd");
181         Schema schema = model1.getSchema();
182         Redefine redefine = schema.getRedefines().iterator().next();
183         GlobalComplexType gct = redefine.getChildren(GlobalComplexType.class).get(0);
184         Sequence seq = (Sequence) gct.getDefinition();
185         GlobalSimpleType simple = redefine.getSimpleTypes().iterator().next();
186         assertTrue(redefine.canPaste(gct));
187         assertTrue(redefine.canPaste(simple));
188         assertFalse(redefine.canPaste(seq));
189     }
190
191     public void testAddToSelfClosingSchema() throws Exception JavaDoc {
192         SchemaModelImpl refmod = (SchemaModelImpl) Util.loadSchemaModel("resources/Empty_selfClosing.xsd");
193         assertEquals(0, refmod.getSchema().getPeer().getChildNodes().getLength());
194         
195         SchemaModelImpl mod = (SchemaModelImpl) Util.loadSchemaModel("resources/Empty.xsd");
196         Schema schema = mod.getSchema();
197
198         Util.setDocumentContentTo(mod.getBaseDocument(), "resources/Empty_selfClosing.xsd");
199         mod.sync();
200         
201         GlobalElement ge = mod.getFactory().createGlobalElement();
202         ge.setName("foo");
203         mod.startTransaction();
204         schema.addElement(ge);
205         schema.setTargetNamespace("far");
206         mod.endTransaction();
207         
208         //Util.dumpToFile(mod.getBaseDocument(), new File("c:/temp/testout.xml"));
209
assertEquals(schema.getPeer().getChildNodes().item(1), ge.getPeer());
210         assertEquals(3, schema.getPeer().getChildNodes().getLength());
211     }
212     
213     public void testNamespaceConsolidation() throws Exception JavaDoc {
214         SchemaModel model = Util.loadSchemaModel("resources/Empty.xsd");
215         SchemaModel model2 = Util.loadSchemaModel("resources/Empty_loanApp.xsd");
216         
217         SchemaComponentFactory factory = model.getFactory();
218         GlobalElement ge = factory.createGlobalElement();
219         assertNotNull(ge.getPeer().lookupPrefix(XMLConstants.W3C_XML_SCHEMA_NS_URI));
220         assertEquals(XMLConstants.W3C_XML_SCHEMA_NS_URI, ge.getPeer().getNamespaceURI());
221         Annotation ann = factory.createAnnotation();
222         ge.setAnnotation(ann);
223         assertNull(ann.getPeer().getAttribute(XMLConstants.XMLNS_ATTRIBUTE));
224         Documentation doc = factory.createDocumentation();
225         ann.addDocumentation(doc);
226         assertNull(doc.getPeer().getAttribute(XMLConstants.XMLNS_ATTRIBUTE));
227         Element copy = (Element) ge.getPeer().cloneNode(true);
228         
229         model.startTransaction();
230         model.getSchema().addElement(ge);
231         model.endTransaction();
232         
233         assertNull(ge.getPeer().getAttribute(XMLConstants.XMLNS_ATTRIBUTE));
234         assertEquals("element", ((Element)ge.getPeer()).getTagName());
235         
236         GlobalElement geCopy = (GlobalElement) model2.getFactory().create(copy, model2.getSchema());
237         model2.startTransaction();
238         model2.getSchema().addElement(geCopy);
239         model2.endTransaction();
240         
241         assertNull(ge.getPeer().getAttribute(XMLConstants.XMLNS_ATTRIBUTE));
242         assertEquals("xs:element", ((Element)geCopy.getPeer()).getTagName());
243     }
244 }
245
Popular Tags