root/trunk/test/java/org/jcoderz/commons/util/XsdUtilTest.java

Revision 1011, 8.2 kB (checked in by amandel, 4 years ago)

Aligned svn keyword settings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 *
4 * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 *    * Redistributions of source code must retain the above copyright
11 *      notice, this list of conditions and the following disclaimer.
12 *    * Redistributions in binary form must reproduce the above
13 *      copyright notice, this list of conditions and the following
14 *      disclaimer in the documentation and/or other materials
15 *      provided with the distribution.
16 *    * Neither the name of the jCoderZ.org Project nor the names of
17 *      its contributors may be used to endorse or promote products
18 *      derived from this software without specific prior written
19 *      permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33package org.jcoderz.commons.util;
34
35import java.math.BigInteger;
36import junit.framework.TestCase;
37
38/**
39 * Tests the XsdUtil class.
40 * @author Andreas Mandel
41 */
42public class XsdUtilTest
43      extends TestCase
44{
45   /** Tests the integerFromString() method. */
46   public final void testIntegerFromStringGood ()
47   {
48      testIntegerFromStringGood("0", BigInteger.ZERO);
49      testIntegerFromStringGood("1", BigInteger.ONE);
50      testIntegerFromStringGood("+0", BigInteger.ZERO);
51      testIntegerFromStringGood("+1", BigInteger.ONE);
52      testIntegerFromStringGood("-0", BigInteger.ZERO);
53      testIntegerFromStringGood("-1", BigInteger.valueOf(-1));
54      testIntegerFromStringGood("-01", BigInteger.valueOf(-1));
55      testIntegerFromStringGood("00", BigInteger.ZERO);
56      testIntegerFromStringGood("01", BigInteger.ONE);
57      testIntegerFromStringGood("2147483647",
58            BigInteger.valueOf(Integer.MAX_VALUE));
59      testIntegerFromStringGood("-2147483648",
60            BigInteger.valueOf(Integer.MIN_VALUE));
61      testIntegerFromStringGood("9223372036854775807",
62            BigInteger.valueOf(Long.MAX_VALUE));
63      testIntegerFromStringGood("-9223372036854775808",
64            BigInteger.valueOf(Long.MIN_VALUE));
65   }
66
67   /** Tests the integerFromString() method. */
68   public final void testIntegerFromStringBad ()
69   {
70      testIntegerFromStringBad("++0");
71      testIntegerFromStringBad(" +0");
72      testIntegerFromStringBad("--0");
73      testIntegerFromStringBad("-+0");
74      testIntegerFromStringBad("+-0");
75      testIntegerFromStringBad("FOO");
76      testIntegerFromStringBad(null);
77      testIntegerFromStringBad("0.1");
78      testIntegerFromStringBad("");
79      testIntegerFromStringBad("123E123");
80   }
81
82   /** Tests the intFromString() method. */
83   public final void testIntFromStringGood ()
84   {
85      testIntFromStringGood("0", 0);
86      testIntFromStringGood("1", 1);
87      testIntFromStringGood("+0", 0);
88      testIntFromStringGood("+1", 1);
89      testIntFromStringGood("-0", 0);
90      testIntFromStringGood("-1", -1);
91      testIntFromStringGood("-01", -1);
92      testIntFromStringGood("00", 0);
93      testIntFromStringGood("01", 1);
94      testIntFromStringGood("2147483647", Integer.MAX_VALUE);
95      testIntFromStringGood("-2147483648", Integer.MIN_VALUE);
96   }
97
98   /** Tests the intFromString() method. */
99   public final void testIntFromStringBad ()
100   {
101      testIntFromStringBad("++0");
102      testIntFromStringBad(" +0");
103      testIntFromStringBad("--0");
104      testIntFromStringBad("-+0");
105      testIntFromStringBad("+-0");
106      testIntFromStringBad("FOO");
107      testIntFromStringBad(null);
108      testIntFromStringBad("0.1");
109      testIntFromStringBad("");
110      testIntFromStringBad("123E123");
111      testIntFromStringBad("9223372036854775807");
112      testIntFromStringBad("-9223372036854775808");
113   }
114
115   /** Tests the longFromString() method. */
116   public final void testLongFromString ()
117   {
118      testLongFromStringGood("0", 0);
119      testLongFromStringGood("1", 1);
120      testLongFromStringGood("+0", 0);
121      testLongFromStringGood("+1", 1);
122      testLongFromStringGood("-0", 0);
123      testLongFromStringGood("-1", -1);
124      testLongFromStringGood("-01", -1);
125      testLongFromStringGood("00", 0);
126      testLongFromStringGood("01", 1);
127      testLongFromStringGood("2147483647", Integer.MAX_VALUE);
128      testLongFromStringGood("-2147483648", Integer.MIN_VALUE);
129      testLongFromStringGood("9223372036854775807", Long.MAX_VALUE);
130      testLongFromStringGood("-9223372036854775808", Long.MIN_VALUE);
131   }
132
133   /** Tests the longFromString() method. */
134   public final void testLongFromStringBad ()
135   {
136      testLongFromStringBad("++0");
137      testLongFromStringBad(" +0");
138      testLongFromStringBad("--0");
139      testLongFromStringBad("-+0");
140      testLongFromStringBad("+-0");
141      testLongFromStringBad("FOO");
142      testLongFromStringBad(null);
143      testLongFromStringBad("0.1");
144      testLongFromStringBad("");
145      testLongFromStringBad("123E123");
146   }
147
148   /**
149    * Special test with no xs:token violations.
150    */
151   public void testIsValidSchemaToken ()
152   {
153      checkValidSchemaToken("XXXX");
154      checkValidSchemaToken("XXX XXXX");
155      checkValidSchemaToken("X X");
156      checkValidSchemaToken("");
157   }
158
159   /**
160    * Special test with xs:token violations.
161    */
162   public void testIsValidSchemaTokenFalse ()
163   {
164      checkBrokenXmlSchemaToken(" ");
165      checkBrokenXmlSchemaToken(" X");
166      checkBrokenXmlSchemaToken("X ");
167      checkBrokenXmlSchemaToken("X  ");
168      checkBrokenXmlSchemaToken("X  X");
169      checkBrokenXmlSchemaToken("X\tX");
170      checkBrokenXmlSchemaToken("X\nX");
171      checkBrokenXmlSchemaToken("X\rX");
172      checkBrokenXmlSchemaToken("X\r  X");
173      checkBrokenXmlSchemaToken(" X\r");
174      checkBrokenXmlSchemaToken(null);
175   }
176
177   private void checkBrokenXmlSchemaToken (String pattern)
178   {
179      if (XsdUtil.isValidToken(pattern))
180      {
181         fail("Pattern is invalid and should return false '"
182               + pattern + "'.");
183      }
184   }
185
186   private void checkValidSchemaToken (String pattern)
187   {
188      if (!XsdUtil.isValidToken(pattern))
189      {
190         fail("Pattern is valid and should return true '"
191               + pattern + "'.");
192      }
193   }
194
195   private void testIntegerFromStringGood (String str, BigInteger i)
196   {
197      assertEquals("From String value unexpected", i,
198            XsdUtil.integerFromString(str));
199   }
200
201   private void testIntegerFromStringBad (String str)
202   {
203      try
204      {
205         XsdUtil.integerFromString(str);
206         fail("String representation should fail for: '" + str + "'");
207      }
208      catch (NumberFormatException ex)
209      {
210         // OK
211      }
212      catch (NullPointerException ex)
213      {
214         // OK
215      }
216   }
217
218   private void testIntFromStringGood (String str, int i)
219   {
220      assertEquals("From String value unexpected", i,
221            XsdUtil.intFromString(str));
222   }
223
224   private void testIntFromStringBad (String str)
225   {
226      try
227      {
228         XsdUtil.intFromString(str);
229         fail("String representation should fail for: '" + str + "'");
230      }
231      catch (NumberFormatException ex)
232      {
233         // OK
234      }
235      catch (NullPointerException ex)
236      {
237         // OK
238      }
239   }
240
241   private void testLongFromStringGood (String str, long i)
242   {
243      assertEquals("From String value unexpected", i,
244            XsdUtil.longFromString(str));
245   }
246
247   private void testLongFromStringBad (String str)
248   {
249      try
250      {
251         XsdUtil.longFromString(str);
252         fail("String representation of long should fail for: '" + str + "'");
253      }
254      catch (NumberFormatException ex)
255      {
256         // OK
257      }
258      catch (NullPointerException ex)
259      {
260         // OK
261      }
262   }
263
264}
Note: See TracBrowser for help on using the browser.