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 XSNonNegativeIntegerStrategyTest extends TestSchema {
26: public void validateValues(String text, Number expected)
27: throws Exception {
28: Object value = new BigInteger(text.trim());
29:
30: Object result = strategy.parse(element(text, qname), value);
31:
32: if (!(result instanceof BigInteger) && result instanceof Number) {
33: result = BigInteger.valueOf(((Number) result).longValue());
34: }
35:
36: assertEquals(integer(expected), integer(result));
37: }
38:
39: public BigInteger integer(Object value) {
40: return (value instanceof BigInteger) ? ((BigInteger) value)
41: : BigInteger.valueOf(((Number) value).longValue());
42: }
43:
44: public Number number(String number) {
45: return BigInteger.valueOf(Integer.valueOf(number).longValue());
46: }
47:
48: /*
49: * Test method for 'org.geotools.xml.strategies.xs.XSNonPositiveIntegerStrategy.parse(Element, Node[], Object)'
50: */
51: public void testNegativeOne() throws Exception {
52: try {
53: validateValues("-1", number("-1"));
54: } catch (ValidationException e) {
55: // yeah!
56: }
57: }
58:
59: public void testZero() throws Exception {
60: validateValues("0", number("0"));
61: }
62:
63: public void testLargePositiveNumber() throws Exception {
64: validateValues("12678967543233", new BigInteger(
65: "12678967543233"));
66: }
67:
68: public void testPositiveNumber() throws Exception {
69: validateValues("1000", new Integer("1000"));
70: }
71:
72: protected QName getQName() {
73: return XS.NONNEGATIVEINTEGER;
74: }
75: }
|