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 static junit.framework.Assert.assertTrue;
23 import static junit.framework.Assert.fail;
24
25 import java.math.BigDecimal;
26 import java.math.BigInteger;
27 import java.util.ArrayList;
28 import java.util.Collection;
29
30 import org.apache.commons.ognl.OgnlException;
31 import org.apache.commons.ognl.OgnlOps;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.junit.runners.Parameterized;
35 import org.junit.runners.Parameterized.Parameters;
36
37 @RunWith(value = Parameterized.class)
38 public class NumericConversionTest
39 extends OgnlTestCase
40 {
41 private static Object[][] TESTS = {
42
43 { "55", Integer.class, new Integer( 55 ) }, { new Integer( 55 ), Integer.class, new Integer( 55 ) },
44 { new Double( 55 ), Integer.class, new Integer( 55 ) }, { Boolean.TRUE, Integer.class, new Integer( 1 ) },
45 { new Byte( (byte) 55 ), Integer.class, new Integer( 55 ) },
46 { new Character( (char) 55 ), Integer.class, new Integer( 55 ) },
47 { new Short( (short) 55 ), Integer.class, new Integer( 55 ) },
48 { new Long( 55 ), Integer.class, new Integer( 55 ) }, { new Float( 55 ), Integer.class, new Integer( 55 ) },
49 { new BigInteger( "55" ), Integer.class, new Integer( 55 ) },
50 { new BigDecimal( "55" ), Integer.class, new Integer( 55 ) },
51
52
53 { "55.1234", Double.class, new Double( 55.1234 ) }, { new Integer( 55 ), Double.class, new Double( 55 ) },
54 { new Double( 55.1234 ), Double.class, new Double( 55.1234 ) },
55 { Boolean.TRUE, Double.class, new Double( 1 ) }, { new Byte( (byte) 55 ), Double.class, new Double( 55 ) },
56 { new Character( (char) 55 ), Double.class, new Double( 55 ) },
57 { new Short( (short) 55 ), Double.class, new Double( 55 ) },
58 { new Long( 55 ), Double.class, new Double( 55 ) },
59 { new Float( 55.1234 ), Double.class, new Double( 55.1234 ), new Integer( 4 ) },
60 { new BigInteger( "55" ), Double.class, new Double( 55 ) },
61 { new BigDecimal( "55.1234" ), Double.class, new Double( 55.1234 ) },
62
63
64 { "true", Boolean.class, Boolean.TRUE }, { new Integer( 55 ), Boolean.class, Boolean.TRUE },
65 { new Double( 55 ), Boolean.class, Boolean.TRUE }, { Boolean.TRUE, Boolean.class, Boolean.TRUE },
66 { new Byte( (byte) 55 ), Boolean.class, Boolean.TRUE },
67 { new Character( (char) 55 ), Boolean.class, Boolean.TRUE },
68 { new Short( (short) 55 ), Boolean.class, Boolean.TRUE }, { new Long( 55 ), Boolean.class, Boolean.TRUE },
69 { new Float( 55 ), Boolean.class, Boolean.TRUE }, { new BigInteger( "55" ), Boolean.class, Boolean.TRUE },
70 { new BigDecimal( "55" ), Boolean.class, Boolean.TRUE },
71
72
73 { "55", Byte.class, new Byte( (byte) 55 ) }, { new Integer( 55 ), Byte.class, new Byte( (byte) 55 ) },
74 { new Double( 55 ), Byte.class, new Byte( (byte) 55 ) }, { Boolean.TRUE, Byte.class, new Byte( (byte) 1 ) },
75 { new Byte( (byte) 55 ), Byte.class, new Byte( (byte) 55 ) },
76 { new Character( (char) 55 ), Byte.class, new Byte( (byte) 55 ) },
77 { new Short( (short) 55 ), Byte.class, new Byte( (byte) 55 ) },
78 { new Long( 55 ), Byte.class, new Byte( (byte) 55 ) }, { new Float( 55 ), Byte.class, new Byte( (byte) 55 ) },
79 { new BigInteger( "55" ), Byte.class, new Byte( (byte) 55 ) },
80 { new BigDecimal( "55" ), Byte.class, new Byte( (byte) 55 ) },
81
82
83 { "55", Character.class, new Character( (char) 55 ) },
84 { new Integer( 55 ), Character.class, new Character( (char) 55 ) },
85 { new Double( 55 ), Character.class, new Character( (char) 55 ) },
86 { Boolean.TRUE, Character.class, new Character( (char) 1 ) },
87 { new Byte( (byte) 55 ), Character.class, new Character( (char) 55 ) },
88 { new Character( (char) 55 ), Character.class, new Character( (char) 55 ) },
89 { new Short( (short) 55 ), Character.class, new Character( (char) 55 ) },
90 { new Long( 55 ), Character.class, new Character( (char) 55 ) },
91 { new Float( 55 ), Character.class, new Character( (char) 55 ) },
92 { new BigInteger( "55" ), Character.class, new Character( (char) 55 ) },
93 { new BigDecimal( "55" ), Character.class, new Character( (char) 55 ) },
94
95
96 { "55", Short.class, new Short( (short) 55 ) }, { new Integer( 55 ), Short.class, new Short( (short) 55 ) },
97 { new Double( 55 ), Short.class, new Short( (short) 55 ) },
98 { Boolean.TRUE, Short.class, new Short( (short) 1 ) },
99 { new Byte( (byte) 55 ), Short.class, new Short( (short) 55 ) },
100 { new Character( (char) 55 ), Short.class, new Short( (short) 55 ) },
101 { new Short( (short) 55 ), Short.class, new Short( (short) 55 ) },
102 { new Long( 55 ), Short.class, new Short( (short) 55 ) },
103 { new Float( 55 ), Short.class, new Short( (short) 55 ) },
104 { new BigInteger( "55" ), Short.class, new Short( (short) 55 ) },
105 { new BigDecimal( "55" ), Short.class, new Short( (short) 55 ) },
106
107
108 { "55", Long.class, new Long( 55 ) }, { new Integer( 55 ), Long.class, new Long( 55 ) },
109 { new Double( 55 ), Long.class, new Long( 55 ) }, { Boolean.TRUE, Long.class, new Long( 1 ) },
110 { new Byte( (byte) 55 ), Long.class, new Long( 55 ) },
111 { new Character( (char) 55 ), Long.class, new Long( 55 ) },
112 { new Short( (short) 55 ), Long.class, new Long( 55 ) }, { new Long( 55 ), Long.class, new Long( 55 ) },
113 { new Float( 55 ), Long.class, new Long( 55 ) }, { new BigInteger( "55" ), Long.class, new Long( 55 ) },
114 { new BigDecimal( "55" ), Long.class, new Long( 55 ) },
115
116
117 { "55.1234", Float.class, new Float( 55.1234 ) }, { new Integer( 55 ), Float.class, new Float( 55 ) },
118 { new Double( 55.1234 ), Float.class, new Float( 55.1234 ) }, { Boolean.TRUE, Float.class, new Float( 1 ) },
119 { new Byte( (byte) 55 ), Float.class, new Float( 55 ) },
120 { new Character( (char) 55 ), Float.class, new Float( 55 ) },
121 { new Short( (short) 55 ), Float.class, new Float( 55 ) }, { new Long( 55 ), Float.class, new Float( 55 ) },
122 { new Float( 55.1234 ), Float.class, new Float( 55.1234 ) },
123 { new BigInteger( "55" ), Float.class, new Float( 55 ) },
124 { new BigDecimal( "55.1234" ), Float.class, new Float( 55.1234 ) },
125
126
127 { "55", BigInteger.class, new BigInteger( "55" ) },
128 { new Integer( 55 ), BigInteger.class, new BigInteger( "55" ) },
129 { new Double( 55 ), BigInteger.class, new BigInteger( "55" ) },
130 { Boolean.TRUE, BigInteger.class, new BigInteger( "1" ) },
131 { new Byte( (byte) 55 ), BigInteger.class, new BigInteger( "55" ) },
132 { new Character( (char) 55 ), BigInteger.class, new BigInteger( "55" ) },
133 { new Short( (short) 55 ), BigInteger.class, new BigInteger( "55" ) },
134 { new Long( 55 ), BigInteger.class, new BigInteger( "55" ) },
135 { new Float( 55 ), BigInteger.class, new BigInteger( "55" ) },
136 { new BigInteger( "55" ), BigInteger.class, new BigInteger( "55" ) },
137 { new BigDecimal( "55" ), BigInteger.class, new BigInteger( "55" ) },
138
139
140 { "55.1234", BigDecimal.class, new BigDecimal( "55.1234" ) },
141 { new Integer( 55 ), BigDecimal.class, new BigDecimal( "55" ) },
142 { new Double( 55.1234 ), BigDecimal.class, new BigDecimal( "55.1234" ), new Integer( 4 ) },
143 { Boolean.TRUE, BigDecimal.class, new BigDecimal( "1" ) },
144 { new Byte( (byte) 55 ), BigDecimal.class, new BigDecimal( "55" ) },
145 { new Character( (char) 55 ), BigDecimal.class, new BigDecimal( "55" ) },
146 { new Short( (short) 55 ), BigDecimal.class, new BigDecimal( "55" ) },
147 { new Long( 55 ), BigDecimal.class, new BigDecimal( "55" ) },
148 { new Float( 55.1234 ), BigDecimal.class, new BigDecimal( "55.1234" ), new Integer( 4 ) },
149 { new BigInteger( "55" ), BigDecimal.class, new BigDecimal( "55" ) },
150 { new BigDecimal( "55.1234" ), BigDecimal.class, new BigDecimal( "55.1234" ) }, };
151
152 private Object value;
153
154 private Class toClass;
155
156 private Object expectedValue;
157
158 private int scale;
159
160
161
162
163
164 @Parameters
165 public static Collection<Object[]> data()
166 {
167 Collection<Object[]> data = new ArrayList<Object[]>(TESTS.length);
168 for ( int i = 0; i < TESTS.length; i++ )
169 {
170 Object[] tmp = new Object[4];
171 tmp[0] = TESTS[i][0];
172 tmp[1] = TESTS[i][1];
173 tmp[2] = TESTS[i][2];
174 tmp[3] = ( TESTS[i].length > 3 ) ? ( (Integer) TESTS[i][3] ).intValue() : -1 ;
175
176 data.add( tmp );
177 }
178 return data;
179 }
180
181
182
183
184
185 public NumericConversionTest( Object value, Class toClass, Object expectedValue, int scale )
186 {
187 super( value + " [" + value.getClass().getName() + "] -> " + toClass.getName() + " == " + expectedValue + " ["
188 + expectedValue.getClass().getName() + "]"
189 + ( ( scale >= 0 ) ? ( " (to within " + scale + " decimal places)" ) : "" ), null, null, null, null, null );
190 this.value = value;
191 this.toClass = toClass;
192 this.expectedValue = expectedValue;
193 this.scale = scale;
194 }
195
196
197
198
199
200 @Test
201 @Override
202 public void runTest()
203 throws OgnlException
204 {
205 Object result;
206
207 result = OgnlOps.convertValue( value, toClass );
208 if ( !isEqual( result, expectedValue ) )
209 {
210 if ( scale >= 0 )
211 {
212 double scalingFactor = Math.pow( 10, scale ), v1 = ( (Number) value ).doubleValue() * scalingFactor, v2 =
213 ( (Number) expectedValue ).doubleValue() * scalingFactor;
214
215 assertTrue( (int) v1 == (int) v2 );
216 }
217 else
218 {
219 fail();
220 }
221 }
222 }
223 }