001: /*
002: * Copyright 2005-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005: * in compliance with the License. You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software distributed under the License
010: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011: * or implied. See the License for the specific language governing permissions and limitations under
012: * the License.
013: */
014:
015: package org.strecks.util;
016:
017: import java.beans.PropertyDescriptor;
018: import java.lang.reflect.InvocationTargetException;
019: import java.lang.reflect.Method;
020:
021: import org.apache.commons.beanutils.PropertyUtils;
022: import org.strecks.exceptions.ApplicationRuntimeException;
023: import org.strecks.injection.handler.impl.TestAction;
024: import org.testng.Assert;
025: import org.testng.annotations.BeforeMethod;
026: import org.testng.annotations.Test;
027:
028: /**
029: * @author Phil Zoio
030: */
031: public class TestPropertyValueSetter {
032:
033: private TestAction action;
034:
035: @BeforeMethod
036: public void setUp() {
037: action = new TestAction();
038: }
039:
040: @Test
041: public void testInvalidType1() throws Exception {
042: try {
043: TestAction action = new TestAction();
044: Method setterMethod = getSetter(action, "setIntegerInput",
045: Integer.class);
046: PropertyValueSetter.setPropertyValue(action, setterMethod,
047: Integer.class, "should be an int but is a String");
048: } catch (ApplicationRuntimeException e) {
049: Assert
050: .assertEquals(
051: e.getMessage(),
052: "Application error: could not set "
053: + "'should be an int but is a String' (type java.lang.String)"
054: + " using method setIntegerInput of "
055: + "class org.strecks.injection.handler.impl.TestAction: illegal argument supplied");
056: }
057: }
058:
059: @Test
060: public void testInvalidType2() {
061: try {
062: TestAction action = new TestAction();
063: PropertyValueSetter.setPropertyValue(action,
064: "integerInput", Integer.class,
065: "should be an int but is a String");
066: } catch (ApplicationRuntimeException e) {
067: Assert
068: .assertEquals(
069: e.getMessage(),
070: "Application error: could not set "
071: + "'should be an int but is a String' (type java.lang.String) "
072: + "for property integerInput of class "
073: + "org.strecks.injection.handler.impl.TestAction: illegal argument supplied");
074: }
075: }
076:
077: @Test
078: public void testPropertyValueSetter1() throws Exception {
079: TestAction action = new TestAction();
080: Method setterMethod = getSetter(action, "setIntegerInput",
081: Integer.class);
082: PropertyValueSetter.setPropertyValue(action, setterMethod,
083: Integer.class, new Integer(1));
084: assert action.getIntegerInput().equals(1);
085: }
086:
087: @Test
088: public void testPropertyValueSetter2() throws Exception {
089: TestAction action = new TestAction();
090: Method setterMethod = getSetter(action, "setIntegerInput",
091: Integer.class);
092: PropertyDescriptor descriptor = BeanUtils
093: .findPropertyForMethod(setterMethod);
094:
095: PropertyValueSetter.setPropertyValue(action, descriptor,
096: new Integer(1));
097: assert action.getIntegerInput().equals(1);
098: }
099:
100: @Test
101: public void testPropertyValueSetter3() throws Exception {
102: TestAction action = new TestAction();
103: PropertyValueSetter.setPropertyValue(action, "integerInput",
104: Integer.class, new Integer(1));
105: assert action.getIntegerInput().equals(1);
106: }
107:
108: @Test
109: public void testPrimitives1() throws NoSuchMethodException {
110: Method setterMethod = getSetter(action, "setByteInput",
111: Byte.TYPE);
112:
113: PropertyValueSetter.setPropertyValue(action, setterMethod,
114: Byte.TYPE, null);
115: assert action.getByteInput() == 0;
116:
117: PropertyValueSetter.setPropertyValue(action, setterMethod,
118: Byte.TYPE, (byte) 1);
119: assert action.getByteInput() == 1;
120:
121: setterMethod = getSetter(action, "setBooleanInput",
122: Boolean.TYPE);
123:
124: PropertyValueSetter.setPropertyValue(action, setterMethod,
125: Boolean.TYPE, null);
126: assert action.isBooleanInput() == false;
127:
128: PropertyValueSetter.setPropertyValue(action, setterMethod,
129: Boolean.TYPE, true);
130: assert action.isBooleanInput() == true;
131:
132: setterMethod = getSetter(action, "setShortInput", Short.TYPE);
133:
134: PropertyValueSetter.setPropertyValue(action, setterMethod,
135: Short.TYPE, null);
136: assert action.getShortInput() == 0;
137:
138: PropertyValueSetter.setPropertyValue(action, setterMethod,
139: Short.TYPE, (short) 2);
140: assert action.getShortInput() == 2;
141:
142: setterMethod = getSetter(action, "setIntInput", Integer.TYPE);
143:
144: PropertyValueSetter.setPropertyValue(action, setterMethod,
145: Integer.TYPE, null);
146: assert action.getIntInput() == 0;
147:
148: PropertyValueSetter.setPropertyValue(action, setterMethod,
149: Integer.TYPE, (int) 3);
150: assert action.getIntInput() == 3;
151:
152: setterMethod = getSetter(action, "setPrimitiveLongInput",
153: Long.TYPE);
154:
155: PropertyValueSetter.setPropertyValue(action, setterMethod,
156: Long.TYPE, null);
157: assert action.getPrimitiveLongInput() == 0;
158:
159: PropertyValueSetter.setPropertyValue(action, setterMethod,
160: Long.TYPE, 4L);
161: assert action.getPrimitiveLongInput() == 4;
162:
163: setterMethod = getSetter(action, "setFloatInput", Float.TYPE);
164:
165: PropertyValueSetter.setPropertyValue(action, setterMethod,
166: Float.TYPE, null);
167: assert action.getFloatInput() == 0;
168:
169: PropertyValueSetter.setPropertyValue(action, setterMethod,
170: Float.TYPE, 5.0F);
171: assert action.getFloatInput() == 5.0F;
172:
173: setterMethod = getSetter(action, "setDoubleInput", Double.TYPE);
174:
175: PropertyValueSetter.setPropertyValue(action, setterMethod,
176: Double.TYPE, null);
177: assert action.getDoubleInput() == 0;
178:
179: PropertyValueSetter.setPropertyValue(action, setterMethod,
180: Double.TYPE, 6.2);
181: assert action.getDoubleInput() == 6.2;
182: }
183:
184: @Test
185: public void testPrimitives2() throws NoSuchMethodException {
186: TestAction action = new TestAction();
187:
188: PropertyValueSetter.setPropertyValue(action, "byteInput",
189: Byte.TYPE, null);
190: assert action.getByteInput() == 0;
191:
192: PropertyValueSetter.setPropertyValue(action, "byteInput",
193: Byte.TYPE, (byte) 1);
194: assert action.getByteInput() == 1;
195:
196: PropertyValueSetter.setPropertyValue(action, "booleanInput",
197: Boolean.TYPE, null);
198: assert action.isBooleanInput() == false;
199:
200: PropertyValueSetter.setPropertyValue(action, "booleanInput",
201: Boolean.TYPE, true);
202: assert action.isBooleanInput() == true;
203:
204: PropertyValueSetter.setPropertyValue(action, "shortInput",
205: Short.TYPE, null);
206: assert action.getShortInput() == 0;
207:
208: PropertyValueSetter.setPropertyValue(action, "shortInput",
209: Short.TYPE, (short) 2);
210: assert action.getShortInput() == 2;
211:
212: PropertyValueSetter.setPropertyValue(action, "intInput",
213: Integer.TYPE, null);
214: assert action.getIntInput() == 0;
215:
216: PropertyValueSetter.setPropertyValue(action, "intInput",
217: Integer.TYPE, (int) 3);
218: assert action.getIntInput() == 3;
219:
220: PropertyValueSetter.setPropertyValue(action,
221: "primitiveLongInput", Long.TYPE, null);
222: assert action.getPrimitiveLongInput() == 0;
223:
224: PropertyValueSetter.setPropertyValue(action,
225: "primitiveLongInput", Long.TYPE, 4L);
226: assert action.getPrimitiveLongInput() == 4;
227:
228: PropertyValueSetter.setPropertyValue(action, "floatInput",
229: Float.TYPE, null);
230: assert action.getFloatInput() == 0;
231:
232: PropertyValueSetter.setPropertyValue(action, "floatInput",
233: Float.TYPE, 5.0F);
234: assert action.getFloatInput() == 5.0F;
235:
236: PropertyValueSetter.setPropertyValue(action, "doubleInput",
237: Double.TYPE, null);
238: assert action.getDoubleInput() == 0;
239:
240: PropertyValueSetter.setPropertyValue(action, "doubleInput",
241: Double.TYPE, 6.2);
242: assert action.getDoubleInput() == 6.2;
243: }
244:
245: @Test
246: public void testNullPrimitives() throws Exception {
247: testNullPrimitive("byteInput");
248: testNullPrimitive("booleanInput");
249: testNullPrimitive("shortInput");
250: testNullPrimitive("intInput");
251: testNullPrimitive("primitiveLongInput");
252: testNullPrimitive("floatInput");
253: testNullPrimitive("doubleInput");
254: }
255:
256: private Method getSetter(TestAction action, String name, Class clazz)
257: throws NoSuchMethodException {
258: Method setterMethod = action.getClass().getMethod(name,
259: new Class[] { clazz });
260: return setterMethod;
261: }
262:
263: private void testNullPrimitive(String string)
264: throws IllegalAccessException, InvocationTargetException,
265: NoSuchMethodException {
266: try {
267: PropertyUtils.setProperty(action, string, null);
268: Assert.fail(string);
269: } catch (IllegalArgumentException e) {
270: }
271: }
272:
273: }
|