View Javadoc

1   /*
2    * $Id: ObjectIndexedTest.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 junit.framework.TestCase;
23  import junit.framework.TestSuite;
24  import org.apache.commons.ognl.Ognl;
25  import org.apache.commons.ognl.OgnlContext;
26  import org.apache.commons.ognl.OgnlException;
27  import org.apache.commons.ognl.OgnlRuntime;
28  import org.apache.commons.ognl.SimpleNode;
29  import org.junit.Before;
30  
31  public class ObjectIndexedTest
32      extends TestCase
33  {
34      protected OgnlContext context;
35  
36      /*
37       * =================================================================== Public static classes
38       * ===================================================================
39       */
40      public static interface TestInterface
41      {
42          String getSunk( String index );
43  
44          void setSunk( String index, String sunk );
45      }
46  
47      public static class Test1
48          extends Object
49          implements TestInterface
50      {
51          public String getSunk( String index )
52          {
53              return "foo";
54          }
55  
56          public void setSunk( String index, String sunk )
57          {
58              /* do nothing */
59          }
60      }
61  
62      public static class Test2
63          extends Test1
64      {
65          public String getSunk( String index )
66          {
67              return "foo";
68          }
69  
70          public void setSunk( String index, String sunk )
71          {
72              /* do nothing */
73          }
74      }
75  
76      public static class Test3
77          extends Test1
78      {
79          public String getSunk( String index )
80          {
81              return "foo";
82          }
83  
84          public void setSunk( String index, String sunk )
85          {
86              /* do nothing */
87          }
88  
89          public String getSunk( Object index )
90          {
91              return null;
92          }
93      }
94  
95      public static class Test4
96          extends Test1
97      {
98          public String getSunk( String index )
99          {
100             return "foo";
101         }
102 
103         public void setSunk( String index, String sunk )
104         {
105             /* do nothing */
106         }
107 
108         public void setSunk( Object index, String sunk )
109         {
110             /* do nothing */
111         }
112     }
113 
114     public static class Test5
115         extends Test1
116     {
117         public String getSunk( String index )
118         {
119             return "foo";
120         }
121 
122         public void setSunk( String index, String sunk )
123         {
124             /* do nothing */
125         }
126 
127         public String getSunk( Object index )
128         {
129             return null;
130         }
131 
132         public void setSunk( Object index, String sunk )
133         {
134             /* do nothing */
135         }
136     }
137 
138     /*
139      * =================================================================== Public static methods
140      * ===================================================================
141      */
142     public static TestSuite suite()
143     {
144         return new TestSuite( ObjectIndexedTest.class );
145     }
146 
147     /*
148      * =================================================================== Constructors
149      * ===================================================================
150      */
151     public ObjectIndexedTest()
152     {
153         super();
154     }
155 
156     public ObjectIndexedTest( String name )
157     {
158         super( name );
159     }
160 
161     /*
162      * =================================================================== Public methods
163      * ===================================================================
164      */
165     public void testPropertyDescriptorReflection()
166         throws Exception
167     {
168         OgnlRuntime.getPropertyDescriptor( java.util.AbstractList.class, "" );
169         OgnlRuntime.getPropertyDescriptor( java.util.AbstractSequentialList.class, "" );
170         OgnlRuntime.getPropertyDescriptor( java.lang.reflect.Array.class, "" );
171         OgnlRuntime.getPropertyDescriptor( java.util.ArrayList.class, "" );
172         OgnlRuntime.getPropertyDescriptor( java.util.BitSet.class, "" );
173         OgnlRuntime.getPropertyDescriptor( java.util.Calendar.class, "" );
174         OgnlRuntime.getPropertyDescriptor( java.lang.reflect.Field.class, "" );
175         OgnlRuntime.getPropertyDescriptor( java.util.LinkedList.class, "" );
176         OgnlRuntime.getPropertyDescriptor( java.util.List.class, "" );
177         OgnlRuntime.getPropertyDescriptor( java.util.Iterator.class, "" );
178         OgnlRuntime.getPropertyDescriptor( java.lang.ThreadLocal.class, "" );
179         OgnlRuntime.getPropertyDescriptor( java.net.URL.class, "" );
180         OgnlRuntime.getPropertyDescriptor( java.util.Vector.class, "" );
181     }
182 
183     public void testObjectIndexAccess()
184         throws OgnlException
185     {
186         SimpleNode expression = (SimpleNode) Ognl.parseExpression( "#ka.sunk[#root]" );
187 
188         context.put( "ka", new Test1() );
189         Ognl.getValue( expression, context, "aksdj" );
190     }
191 
192     public void testObjectIndexInSubclass()
193         throws OgnlException
194     {
195         SimpleNode expression = (SimpleNode) Ognl.parseExpression( "#ka.sunk[#root]" );
196 
197         context.put( "ka", new Test2() );
198         Ognl.getValue( expression, context, "aksdj" );
199     }
200 
201     public void testMultipleObjectIndexGetters()
202         throws OgnlException
203     {
204         SimpleNode expression = (SimpleNode) Ognl.parseExpression( "#ka.sunk[#root]" );
205 
206         context.put( "ka", new Test3() );
207         try
208         {
209             Ognl.getValue( expression, context, new Test3() );
210             fail();
211         }
212         catch ( OgnlException ex )
213         {
214             /* Should throw */
215         }
216     }
217 
218     public void testMultipleObjectIndexSetters()
219         throws OgnlException
220     {
221         SimpleNode expression = (SimpleNode) Ognl.parseExpression( "#ka.sunk[#root]" );
222 
223         context.put( "ka", new Test4() );
224         try
225         {
226             Ognl.getValue( expression, context, "aksdj" );
227             fail();
228         }
229         catch ( OgnlException ex )
230         {
231             /* Should throw */
232         }
233     }
234 
235     public void testMultipleObjectIndexMethodPairs()
236         throws OgnlException
237     {
238         SimpleNode expression = (SimpleNode) Ognl.parseExpression( "#ka.sunk[#root]" );
239 
240         context.put( "ka", new Test5() );
241         try
242         {
243             Ognl.getValue( expression, context, "aksdj" );
244             fail();
245         }
246         catch ( OgnlException ex )
247         {
248             /* Should throw */
249         }
250     }
251 
252     /*
253      * =================================================================== Overridden methods
254      * ===================================================================
255      */
256     @Override
257     @Before
258     protected void setUp()
259     {
260         context = (OgnlContext) Ognl.createDefaultContext( null );
261     }
262 }