001 /*
002 * $Id: ObjectIndexedTest.java 1104080 2011-05-17 09:22:09Z 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 junit.framework.TestCase;
023 import junit.framework.TestSuite;
024 import org.apache.commons.ognl.Ognl;
025 import org.apache.commons.ognl.OgnlContext;
026 import org.apache.commons.ognl.OgnlException;
027 import org.apache.commons.ognl.OgnlRuntime;
028 import org.apache.commons.ognl.SimpleNode;
029 import org.junit.Before;
030
031 public class ObjectIndexedTest
032 extends TestCase
033 {
034 protected OgnlContext context;
035
036 /*
037 * =================================================================== Public static classes
038 * ===================================================================
039 */
040 public static interface TestInterface
041 {
042 String getSunk( String index );
043
044 void setSunk( String index, String sunk );
045 }
046
047 public static class Test1
048 extends Object
049 implements TestInterface
050 {
051 public String getSunk( String index )
052 {
053 return "foo";
054 }
055
056 public void setSunk( String index, String sunk )
057 {
058 /* do nothing */
059 }
060 }
061
062 public static class Test2
063 extends Test1
064 {
065 public String getSunk( String index )
066 {
067 return "foo";
068 }
069
070 public void setSunk( String index, String sunk )
071 {
072 /* do nothing */
073 }
074 }
075
076 public static class Test3
077 extends Test1
078 {
079 public String getSunk( String index )
080 {
081 return "foo";
082 }
083
084 public void setSunk( String index, String sunk )
085 {
086 /* do nothing */
087 }
088
089 public String getSunk( Object index )
090 {
091 return null;
092 }
093 }
094
095 public static class Test4
096 extends Test1
097 {
098 public String getSunk( String index )
099 {
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 }