001: package org.ognl.test;
002:
003: import junit.framework.TestCase;
004: import junit.framework.TestSuite;
005: import ognl.Ognl;
006: import ognl.OgnlContext;
007: import ognl.OgnlException;
008: import ognl.OgnlRuntime;
009: import ognl.SimpleNode;
010:
011: public class ObjectIndexedTest extends TestCase {
012: protected OgnlContext context;
013:
014: /*===================================================================
015: Public static classes
016: ===================================================================*/
017: public static interface TestInterface {
018: String getSunk(String index);
019:
020: void setSunk(String index, String sunk);
021: }
022:
023: public static class Test1 extends Object implements TestInterface {
024: public String getSunk(String index) {
025: return "foo";
026: }
027:
028: public void setSunk(String index, String sunk) {
029: /* do nothing */
030: }
031: }
032:
033: public static class Test2 extends Test1 {
034: public String getSunk(String index) {
035: return "foo";
036: }
037:
038: public void setSunk(String index, String sunk) {
039: /* do nothing */
040: }
041: }
042:
043: public static class Test3 extends Test1 {
044: public String getSunk(String index) {
045: return "foo";
046: }
047:
048: public void setSunk(String index, String sunk) {
049: /* do nothing */
050: }
051:
052: public String getSunk(Object index) {
053: return null;
054: }
055: }
056:
057: public static class Test4 extends Test1 {
058: public String getSunk(String index) {
059: return "foo";
060: }
061:
062: public void setSunk(String index, String sunk) {
063: /* do nothing */
064: }
065:
066: public void setSunk(Object index, String sunk) {
067: /* do nothing */
068: }
069: }
070:
071: public static class Test5 extends Test1 {
072: public String getSunk(String index) {
073: return "foo";
074: }
075:
076: public void setSunk(String index, String sunk) {
077: /* do nothing */
078: }
079:
080: public String getSunk(Object index) {
081: return null;
082: }
083:
084: public void setSunk(Object index, String sunk) {
085: /* do nothing */
086: }
087: }
088:
089: /*===================================================================
090: Public static methods
091: ===================================================================*/
092: public static TestSuite suite() {
093: return new TestSuite(ObjectIndexedTest.class);
094: }
095:
096: /*===================================================================
097: Constructors
098: ===================================================================*/
099: public ObjectIndexedTest() {
100: super ();
101: }
102:
103: public ObjectIndexedTest(String name) {
104: super (name);
105: }
106:
107: /*===================================================================
108: Public methods
109: ===================================================================*/
110: public void testPropertyDescriptorReflection() throws Exception {
111: OgnlRuntime.getPropertyDescriptor(java.util.AbstractList.class,
112: "");
113: OgnlRuntime.getPropertyDescriptor(
114: java.util.AbstractSequentialList.class, "");
115: OgnlRuntime.getPropertyDescriptor(
116: java.lang.reflect.Array.class, "");
117: OgnlRuntime
118: .getPropertyDescriptor(java.util.ArrayList.class, "");
119: OgnlRuntime.getPropertyDescriptor(java.util.BitSet.class, "");
120: OgnlRuntime.getPropertyDescriptor(java.util.Calendar.class, "");
121: OgnlRuntime.getPropertyDescriptor(
122: java.lang.reflect.Field.class, "");
123: OgnlRuntime.getPropertyDescriptor(java.util.LinkedList.class,
124: "");
125: OgnlRuntime.getPropertyDescriptor(java.util.List.class, "");
126: OgnlRuntime.getPropertyDescriptor(java.util.Iterator.class, "");
127: OgnlRuntime.getPropertyDescriptor(java.lang.ThreadLocal.class,
128: "");
129: OgnlRuntime.getPropertyDescriptor(java.net.URL.class, "");
130: OgnlRuntime.getPropertyDescriptor(java.util.Vector.class, "");
131: }
132:
133: public void testObjectIndexAccess() throws OgnlException {
134: SimpleNode expression = (SimpleNode) Ognl
135: .parseExpression("#ka.sunk[#root]");
136:
137: context.put("ka", new Test1());
138: Ognl.getValue(expression, context, "aksdj");
139: }
140:
141: public void testObjectIndexInSubclass() throws OgnlException {
142: SimpleNode expression = (SimpleNode) Ognl
143: .parseExpression("#ka.sunk[#root]");
144:
145: context.put("ka", new Test2());
146: Ognl.getValue(expression, context, "aksdj");
147: }
148:
149: public void testMultipleObjectIndexGetters() throws OgnlException {
150: SimpleNode expression = (SimpleNode) Ognl
151: .parseExpression("#ka.sunk[#root]");
152:
153: context.put("ka", new Test3());
154: try {
155: Ognl.getValue(expression, context, new Test3());
156: fail();
157: } catch (OgnlException ex) {
158: /* Should throw */
159: }
160: }
161:
162: public void testMultipleObjectIndexSetters() throws OgnlException {
163: SimpleNode expression = (SimpleNode) Ognl
164: .parseExpression("#ka.sunk[#root]");
165:
166: context.put("ka", new Test4());
167: try {
168: Ognl.getValue(expression, context, "aksdj");
169: fail();
170: } catch (OgnlException ex) {
171: /* Should throw */
172: }
173: }
174:
175: public void testMultipleObjectIndexMethodPairs()
176: throws OgnlException {
177: SimpleNode expression = (SimpleNode) Ognl
178: .parseExpression("#ka.sunk[#root]");
179:
180: context.put("ka", new Test5());
181: try {
182: Ognl.getValue(expression, context, "aksdj");
183: fail();
184: } catch (OgnlException ex) {
185: /* Should throw */
186: }
187: }
188:
189: /*===================================================================
190: Overridden methods
191: ===================================================================*/
192: protected void setUp() {
193: context = (OgnlContext) Ognl.createDefaultContext(null);
194: }
195: }
|