001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.xs.bindings;
017:
018: import java.math.BigDecimal;
019:
020: import junit.framework.TestCase;
021:
022: import org.eclipse.xsd.XSDElementDeclaration;
023: import org.eclipse.xsd.XSDMaxExclusiveFacet;
024: import org.eclipse.xsd.XSDMaxInclusiveFacet;
025: import org.eclipse.xsd.XSDMinExclusiveFacet;
026: import org.eclipse.xsd.XSDMinInclusiveFacet;
027: import org.eclipse.xsd.XSDTotalDigitsFacet;
028: import org.eclipse.xsd.XSDTypeDefinition;
029: import org.eclipse.xsd.impl.XSDElementDeclarationImpl;
030: import org.eclipse.xsd.impl.XSDMaxExclusiveFacetImpl;
031: import org.eclipse.xsd.impl.XSDMaxInclusiveFacetImpl;
032: import org.eclipse.xsd.impl.XSDMinExclusiveFacetImpl;
033: import org.eclipse.xsd.impl.XSDMinInclusiveFacetImpl;
034: import org.eclipse.xsd.impl.XSDSimpleTypeDefinitionImpl;
035: import org.eclipse.xsd.impl.XSDTotalDigitsFacetImpl;
036: import org.geotools.xml.ElementInstance;
037: import org.geotools.xml.Node;
038: import org.geotools.xml.impl.ElementImpl;
039:
040: public class XSDecimalStrategyTest extends TestCase {
041: /*
042: * Test method for 'org.geotools.xml.strategies.xs.XSDecimalStrategy.parse(Element, Node[], Object)'
043: */
044: public void testParse() throws Exception {
045: //Valid values
046: validateValues("50", 5, 0, 0, 100, 100);
047: validateValues("50", 3, 49, 50, 50, 51);
048:
049: validateValues("50.0", 3, 49, 50, 51, 52);
050: validateValues("50.8", 4, 49, 50, 51, 52);
051: validateValues("51.0", 4, 49, 50, 51, 52);
052:
053: //Invalid values
054: //TODO: enable these test when facets have been properly implemented
055: // try {
056: // validateValues("50", 1, 49, 50, 51, 52);
057: // assertTrue("Exception should have been thrown.", false);
058: // } catch (ValidationException e) {
059: //
060: // }
061: //
062: // try {
063: // validateValues("48", 2, 49, 50, 51, 52);
064: // assertTrue("Exception should have been thrown.", false);
065: // } catch (ValidationException e) {
066: //
067: // }
068: //
069: // try {
070: // validateValues("49", 2, 49, 50, 51, 52);
071: // assertTrue("Exception should have been thrown.", false);
072: // } catch (ValidationException e) {
073: //
074: // }
075: //
076: // try {
077: // validateValues("52", 2, 49, 50, 51, 52);
078: // assertTrue("Exception should have been thrown.", false);
079: // } catch (ValidationException e) {
080: //
081: // }
082: //
083: // try {
084: // validateValues("53", 2, 49, 50, 51, 52);
085: // assertTrue("Exception should have been thrown.", false);
086: // } catch (ValidationException e) {
087: //
088: // }
089: }
090:
091: public void validateValues(String elementText, int totalDigits,
092: double minExc, double minInc, double maxInc, double maxExc)
093: throws Exception {
094: XSDecimalBinding strat = new XSDecimalBinding();
095:
096: XSDElementDeclaration declaration = makeDeclaration(
097: totalDigits, new BigDecimal(minExc), new BigDecimal(
098: minInc), new BigDecimal(maxInc),
099: new BigDecimal(maxExc));
100:
101: ElementInstance element = new ElementImpl(declaration);
102: element.setText(elementText);
103:
104: Node[] children = new Node[] {};
105: Object value = null;
106:
107: BigDecimal decimal = (BigDecimal) strat.parse(element, element
108: .getText().trim());
109:
110: assertNotNull(decimal);
111: }
112:
113: private XSDElementDeclaration makeDeclaration(final int digits,
114: final BigDecimal minExc, final BigDecimal minInc,
115: final BigDecimal maxInc, final BigDecimal maxExc) {
116: return new XSDElementDeclarationImpl() {
117: public XSDTypeDefinition getTypeDefinition() {
118: return new XSDSimpleTypeDefinitionImpl() {
119: public XSDTotalDigitsFacet getTotalDigitsFacet() {
120: return new XSDTotalDigitsFacetImpl() {
121: public int getValue() {
122: return digits;
123: }
124: };
125: }
126:
127: public XSDMinInclusiveFacet getMinInclusiveFacet() {
128: return new XSDMinInclusiveFacetImpl() {
129: public Object getValue() {
130: return minInc;
131: }
132: };
133: }
134:
135: public XSDMinExclusiveFacet getMinExclusiveFacet() {
136: return new XSDMinExclusiveFacetImpl() {
137: public Object getValue() {
138: return minExc;
139: }
140: };
141: }
142:
143: public XSDMaxInclusiveFacet getMaxInclusiveFacet() {
144: return new XSDMaxInclusiveFacetImpl() {
145: public Object getValue() {
146: return maxInc;
147: }
148: };
149: }
150:
151: public XSDMaxExclusiveFacet getMaxExclusiveFacet() {
152: return new XSDMaxExclusiveFacetImpl() {
153: public Object getValue() {
154: return maxExc;
155: }
156: };
157: }
158: };
159: }
160: };
161: }
162: }
|