View Javadoc

1   /*
2    * $Id: IndexedPropertyTest.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.Indexed;
23  import org.apache.commons.ognl.test.objects.Root;
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.Collection;
30  
31  @RunWith(value = Parameterized.class)
32  public class IndexedPropertyTest
33      extends OgnlTestCase
34  {
35  
36      private static Indexed INDEXED = new Indexed();
37  
38      private static Root ROOT = new Root();
39  
40      private static Object[][] TESTS = {
41          // Indexed properties
42          { INDEXED, "getValues", INDEXED.getValues() }, // gets String[]
43          { INDEXED, "[\"values\"]", INDEXED.getValues() }, // String[]
44          { INDEXED.getValues(), "[0]", INDEXED.getValues()[0] }, // "foo"
45          { INDEXED, "getValues()[0]", INDEXED.getValues()[0] }, // "foo" directly from array
46          { INDEXED, "values[0]", INDEXED.getValues( 0 ) }, // "foo" + "xxx"
47          { INDEXED, "values[^]", INDEXED.getValues( 0 ) }, // "foo" + "xxx"
48          { INDEXED, "values[|]", INDEXED.getValues( 1 ) }, // "bar" + "xxx"
49          { INDEXED, "values[$]", INDEXED.getValues( 2 ) }, // "baz" + "xxx"
50          { INDEXED, "values[1]", "bar" + "xxx", "xxxx" + "xxx", "xxxx" + "xxx" }, // set through setValues(int, String)
51          { INDEXED, "values[1]", "xxxx" + "xxx" }, // getValues(int) again to check if setValues(int, String) was called
52          { INDEXED, "setValues(2, \"xxxx\")", null }, // was "baz" -> "xxxx"
53          { INDEXED, "getTitle(list.size)", "Title count 3" }, { INDEXED, "source.total", 1 },
54          { ROOT, "indexer.line[index]", "line:1" }, { INDEXED, "list[2].longValue()", (long) 3 },
55          { ROOT, "map.value.id", (long) 1 }, { INDEXED, "property['hoodak']", null, "random string", "random string" } };
56  
57      /*
58       * =================================================================== Public static methods
59       * ===================================================================
60       */
61      @Parameters
62      public static Collection<Object[]> data()
63      {
64          Collection<Object[]> data = new ArrayList<Object[]>(TESTS.length);
65          for ( int i = 0; i < TESTS.length; i++ )
66          {
67              Object[] tmp = new Object[6];
68              tmp[0] = TESTS[i][1];
69              tmp[1] = TESTS[i][0];
70              tmp[2] = TESTS[i][1];
71  
72              switch ( TESTS[i].length )
73              {
74                  case 3:
75                      tmp[3] = TESTS[i][2];
76                      break;
77  
78                  case 4:
79                      tmp[3] = TESTS[i][2];
80                      tmp[4] = TESTS[i][3];
81                      break;
82  
83                  case 5:
84                      tmp[3] = TESTS[i][2];
85                      tmp[4] = TESTS[i][3];
86                      tmp[5] = TESTS[i][4];
87                      break;
88  
89                  default:
90                      throw new RuntimeException( "don't understand TEST format with length " + TESTS[i].length );
91              }
92  
93              data.add( tmp );
94          }
95          return data;
96      }
97  
98      /*
99       * =================================================================== Constructors
100      * ===================================================================
101      */
102     public IndexedPropertyTest( String name, Object root, String expressionString, Object expectedResult,
103                                 Object setValue, Object expectedAfterSetResult )
104     {
105         super( name, root, expressionString, expectedResult, setValue, expectedAfterSetResult );
106     }
107 }