001: /*
002: * Created on Dec 7, 2003
003: *
004: /*
005: Copyright (c) 2003 eInnovation Inc. All rights reserved
006:
007: This library is free software; you can redistribute it and/or modify it under the terms
008: of the GNU Lesser General Public License as published by the Free Software Foundation;
009: either version 2.1 of the License, or (at your option) any later version.
010:
011: This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
012: without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
013: See the GNU Lesser General Public License for more details.
014: */
015: package com.openedit.modules;
016:
017: import java.util.Date;
018: import java.util.List;
019:
020: import junit.framework.TestCase;
021:
022: import com.openedit.TestWebPageRequest;
023: import com.openedit.WebPageRequest;
024: import com.openedit.modules.reflect.Reflector;
025:
026: /**
027: * @author Matt Avery, mavery@einnovation.com
028: */
029: public class ReflectorTest extends TestCase {
030: public static final String KEY = "test-object.testMethod";
031: protected Reflector fieldReflector;
032: protected WebPageRequest fieldContext;
033:
034: public ReflectorTest(String arg0) {
035: super (arg0);
036: }
037:
038: // public static void main(String[] args)
039: // {
040: // junit.swingui.TestRunner.run(ReflectorTest.class);
041: // }
042:
043: protected void setUp() throws Exception {
044: WebPageRequest context = getWebPageContext();
045: context.setRequestParameter(KEY, "Hello");
046: }
047:
048: public void testFindMethod() {
049: class TestObject {
050: public void testMethod(WebPageRequest inContext) {
051: }
052: }
053: Object sessionObject = new TestObject();
054: int dotIndex = KEY.indexOf('.');
055: String methodName = KEY.substring(dotIndex + 1, KEY.length());
056: List methods = getReflector().findMethods(sessionObject,
057: methodName);
058: assertEquals(1, methods.size());
059: class TestObject2 {
060: public void notTheMethod(WebPageRequest inContext) {
061: }
062: }
063: sessionObject = new TestObject2();
064: methods = getReflector().findMethods(sessionObject, methodName);
065: assertEquals(0, methods.size());
066:
067: class TestObject3 {
068: protected boolean fieldDateStringBoolean;
069: protected boolean fieldDateBoolean;
070:
071: public void setDate(String inDateString) {
072: fieldDateStringBoolean = true;
073: }
074:
075: public void setDate(Date inDate) {
076: fieldDateBoolean = true;
077: }
078:
079: public boolean dateStringSet() {
080: return fieldDateStringBoolean;
081: }
082:
083: public boolean dateSet() {
084: return fieldDateBoolean;
085: }
086: }
087: Object testObject = new TestObject3();
088: methods = getReflector().findMethods(testObject, "setDate");
089: assertEquals(2, methods.size());
090: }
091:
092: public void testReflectOnRequestParameters() throws Exception {
093: TestObject testObject = new TestObject();
094: getWebPageContext().setRequestParameter("setString", "Hello");
095: getWebPageContext().setRequestParameter("setSomething",
096: "goodbye");
097: getWebPageContext().setRequestParameter("setStringArray",
098: "Hello");
099: getWebPageContext().setRequestParameter("setInt", "5");
100: // getWebPageContext().setRequestParameter("setInvalidSetter", "blah");
101: getReflector().reflectOnRequestParameters(getWebPageContext(),
102: testObject);
103: assertEquals("Hello", testObject.getString());
104: assertEquals("Hello", testObject.getStringArray()[0]);
105: assertEquals(5, testObject.getInt());
106: assertTrue(!testObject.invalidSetterCalled);
107:
108: getWebPageContext().setRequestParameter("setStringArray",
109: new String[] { "Yahoo", "Goodbye" });
110: getReflector().reflectOnRequestParameters(getWebPageContext(),
111: testObject);
112: assertEquals("Yahoo", testObject.getStringArray()[0]);
113: assertEquals("Goodbye", testObject.getStringArray()[1]);
114: }
115:
116: public Reflector getReflector() {
117: if (fieldReflector == null) {
118: fieldReflector = new Reflector();
119: }
120: return fieldReflector;
121: }
122:
123: private WebPageRequest getWebPageContext() {
124: if (fieldContext == null) {
125: fieldContext = new TestWebPageRequest();
126: }
127: return fieldContext;
128: }
129:
130: }
|