001 /* 002 * $Id: IndexAccessTest.java 1188000 2011-10-23 23:10:24Z mcucchiara $ 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 */ 020 package org.apache.commons.ognl.test; 021 022 import org.apache.commons.ognl.MethodFailedException; 023 import org.apache.commons.ognl.NoSuchPropertyException; 024 import org.apache.commons.ognl.test.objects.IndexedSetObject; 025 import org.apache.commons.ognl.test.objects.Root; 026 import org.junit.runner.RunWith; 027 import org.junit.runners.Parameterized; 028 import org.junit.runners.Parameterized.Parameters; 029 030 import java.util.ArrayList; 031 import java.util.Collection; 032 033 @RunWith(value = Parameterized.class) 034 public class IndexAccessTest 035 extends OgnlTestCase 036 { 037 038 private static Root ROOT = new Root(); 039 040 private static IndexedSetObject INDEXED_SET = new IndexedSetObject(); 041 042 private static Object[][] TESTS = 043 { 044 { ROOT, "list[index]", ROOT.getList().get( ROOT.getIndex() ) }, 045 { ROOT, "list[objectIndex]", ROOT.getList().get( ROOT.getObjectIndex().intValue() ) }, 046 { ROOT, "array[objectIndex]", ROOT.getArray()[ROOT.getObjectIndex().intValue()] }, 047 { ROOT, "array[getObjectIndex()]", ROOT.getArray()[ROOT.getObjectIndex().intValue()] }, 048 { ROOT, "array[genericIndex]", ROOT.getArray()[( (Integer) ROOT.getGenericIndex() ).intValue()] }, 049 { ROOT, "booleanArray[self.objectIndex]", Boolean.FALSE }, 050 { ROOT, "booleanArray[getObjectIndex()]", Boolean.FALSE }, 051 { ROOT, "booleanArray[nullIndex]", NoSuchPropertyException.class }, 052 { ROOT, "list[size() - 1]", MethodFailedException.class }, 053 { ROOT, "(index == (array.length - 3)) ? 'toggle toggleSelected' : 'toggle'", "toggle toggleSelected" }, 054 { ROOT, "\"return toggleDisplay('excdisplay\"+index+\"', this)\"", 055 "return toggleDisplay('excdisplay1', this)" }, { ROOT, "map[mapKey].split('=')[0]", "StringStuff" }, 056 { ROOT, "booleanValues[index1][index2]", Boolean.FALSE }, 057 { ROOT, "tab.searchCriteria[index1].displayName", "Woodland creatures" }, 058 { ROOT, "tab.searchCriteriaSelections[index1][index2]", Boolean.TRUE }, 059 { ROOT, "tab.searchCriteriaSelections[index1][index2]", Boolean.TRUE, Boolean.FALSE, Boolean.FALSE }, 060 { ROOT, "map['bar'].value", 100, 50, 50 }, { INDEXED_SET, "thing[\"x\"].val", 1, 2, 2 } }; 061 062 /* 063 * =================================================================== Public static methods 064 * =================================================================== 065 */ 066 @Parameters 067 public static Collection<Object[]> data() 068 { 069 Collection<Object[]> data = new ArrayList<Object[]>(TESTS.length); 070 for ( int i = 0; i < TESTS.length; i++ ) 071 { 072 Object[] tmp = new Object[6]; 073 tmp[0] = TESTS[i][1]; 074 tmp[1] = TESTS[i][0]; 075 tmp[2] = TESTS[i][1]; 076 tmp[3] = TESTS[i][2]; 077 078 if ( TESTS[i].length == 5 ) 079 { 080 tmp[4] = TESTS[i][3]; 081 tmp[5] = TESTS[i][4]; 082 } 083 084 data.add( tmp ); 085 } 086 return data; 087 } 088 089 /* 090 * =================================================================== Constructors 091 * =================================================================== 092 */ 093 public IndexAccessTest( String name, Object root, String expressionString, Object expectedResult, Object setValue, 094 Object expectedAfterSetResult ) 095 { 096 super( name, root, expressionString, expectedResult, setValue, expectedAfterSetResult ); 097 } 098 }