001: package org.apache.velocity.test;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.util.HashMap;
023: import java.util.Map;
024:
025: import junit.framework.Test;
026: import junit.framework.TestSuite;
027:
028: import org.apache.velocity.runtime.RuntimeInstance;
029: import org.apache.velocity.test.misc.GetPutObject;
030: import org.apache.velocity.test.misc.UberspectorTestObject;
031: import org.apache.velocity.util.introspection.Uberspect;
032: import org.apache.velocity.util.introspection.VelPropertyGet;
033: import org.apache.velocity.util.introspection.VelPropertySet;
034:
035: public class UberspectorTestCase extends BaseTestCase {
036: private RuntimeInstance ri;
037:
038: public UberspectorTestCase(String name) {
039: super (name);
040: }
041:
042: public static Test suite() {
043: return new TestSuite(UberspectorTestCase.class);
044: }
045:
046: public void setUp() throws Exception {
047: ri = new RuntimeInstance();
048: ri.init();
049: }
050:
051: public void testNullObjects() throws Exception {
052: // How about some null objects... Gee, I'm mean. ;-)
053: Uberspect u = ri.getUberspect();
054:
055: VelPropertyGet getter = u.getPropertyGet(null, "foo", null);
056: assertNull(getter);
057:
058: VelPropertySet setter = u.getPropertySet(null, "foo",
059: Object.class, null);
060: assertNull(setter);
061: }
062:
063: public void testEmptyPropertyGetter() throws Exception {
064: Uberspect u = ri.getUberspect();
065: Map map = new HashMap();
066:
067: VelPropertyGet getter = u.getPropertyGet(map, "", null);
068:
069: // Don't screw up on empty properties. That should map to get("")
070: assertNotNull(getter);
071: assertEquals("Found wrong method", "get", getter
072: .getMethodName());
073: }
074:
075: public void testEmptyPropertySetter() throws Exception {
076: Uberspect u = ri.getUberspect();
077: Map map = new HashMap();
078:
079: VelPropertySet setter = u.getPropertySet(map, "", Object.class,
080: null);
081:
082: // Don't screw up on empty properties. That should map to put("", Object)
083: assertNotNull(setter);
084: assertEquals("Found wrong method", "put", setter
085: .getMethodName());
086: }
087:
088: public void testNullPropertyGetter() throws Exception {
089: Uberspect u = ri.getUberspect();
090: GetPutObject gpo = new GetPutObject();
091: Map map = new HashMap();
092:
093: VelPropertyGet getter = u.getPropertyGet(gpo, null, null);
094:
095: // Don't screw up on null properties. That should map to get() on the GPO.
096: assertNotNull(getter);
097: assertEquals("Found wrong method", "get", getter
098: .getMethodName());
099:
100: // And should be null on a Map which does not have a get()
101: getter = u.getPropertyGet(map, null, null);
102: assertNull(getter);
103:
104: }
105:
106: public void testNullPropertySetter() throws Exception {
107: Uberspect u = ri.getUberspect();
108: GetPutObject gpo = new GetPutObject();
109: Map map = new HashMap();
110:
111: // Don't screw up on null properties. That should map to put() on the GPO.
112: VelPropertySet setter = u.getPropertySet(gpo, null, "", null);
113: assertNotNull(setter);
114: assertEquals("Found wrong method", "put", setter
115: .getMethodName());
116:
117: // And should be null on a Map which does not have a put()
118: setter = u.getPropertySet(map, null, "", null);
119: assertNull(setter);
120: }
121:
122: public void testNullParameterType() throws Exception {
123: VelPropertySet setter;
124:
125: Uberspect u = ri.getUberspect();
126: UberspectorTestObject uto = new UberspectorTestObject();
127:
128: // setRegular()
129: setter = u.getPropertySet(uto, "Regular", null, null);
130: assertNotNull(setter);
131: assertEquals("Found wrong method", "setRegular", setter
132: .getMethodName());
133:
134: // setAmbigous() - String and StringBuffer available
135: setter = u.getPropertySet(uto, "Ambigous", null, null);
136: assertNull(setter);
137:
138: // setAmbigous() - same with Object?
139: setter = u.getPropertySet(uto, "Ambigous", new Object(), null);
140: assertNull(setter);
141: }
142:
143: public void testMultipleParameterTypes() throws Exception {
144: VelPropertySet setter;
145:
146: Uberspect u = ri.getUberspect();
147: UberspectorTestObject uto = new UberspectorTestObject();
148:
149: // setAmbigous() - String
150: setter = u.getPropertySet(uto, "Ambigous", "", null);
151: assertNotNull(setter);
152: assertEquals("Found wrong method", "setAmbigous", setter
153: .getMethodName());
154:
155: // setAmbigous() - StringBuffer
156: setter = u.getPropertySet(uto, "Ambigous", new StringBuffer(),
157: null);
158: assertNotNull(setter);
159: assertEquals("Found wrong method", "setAmbigous", setter
160: .getMethodName());
161: }
162:
163: public void testRegularGetters() throws Exception {
164: VelPropertyGet getter;
165:
166: Uberspect u = ri.getUberspect();
167: UberspectorTestObject uto = new UberspectorTestObject();
168:
169: // getRegular()
170: getter = u.getPropertyGet(uto, "Regular", null);
171: assertNotNull(getter);
172: assertEquals("Found wrong method", "getRegular", getter
173: .getMethodName());
174:
175: // Lowercase regular
176: getter = u.getPropertyGet(uto, "regular", null);
177: assertNotNull(getter);
178: assertEquals("Found wrong method", "getRegular", getter
179: .getMethodName());
180:
181: // lowercase: getpremium()
182: getter = u.getPropertyGet(uto, "premium", null);
183: assertNotNull(getter);
184: assertEquals("Found wrong method", "getpremium", getter
185: .getMethodName());
186:
187: // test uppercase: getpremium()
188: getter = u.getPropertyGet(uto, "Premium", null);
189: assertNotNull(getter);
190: assertEquals("Found wrong method", "getpremium", getter
191: .getMethodName());
192: }
193:
194: public void testBooleanGetters() throws Exception {
195: VelPropertyGet getter;
196:
197: Uberspect u = ri.getUberspect();
198: UberspectorTestObject uto = new UberspectorTestObject();
199:
200: // getRegular()
201: getter = u.getPropertyGet(uto, "RegularBool", null);
202: assertNotNull(getter);
203: assertEquals("Found wrong method", "isRegularBool", getter
204: .getMethodName());
205:
206: // Lowercase regular
207: getter = u.getPropertyGet(uto, "regularBool", null);
208: assertNotNull(getter);
209: assertEquals("Found wrong method", "isRegularBool", getter
210: .getMethodName());
211:
212: // lowercase: getpremiumBool()
213: getter = u.getPropertyGet(uto, "premiumBool", null);
214: assertNotNull(getter);
215: assertEquals("Found wrong method", "ispremiumBool", getter
216: .getMethodName());
217:
218: // test uppercase: ()
219: getter = u.getPropertyGet(uto, "PremiumBool", null);
220: assertNotNull(getter);
221: assertEquals("Found wrong method", "ispremiumBool", getter
222: .getMethodName());
223: }
224:
225: public void testRegularSetters() throws Exception {
226: VelPropertySet setter;
227:
228: Uberspect u = ri.getUberspect();
229: UberspectorTestObject uto = new UberspectorTestObject();
230:
231: // setRegular()
232: setter = u.getPropertySet(uto, "Regular", "", null);
233: assertNotNull(setter);
234: assertEquals("Found wrong method", "setRegular", setter
235: .getMethodName());
236:
237: // Lowercase regular
238: setter = u.getPropertySet(uto, "regular", "", null);
239: assertNotNull(setter);
240: assertEquals("Found wrong method", "setRegular", setter
241: .getMethodName());
242:
243: // lowercase: setpremium()
244: setter = u.getPropertySet(uto, "premium", "", null);
245: assertNotNull(setter);
246: assertEquals("Found wrong method", "setpremium", setter
247: .getMethodName());
248:
249: // test uppercase: getpremium()
250: setter = u.getPropertySet(uto, "Premium", "", null);
251: assertNotNull(setter);
252: assertEquals("Found wrong method", "setpremium", setter
253: .getMethodName());
254: }
255:
256: /*
257: *
258: * public void testMapGetSet()
259: * throws Exception
260: * {
261: * Uberspect u = ri.getUberspect();
262: * Map map = new HashMap();
263: *
264: * VelPropertyGet getter = u.getPropertyGet(map, "", null);
265: * VelPropertySet setter = u.getPropertySet(map, "", Object.class, null);
266: *
267: * assertNotNull("Got a null getter", getter);
268: * assertNotNull("Got a null setter", setter);
269: *
270: * assertEquals("Got wrong getter", "foo", getter.getMethodName());
271: * assertEquals("Got wrong setter", "bar", setter.getMethodName());
272: * }
273: */
274: }
|