View Javadoc

1   /*
2    * $Id: ArrayElementsTest.java 1230454 2012-01-12 09:35:53Z 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.Before;
24  import org.junit.runner.RunWith;
25  import org.junit.runners.Parameterized;
26  import org.junit.runners.Parameterized.Parameters;
27  
28  import java.util.ArrayList;
29  import java.util.Arrays;
30  import java.util.Collection;
31  
32  @RunWith(value = Parameterized.class)
33  public class ArrayElementsTest
34      extends OgnlTestCase
35  {
36  
37      private static String[] STRING_ARRAY = new String[] { "hello", "world" };
38  
39      private static int[] INT_ARRAY = new int[] { 10, 20 };
40  
41      private static Root ROOT = new Root();
42  
43      private static Object[][] TESTS = {
44          // Array elements test
45          { STRING_ARRAY, "length", 2 },
46          { STRING_ARRAY, "#root[1]", "world" },
47          { INT_ARRAY, "#root[1]", 20 },
48          { INT_ARRAY, "#root[1]", 20, "50", 50 },
49          { INT_ARRAY, "#root[1]", 50, new String[] { "50", "100" }, 50 },
50          { ROOT, "intValue", 0, new String[] { "50", "100" }, 50 },
51          { ROOT, "array", ROOT.getArray(), new String[] { "50", "100" }, new int[] { 50, 100 } },
52          { null, "\"{Hello}\".toCharArray()[6]", '}' },
53          { null, "\"Tapestry\".toCharArray()[2]", 'p' },
54          { null, "{'1','2','3'}",
55              Arrays.asList( '1', '2', '3' ) },
56          { null, "{ true, !false }", Arrays.asList( Boolean.TRUE, Boolean.TRUE ) } };
57  
58      /*
59       * =================================================================== Private static methods
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" );
96              }
97  
98              data.add( tmp );
99          }
100         return data;
101     }
102 
103     /*
104      * =================================================================== Constructors
105      * ===================================================================
106      */
107     public ArrayElementsTest( String name, Object root, String expressionString, Object expectedResult,
108                               Object setValue, Object expectedAfterSetResult )
109     {
110         super( name, root, expressionString, expectedResult, setValue, expectedAfterSetResult );
111     }
112 
113     /*
114      * =================================================================== Overridden methods
115      * ===================================================================
116      */
117     @Override
118     @Before
119     public void setUp()
120     {
121         super.setUp();
122         /**
123          * TypeConverter arrayConverter;
124          * arrayConverter = new DefaultTypeConverter() { public Object convertValue(Map context, Object target, Member
125          * member, String propertyName, Object value, Class toType) { if (value.getClass().isArray()) { if
126          * (!toType.isArray()) { value = Array.get(value, 0); } } return super.convertValue(context, target, member,
127          * propertyName, value, toType); } }; _context.setTypeConverter(arrayConverter);
128          */
129     }
130 }