1 19 20 package org.netbeans.modules.xml.axi.datatype; 21 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.List ; 25 import junit.framework.*; 26 import org.netbeans.modules.xml.axi.*; 27 import org.netbeans.modules.xml.axi.visitor.DefaultVisitor; 28 29 30 34 public class DatatypeTest extends AbstractTestCase { 35 36 public static final String TEST_XSD = "resources/types.xsd"; 37 public static final String GLOBAL_ELEMENT = "purchaseOrder"; 38 39 private List <AbstractAttribute> attList;; 40 41 public DatatypeTest(String testName) { 42 super(testName, TEST_XSD, GLOBAL_ELEMENT); 43 } 44 45 protected void setUp() throws Exception { 46 super.setUp(); 47 attList = new ArrayList <AbstractAttribute>(); 48 } 49 50 public static Test suite() { 51 TestSuite suite = new TestSuite(DatatypeTest.class); 52 53 return suite; 54 } 55 56 59 public void testCreateDatatype() { 60 validateSchema(axiModel.getSchemaModel()); 61 Element element = globalElement; 62 assertNotNull(element); 63 String contentType; 64 List <Attribute> attrs = checkElement(element, "string"); 65 for(Attribute attr:attrs) { 66 if(attr.getName().equals("orderDate")) 67 checkAttribute(attr, "date"); 68 else if(attr.getName().equals("shipDate")) 69 checkAttribute(attr, "string"); 70 else if(attr.getName().equals("backOrder")) 71 checkAttribute(attr, "string"); 72 } 73 Collection <AbstractElement> addresses = element.getChildElements(); 74 for(AbstractElement ae:addresses) { 75 if(ae instanceof Element) { 76 Element ce = (Element)ae; 77 if(ce.getName().equals("shipTo")) { 78 checkElement(ce, "string"); 79 Collection <AbstractElement> addrs = ce.getChildElements(); 80 for(AbstractElement addr:addrs) { 81 if(addr instanceof Element) { 82 Element address = (Element)addr; 83 if(address.getName().equals("zip")) { 84 attrs = checkElement(address, "decimal"); 85 } 86 } 87 } 88 } else if(ce.getName().equals("items")) { 89 Collection <AbstractElement> items = ce.getChildElements(); 90 for(AbstractElement i:items) { 91 if(i instanceof Element) { 92 Element item = (Element)i; 93 if(item.getName().equals("item")) { 94 checkElement(item, "string"); 95 for(Attribute attr:attrs) { 96 if(attr.getName().equals("partNum")) 97 checkAttribute(attr, "SKU"); 98 } 99 } 100 } 101 } 102 printDebug("\n"); 103 } 104 } 105 } 106 assertTrue("Should reach here", true); 107 validateSchema(axiModel.getSchemaModel()); 108 } 109 110 private List checkElement(final Element element, String elementType) { 111 attList.clear(); 112 TestVisitor visitor = new TestVisitor(); 113 visitor.visit(element); 114 if(element.getType() instanceof Datatype) { 115 Datatype type = (Datatype) element.getType(); 116 String contentType = type != null ? type.getName() : ""; 117 printDebug("\n\n=================\nAXI Element: "+element.getName()+"["+contentType+"]"); 118 if(type != null) { 119 assertEquals("Element datatype", elementType, contentType); 120 if(type.hasFacets()) 121 printFacets(type); 122 } 123 } 124 return attList; 125 } 126 127 private void checkAttribute(Attribute attr, String attrType) { 128 String attName = ""; 129 if(attr.getName() != null) 130 attName = attr.getName(); 131 Datatype type = (Datatype) attr.getType(); 132 printDebug("\n=================\nAXI Attribute: "+attName+"["+type.getName()+"]"); 133 if(attrType != null) 134 assertEquals("Attribute datatype", attrType, type.getName()); 135 if(type.hasFacets()) 136 printFacets(type); 137 } 138 139 private void printFacets(final Datatype type) { 140 printDebug("\nlength: "+type.getLengths()); 141 printDebug("\nminLength: "+type.getMinLengths()); 142 printDebug("\nmaxLength: "+type.getMaxLengths()); 143 printDebug("\npattern: "+type.getPatterns()); 144 printDebug("\nenum: "+type.getEnumerations()); 145 printDebug("\nwhitespace: "+type.getWhiteSpaces()); 146 } 147 148 private void printDebug(String message) { 149 } 151 152 private class TestVisitor extends DefaultVisitor { 153 154 private int depth = 0; 155 156 159 public TestVisitor() { 160 try { 161 } catch(Exception ex) { 162 assertTrue("Should not be here", false); 164 } 165 } 166 167 public void visit(Element element) { 168 for(AbstractAttribute attr : element.getAttributes()) { 169 visit(attr); 170 } 171 visitChildren(element); 172 } 173 174 public void visit(AbstractAttribute attribute) { 175 attList.add(attribute); 176 } 177 178 protected void visitChildren(AXIComponent component) { 179 for(AXIComponent child: component.getChildren()) { 180 child.accept(this); 181 } 182 } 183 } 184 } 185 | Popular Tags |