001 /*
002 * $Id: NumberFormatExceptionTest.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 org.apache.commons.ognl.OgnlException;
023 import org.apache.commons.ognl.test.objects.Simple;
024 import org.junit.runner.RunWith;
025 import org.junit.runners.Parameterized;
026 import org.junit.runners.Parameterized.Parameters;
027
028 import java.math.BigDecimal;
029 import java.math.BigInteger;
030 import java.util.ArrayList;
031 import java.util.Collection;
032
033 @RunWith(value = Parameterized.class)
034 public class NumberFormatExceptionTest
035 extends OgnlTestCase
036 {
037 private static Simple SIMPLE = new Simple();
038
039 private static Object[][] TESTS = {
040 // NumberFormatException handling (default is to throw NumberFormatException on bad string conversions)
041 { SIMPLE, "floatValue", 0f, 10f, 10f },
042 { SIMPLE, "floatValue", 10f, "x10x", OgnlException.class },
043
044 { SIMPLE, "intValue", 0, 34, 34 },
045 { SIMPLE, "intValue", 34, "foobar", OgnlException.class },
046 { SIMPLE, "intValue", 34, "", OgnlException.class },
047 { SIMPLE, "intValue", 34, " \t", OgnlException.class },
048 { SIMPLE, "intValue", 34, " \t1234\t\t", 1234 },
049
050 { SIMPLE, "bigIntValue", BigInteger.valueOf( 0 ), BigInteger.valueOf( 34 ), BigInteger.valueOf( 34 ) },
051 { SIMPLE, "bigIntValue", BigInteger.valueOf( 34 ), null, null },
052 { SIMPLE, "bigIntValue", null, "", OgnlException.class },
053 { SIMPLE, "bigIntValue", null, "foobar", OgnlException.class },
054
055 { SIMPLE, "bigDecValue", new BigDecimal( 0.0 ), new BigDecimal( 34.55 ), new BigDecimal( 34.55 ) },
056 { SIMPLE, "bigDecValue", new BigDecimal( 34.55 ), null, null },
057 { SIMPLE, "bigDecValue", null, "", OgnlException.class },
058 { SIMPLE, "bigDecValue", null, "foobar", OgnlException.class }
059
060 };
061
062 /*
063 * =================================================================== Public static methods
064 * ===================================================================
065 */
066 @Parameters
067 public static Collection<Object[]> data()
068 {
069 Collection<Object[]> data = new ArrayList<Object[]>(TESTS.length);
070 for ( Object[] TEST : TESTS )
071 {
072 Object[] tmp = new Object[6];
073 tmp[0] = TEST[1];
074 tmp[1] = TEST[0];
075 tmp[2] = TEST[1];
076
077 switch ( TEST.length )
078 {
079 case 3:
080 tmp[3] = TEST[2];
081 break;
082
083 case 4:
084 tmp[3] = TEST[2];
085 tmp[4] = TEST[3];
086 break;
087
088 case 5:
089 tmp[3] = TEST[2];
090 tmp[4] = TEST[3];
091 tmp[5] = TEST[4];
092 break;
093
094 default:
095 throw new RuntimeException( "don't understand TEST format with length " + TEST.length );
096 }
097
098 data.add( tmp );
099 }
100 return data;
101 }
102
103 /*
104 * =================================================================== Constructors
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 }