001: //--------------------------------------------------------------------------
002: // Copyright (c) 2004, Drew Davidson and Luke Blanshard
003: // All rights reserved.
004: //
005: // Redistribution and use in source and binary forms, with or without
006: // modification, are permitted provided that the following conditions are
007: // met:
008: //
009: // Redistributions of source code must retain the above copyright notice,
010: // this list of conditions and the following disclaimer.
011: // Redistributions in binary form must reproduce the above copyright
012: // notice, this list of conditions and the following disclaimer in the
013: // documentation and/or other materials provided with the distribution.
014: // Neither the name of the Drew Davidson nor the names of its contributors
015: // may be used to endorse or promote products derived from this software
016: // without specific prior written permission.
017: //
018: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
019: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
020: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
021: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
022: // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
023: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
024: // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
025: // OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: // AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
027: // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
028: // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
029: // DAMAGE.
030: //--------------------------------------------------------------------------
031: package org.ognl.test;
032:
033: import java.math.BigDecimal;
034: import java.math.BigInteger;
035: import junit.framework.TestSuite;
036: import ognl.OgnlException;
037: import ognl.OgnlOps;
038:
039: public class NumericConversionTest extends OgnlTestCase {
040: private static Object[][] TESTS = {
041: /* To Integer.class */
042: { "55", Integer.class, new Integer(55) },
043: { new Integer(55), Integer.class, new Integer(55) },
044: { new Double(55), Integer.class, new Integer(55) },
045: { Boolean.TRUE, Integer.class, new Integer(1) },
046: { new Byte((byte) 55), Integer.class, new Integer(55) },
047: { new Character((char) 55), Integer.class, new Integer(55) },
048: { new Short((short) 55), Integer.class, new Integer(55) },
049: { new Long(55), Integer.class, new Integer(55) },
050: { new Float(55), Integer.class, new Integer(55) },
051: { new BigInteger("55"), Integer.class, new Integer(55) },
052: { new BigDecimal("55"), Integer.class, new Integer(55) },
053:
054: /* To Double.class */
055: { "55.1234", Double.class, new Double(55.1234) },
056: { new Integer(55), Double.class, new Double(55) },
057: { new Double(55.1234), Double.class, new Double(55.1234) },
058: { Boolean.TRUE, Double.class, new Double(1) },
059: { new Byte((byte) 55), Double.class, new Double(55) },
060: { new Character((char) 55), Double.class, new Double(55) },
061: { new Short((short) 55), Double.class, new Double(55) },
062: { new Long(55), Double.class, new Double(55) },
063: { new Float(55.1234), Double.class, new Double(55.1234),
064: new Integer(4) },
065: { new BigInteger("55"), Double.class, new Double(55) },
066: { new BigDecimal("55.1234"), Double.class,
067: new Double(55.1234) },
068:
069: /* To Boolean.class */
070: { "true", Boolean.class, Boolean.TRUE },
071: { new Integer(55), Boolean.class, Boolean.TRUE },
072: { new Double(55), Boolean.class, Boolean.TRUE },
073: { Boolean.TRUE, Boolean.class, Boolean.TRUE },
074: { new Byte((byte) 55), Boolean.class, Boolean.TRUE },
075: { new Character((char) 55), Boolean.class, Boolean.TRUE },
076: { new Short((short) 55), Boolean.class, Boolean.TRUE },
077: { new Long(55), Boolean.class, Boolean.TRUE },
078: { new Float(55), Boolean.class, Boolean.TRUE },
079: { new BigInteger("55"), Boolean.class, Boolean.TRUE },
080: { new BigDecimal("55"), Boolean.class, Boolean.TRUE },
081:
082: /* To Byte.class */
083: { "55", Byte.class, new Byte((byte) 55) },
084: { new Integer(55), Byte.class, new Byte((byte) 55) },
085: { new Double(55), Byte.class, new Byte((byte) 55) },
086: { Boolean.TRUE, Byte.class, new Byte((byte) 1) },
087: { new Byte((byte) 55), Byte.class, new Byte((byte) 55) },
088: { new Character((char) 55), Byte.class, new Byte((byte) 55) },
089: { new Short((short) 55), Byte.class, new Byte((byte) 55) },
090: { new Long(55), Byte.class, new Byte((byte) 55) },
091: { new Float(55), Byte.class, new Byte((byte) 55) },
092: { new BigInteger("55"), Byte.class, new Byte((byte) 55) },
093: { new BigDecimal("55"), Byte.class, new Byte((byte) 55) },
094:
095: /* To Character.class */
096: { "55", Character.class, new Character((char) 55) },
097: { new Integer(55), Character.class,
098: new Character((char) 55) },
099: { new Double(55), Character.class, new Character((char) 55) },
100: { Boolean.TRUE, Character.class, new Character((char) 1) },
101: { new Byte((byte) 55), Character.class,
102: new Character((char) 55) },
103: { new Character((char) 55), Character.class,
104: new Character((char) 55) },
105: { new Short((short) 55), Character.class,
106: new Character((char) 55) },
107: { new Long(55), Character.class, new Character((char) 55) },
108: { new Float(55), Character.class, new Character((char) 55) },
109: { new BigInteger("55"), Character.class,
110: new Character((char) 55) },
111: { new BigDecimal("55"), Character.class,
112: new Character((char) 55) },
113:
114: /* To Short.class */
115: { "55", Short.class, new Short((short) 55) },
116: { new Integer(55), Short.class, new Short((short) 55) },
117: { new Double(55), Short.class, new Short((short) 55) },
118: { Boolean.TRUE, Short.class, new Short((short) 1) },
119: { new Byte((byte) 55), Short.class, new Short((short) 55) },
120: { new Character((char) 55), Short.class,
121: new Short((short) 55) },
122: { new Short((short) 55), Short.class, new Short((short) 55) },
123: { new Long(55), Short.class, new Short((short) 55) },
124: { new Float(55), Short.class, new Short((short) 55) },
125: { new BigInteger("55"), Short.class, new Short((short) 55) },
126: { new BigDecimal("55"), Short.class, new Short((short) 55) },
127:
128: /* To Long.class */
129: { "55", Long.class, new Long(55) },
130: { new Integer(55), Long.class, new Long(55) },
131: { new Double(55), Long.class, new Long(55) },
132: { Boolean.TRUE, Long.class, new Long(1) },
133: { new Byte((byte) 55), Long.class, new Long(55) },
134: { new Character((char) 55), Long.class, new Long(55) },
135: { new Short((short) 55), Long.class, new Long(55) },
136: { new Long(55), Long.class, new Long(55) },
137: { new Float(55), Long.class, new Long(55) },
138: { new BigInteger("55"), Long.class, new Long(55) },
139: { new BigDecimal("55"), Long.class, new Long(55) },
140:
141: /* To Float.class */
142: { "55.1234", Float.class, new Float(55.1234) },
143: { new Integer(55), Float.class, new Float(55) },
144: { new Double(55.1234), Float.class, new Float(55.1234) },
145: { Boolean.TRUE, Float.class, new Float(1) },
146: { new Byte((byte) 55), Float.class, new Float(55) },
147: { new Character((char) 55), Float.class, new Float(55) },
148: { new Short((short) 55), Float.class, new Float(55) },
149: { new Long(55), Float.class, new Float(55) },
150: { new Float(55.1234), Float.class, new Float(55.1234) },
151: { new BigInteger("55"), Float.class, new Float(55) },
152: { new BigDecimal("55.1234"), Float.class,
153: new Float(55.1234) },
154:
155: /* To BigInteger.class */
156: { "55", BigInteger.class, new BigInteger("55") },
157: { new Integer(55), BigInteger.class, new BigInteger("55") },
158: { new Double(55), BigInteger.class, new BigInteger("55") },
159: { Boolean.TRUE, BigInteger.class, new BigInteger("1") },
160: { new Byte((byte) 55), BigInteger.class,
161: new BigInteger("55") },
162: { new Character((char) 55), BigInteger.class,
163: new BigInteger("55") },
164: { new Short((short) 55), BigInteger.class,
165: new BigInteger("55") },
166: { new Long(55), BigInteger.class, new BigInteger("55") },
167: { new Float(55), BigInteger.class, new BigInteger("55") },
168: { new BigInteger("55"), BigInteger.class,
169: new BigInteger("55") },
170: { new BigDecimal("55"), BigInteger.class,
171: new BigInteger("55") },
172:
173: /* To BigDecimal.class */
174: { "55.1234", BigDecimal.class, new BigDecimal("55.1234") },
175: { new Integer(55), BigDecimal.class, new BigDecimal("55") },
176: { new Double(55.1234), BigDecimal.class,
177: new BigDecimal("55.1234"), new Integer(4) },
178: { Boolean.TRUE, BigDecimal.class, new BigDecimal("1") },
179: { new Byte((byte) 55), BigDecimal.class,
180: new BigDecimal("55") },
181: { new Character((char) 55), BigDecimal.class,
182: new BigDecimal("55") },
183: { new Short((short) 55), BigDecimal.class,
184: new BigDecimal("55") },
185: { new Long(55), BigDecimal.class, new BigDecimal("55") },
186: { new Float(55.1234), BigDecimal.class,
187: new BigDecimal("55.1234"), new Integer(4) },
188: { new BigInteger("55"), BigDecimal.class,
189: new BigDecimal("55") },
190: { new BigDecimal("55.1234"), BigDecimal.class,
191: new BigDecimal("55.1234") }, };
192:
193: private Object value;
194: private Class toClass;
195: private Object expectedValue;
196: private int scale;
197:
198: /*===================================================================
199: Public static methods
200: ===================================================================*/
201: public static TestSuite suite() {
202: TestSuite result = new TestSuite();
203:
204: for (int i = 0; i < TESTS.length; i++) {
205: result.addTest(new NumericConversionTest(TESTS[i][0],
206: (Class) TESTS[i][1], TESTS[i][2],
207: (TESTS[i].length > 3) ? ((Integer) TESTS[i][3])
208: .intValue() : -1));
209: }
210: return result;
211: }
212:
213: /*===================================================================
214: Constructors
215: ===================================================================*/
216: public NumericConversionTest(Object value, Class toClass,
217: Object expectedValue, int scale) {
218: super (
219: value
220: + " ["
221: + value.getClass().getName()
222: + "] -> "
223: + toClass.getName()
224: + " == "
225: + expectedValue
226: + " ["
227: + expectedValue.getClass().getName()
228: + "]"
229: + ((scale >= 0) ? (" (to within " + scale + " decimal places)")
230: : ""));
231: this .value = value;
232: this .toClass = toClass;
233: this .expectedValue = expectedValue;
234: this .scale = scale;
235: }
236:
237: /*===================================================================
238: Overridden methods
239: ===================================================================*/
240: protected void runTest() throws OgnlException {
241: Object result;
242:
243: result = OgnlOps.convertValue(value, toClass);
244: if (!isEqual(result, expectedValue)) {
245: if (scale >= 0) {
246: double scalingFactor = Math.pow(10, scale), v1 = ((Number) value)
247: .doubleValue()
248: * scalingFactor, v2 = ((Number) expectedValue)
249: .doubleValue()
250: * scalingFactor;
251:
252: assertTrue((int) v1 == (int) v2);
253: } else {
254: fail();
255: }
256: }
257: }
258: }
|