1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.commons.ognl.test;
21
22 import org.apache.commons.ognl.OgnlException;
23 import org.apache.commons.ognl.test.objects.Simple;
24 import org.junit.runner.RunWith;
25 import org.junit.runners.Parameterized;
26 import org.junit.runners.Parameterized.Parameters;
27
28 import java.math.BigDecimal;
29 import java.math.BigInteger;
30 import java.util.ArrayList;
31 import java.util.Collection;
32
33 @RunWith(value = Parameterized.class)
34 public class NumberFormatExceptionTest
35 extends OgnlTestCase
36 {
37 private static Simple SIMPLE = new Simple();
38
39 private static Object[][] TESTS = {
40
41 { SIMPLE, "floatValue", 0f, 10f, 10f },
42 { SIMPLE, "floatValue", 10f, "x10x", OgnlException.class },
43
44 { SIMPLE, "intValue", 0, 34, 34 },
45 { SIMPLE, "intValue", 34, "foobar", OgnlException.class },
46 { SIMPLE, "intValue", 34, "", OgnlException.class },
47 { SIMPLE, "intValue", 34, " \t", OgnlException.class },
48 { SIMPLE, "intValue", 34, " \t1234\t\t", 1234 },
49
50 { SIMPLE, "bigIntValue", BigInteger.valueOf( 0 ), BigInteger.valueOf( 34 ), BigInteger.valueOf( 34 ) },
51 { SIMPLE, "bigIntValue", BigInteger.valueOf( 34 ), null, null },
52 { SIMPLE, "bigIntValue", null, "", OgnlException.class },
53 { SIMPLE, "bigIntValue", null, "foobar", OgnlException.class },
54
55 { SIMPLE, "bigDecValue", new BigDecimal( 0.0 ), new BigDecimal( 34.55 ), new BigDecimal( 34.55 ) },
56 { SIMPLE, "bigDecValue", new BigDecimal( 34.55 ), null, null },
57 { SIMPLE, "bigDecValue", null, "", OgnlException.class },
58 { SIMPLE, "bigDecValue", null, "foobar", OgnlException.class }
59
60 };
61
62
63
64
65
66 @Parameters
67 public static Collection<Object[]> data()
68 {
69 Collection<Object[]> data = new ArrayList<Object[]>(TESTS.length);
70 for ( Object[] TEST : TESTS )
71 {
72 Object[] tmp = new Object[6];
73 tmp[0] = TEST[1];
74 tmp[1] = TEST[0];
75 tmp[2] = TEST[1];
76
77 switch ( TEST.length )
78 {
79 case 3:
80 tmp[3] = TEST[2];
81 break;
82
83 case 4:
84 tmp[3] = TEST[2];
85 tmp[4] = TEST[3];
86 break;
87
88 case 5:
89 tmp[3] = TEST[2];
90 tmp[4] = TEST[3];
91 tmp[5] = TEST[4];
92 break;
93
94 default:
95 throw new RuntimeException( "don't understand TEST format with length " + TEST.length );
96 }
97
98 data.add( tmp );
99 }
100 return data;
101 }
102
103
104
105
106
107 public NumberFormatExceptionTest( String name, Object root, String expressionString, Object expectedResult,
108 Object setValue, Object expectedAfterSetResult )
109 {
110 super( name, root, expressionString, expectedResult, setValue, true, expectedAfterSetResult, true );
111 }
112 }