Project Report: fawkez

Packagesummary org.jcoderz.commons.util

org.jcoderz.commons.util.XsdUtilTest

LineHitsNoteSource
1  /*
2   * $Id: XsdUtilTest.java 1011 2008-06-16 17:57:36Z amandel $
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   */
33  package org.jcoderz.commons.util;
34  
35  import java.math.BigInteger;
36  import junit.framework.TestCase;
37  
38  /**
39   * Tests the XsdUtil class.
40   * @author Andreas Mandel
41   */
42100 public class XsdUtilTest
43        extends TestCase
44  {
45     /** Tests the integerFromString() method. */
46     public final void testIntegerFromStringGood ()
47     {
48100       testIntegerFromStringGood("0", BigInteger.ZERO);
49100       testIntegerFromStringGood("1", BigInteger.ONE);
50100       testIntegerFromStringGood("+0", BigInteger.ZERO);
51100       testIntegerFromStringGood("+1", BigInteger.ONE);
52100       testIntegerFromStringGood("-0", BigInteger.ZERO);
53100       testIntegerFromStringGood("-1", BigInteger.valueOf(-1));
54100       testIntegerFromStringGood("-01", BigInteger.valueOf(-1));
55100       testIntegerFromStringGood("00", BigInteger.ZERO);
56100       testIntegerFromStringGood("01", BigInteger.ONE);
57100       testIntegerFromStringGood("2147483647",
58              BigInteger.valueOf(Integer.MAX_VALUE));
59100       testIntegerFromStringGood("-2147483648",
60              BigInteger.valueOf(Integer.MIN_VALUE));
61100       testIntegerFromStringGood("9223372036854775807",
62              BigInteger.valueOf(Long.MAX_VALUE));
63100       testIntegerFromStringGood("-9223372036854775808",
64              BigInteger.valueOf(Long.MIN_VALUE));
65100    }
66  
67     /** Tests the integerFromString() method. */
68     public final void testIntegerFromStringBad ()
69     {
70100       testIntegerFromStringBad("++0");
71100       testIntegerFromStringBad(" +0");
72100       testIntegerFromStringBad("--0");
73100       testIntegerFromStringBad("-+0");
74100       testIntegerFromStringBad("+-0");
75100       testIntegerFromStringBad("FOO");
76100       testIntegerFromStringBad(null);
77100       testIntegerFromStringBad("0.1");
78100       testIntegerFromStringBad("");
79100       testIntegerFromStringBad("123E123");
80100    }
81  
82     /** Tests the intFromString() method. */
83     public final void testIntFromStringGood ()
84     {
85100       testIntFromStringGood("0", 0);
86100       testIntFromStringGood("1", 1);
87100       testIntFromStringGood("+0", 0);
88100       testIntFromStringGood("+1", 1);
89100       testIntFromStringGood("-0", 0);
90100       testIntFromStringGood("-1", -1);
91100       testIntFromStringGood("-01", -1);
92100       testIntFromStringGood("00", 0);
93100       testIntFromStringGood("01", 1);
94100       testIntFromStringGood("2147483647", Integer.MAX_VALUE);
95100       testIntFromStringGood("-2147483648", Integer.MIN_VALUE);
96100    }
97  
98     /** Tests the intFromString() method. */
99     public final void testIntFromStringBad ()
100     {
101100       testIntFromStringBad("++0");
102100       testIntFromStringBad(" +0");
103100       testIntFromStringBad("--0");
104100       testIntFromStringBad("-+0");
105100       testIntFromStringBad("+-0");
106100       testIntFromStringBad("FOO");
107100       testIntFromStringBad(null);
108100       testIntFromStringBad("0.1");
109100       testIntFromStringBad("");
110100       testIntFromStringBad("123E123");
111100       testIntFromStringBad("9223372036854775807");
112100       testIntFromStringBad("-9223372036854775808");
113100    }
114  
115     /** Tests the longFromString() method. */
116     public final void testLongFromString ()
117     {
118100       testLongFromStringGood("0", 0);
119100       testLongFromStringGood("1", 1);
120100       testLongFromStringGood("+0", 0);
121100       testLongFromStringGood("+1", 1);
122100       testLongFromStringGood("-0", 0);
123100       testLongFromStringGood("-1", -1);
124100       testLongFromStringGood("-01", -1);
125100       testLongFromStringGood("00", 0);
126100       testLongFromStringGood("01", 1);
127100       testLongFromStringGood("2147483647", Integer.MAX_VALUE);
128100       testLongFromStringGood("-2147483648", Integer.MIN_VALUE);
129100       testLongFromStringGood("9223372036854775807", Long.MAX_VALUE);
130100       testLongFromStringGood("-9223372036854775808", Long.MIN_VALUE);
131100    }
132  
133     /** Tests the longFromString() method. */
134     public final void testLongFromStringBad ()
135     {
136100       testLongFromStringBad("++0");
137100       testLongFromStringBad(" +0");
138100       testLongFromStringBad("--0");
139100       testLongFromStringBad("-+0");
140100       testLongFromStringBad("+-0");
141100       testLongFromStringBad("FOO");
142100       testLongFromStringBad(null);
143100       testLongFromStringBad("0.1");
144100       testLongFromStringBad("");
145100       testLongFromStringBad("123E123");
146100    }
147  
148     /**
149      * Special test with no xs:token violations.
150      */
151     public void testIsValidSchemaToken ()
152     {
153100       checkValidSchemaToken("XXXX");
154100       checkValidSchemaToken("XXX XXXX");
155100       checkValidSchemaToken("X X");
156100       checkValidSchemaToken("");
157100    }
158  
159     /**
160      * Special test with xs:token violations.
161      */
162     public void testIsValidSchemaTokenFalse ()
163     {
164100       checkBrokenXmlSchemaToken(" ");
165100       checkBrokenXmlSchemaToken(" X");
166100       checkBrokenXmlSchemaToken("X ");
167100       checkBrokenXmlSchemaToken("X ");
168100       checkBrokenXmlSchemaToken("X X");
169100       checkBrokenXmlSchemaToken("X\tX");
170100       checkBrokenXmlSchemaToken("X\nX");
171100       checkBrokenXmlSchemaToken("X\rX");
172100       checkBrokenXmlSchemaToken("X\r X");
173100       checkBrokenXmlSchemaToken(" X\r");
174100       checkBrokenXmlSchemaToken(null);
175100    }
176  
177     private void checkBrokenXmlSchemaToken (String pattern)
178     {
179100       if (XsdUtil.isValidToken(pattern))
180        {
1810          fail("Pattern is invalid and should return false '"
182                 + pattern + "'.");
183        }
184100    }
185  
186     private void checkValidSchemaToken (String pattern)
187     {
188100       if (!XsdUtil.isValidToken(pattern))
189        {
1900          fail("Pattern is valid and should return true '"
191                 + pattern + "'.");
192        }
193100    }
194  
195     private void testIntegerFromStringGood (String str, BigInteger i)
196     {
197100       assertEquals("From String value unexpected", i,
198              XsdUtil.integerFromString(str));
199100    }
200  
201     private void testIntegerFromStringBad (String str)
202     {
203        try
204        {
2050          XsdUtil.integerFromString(str);
2060          fail("String representation should fail for: '" + str + "'");
207        }
208100       catch (NumberFormatException ex)
209        {
210           // OK
211        }
212100       catch (NullPointerException ex)
213        {
214           // OK
21550       }
216100    }
217  
218     private void testIntFromStringGood (String str, int i)
219     {
220100       assertEquals("From String value unexpected", i,
221              XsdUtil.intFromString(str));
222100    }
223  
224     private void testIntFromStringBad (String str)
225     {
226        try
227        {
2280          XsdUtil.intFromString(str);
2290          fail("String representation should fail for: '" + str + "'");
230        }
231100       catch (NumberFormatException ex)
232        {
233           // OK
234        }
235100       catch (NullPointerException ex)
236        {
237           // OK
23850       }
239100    }
240  
241     private void testLongFromStringGood (String str, long i)
242     {
243100       assertEquals("From String value unexpected", i,
244              XsdUtil.longFromString(str));
245100    }
246  
247     private void testLongFromStringBad (String str)
248     {
249        try
250        {
2510          XsdUtil.longFromString(str);
2520          fail("String representation of long should fail for: '" + str + "'");
253        }
254100       catch (NumberFormatException ex)
255        {
256           // OK
257        }
258100       catch (NullPointerException ex)
259        {
260           // OK
26150       }
262100    }
263  
264  }

Findings in this File

f (1) Avoid catching NullPointerException; consider removing the cause of the NPE. Not required for testcode.
f (2) Avoid catching NullPointerException; consider removing the cause of the NPE. Not required for testcode.
f (3) Avoid catching NullPointerException; consider removing the cause of the NPE. Not required for testcode.