001 /* 002 * $Id: NumericConversionTest.java 1104080 2011-05-17 09:22:09Z mcucchiara $ 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 */ 020 package org.apache.commons.ognl.test; 021 022 import static junit.framework.Assert.assertTrue; 023 import static junit.framework.Assert.fail; 024 025 import java.math.BigDecimal; 026 import java.math.BigInteger; 027 import java.util.ArrayList; 028 import java.util.Collection; 029 030 import org.apache.commons.ognl.OgnlException; 031 import org.apache.commons.ognl.OgnlOps; 032 import org.junit.Test; 033 import org.junit.runner.RunWith; 034 import org.junit.runners.Parameterized; 035 import org.junit.runners.Parameterized.Parameters; 036 037 @RunWith(value = Parameterized.class) 038 public class NumericConversionTest 039 extends OgnlTestCase 040 { 041 private static Object[][] TESTS = { 042 /* To Integer.class */ 043 { "55", Integer.class, new Integer( 55 ) }, { new Integer( 55 ), Integer.class, new Integer( 55 ) }, 044 { new Double( 55 ), Integer.class, new Integer( 55 ) }, { Boolean.TRUE, Integer.class, new Integer( 1 ) }, 045 { new Byte( (byte) 55 ), Integer.class, new Integer( 55 ) }, 046 { new Character( (char) 55 ), Integer.class, new Integer( 55 ) }, 047 { new Short( (short) 55 ), Integer.class, new Integer( 55 ) }, 048 { new Long( 55 ), Integer.class, new Integer( 55 ) }, { new Float( 55 ), Integer.class, new Integer( 55 ) }, 049 { new BigInteger( "55" ), Integer.class, new Integer( 55 ) }, 050 { new BigDecimal( "55" ), Integer.class, new Integer( 55 ) }, 051 052 /* To Double.class */ 053 { "55.1234", Double.class, new Double( 55.1234 ) }, { new Integer( 55 ), Double.class, new Double( 55 ) }, 054 { new Double( 55.1234 ), Double.class, new Double( 55.1234 ) }, 055 { Boolean.TRUE, Double.class, new Double( 1 ) }, { new Byte( (byte) 55 ), Double.class, new Double( 55 ) }, 056 { new Character( (char) 55 ), Double.class, new Double( 55 ) }, 057 { new Short( (short) 55 ), Double.class, new Double( 55 ) }, 058 { new Long( 55 ), Double.class, new Double( 55 ) }, 059 { new Float( 55.1234 ), Double.class, new Double( 55.1234 ), new Integer( 4 ) }, 060 { new BigInteger( "55" ), Double.class, new Double( 55 ) }, 061 { new BigDecimal( "55.1234" ), Double.class, new Double( 55.1234 ) }, 062 063 /* To Boolean.class */ 064 { "true", Boolean.class, Boolean.TRUE }, { new Integer( 55 ), Boolean.class, Boolean.TRUE }, 065 { new Double( 55 ), Boolean.class, Boolean.TRUE }, { Boolean.TRUE, Boolean.class, Boolean.TRUE }, 066 { new Byte( (byte) 55 ), Boolean.class, Boolean.TRUE }, 067 { new Character( (char) 55 ), Boolean.class, Boolean.TRUE }, 068 { new Short( (short) 55 ), Boolean.class, Boolean.TRUE }, { new Long( 55 ), Boolean.class, Boolean.TRUE }, 069 { new Float( 55 ), Boolean.class, Boolean.TRUE }, { new BigInteger( "55" ), Boolean.class, Boolean.TRUE }, 070 { new BigDecimal( "55" ), Boolean.class, Boolean.TRUE }, 071 072 /* To Byte.class */ 073 { "55", Byte.class, new Byte( (byte) 55 ) }, { new Integer( 55 ), Byte.class, new Byte( (byte) 55 ) }, 074 { new Double( 55 ), Byte.class, new Byte( (byte) 55 ) }, { Boolean.TRUE, Byte.class, new Byte( (byte) 1 ) }, 075 { new Byte( (byte) 55 ), Byte.class, new Byte( (byte) 55 ) }, 076 { new Character( (char) 55 ), Byte.class, new Byte( (byte) 55 ) }, 077 { new Short( (short) 55 ), Byte.class, new Byte( (byte) 55 ) }, 078 { new Long( 55 ), Byte.class, new Byte( (byte) 55 ) }, { new Float( 55 ), Byte.class, new Byte( (byte) 55 ) }, 079 { new BigInteger( "55" ), Byte.class, new Byte( (byte) 55 ) }, 080 { new BigDecimal( "55" ), Byte.class, new Byte( (byte) 55 ) }, 081 082 /* To Character.class */ 083 { "55", Character.class, new Character( (char) 55 ) }, 084 { new Integer( 55 ), Character.class, new Character( (char) 55 ) }, 085 { new Double( 55 ), Character.class, new Character( (char) 55 ) }, 086 { Boolean.TRUE, Character.class, new Character( (char) 1 ) }, 087 { new Byte( (byte) 55 ), Character.class, new Character( (char) 55 ) }, 088 { new Character( (char) 55 ), Character.class, new Character( (char) 55 ) }, 089 { new Short( (short) 55 ), Character.class, new Character( (char) 55 ) }, 090 { new Long( 55 ), Character.class, new Character( (char) 55 ) }, 091 { new Float( 55 ), Character.class, new Character( (char) 55 ) }, 092 { new BigInteger( "55" ), Character.class, new Character( (char) 55 ) }, 093 { new BigDecimal( "55" ), Character.class, new Character( (char) 55 ) }, 094 095 /* To Short.class */ 096 { "55", Short.class, new Short( (short) 55 ) }, { new Integer( 55 ), Short.class, new Short( (short) 55 ) }, 097 { new Double( 55 ), Short.class, new Short( (short) 55 ) }, 098 { Boolean.TRUE, Short.class, new Short( (short) 1 ) }, 099 { 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 /* To Long.class */ 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 /* To Float.class */ 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 /* To BigInteger.class */ 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 /* To BigDecimal.class */ 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 * =================================================================== Public static methods 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 * =================================================================== Constructors 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 * =================================================================== Overridden methods 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 }