View Javadoc

1   /*
2    * $Id: SetterWithConversionTest.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.test.objects.Root;
23  import org.junit.runner.RunWith;
24  import org.junit.runners.Parameterized;
25  import org.junit.runners.Parameterized.Parameters;
26  
27  import java.util.ArrayList;
28  import java.util.Collection;
29  
30  @RunWith(value = Parameterized.class)
31  public class SetterWithConversionTest
32      extends OgnlTestCase
33  {
34      private static Root ROOT = new Root();
35  
36      private static Object[][] TESTS = {
37          // Property set with conversion
38          { ROOT, "intValue", new Integer( 0 ), new Double( 6.5 ), new Integer( 6 ) },
39          { ROOT, "intValue", new Integer( 6 ), new Double( 1025.87645 ), new Integer( 1025 ) },
40          { ROOT, "intValue", new Integer( 1025 ), "654", new Integer( 654 ) },
41          { ROOT, "stringValue", null, new Integer( 25 ), "25" },
42          { ROOT, "stringValue", "25", new Float( 100.25 ), "100.25" },
43          { ROOT, "anotherStringValue", "foo", new Integer( 0 ), "0" },
44          { ROOT, "anotherStringValue", "0", new Double( 0.5 ), "0.5" },
45          { ROOT, "anotherIntValue", new Integer( 123 ), "5", new Integer( 5 ) },
46          { ROOT, "anotherIntValue", new Integer( 5 ), new Double( 100.25 ), new Integer( 100 ) },
47      // { ROOT, "anotherIntValue", new Integer(100), new String[] { "55" }, new Integer(55)},
48      // { ROOT, "yetAnotherIntValue", new Integer(46), new String[] { "55" }, new Integer(55)},
49  
50          };
51  
52      /*
53       * =================================================================== Public static methods
54       * ===================================================================
55       */
56      @Parameters
57      public static Collection<Object[]> data()
58      {
59          Collection<Object[]> data = new ArrayList<Object[]>(TESTS.length);
60          for ( Object[] TEST : TESTS )
61          {
62              Object[] tmp = new Object[6];
63              tmp[0] = TEST[1];
64              tmp[1] = TEST[0];
65              tmp[2] = TEST[1];
66  
67              switch (TEST.length) {
68                  case 3:
69                      tmp[3] = TEST[2];
70                      break;
71  
72                  case 4:
73                      tmp[3] = TEST[2];
74                      tmp[4] = TEST[3];
75                      break;
76  
77                  case 5:
78                      tmp[3] = TEST[2];
79                      tmp[4] = TEST[3];
80                      tmp[5] = TEST[4];
81                      break;
82  
83                  default:
84                      throw new RuntimeException("don't understand TEST format with length " + TEST.length);
85              }
86  
87              data.add(tmp);
88          }
89          return data;
90      }
91  
92      /*
93       * =================================================================== Constructors
94       * ===================================================================
95       */
96      public SetterWithConversionTest( String name, Object root, String expressionString, Object expectedResult,
97                                       Object setValue, Object expectedAfterSetResult )
98      {
99          super( name, root, expressionString, expectedResult, setValue, expectedAfterSetResult );
100     }
101 }