001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.xml.axi.datatype;
043:
044: import java.util.ArrayList;
045: import java.util.Collection;
046: import java.util.List;
047: import junit.framework.*;
048: import org.netbeans.modules.xml.axi.*;
049: import org.netbeans.modules.xml.axi.visitor.DefaultVisitor;
050:
051: /**
052: *
053: * @author Ayub Khan
054: */
055: public class DatatypeTest extends AbstractTestCase {
056:
057: public static final String TEST_XSD = "resources/types.xsd";
058: public static final String GLOBAL_ELEMENT = "purchaseOrder";
059:
060: private List<AbstractAttribute> attList;;
061:
062: public DatatypeTest(String testName) {
063: super (testName, TEST_XSD, GLOBAL_ELEMENT);
064: }
065:
066: protected void setUp() throws Exception {
067: super .setUp();
068: attList = new ArrayList<AbstractAttribute>();
069: }
070:
071: public static Test suite() {
072: TestSuite suite = new TestSuite(DatatypeTest.class);
073:
074: return suite;
075: }
076:
077: /**
078: * Test of createElement method, of class org.netbeans.modules.xml.axi.XAMFactory.
079: */
080: public void testCreateDatatype() {
081: validateSchema(axiModel.getSchemaModel());
082: Element element = globalElement;
083: assertNotNull(element);
084: String contentType;
085: List<Attribute> attrs = checkElement(element, "string");
086: for (Attribute attr : attrs) {
087: if (attr.getName().equals("orderDate"))
088: checkAttribute(attr, "date");
089: else if (attr.getName().equals("shipDate"))
090: checkAttribute(attr, "string");
091: else if (attr.getName().equals("backOrder"))
092: checkAttribute(attr, "string");
093: }
094: Collection<AbstractElement> addresses = element
095: .getChildElements();
096: for (AbstractElement ae : addresses) {
097: if (ae instanceof Element) {
098: Element ce = (Element) ae;
099: if (ce.getName().equals("shipTo")) {
100: checkElement(ce, "string");
101: Collection<AbstractElement> addrs = ce
102: .getChildElements();
103: for (AbstractElement addr : addrs) {
104: if (addr instanceof Element) {
105: Element address = (Element) addr;
106: if (address.getName().equals("zip")) {
107: attrs = checkElement(address, "decimal");
108: }
109: }
110: }
111: } else if (ce.getName().equals("items")) {
112: Collection<AbstractElement> items = ce
113: .getChildElements();
114: for (AbstractElement i : items) {
115: if (i instanceof Element) {
116: Element item = (Element) i;
117: if (item.getName().equals("item")) {
118: checkElement(item, "string");
119: for (Attribute attr : attrs) {
120: if (attr.getName()
121: .equals("partNum"))
122: checkAttribute(attr, "SKU");
123: }
124: }
125: }
126: }
127: printDebug("\n");
128: }
129: }
130: }
131: assertTrue("Should reach here", true);
132: validateSchema(axiModel.getSchemaModel());
133: }
134:
135: private List checkElement(final Element element, String elementType) {
136: attList.clear();
137: TestVisitor visitor = new TestVisitor();
138: visitor.visit(element);
139: if (element.getType() instanceof Datatype) {
140: Datatype type = (Datatype) element.getType();
141: String contentType = type != null ? type.getName() : "";
142: printDebug("\n\n=================\nAXI Element: "
143: + element.getName() + "[" + contentType + "]");
144: if (type != null) {
145: assertEquals("Element datatype", elementType,
146: contentType);
147: if (type.hasFacets())
148: printFacets(type);
149: }
150: }
151: return attList;
152: }
153:
154: private void checkAttribute(Attribute attr, String attrType) {
155: String attName = "";
156: if (attr.getName() != null)
157: attName = attr.getName();
158: Datatype type = (Datatype) attr.getType();
159: printDebug("\n=================\nAXI Attribute: " + attName
160: + "[" + type.getName() + "]");
161: if (attrType != null)
162: assertEquals("Attribute datatype", attrType, type.getName());
163: if (type.hasFacets())
164: printFacets(type);
165: }
166:
167: private void printFacets(final Datatype type) {
168: printDebug("\nlength: " + type.getLengths());
169: printDebug("\nminLength: " + type.getMinLengths());
170: printDebug("\nmaxLength: " + type.getMaxLengths());
171: printDebug("\npattern: " + type.getPatterns());
172: printDebug("\nenum: " + type.getEnumerations());
173: printDebug("\nwhitespace: " + type.getWhiteSpaces());
174: }
175:
176: private void printDebug(String message) {
177: // printDebug(message);
178: }
179:
180: private class TestVisitor extends DefaultVisitor {
181:
182: private int depth = 0;
183:
184: /**
185: * Creates a new instance of TestVisitor
186: */
187: public TestVisitor() {
188: try {
189: } catch (Exception ex) {
190: //ex.printStackTrace();
191: assertTrue("Should not be here", false);
192: }
193: }
194:
195: public void visit(Element element) {
196: for (AbstractAttribute attr : element.getAttributes()) {
197: visit(attr);
198: }
199: visitChildren(element);
200: }
201:
202: public void visit(AbstractAttribute attribute) {
203: attList.add(attribute);
204: }
205:
206: protected void visitChildren(AXIComponent component) {
207: for (AXIComponent child : component.getChildren()) {
208: child.accept(this);
209: }
210: }
211: }
212: }
|