001 /*
002 * $Id: InterfaceInheritanceTest.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.OgnlRuntime;
023 import org.apache.commons.ognl.test.objects.Bean1;
024 import org.apache.commons.ognl.test.objects.BeanProvider;
025 import org.apache.commons.ognl.test.objects.BeanProviderAccessor;
026 import org.apache.commons.ognl.test.objects.EvenOdd;
027 import org.apache.commons.ognl.test.objects.ListSourceImpl;
028 import org.apache.commons.ognl.test.objects.Root;
029 import org.junit.Before;
030 import org.junit.Test;
031 import org.junit.runner.RunWith;
032 import org.junit.runners.Parameterized;
033 import org.junit.runners.Parameterized.Parameters;
034
035 import java.util.ArrayList;
036 import java.util.Collection;
037 import java.util.List;
038
039 @RunWith(value = Parameterized.class)
040 public class InterfaceInheritanceTest
041 extends OgnlTestCase
042 {
043
044 private static Root ROOT = new Root();
045
046 static
047 {
048 ROOT.getBeans().setBean( "testBean", new Bean1() );
049 ROOT.getBeans().setBean( "evenOdd", new EvenOdd() );
050
051 List list = new ListSourceImpl();
052 list.add( "test1" );
053
054 ROOT.getMap().put( "customList", list );
055 }
056
057 private static Object[][] TESTS = { { ROOT, "myMap", ROOT.getMyMap() }, { ROOT, "myMap.test", ROOT },
058 { ROOT.getMyMap(), "list", ROOT.getList() }, { ROOT, "myMap.array[0]", new Integer( ROOT.getArray()[0] ) },
059 { ROOT, "myMap.list[1]", ROOT.getList().get( 1 ) }, { ROOT, "myMap[^]", new Integer( 99 ) },
060 { ROOT, "myMap[$]", null },
061 { ROOT.getMyMap(), "array[$]", new Integer( ROOT.getArray()[ROOT.getArray().length - 1] ) },
062 { ROOT, "[\"myMap\"]", ROOT.getMyMap() }, { ROOT, "myMap[null]", null }, { ROOT, "myMap[#x = null]", null },
063 { ROOT, "myMap.(null,test)", ROOT }, { ROOT, "myMap[null] = 25", new Integer( 25 ) },
064 { ROOT, "myMap[null]", new Integer( 25 ), new Integer( 50 ), new Integer( 50 ) },
065 { ROOT, "beans.testBean", ROOT.getBeans().getBean( "testBean" ) }, { ROOT, "beans.evenOdd.next", "even" },
066 { ROOT, "map.comp.form.clientId", "form1" }, { ROOT, "map.comp.getCount(genericIndex)", Integer.valueOf( 0 ) },
067 { ROOT, "map.customList.total", Integer.valueOf( 1 ) }, { ROOT, "myTest.theMap['key']", "value" },
068 { ROOT, "contentProvider.hasChildren(property)", Boolean.TRUE },
069 { ROOT, "objectIndex instanceof java.lang.Object", Boolean.TRUE } };
070
071 /*
072 * =================================================================== Public static methods
073 * ===================================================================
074 */
075 @Parameters
076 public static Collection<Object[]> data()
077 {
078 Collection<Object[]> data = new ArrayList<Object[]>(TESTS.length);
079 for ( int i = 0; i < TESTS.length; i++ )
080 {
081 Object[] tmp = new Object[6];
082 tmp[0] = TESTS[i][1];
083 tmp[1] = TESTS[i][0];
084 tmp[2] = TESTS[i][1];
085
086 switch ( TESTS[i].length )
087 {
088 case 3:
089 tmp[3] = TESTS[i][2];
090 break;
091
092 case 4:
093 tmp[3] = TESTS[i][2];
094 tmp[4] = TESTS[i][3];
095 break;
096
097 case 5:
098 tmp[3] = TESTS[i][2];
099 tmp[4] = TESTS[i][3];
100 tmp[5] = TESTS[i][4];
101 break;
102
103 default:
104 throw new RuntimeException( "don't understand TEST format with length " + TESTS[i].length );
105 }
106
107 data.add( tmp );
108 }
109 return data;
110 }
111
112 /*
113 * =================================================================== Constructors
114 * ===================================================================
115 */
116 public InterfaceInheritanceTest( String name, Object root, String expressionString, Object expectedResult,
117 Object setValue, Object expectedAfterSetResult )
118 {
119 super( name, root, expressionString, expectedResult, setValue, expectedAfterSetResult );
120 }
121
122 @Override
123 @Before
124 public void setUp()
125 {
126 super.setUp();
127
128 OgnlRuntime.setPropertyAccessor( BeanProvider.class, new BeanProviderAccessor() );
129 }
130
131 @Override
132 @Test
133
134 public void runTest()
135 throws Exception
136 {
137 super.runTest();
138 }
139 }