KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > model > readwrite > PurchaseOrderTest


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

7
8 package org.netbeans.modules.xml.schema.model.readwrite;
9
10 import java.io.File JavaDoc;
11 import java.util.Collection JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import javax.swing.text.Document JavaDoc;
14 import junit.framework.*;
15 import org.netbeans.junit.NbTestCase;
16 import org.netbeans.modules.xml.schema.model.*;
17 import org.netbeans.modules.xml.schema.model.impl.SchemaComponentFactoryImpl;
18 import org.netbeans.modules.xml.schema.model.impl.SchemaImpl;
19 import org.netbeans.modules.xml.schema.model.impl.SchemaModelImpl;
20 import org.netbeans.modules.xml.schema.model.visitor.DefaultSchemaVisitor;
21 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
22 import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
23 import org.openide.awt.UndoRedo.Empty;
24 import org.w3c.dom.Text JavaDoc;
25 /**
26  *
27  * @author nn136682
28  */

29 public class PurchaseOrderTest extends NbTestCase {
30     
31     public PurchaseOrderTest(String JavaDoc testName) {
32         super(testName);
33         
34     }
35     
36     public static final String JavaDoc TEST_XSD = "resources/PurchaseOrder.xsd";
37     
38     protected void setUp() throws Exception JavaDoc {
39     }
40     
41     protected void tearDown() throws Exception JavaDoc {
42         TestCatalogModel.getDefault().clearDocumentPool();
43     }
44     
45     public String JavaDoc getSchemaResourcePath() {
46         return TEST_XSD;
47     }
48     
49     public void testRead() throws Exception JavaDoc {
50         SchemaModel model = Util.loadSchemaModel(getSchemaResourcePath());
51         SchemaImpl si = (SchemaImpl) model.getSchema();
52         si.accept(new SchemaTestVisitor());
53         assertTrue(seenRef);
54     }
55     
56     public void testWrite() throws Exception JavaDoc {
57         SchemaModel model = Util.createEmptySchemaModel();
58         SchemaImpl si = (SchemaImpl)model.getSchema();
59         assertNotNull(si);
60         SchemaComponentFactory factory = model.getFactory();
61         
62         //set attributes
63
model.startTransaction();
64         si.setAttributeFormDefault(Form.UNQUALIFIED);
65         si.setElementFormDefault(Form.UNQUALIFIED);
66         si.setTargetNamespace("http://www.example.com/PO1");
67         
68         
69         //create and add the USAddress global complex type
70
GlobalComplexType usAddressComplexType = factory.createGlobalComplexType();
71         si.addComplexType(usAddressComplexType);
72         assertNull("no xsd prefix", ((AbstractDocumentComponent)si).getPeer().getPrefix());
73         usAddressComplexType.setName("USAddress");
74         Sequence sequence = factory.createSequence();
75         //create and add name element
76
LocalElement el = factory.createLocalElement();
77         el.setName("name");
78         GlobalSimpleType stringType = Util.getPrimitiveType("string");
79         NamedComponentReference<GlobalSimpleType> ref =
80                 factory.createGlobalReference(stringType, GlobalSimpleType.class,
81                 el);
82         el.setType(ref);
83         assertEquals("refString", "xsd:string", ref.getRefString());
84         sequence.addContent(el, 0);
85         
86         //FIXME
87
//assertNull("no xmlns:xsd", ((AbstractDocumentComponent)el).getPeer().getAttribute("xmlns:xsd"));
88

89         //create and add street element
90
el = factory.createLocalElement();
91         el.setName("street");
92         ref = factory.createGlobalReference(stringType, GlobalSimpleType.class,
93                 el);
94         
95         el.setType(ref);
96         sequence.addContent(el, 1);
97         usAddressComplexType.setDefinition(sequence);
98         
99         //create the comment global element
100
GlobalElement commentGE = factory.createGlobalElement();
101         si.addElement(commentGE);
102         commentGE.setName("comment");
103         ref = factory.createGlobalReference(stringType, GlobalSimpleType.class,
104                 commentGE);
105         commentGE.setType(ref);
106         
107         //create and add the PurchaseOrderType
108
GlobalComplexType poComplexType = factory.createGlobalComplexType();
109         si.addComplexType(poComplexType);
110         poComplexType.setName("PurchaseOrderType");
111         sequence = factory.createSequence();
112         el = factory.createLocalElement();
113         el.setName("shipTo");
114         NamedComponentReference<GlobalComplexType>ref2 =
115                 factory.createGlobalReference(usAddressComplexType,
116                 GlobalComplexType.class, el);
117         
118         el.setType(ref2);
119         sequence.addContent(el, 0);
120         el = factory.createLocalElement();
121         el.setName("billTo");
122         ref2 = factory.createGlobalReference(usAddressComplexType,
123                 GlobalComplexType.class, el);
124         
125         el.setType(ref);
126         sequence.addContent(el, 1);
127         ElementReference er = factory.createElementReference();
128         NamedComponentReference<GlobalElement> refEl =
129                 factory.createGlobalReference(commentGE, GlobalElement.class, el);
130         
131         er.setRef(refEl);
132         er.setMinOccurs(0);
133         sequence.addContent(er, 2);
134         poComplexType.setDefinition(sequence);
135         
136         //create purchaseOrder global element
137
GlobalElement poGE = factory.createGlobalElement();
138         si.addElement(poGE);
139         poGE.setName("purchaseOrder");
140         ref2 = factory.createGlobalReference(poComplexType,
141                 GlobalComplexType.class, poGE);
142         
143         poGE.setType(ref2);
144         
145         //create simple type
146
GlobalSimpleType simpleType = factory.createGlobalSimpleType();
147         si.addSimpleType(simpleType);
148         simpleType.setName("allNNI");
149         Annotation ann = factory.createAnnotation();
150         simpleType.setAnnotation(ann);
151         Documentation documentation = factory.createDocumentation();
152         ann.addDocumentation(documentation);
153         Text JavaDoc txt = model.getDocument().createTextNode("documentation for simple type");
154         org.w3c.dom.Element JavaDoc e = documentation.getDocumentationElement();
155         e.appendChild(txt);
156         documentation.setDocumentationElement(e);
157         Union union = factory.createUnion();
158         GlobalSimpleType nonNegativeInteger = Util.getPrimitiveType("nonNegativeInteger");
159         NamedComponentReference<GlobalSimpleType> nniRef =
160                 factory.createGlobalReference(nonNegativeInteger,
161                 GlobalSimpleType.class, union);
162         union.addMemberType(nniRef);
163         GlobalSimpleType nonPositiveInteger = Util.getPrimitiveType("nonPositiveInteger");
164         NamedComponentReference<GlobalSimpleType> npiRef =
165                 factory.createGlobalReference(nonPositiveInteger,
166                 GlobalSimpleType.class, union);
167         union.addMemberType(npiRef);
168         simpleType.setDefinition(union);
169         
170         si.setVersion("1.3");
171         model.endTransaction();
172         //model.flush();
173
//Util.dumpToTempFile(doc);
174
Document JavaDoc d = (Document JavaDoc) model.getModelSource().getLookup().lookup(Document JavaDoc.class);
175         //System.out.println(d.getText(0,d.getLength()));
176

177         //verify attributes
178
assertEquals("schema's attributeFormDefault", Form.UNQUALIFIED.toString(), si.getAttributeFormDefault().toString());
179         assertEquals("schema's elementFormDefault", Form.UNQUALIFIED.toString(), si.getElementFormDefault().toString());
180         assertEquals("schema's targetNamespace: ", "http://www.example.com/PO1", si.getTargetNamespace());
181         
182         //Util.dumpToFile(si.getModel().getBaseDocument(), new File(getWorkDir(), "test.xsd"));
183

184         si.accept(new SchemaTestVisitor());
185         assertTrue(seenRef);
186         assertEquals("testWrite read again", 1, countShipToVisit);
187     }
188     
189     private class SchemaTestVisitor extends DefaultSchemaVisitor {
190         
191         /** Creates a new instance of SchemaTestVisitor */
192         public SchemaTestVisitor() {
193         }
194         
195         public void visit(Schema e) {
196             java.util.List JavaDoc<SchemaComponent> ch = e.getChildren();
197             for (SchemaComponent c : ch) {
198                 if (c instanceof GlobalComplexType) {
199                     visit((GlobalComplexType)c);
200                 } else if (c instanceof GlobalElement) {
201                     visit((GlobalElement) c);
202                 } else if (c instanceof GlobalSimpleType){
203                     visit((GlobalSimpleType)c);
204                 }
205             }
206         }
207         
208         public void visit(GlobalSimpleType e){
209             Collection JavaDoc<SchemaComponent> ch = e.getChildren();
210             for (SchemaComponent c : ch) {
211                 if (c instanceof Union) {
212                     visit((Union)c);
213                 }
214             }
215         }
216         
217         public void visit(Union e){
218             Collection JavaDoc<NamedComponentReference<GlobalSimpleType>> mts = e.getMemberTypes();
219             assertEquals("Number of union member types", 2, mts.size());
220             Iterator JavaDoc<NamedComponentReference<GlobalSimpleType>> iterator = mts.iterator();
221             NamedComponentReference<GlobalSimpleType> mt = iterator.next();
222             assertFalse(mt.isBroken());
223             assertEquals("First union member", "nonNegativeInteger", mt.get().getName());
224             mt = iterator.next();
225             assertFalse(mt.isBroken());
226             assertEquals("Second union member", "nonPositiveInteger", mt.get().getName());
227         }
228         
229         public void visit(GlobalComplexType e) {
230             
231             Collection JavaDoc<SchemaComponent> ch = e.getChildren();
232             for (SchemaComponent c : ch) {
233                 if (c instanceof Sequence) {
234                     visit((Sequence)c);
235                 }
236             }
237         }
238         
239         public void visit(Sequence e) {
240             
241             Collection JavaDoc<SchemaComponent> ch = e.getChildren();
242             for (SchemaComponent c : ch) {
243                 if (c instanceof LocalElement) {
244                     visit((LocalElement)c);
245                 }
246                 if (c instanceof ElementReference) {
247                     visit((ElementReference)c);
248                 }
249             }
250         }
251     /*
252       <complexType name="PurchaseOrderType">
253         <sequence>
254           <element name="shipTo" type="po:USAddress"/>
255           <element name="billTo" type="po:USAddress"/>
256           <element ref="po:comment" minOccurs="0"/>
257       <complexType name="USAddress">
258         <sequence>
259           <element name="name" type="string"/>
260           <element name="street" type="string"/>
261      */

262         public void visit(ElementReference e) {
263             if (e.getRef() != null && e.getRef().get() != null) {
264                 NamedComponentReference<GlobalElement> ge = e.getRef();
265                 assertTrue("PurchaseOrderType.ref(po:comment)", ge.getRefString().endsWith(":comment"));
266                 seenRef = true;
267             }
268         }
269         
270         public void visit(LocalElement e) {
271             if (e.getName().equals("shipTo")) {
272                 NamedComponentReference<? extends GlobalType> t = e.getType();
273                 assertTrue("PurchaseOrderType:shipTo ref GlobalComplexType", t.get() instanceof GlobalComplexType);
274                 GlobalComplexType gt = (GlobalComplexType)t.get();
275                 assertEquals("PurchaseOrderType:shipTo complexType.name", "USAddress", gt.getName());
276                 countShipToVisit++;
277             } else if (e.getName().equals("street")) {
278                 NamedComponentReference<? extends GlobalType> t = e.getType();
279                 GlobalSimpleType gst = (GlobalSimpleType) t.get();
280                 assertEquals("USAddress:street type string", "string", gst.getName());
281             }
282         }
283         
284         /*
285               <element name="purchaseOrder" type="po:PurchaseOrderType"/>
286               <element name="comment" type="string"/>
287          */

288         public void visit(GlobalElement e) {
289             
290             if (e.getName().equals("purchaseOrder")) {
291                 assertTrue("purchaseOrder is of ComplexType", e.getType().get() instanceof GlobalComplexType);
292                 GlobalComplexType gct = (GlobalComplexType) e.getType().get();
293                 assertEquals("purchaseOrder.type", "PurchaseOrderType", gct.getName());
294             } else if (e.getName().equals("comment")) {
295                 
296                 assertTrue("comment is a PrimitiveType (GlobalSimpleType)", e.getType().get() instanceof GlobalSimpleType);
297             }
298         }
299     }
300     
301     boolean seenRef = false;
302     int countShipToVisit = 0;
303
304     public void testPrefixConsolidation() throws Exception JavaDoc {
305         TestCatalogModel.getDefault().clearDocumentPool();
306         SchemaModelImpl model = (SchemaModelImpl) Util.createEmptySchemaModel();
307         Schema s = model.getSchema();
308         SchemaComponentFactory factory = model.getFactory();
309         
310         GlobalElement ge1 = factory.createGlobalElement();
311         ge1.setName("my-auto-loan-application");
312         LocalComplexType lct = factory.createLocalComplexType();
313         assertNotNull(lct.getPeer().getAttribute("xmlns:xsd"));
314         ge1.setInlineType(lct);
315         assertNull(lct.getPeer().getAttribute("xmlns:xsd"));
316         ComplexContent cc = factory.createComplexContent();
317         assertNotNull(cc.getPeer().getAttribute("xmlns:xsd"));
318         lct.setDefinition(cc);
319         assertNull(cc.getPeer().getAttribute("xmlns:xsd"));
320         
321         model.startTransaction();
322         model.getSchema().addElement(ge1);
323         model.endTransaction();
324         
325         assertNull(lct.getPeer().getAttribute("xmlns:xsd"));
326         assertNull(cc.getPeer().getAttribute("xmlns:xsd"));
327     }
328 }
329
Popular Tags