001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.util.lang;
018:
019: import java.lang.reflect.Field;
020: import java.lang.reflect.Method;
021: import java.util.ArrayList;
022: import java.util.HashMap;
023: import java.util.List;
024: import java.util.Locale;
025: import java.util.Map;
026: import java.util.Vector;
027:
028: import junit.framework.TestCase;
029:
030: import org.apache.wicket.WicketRuntimeException;
031: import org.apache.wicket.protocol.http.HttpSessionStore;
032: import org.apache.wicket.protocol.http.MockWebApplication;
033: import org.apache.wicket.protocol.http.WebApplication;
034: import org.apache.wicket.session.ISessionStore;
035: import org.apache.wicket.util.convert.ConversionException;
036: import org.apache.wicket.util.convert.ConverterLocator;
037:
038: /**
039: * @author jcompagner
040: *
041: */
042: public class PropertyResolverTest extends TestCase {
043: private static final PropertyResolverConverter CONVERTER = new PropertyResolverConverter(
044: new ConverterLocator(), Locale.US);
045:
046: private Person person;
047: private MockWebApplication app;
048:
049: /**
050: * @see junit.framework.TestCase#setUp()
051: */
052: protected void setUp() throws Exception {
053: person = new Person();
054: app = new MockWebApplication(new WebApplication() {
055:
056: public Class getHomePage() {
057: return null;
058: }
059:
060: protected void outputDevelopmentModeWarning() {
061: // Do nothing.
062: }
063:
064: protected ISessionStore newSessionStore() {
065: // Don't use a filestore, or we spawn lots of threads, which makes things slow.
066: return new HttpSessionStore(this );
067: }
068:
069: }, "/foo");
070: }
071:
072: protected void tearDown() throws Exception {
073: super .tearDown();
074: PropertyResolver.destroy(app.getApplication());
075: }
076:
077: /**
078: * @throws Exception
079: */
080: public void testSimpleExpression() throws Exception {
081: String name = (String) PropertyResolver
082: .getValue("name", person);
083: assertNull(name);
084:
085: PropertyResolver.setValue("name", person, "wicket", CONVERTER);
086: name = (String) PropertyResolver.getValue("name", person);
087: assertEquals(name, "wicket");
088: }
089:
090: /**
091: * @throws Exception
092: */
093: public void testPrimitiveValue() throws Exception {
094: Integer integer = (Integer) PropertyResolver.getValue("age",
095: person);
096: assertTrue(integer.intValue() == 0);
097:
098: PropertyResolver.setValue("age", person, new Integer(10),
099: CONVERTER);
100: integer = (Integer) PropertyResolver.getValue("age", person);
101: assertTrue(integer.intValue() == 10);
102:
103: try {
104: PropertyResolver.setValue("age", person, null, CONVERTER);
105: fail("primitive type can't be set to null");
106: } catch (ConversionException ce) {
107: // ignore should happen
108: }
109: }
110:
111: /**
112: * @throws Exception
113: */
114: public void testPathExpression() throws Exception {
115: person.setAddress(new Address());
116: PropertyResolver.setValue("address.street", person,
117: "wicket-street", CONVERTER);
118: String street = (String) PropertyResolver.getValue(
119: "address.street", person);
120: assertEquals(street, "wicket-street");
121:
122: }
123:
124: /**
125: * @throws Exception
126: */
127: public void testNull() throws Exception {
128: String street = (String) PropertyResolver.getValue(
129: "address.street", person);
130: assertNull(street);
131: }
132:
133: /**
134: * @throws Exception
135: */
136: public void testNullCreation() throws Exception {
137: PropertyResolver.setValue("address.street", person,
138: "wicket-street", CONVERTER);
139: String street = (String) PropertyResolver.getValue(
140: "address.street", person);
141: assertEquals(street, "wicket-street");
142:
143: try {
144: PropertyResolver.setValue("country.name", person, "US",
145: CONVERTER);
146: throw new Exception(
147: "name can't be set on a country that doesn't have default constructor");
148: } catch (WicketRuntimeException ex) {
149: }
150: }
151:
152: /**
153: * @throws Exception
154: */
155: public void testGetterOnly() throws Exception {
156: PropertyResolver.setValue("country", person, new Country("US"),
157: CONVERTER);
158: PropertyResolver.getValue("country.name", person);
159:
160: try {
161: PropertyResolver.setValue("country.name", person, "NL",
162: CONVERTER);
163: } catch (WicketRuntimeException ex) {
164: }
165: }
166:
167: /**
168: * @throws Exception
169: */
170: public void testPathExpressionWithConversion() throws Exception {
171: person.setAddress(new Address());
172: PropertyResolver.setValue("address.number", person, "10",
173: CONVERTER);
174: Integer number = (Integer) PropertyResolver.getValue(
175: "address.number", person);
176: assertEquals(number, new Integer(10));
177:
178: try {
179: PropertyResolver.setValue("address.number", person, "10a",
180: CONVERTER);
181: throw new Exception("Conversion error should be thrown");
182: } catch (ConversionException ex) {
183: }
184:
185: }
186:
187: /**
188: * @throws Exception
189: */
190: public void testMapLookup() throws Exception {
191: Address address = new Address();
192: PropertyResolver.setValue("addressMap", person, new HashMap(),
193: CONVERTER);
194: PropertyResolver.setValue("addressMap.address", person,
195: address, CONVERTER);
196: PropertyResolver.setValue("addressMap.address.street", person,
197: "wicket-street", CONVERTER);
198: String street = (String) PropertyResolver.getValue(
199: "addressMap.address.street", person);
200: assertEquals(street, "wicket-street");
201: }
202:
203: /**
204: * @throws Exception
205: */
206: public void testListLookup() throws Exception {
207: PropertyResolver.setValue("addressList", person,
208: new ArrayList(), CONVERTER);
209: PropertyResolver.setValue("addressList.0", person,
210: new Address(), CONVERTER);
211: PropertyResolver.setValue("addressList.10", person,
212: new Address(), CONVERTER);
213: PropertyResolver.setValue("addressList.1", person,
214: new Address(), CONVERTER);
215: PropertyResolver.setValue("addressList.1.street", person,
216: "wicket-street", CONVERTER);
217:
218: String street = (String) PropertyResolver.getValue(
219: "addressList.0.street", person);
220: assertNull(street);
221: street = (String) PropertyResolver.getValue(
222: "addressList.1.street", person);
223: assertEquals(street, "wicket-street");
224: }
225:
226: /**
227: * @throws Exception
228: */
229: public void testArrayLookup() throws Exception {
230: PropertyResolver.setValue("addressArray", person,
231: new Address[] { new Address(), null }, CONVERTER);
232: PropertyResolver.setValue("addressArray.0.street", person,
233: "wicket-street", CONVERTER);
234: String street = (String) PropertyResolver.getValue(
235: "addressArray.0.street", person);
236: assertEquals(street, "wicket-street");
237:
238: PropertyResolver.setValue("addressArray.1.street", person,
239: "wicket-street", CONVERTER);
240: street = (String) PropertyResolver.getValue(
241: "addressArray.1.street", person);
242: assertEquals(street, "wicket-street");
243: }
244:
245: /**
246: * @throws Exception
247: */
248: public void testArrayLookupByBrackets() throws Exception {
249: PropertyResolver.setValue("addressArray", person,
250: new Address[] { new Address(), null }, CONVERTER);
251: PropertyResolver.setValue("addressArray[0].street", person,
252: "wicket-street", CONVERTER);
253: String street = (String) PropertyResolver.getValue(
254: "addressArray[0].street", person);
255: assertEquals(street, "wicket-street");
256:
257: PropertyResolver.setValue("addressArray[1].street", person,
258: "wicket-street", CONVERTER);
259: street = (String) PropertyResolver.getValue(
260: "addressArray[1].street", person);
261: assertEquals(street, "wicket-street");
262: }
263:
264: /**
265: * @throws Exception
266: */
267: public void testPropertyByIndexLookup() throws Exception {
268: PropertyResolver.setValue("addressAt.0", person, new Address(),
269: CONVERTER);
270: PropertyResolver.setValue("addressAt.0.street", person,
271: "wicket-street", CONVERTER);
272: String street = (String) PropertyResolver.getValue(
273: "addressAt.0.street", person);
274: assertEquals(street, "wicket-street");
275: }
276:
277: /**
278: * @throws Exception
279: */
280: public void testListSizeLookup() throws Exception {
281: List/*<Address>*/addresses = new ArrayList/*<Address>*/();
282: addresses.add(new Address());
283: addresses.add(new Address());
284: person.setAddressList(addresses);
285: Object size = PropertyResolver.getValue("addressList.size",
286: person);
287: assertEquals(size, new Integer(2));
288: size = PropertyResolver.getValue("addressList.size()", person);
289: assertEquals(size, new Integer(2));
290: }
291:
292: /**
293: * @throws Exception
294: */
295: public void testMapSizeLookup() throws Exception {
296: Map/*<String, Address>*/addresses = new HashMap/*<String, Address>*/();
297: Address address = new Address();
298: addresses.put("size", address);
299: addresses.put("test", new Address());
300: person.setAddressMap(addresses);
301: Object addressFromMap = PropertyResolver.getValue(
302: "addressMap.size", person);
303: assertEquals(addressFromMap, address);
304: Object size = PropertyResolver.getValue("addressMap.size()",
305: person);
306: assertEquals(size, new Integer(2));
307: }
308:
309: /**
310: * @throws Exception
311: */
312: public void testArraytSizeLookup() throws Exception {
313: person.setAddressArray(new Address[] { new Address(),
314: new Address() });
315: Object size = PropertyResolver.getValue("addressArray.length",
316: person);
317: assertEquals(size, new Integer(2));
318: size = PropertyResolver.getValue("addressArray.size", person);
319: assertEquals(size, new Integer(2));
320: }
321:
322: /**
323: * @throws Exception
324: */
325: public void testMethodLookup() throws Exception {
326: Address[] addresses = new Address[] { new Address(),
327: new Address() };
328: person.setAddressArray(addresses);
329: Object value = PropertyResolver.getValue("getAddressArray()",
330: person);
331: assertEquals(value, addresses);
332: }
333:
334: /**
335: * @throws Exception
336: */
337: public void testField() throws Exception {
338: Address address = new Address();
339: PropertyResolver.setValue("address2", person, address,
340: CONVERTER);
341: Address address2 = (Address) PropertyResolver.getValue(
342: "address2", person);
343: assertEquals(address, address2);
344:
345: try {
346: PropertyResolver.setValue("address3", person, address,
347: CONVERTER);
348: throw new RuntimeException("Shoudln't come here");
349: } catch (RuntimeException ex) {
350:
351: }
352: }
353:
354: /**
355: * @throws Exception
356: */
357: public void testPrivateField() throws Exception {
358: Address address = new Address();
359: PropertyResolver.setValue("privateAddress", person, address,
360: CONVERTER);
361: Address address2 = (Address) PropertyResolver.getValue(
362: "privateAddress", person);
363: assertEquals(address, address2);
364: }
365:
366: /**
367: * @throws Exception
368: */
369: public void testPrivateFieldOfSuperClass() throws Exception {
370: Person2 person2 = new Person2();
371: Address address = new Address();
372: PropertyResolver.setValue("privateAddress", person2, address,
373: CONVERTER);
374: Address address2 = (Address) PropertyResolver.getValue(
375: "privateAddress", person2);
376: assertEquals(address, address2);
377: }
378:
379: /**
380: *
381: */
382: public void testGetTargetClass() {
383: Address address = new Address();
384:
385: Class clazz = PropertyResolver.getPropertyClass("number",
386: address);
387: assertEquals(int.class, clazz);
388:
389: Person person = new Person();
390: person.setAddress(new Address());
391:
392: clazz = PropertyResolver.getPropertyClass("address.number",
393: person);
394: assertEquals(int.class, clazz);
395:
396: person.setAddressArray(new Address[] { new Address(),
397: new Address() });
398: clazz = PropertyResolver.getPropertyClass("addressArray[0]",
399: person);
400: assertEquals(Address.class, clazz);
401:
402: clazz = PropertyResolver.getPropertyClass(
403: "addressArray[0].number", person);
404: assertEquals(int.class, clazz);
405: }
406:
407: /**
408: *
409: */
410: public void testGetTargetField() {
411: Address address = new Address();
412:
413: Field field = PropertyResolver.getPropertyField("number",
414: address);
415: assertEquals(field.getName(), "number");
416: assertEquals(field.getType(), int.class);
417:
418: Person person = new Person();
419: person.setAddress(new Address());
420:
421: field = PropertyResolver.getPropertyField("address.number",
422: person);
423: assertEquals(field.getName(), "number");
424: assertEquals(field.getType(), int.class);
425:
426: person.setAddressArray(new Address[] { new Address(),
427: new Address() });
428: field = PropertyResolver.getPropertyField(
429: "addressArray[0].number", person);
430: assertEquals(field.getName(), "number");
431: assertEquals(field.getType(), int.class);
432: }
433:
434: /**
435: *
436: */
437: public void testGetTargetGetter() {
438: Address address = new Address();
439:
440: Method method = PropertyResolver.getPropertyGetter("number",
441: address);
442: assertEquals(method.getName(), "getNumber");
443: assertEquals(method.getReturnType(), int.class);
444:
445: Person person = new Person();
446: person.setAddress(new Address());
447:
448: method = PropertyResolver.getPropertyGetter("address.number",
449: person);
450: assertEquals(method.getName(), "getNumber");
451: assertEquals(method.getReturnType(), int.class);
452:
453: person.setAddressArray(new Address[] { new Address(),
454: new Address() });
455: method = PropertyResolver.getPropertyGetter(
456: "addressArray[0].number", person);
457: assertEquals(method.getName(), "getNumber");
458: assertEquals(method.getReturnType(), int.class);
459: }
460:
461: /**
462: *
463: */
464: public void testGetTargetSetter() {
465: Address address = new Address();
466:
467: // FIXME: We shouldn't need to run this first in order for the getName() stuff to work.
468: // See WICKET-668 for details.
469: //PropertyResolver.setValue("number", address, new Integer(1), CONVERTER);
470:
471: Method method = PropertyResolver.getPropertySetter("number",
472: address);
473: assertEquals(method.getName(), "setNumber");
474:
475: Person person = new Person();
476: person.setAddress(new Address());
477:
478: method = PropertyResolver.getPropertySetter("address.number",
479: person);
480: assertEquals(method.getName(), "setNumber");
481:
482: person.setAddressArray(new Address[] { new Address(),
483: new Address() });
484: method = PropertyResolver.getPropertySetter(
485: "addressArray[0].number", person);
486: assertEquals(method.getName(), "setNumber");
487: }
488:
489: /**
490: * Used for models in testing.
491: */
492: private static class InnerVectorPOJO extends Vector {
493: private static final long serialVersionUID = 1L;
494:
495: /**
496: *
497: */
498: public String testValue = "vector";
499: }
500:
501: /**
502: * Tests the PropertyModel with vector.
503: */
504: public void testPropertyModel() {
505: String value = (String) PropertyResolver.getValue("testValue",
506: new InnerVectorPOJO());
507: assertEquals("vector", value);
508: }
509: }
|