01: /*
02: * GeoTools - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation;
09: * version 2.1 of the License.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: */
16: package org.geotools.xs.bindings;
17:
18: import java.math.BigInteger;
19:
20: import javax.xml.bind.ValidationException;
21: import javax.xml.namespace.QName;
22:
23: import org.geotools.xs.TestSchema;
24:
25: public class XSNonPositiveIntegerStrategyTest extends TestSchema {
26: /**
27: * nonPositiveInteger has a lexical representation consisting of an
28: * optional preceding sign followed by a finite-length sequence of
29: * decimal digits (#x30-#x39). The sign may be "+" or may be omitted
30: * only for lexical forms denoting zero; in all other lexical forms,
31: * the negative sign ("-") must be present.
32: *
33: * For example: -1, 0, -12678967543233, -100000.
34: * @throws Exception
35: *
36: */
37: public void validateValues(String text, Number expected)
38: throws Exception {
39: Object value = new BigInteger(text.trim());
40:
41: Object result = strategy.parse(element(text, qname), value);
42:
43: if (!(result instanceof BigInteger) && result instanceof Number) {
44: result = BigInteger.valueOf(((Number) result).longValue());
45: }
46:
47: assertEquals(integer(expected), integer(result));
48: }
49:
50: public BigInteger integer(Object value) {
51: return (value instanceof BigInteger) ? ((BigInteger) value)
52: : BigInteger.valueOf(((Number) value).longValue());
53: }
54:
55: public Number number(String number) {
56: return BigInteger.valueOf(Integer.valueOf(number).longValue());
57: }
58:
59: /*
60: * Test method for 'org.geotools.xml.strategies.xs.XSNonPositiveIntegerStrategy.parse(Element, Node[], Object)'
61: */
62: public void testNegativeOne() throws Exception {
63: validateValues("-1", number("-1"));
64: }
65:
66: public void testZero() throws Exception {
67: validateValues("0", number("0"));
68: }
69:
70: public void testLargePositiveNumber() throws Exception {
71: try {
72: validateValues("-12678967543233", new BigInteger(
73: "-12678967543233"));
74: } catch (ValidationException expected) {
75: // yeah!
76: }
77: }
78:
79: public void testNegativeNumber() throws Exception {
80: validateValues("-100000", new Integer("-100000"));
81: }
82:
83: protected QName getQName() {
84: return XS.NONPOSITIVEINTEGER;
85: }
86: }
|