View Javadoc

1   /*
2    * $Id: InterfaceInheritanceTest.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.OgnlRuntime;
23  import org.apache.commons.ognl.test.objects.Bean1;
24  import org.apache.commons.ognl.test.objects.BeanProvider;
25  import org.apache.commons.ognl.test.objects.BeanProviderAccessor;
26  import org.apache.commons.ognl.test.objects.EvenOdd;
27  import org.apache.commons.ognl.test.objects.ListSourceImpl;
28  import org.apache.commons.ognl.test.objects.Root;
29  import org.junit.Before;
30  import org.junit.Test;
31  import org.junit.runner.RunWith;
32  import org.junit.runners.Parameterized;
33  import org.junit.runners.Parameterized.Parameters;
34  
35  import java.util.ArrayList;
36  import java.util.Collection;
37  import java.util.List;
38  
39  @RunWith(value = Parameterized.class)
40  public class InterfaceInheritanceTest
41      extends OgnlTestCase
42  {
43  
44      private static Root ROOT = new Root();
45  
46      static
47      {
48          ROOT.getBeans().setBean( "testBean", new Bean1() );
49          ROOT.getBeans().setBean( "evenOdd", new EvenOdd() );
50  
51          List list = new ListSourceImpl();
52          list.add( "test1" );
53  
54          ROOT.getMap().put( "customList", list );
55      }
56  
57      private static Object[][] TESTS = { { ROOT, "myMap", ROOT.getMyMap() }, { ROOT, "myMap.test", ROOT },
58          { ROOT.getMyMap(), "list", ROOT.getList() }, { ROOT, "myMap.array[0]", new Integer( ROOT.getArray()[0] ) },
59          { ROOT, "myMap.list[1]", ROOT.getList().get( 1 ) }, { ROOT, "myMap[^]", new Integer( 99 ) },
60          { ROOT, "myMap[$]", null },
61          { ROOT.getMyMap(), "array[$]", new Integer( ROOT.getArray()[ROOT.getArray().length - 1] ) },
62          { ROOT, "[\"myMap\"]", ROOT.getMyMap() }, { ROOT, "myMap[null]", null }, { ROOT, "myMap[#x = null]", null },
63          { ROOT, "myMap.(null,test)", ROOT }, { ROOT, "myMap[null] = 25", new Integer( 25 ) },
64          { ROOT, "myMap[null]", new Integer( 25 ), new Integer( 50 ), new Integer( 50 ) },
65          { ROOT, "beans.testBean", ROOT.getBeans().getBean( "testBean" ) }, { ROOT, "beans.evenOdd.next", "even" },
66          { ROOT, "map.comp.form.clientId", "form1" }, { ROOT, "map.comp.getCount(genericIndex)", Integer.valueOf( 0 ) },
67          { ROOT, "map.customList.total", Integer.valueOf( 1 ) }, { ROOT, "myTest.theMap['key']", "value" },
68          { ROOT, "contentProvider.hasChildren(property)", Boolean.TRUE },
69          { ROOT, "objectIndex instanceof java.lang.Object", Boolean.TRUE } };
70  
71      /*
72       * =================================================================== Public static methods
73       * ===================================================================
74       */
75      @Parameters
76      public static Collection<Object[]> data()
77      {
78          Collection<Object[]> data = new ArrayList<Object[]>(TESTS.length);
79          for ( int i = 0; i < TESTS.length; i++ )
80          {
81              Object[] tmp = new Object[6];
82              tmp[0] = TESTS[i][1];
83              tmp[1] = TESTS[i][0];
84              tmp[2] = TESTS[i][1];
85  
86              switch ( TESTS[i].length )
87              {
88                  case 3:
89                      tmp[3] = TESTS[i][2];
90                      break;
91  
92                  case 4:
93                      tmp[3] = TESTS[i][2];
94                      tmp[4] = TESTS[i][3];
95                      break;
96  
97                  case 5:
98                      tmp[3] = TESTS[i][2];
99                      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 }