Source code: org/ognl/test/NumberFormatExceptionTest.java
1 //--------------------------------------------------------------------------
2 // Copyright (c) 2004, Drew Davidson and Luke Blanshard
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 // Neither the name of the Drew Davidson nor the names of its contributors
15 // may be used to endorse or promote products derived from this software
16 // without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25 // OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 // AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
29 // DAMAGE.
30 //--------------------------------------------------------------------------
31 package org.ognl.test;
32
33 import java.math.*;
34 import junit.framework.TestSuite;
35 import ognl.OgnlException;
36 import org.ognl.test.objects.Simple;
37
38 public class NumberFormatExceptionTest extends OgnlTestCase
39 {
40 private static Simple SIMPLE = new Simple();
41
42 private static Object[][] TESTS = {
43 // NumberFormatException handling (default is to throw NumberFormatException on bad string conversions)
44 { SIMPLE, "floatValue", new Float(0f), new Float(10f), new Float(10f) }, /* set float to 10.0f */
45 { SIMPLE, "floatValue", new Float(10f), "x10x", OgnlException.class }, /* set float to invalid format string, should yield OgnlException */
46
47 { SIMPLE, "intValue", new Integer(0), new Integer(34), new Integer(34) }, /* set int to 34 */
48 { SIMPLE, "intValue", new Integer(34), "foobar", OgnlException.class }, /* set int to invalid format string, should yield OgnlException */
49 { SIMPLE, "intValue", new Integer(34), "", OgnlException.class }, /* set int to empty string, should yield 0gnlException */
50 { SIMPLE, "intValue", new Integer(34), " \t", OgnlException.class }, /* set int to whitespace-only string, should yield 0gnlException */
51 { SIMPLE, "intValue", new Integer(34), " \t1234\t\t", new Integer(1234) }, /* set int to whitespace-laden valid string, should yield 1234 */
52
53 { SIMPLE, "bigIntValue", BigInteger.valueOf(0), BigInteger.valueOf(34), BigInteger.valueOf(34) }, /* set bigint to 34 */
54 { SIMPLE, "bigIntValue", BigInteger.valueOf(34), null, null }, /* set bigint to null string, should yield 0 */
55 { SIMPLE, "bigIntValue", null, "", OgnlException.class }, /* set bigint to empty string, should yield 0gnlException */
56 { SIMPLE, "bigIntValue", null, "foobar", OgnlException.class }, /* set bigint to invalid format string, should yield OgnlException */
57
58 { SIMPLE, "bigDecValue", new BigDecimal(0.0), new BigDecimal(34.55), new BigDecimal(34.55) }, /* set bigdec to 34.55 */
59 { SIMPLE, "bigDecValue", new BigDecimal(34.55), null, null }, /* set bigdec to null string, should yield 0.0 */
60 { SIMPLE, "bigDecValue", null, "", OgnlException.class }, /* set bigdec to empty string, should yield 0gnlException */
61 { SIMPLE, "bigDecValue", null, "foobar", OgnlException.class }, /* set bigdec to invalid format string, should yield OgnlException */
62 };
63
64 /*===================================================================
65 Public static methods
66 ===================================================================*/
67 public static TestSuite suite()
68 {
69 TestSuite result = new TestSuite();
70
71 for (int i = 0; i < TESTS.length; i++) {
72 if (TESTS[i].length == 3) {
73 result.addTest(new NumberFormatExceptionTest((String)TESTS[i][1], TESTS[i][0], (String)TESTS[i][1], TESTS[i][2]));
74 } else {
75 if (TESTS[i].length == 4) {
76 result.addTest(new NumberFormatExceptionTest((String)TESTS[i][1], TESTS[i][0], (String)TESTS[i][1], TESTS[i][2], TESTS[i][3]));
77 } else {
78 if (TESTS[i].length == 5) {
79 result.addTest(new NumberFormatExceptionTest((String)TESTS[i][1], TESTS[i][0], (String)TESTS[i][1], TESTS[i][2], TESTS[i][3], TESTS[i][4]));
80 } else {
81 throw new RuntimeException("don't understand TEST format");
82 }
83 }
84 }
85 }
86 return result;
87 }
88
89 /*===================================================================
90 Constructors
91 ===================================================================*/
92 public NumberFormatExceptionTest()
93 {
94 super();
95 }
96
97 public NumberFormatExceptionTest(String name)
98 {
99 super(name);
100 }
101
102 public NumberFormatExceptionTest(String name, Object root, String expressionString, Object expectedResult, Object setValue, Object expectedAfterSetResult)
103 {
104 super(name, root, expressionString, expectedResult, setValue, expectedAfterSetResult);
105 }
106
107 public NumberFormatExceptionTest(String name, Object root, String expressionString, Object expectedResult, Object setValue)
108 {
109 super(name, root, expressionString, expectedResult, setValue);
110 }
111
112 public NumberFormatExceptionTest(String name, Object root, String expressionString, Object expectedResult)
113 {
114 super(name, root, expressionString, expectedResult);
115 }
116 }