KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > axi > datatype > DatatypeTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.axi.datatype;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.List JavaDoc;
25 import junit.framework.*;
26 import org.netbeans.modules.xml.axi.*;
27 import org.netbeans.modules.xml.axi.visitor.DefaultVisitor;
28
29
30 /**
31  *
32  * @author Ayub Khan
33  */

34 public class DatatypeTest extends AbstractTestCase {
35     
36     public static final String JavaDoc TEST_XSD = "resources/types.xsd";
37     public static final String JavaDoc GLOBAL_ELEMENT = "purchaseOrder";
38     
39     private List JavaDoc<AbstractAttribute> attList;;
40     
41     public DatatypeTest(String JavaDoc testName) {
42         super(testName, TEST_XSD, GLOBAL_ELEMENT);
43     }
44     
45     protected void setUp() throws Exception JavaDoc {
46         super.setUp();
47         attList = new ArrayList JavaDoc<AbstractAttribute>();
48     }
49     
50     public static Test suite() {
51         TestSuite suite = new TestSuite(DatatypeTest.class);
52         
53         return suite;
54     }
55     
56     /**
57      * Test of createElement method, of class org.netbeans.modules.xml.axi.XAMFactory.
58      */

59     public void testCreateDatatype() {
60         validateSchema(axiModel.getSchemaModel());
61         Element element = globalElement;
62         assertNotNull(element);
63         String JavaDoc contentType;
64         List JavaDoc<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 JavaDoc<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 JavaDoc<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 JavaDoc<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 JavaDoc checkElement(final Element element, String JavaDoc 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 JavaDoc 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 JavaDoc attrType) {
128         String JavaDoc 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 JavaDoc message) {
149 // printDebug(message);
150
}
151     
152     private class TestVisitor extends DefaultVisitor {
153         
154         private int depth = 0;
155         
156         /**
157          * Creates a new instance of TestVisitor
158          */

159         public TestVisitor() {
160             try {
161             } catch(Exception JavaDoc ex) {
162                 //ex.printStackTrace();
163
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