View Javadoc

1   /*
2    * $Id: IndexAccessTest.java 1188000 2011-10-23 23:10:24Z 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.MethodFailedException;
23  import org.apache.commons.ognl.NoSuchPropertyException;
24  import org.apache.commons.ognl.test.objects.IndexedSetObject;
25  import org.apache.commons.ognl.test.objects.Root;
26  import org.junit.runner.RunWith;
27  import org.junit.runners.Parameterized;
28  import org.junit.runners.Parameterized.Parameters;
29  
30  import java.util.ArrayList;
31  import java.util.Collection;
32  
33  @RunWith(value = Parameterized.class)
34  public class IndexAccessTest
35      extends OgnlTestCase
36  {
37  
38      private static Root ROOT = new Root();
39  
40      private static IndexedSetObject INDEXED_SET = new IndexedSetObject();
41  
42      private static Object[][] TESTS =
43          {
44              { ROOT, "list[index]", ROOT.getList().get( ROOT.getIndex() ) },
45              { ROOT, "list[objectIndex]", ROOT.getList().get( ROOT.getObjectIndex().intValue() ) },
46              { ROOT, "array[objectIndex]", ROOT.getArray()[ROOT.getObjectIndex().intValue()] },
47              { ROOT, "array[getObjectIndex()]", ROOT.getArray()[ROOT.getObjectIndex().intValue()] },
48              { ROOT, "array[genericIndex]", ROOT.getArray()[( (Integer) ROOT.getGenericIndex() ).intValue()] },
49              { ROOT, "booleanArray[self.objectIndex]", Boolean.FALSE },
50              { ROOT, "booleanArray[getObjectIndex()]", Boolean.FALSE },
51              { ROOT, "booleanArray[nullIndex]", NoSuchPropertyException.class },
52              { ROOT, "list[size() - 1]", MethodFailedException.class },
53              { ROOT, "(index == (array.length - 3)) ? 'toggle toggleSelected' : 'toggle'", "toggle toggleSelected" },
54              { ROOT, "\"return toggleDisplay('excdisplay\"+index+\"', this)\"",
55                  "return toggleDisplay('excdisplay1', this)" }, { ROOT, "map[mapKey].split('=')[0]", "StringStuff" },
56              { ROOT, "booleanValues[index1][index2]", Boolean.FALSE },
57              { ROOT, "tab.searchCriteria[index1].displayName", "Woodland creatures" },
58              { ROOT, "tab.searchCriteriaSelections[index1][index2]", Boolean.TRUE },
59              { ROOT, "tab.searchCriteriaSelections[index1][index2]", Boolean.TRUE, Boolean.FALSE, Boolean.FALSE },
60              { ROOT, "map['bar'].value", 100, 50, 50 }, { INDEXED_SET, "thing[\"x\"].val", 1, 2, 2 } };
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 ( int i = 0; i < TESTS.length; i++ )
71          {
72              Object[] tmp = new Object[6];
73              tmp[0] = TESTS[i][1];
74              tmp[1] = TESTS[i][0];
75              tmp[2] = TESTS[i][1];
76              tmp[3] = TESTS[i][2];
77  
78              if ( TESTS[i].length == 5 )
79              {
80                  tmp[4] = TESTS[i][3];
81                  tmp[5] = TESTS[i][4];
82              }
83  
84              data.add( tmp );
85          }
86          return data;
87      }
88  
89      /*
90       * =================================================================== Constructors
91       * ===================================================================
92       */
93      public IndexAccessTest( String name, Object root, String expressionString, Object expectedResult, Object setValue,
94                              Object expectedAfterSetResult )
95      {
96          super( name, root, expressionString, expectedResult, setValue, expectedAfterSetResult );
97      }
98  }