1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
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
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
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
106 }
107
108 public void setSunk( Object index, String sunk )
109 {
110
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
125 }
126
127 public String getSunk( Object index )
128 {
129 return null;
130 }
131
132 public void setSunk( Object index, String sunk )
133 {
134
135 }
136 }
137
138
139
140
141
142 public static TestSuite suite()
143 {
144 return new TestSuite( ObjectIndexedTest.class );
145 }
146
147
148
149
150
151 public ObjectIndexedTest()
152 {
153 super();
154 }
155
156 public ObjectIndexedTest( String name )
157 {
158 super( name );
159 }
160
161
162
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
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
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
249 }
250 }
251
252
253
254
255
256 @Override
257 @Before
258 protected void setUp()
259 {
260 context = (OgnlContext) Ognl.createDefaultContext( null );
261 }
262 }