1 9 10 package org.netbeans.modules.xml.schema.model.readwrite; 11 12 import java.io.File ; 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 24 public class LoanApplicationTest extends TestCase implements TestSchemaReadWrite { 25 26 27 public LoanApplicationTest(String testName) { 28 super(testName); 29 } 30 31 private static final String TEST_XSD = "resources/loanApplication.xsd"; 32 33 public String getSchemaResourcePath() { 34 return TEST_XSD; 35 } 36 37 protected void setUp() throws Exception { 38 } 39 40 protected void tearDown() throws Exception { 41 TestCatalogModel.getDefault().clearDocumentPool(); 42 } 43 44 public void testRead() throws Exception { 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 { 51 String 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 { 60 SchemaModel model = Util.loadSchemaModel("resources/Empty_loanApp.xsd"); 61 Schema s = model.getSchema(); 62 SchemaComponentFactory factory = model.getFactory(); 63 64 model.startTransaction(); 65 s.setAttributeFormDefault(Form.QUALIFIED); 67 s.setElementFormDefault(Form.UNQUALIFIED); 68 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 GlobalSimpleType gst2 = Util.createGlobalSimpleType(model, "State"); 90 GlobalComplexType gct1 = Util.createGlobalComplexType(model, "Applicant"); 92 93 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 GlobalComplexType gct3 = Util.createGlobalComplexType(model, "PhoneNumber"); 114 GlobalComplexType gct4 = Util.createGlobalComplexType(model, "Occupancy"); 116 GlobalComplexType gct5 = Util.createGlobalComplexType(model, "Residence"); 118 GlobalComplexType gct6 = Util.createGlobalComplexType(model, "Car"); 120 GlobalComplexType gct7 = Util.createGlobalComplexType(model, "Duration"); 122 model.endTransaction(); 123 model = Util.dumpAndReloadModel(model); 125 checkRead(model.getSchema()); 126 } 127 128 public void testPrimitiveTypePrefix() throws Exception { 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 assertEquals("xs:string", ge1.getType().getRefString()); 143 } 144 } 145 | Popular Tags |