KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * LoanApplicationTest.java
3  *
4  * Created on October 26, 2005, 10:31 PM
5  *
6  * To change this template, choose Tools | Template Manager
7  * and open the template in the editor.
8  */

9
10 package org.netbeans.modules.xml.schema.model.readwrite;
11
12 import java.io.File JavaDoc;
13 import junit.framework.TestCase;
14 import org.netbeans.modules.xml.schema.model.*;
15 import org.netbeans.modules.xml.schema.model.Util;
16 import org.netbeans.modules.xml.schema.model.impl.SchemaModelImpl;
17 import org.netbeans.modules.xml.schema.model.visitor.FindSchemaComponentFromDOM;
18 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
19
20 /**
21  *
22  * @author rico
23  */

24 public class LoanApplicationTest extends TestCase implements TestSchemaReadWrite {
25     
26     /** Creates a new instance of LoanApplicationTest */
27     public LoanApplicationTest(String JavaDoc testName) {
28         super(testName);
29     }
30     
31     private static final String JavaDoc TEST_XSD = "resources/loanApplication.xsd";
32     
33     public String JavaDoc getSchemaResourcePath() {
34         return TEST_XSD;
35     }
36     
37     protected void setUp() throws Exception JavaDoc {
38     }
39     
40     protected void tearDown() throws Exception JavaDoc {
41         TestCatalogModel.getDefault().clearDocumentPool();
42     }
43     
44     public void testRead() throws Exception JavaDoc {
45         SchemaModel model = Util.loadSchemaModel(TEST_XSD);
46         Schema schema = model.getSchema();
47         checkRead(schema);
48     }
49     
50     private void checkRead(Schema schema) throws Exception JavaDoc {
51         String JavaDoc xpath = "/xs:schema/xs:element[@name='auto-loan-application']/xs:complexType/xs:sequence/xs:element[@name='loan-type']";
52         SchemaComponent sc = new FindSchemaComponentFromDOM().findComponent(schema, xpath);
53         assertTrue("loan-type", sc instanceof LocalElement);
54         LocalElement le = (LocalElement) sc;
55         GlobalSimpleType gst = (GlobalSimpleType) le.getType().get();
56         assertEquals("loan-type type name", "LoanType", gst.getName());
57     }
58     
59     public void testWrite() throws Exception JavaDoc {
60         SchemaModel model = Util.loadSchemaModel("resources/Empty_loanApp.xsd");
61         Schema s = model.getSchema();
62         SchemaComponentFactory factory = model.getFactory();
63         
64         model.startTransaction();
65         //set attributes
66
s.setAttributeFormDefault(Form.QUALIFIED);
67         s.setElementFormDefault(Form.UNQUALIFIED);
68     //<xs:element name="auto-loan-application">
69
GlobalElement ge1 = Util.createGlobalElement(model, "auto-loan-application");
70         assertEquals("xs prefix", "xs", ((AbstractDocumentComponent)ge1).getPeer().getPrefix());
71         Util.createAnnotation(model, ge1, "A loan application");
72         LocalComplexType t2 = Util.createLocalComplexType(model, ge1);
73         Sequence seq = Util.createSequence(model, t2);
74         LocalElement le = Util.createLocalElement(model, seq, "loan-type", 1);
75         LocalElement loanType = le;
76         le = Util.createLocalElement(model, seq, "term", 2);
77         le.setType(factory.createGlobalReference(
78                 Util.getPrimitiveType("integer"), GlobalSimpleType.class, le));
79         le = Util.createLocalElement(model, seq, "amount", 3);
80         LocalSimpleType lst = Util.createLocalSimpleType(model, le);
81         SimpleTypeRestriction restriction = Util.createSimpleRestriction(model, lst);
82         restriction.setBase(factory.createGlobalReference(
83                 Util.getPrimitiveType("decimal"), GlobalSimpleType.class, restriction));
84         assertEquals("xs:decimal", restriction.getBase().getRefString());
85         
86         GlobalSimpleType gst1 = Util.createGlobalSimpleType(model, "LoanType");
87         loanType.setType(factory.createGlobalReference(gst1, GlobalSimpleType.class, le));
88     //<xs:simpleType name="State">
89
GlobalSimpleType gst2 = Util.createGlobalSimpleType(model, "State");
90     //<xs:complexType name="Applicant">
91
GlobalComplexType gct1 = Util.createGlobalComplexType(model, "Applicant");
92         
93     //<xs:complexType name="Address">
94
GlobalComplexType gct2 = Util.createGlobalComplexType(model, "Address");
95         seq = Util.createSequence(model, gct2);
96         Util.createLocalElement(model, seq, "address1", 1);
97         Util.createLocalElement(model, seq, "address2", 2);
98         Util.createLocalElement(model, seq, "city", 3);
99         le = Util.createLocalElement(model, seq, "state", 4);
100         le.setType(factory.createGlobalReference(gst2, GlobalSimpleType.class, le));
101         le = Util.createLocalElement(model, seq, "zip", 5);
102         lst = Util.createLocalSimpleType(model, le);
103         restriction = Util.createSimpleRestriction(model, lst);
104         restriction.setBase(factory.createGlobalReference(
105                 Util.getPrimitiveType("string"), GlobalSimpleType.class, restriction));
106         MinLength min = factory.createMinLength(); min.setValue(5);
107         restriction.addMinLength(min);
108         MaxLength max = factory.createMaxLength(); max.setValue(5);
109         restriction.addMaxLength(max);
110         Pattern pat = factory.createPattern(); pat.setValue("\\d{5}");
111         
112     //<xs:complexType name="PhoneNumber">
113
GlobalComplexType gct3 = Util.createGlobalComplexType(model, "PhoneNumber");
114     //<xs:complexType name="Occupancy">
115
GlobalComplexType gct4 = Util.createGlobalComplexType(model, "Occupancy");
116     //<xs:complexType name="Residence">
117
GlobalComplexType gct5 = Util.createGlobalComplexType(model, "Residence");
118     //<xs:complexType name="Car">
119
GlobalComplexType gct6 = Util.createGlobalComplexType(model, "Car");
120     //<xs:complexType name="Duration">
121
GlobalComplexType gct7 = Util.createGlobalComplexType(model, "Duration");
122         model.endTransaction();
123         //Util.dumpToFile(model.getBaseDocument(), new File("C:\\temp\\test.xml"));
124
model = Util.dumpAndReloadModel(model);
125         checkRead(model.getSchema());
126     }
127     
128     public void testPrimitiveTypePrefix() throws Exception JavaDoc {
129         SchemaModelImpl model = (SchemaModelImpl) Util.loadSchemaModel("resources/Empty_loanApp.xsd");
130         Schema s = model.getSchema();
131         SchemaComponentFactory factory = model.getFactory();
132         
133         GlobalElement ge1 = model.getFactory().createGlobalElement();
134         ge1.setName("auto-loan-application");
135         ge1.setType(factory.createGlobalReference(
136                 Util.getPrimitiveType("string"), GlobalSimpleType.class, ge1));
137         model.startTransaction();
138         model.getSchema().addElement(ge1);
139         model.endTransaction();
140         
141         //Util.dumpToFile(model.getBaseDocument(), new File("c:/temp/test.xsd"));
142
assertEquals("xs:string", ge1.getType().getRefString());
143     }
144 }
145
Popular Tags