View Javadoc

1   /*
2    * $Id: NumberFormatExceptionTest.java 1104080 2011-05-17 09:22:09Z mcucchiara $
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   * http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
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          // NumberFormatException handling (default is to throw NumberFormatException on bad string conversions)
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       * =================================================================== Public static methods
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      * =================================================================== 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 }