View Javadoc

1   /*
2    * $Id: PropertyNotFoundTest.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 java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.Map;
25  
26  import org.apache.commons.ognl.OgnlContext;
27  import org.apache.commons.ognl.OgnlException;
28  import org.apache.commons.ognl.OgnlRuntime;
29  import org.apache.commons.ognl.PropertyAccessor;
30  import org.junit.Before;
31  import org.junit.runner.RunWith;
32  import org.junit.runners.Parameterized;
33  import org.junit.runners.Parameterized.Parameters;
34  
35  @RunWith(value = Parameterized.class)
36  public class PropertyNotFoundTest
37      extends OgnlTestCase
38  {
39      private static final Blah BLAH = new Blah();
40  
41      private static Object[][] TESTS = { { BLAH, "webwork.token.name", OgnlException.class, "W value",
42          OgnlException.class }, };
43  
44      /*
45       * =================================================================== Public static classes
46       * ===================================================================
47       */
48      public static class Blah
49      {
50          String x;
51  
52          String y;
53  
54          public String getX()
55          {
56              return x;
57          }
58  
59          public void setX( String x )
60          {
61              this.x = x;
62          }
63  
64          public String getY()
65          {
66              return y;
67          }
68  
69          public void setY( String y )
70          {
71              this.y = y;
72          }
73      }
74  
75      public static class BlahPropertyAccessor
76          implements PropertyAccessor
77      {
78          public void setProperty( Map context, Object target, Object name, Object value )
79              throws OgnlException
80          {
81          }
82  
83          public Object getProperty( Map context, Object target, Object name )
84              throws OgnlException
85          {
86              if ( "x".equals( name ) || "y".equals( name ) )
87              {
88                  return OgnlRuntime.getProperty( (OgnlContext) context, target, name );
89              }
90              return null;
91          }
92  
93          public String getSourceAccessor( OgnlContext context, Object target, Object index )
94          {
95              return index.toString();
96          }
97  
98          public String getSourceSetter( OgnlContext context, Object target, Object index )
99          {
100             return index.toString();
101         }
102     }
103 
104     /*
105      * =================================================================== Public static methods
106      * ===================================================================
107      */
108     @Parameters
109     public static Collection<Object[]> data()
110     {
111         Collection<Object[]> data = new ArrayList<Object[]>(TESTS.length);
112         for ( int i = 0; i < TESTS.length; i++ )
113         {
114             Object[] tmp = new Object[6];
115             tmp[0] = TESTS[i][1];
116             tmp[1] = TESTS[i][0];
117             tmp[2] = TESTS[i][1];
118 
119             switch ( TESTS[i].length )
120             {
121                 case 3:
122                     tmp[3] = TESTS[i][2];
123                     break;
124 
125                 case 4:
126                     tmp[3] = TESTS[i][2];
127                     tmp[4] = TESTS[i][3];
128                     break;
129 
130                 case 5:
131                     tmp[3] = TESTS[i][2];
132                     tmp[4] = TESTS[i][3];
133                     tmp[5] = TESTS[i][4];
134                     break;
135 
136                 default:
137                     throw new RuntimeException( "don't understand TEST format with length " + TESTS[i].length );
138             }
139 
140             data.add( tmp );
141         }
142         return data;
143     }
144 
145     /*
146      * =================================================================== Constructors
147      * ===================================================================
148      */
149     public PropertyNotFoundTest( String name, Object root, String expressionString, Object expectedResult,
150                                  Object setValue, Object expectedAfterSetResult )
151     {
152         super( name, root, expressionString, expectedResult, setValue, expectedAfterSetResult );
153     }
154 
155     @Before
156     @Override
157     public void setUp()
158     {
159         super.setUp();
160         OgnlRuntime.setPropertyAccessor( Blah.class, new BlahPropertyAccessor() );
161     }
162 }