View Javadoc

1   /*
2    * $Id: ObjectIndexedPropertyTest.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.OgnlException;
23  import org.apache.commons.ognl.test.objects.Bean1;
24  import org.apache.commons.ognl.test.objects.ObjectIndexed;
25  import org.junit.runner.RunWith;
26  import org.junit.runners.Parameterized;
27  import org.junit.runners.Parameterized.Parameters;
28  
29  import java.util.ArrayList;
30  import java.util.Collection;
31  
32  @RunWith(value = Parameterized.class)
33  public class ObjectIndexedPropertyTest
34      extends OgnlTestCase
35  {
36  
37      private static ObjectIndexed OBJECT_INDEXED = new ObjectIndexed();
38  
39      private static Bean1 root = new Bean1();
40  
41      private static Object[][] TESTS = {
42          // Arbitrary indexed properties
43          { OBJECT_INDEXED, "attributes[\"bar\"]", "baz" }, // get non-indexed property through
44          // attributes Map
45          { OBJECT_INDEXED, "attribute[\"foo\"]", "bar" }, // get indexed property
46          { OBJECT_INDEXED, "attribute[\"bar\"]", "baz", "newValue", "newValue" }, // set
47          // indexed
48          // property
49          { OBJECT_INDEXED, "attribute[\"bar\"]", "newValue" },// get indexed property back to
50          // confirm
51          { OBJECT_INDEXED, "attributes[\"bar\"]", "newValue" }, // get property back through Map
52          // to confirm
53          { OBJECT_INDEXED, "attribute[\"other\"].attribute[\"bar\"]", "baz" }, // get indexed
54          // property from
55          // indexed, then
56          // through other
57          { OBJECT_INDEXED, "attribute[\"other\"].attributes[\"bar\"]", "baz" }, // get property
58          // back through
59          // Map to
60          // confirm
61          { OBJECT_INDEXED, "attribute[$]", OgnlException.class }, // illegal DynamicSubscript
62          // access to object indexed
63          // property
64          { root, "bean2.bean3.indexedValue[25]", null } };
65  
66      /*
67       * =================================================================== Public static methods
68       * ===================================================================
69       */
70      @Parameters
71      public static Collection<Object[]> data()
72      {
73          Collection<Object[]> data = new ArrayList<Object[]>(TESTS.length);
74          for ( int i = 0; i < TESTS.length; i++ )
75          {
76              Object[] tmp = new Object[6];
77              tmp[0] = TESTS[i][1];
78              tmp[1] = TESTS[i][0];
79              tmp[2] = TESTS[i][1];
80  
81              switch ( TESTS[i].length )
82              {
83                  case 3:
84                      tmp[3] = TESTS[i][2];
85                      break;
86  
87                  case 4:
88                      tmp[3] = TESTS[i][2];
89                      tmp[4] = TESTS[i][3];
90                      break;
91  
92                  case 5:
93                      tmp[3] = TESTS[i][2];
94                      tmp[4] = TESTS[i][3];
95                      tmp[5] = TESTS[i][4];
96                      break;
97  
98                  default:
99                      throw new RuntimeException( "don't understand TEST format with length " + TESTS[i].length );
100             }
101 
102             data.add( tmp );
103         }
104         return data;
105     }
106 
107     /*
108      * =================================================================== Constructors
109      * ===================================================================
110      */
111     public ObjectIndexedPropertyTest( String name, Object root, String expressionString, Object expectedResult,
112                                       Object setValue, Object expectedAfterSetResult )
113     {
114         super( name, root, expressionString, expectedResult, setValue, expectedAfterSetResult );
115     }
116 }