001: /*
002: * Copyright (c) 2002-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.binding.tests;
032:
033: import java.lang.reflect.InvocationTargetException;
034: import java.lang.reflect.Method;
035: import java.text.NumberFormat;
036:
037: import junit.framework.TestCase;
038:
039: import com.jgoodies.binding.beans.BeanAdapter;
040: import com.jgoodies.binding.tests.beans.TestBean;
041: import com.jgoodies.binding.value.ConverterFactory;
042: import com.jgoodies.binding.value.ValueHolder;
043: import com.jgoodies.binding.value.ValueModel;
044:
045: /**
046: * Checks reflection access to a bunch of ValueModel implementations.
047: *
048: * @author Karsten Lentzsch
049: * @version $Revision: 1.7 $
050: */
051: public final class ReflectionTest extends TestCase {
052:
053: /**
054: * Checks reflection access to ValueModels returned by the BeanAdapter.
055: */
056: public void testAccessToBeanAdapterModels() {
057: BeanAdapter<TestBean> adapter = new BeanAdapter<TestBean>(
058: new TestBean());
059: ValueModel valueModel = adapter
060: .getValueModel("readWriteObjectProperty");
061: checkAccess(valueModel, null, "BeanAdapter's ValueModel");
062: }
063:
064: // Checking Access to ConverterFactory Converters *************************
065:
066: /**
067: * Checks reflection access to the converting ValueModel
068: * returned by <code>ConverterFactory.createBooleanNegator</code>.
069: */
070: public void testAccessToBooleanNegator() {
071: ValueModel booleanNegator = ConverterFactory
072: .createBooleanNegator(new ValueHolder(true));
073: checkAccess(booleanNegator, null,
074: "ConverterFactory#createBooleanNegator");
075: }
076:
077: /**
078: * Checks reflection access to the converting ValueModel
079: * returned by <code>ConverterFactory.createBooleanToStringConverter</code>.
080: */
081: public void testAccessToBooleanToStringConverter() {
082: ValueModel stringConverter = ConverterFactory
083: .createBooleanToStringConverter(new ValueHolder(true),
084: "true", "false");
085: checkAccess(stringConverter, "true",
086: "ConverterFactory#createBooleanToStringConverter");
087: }
088:
089: /**
090: * Checks reflection access to the converting ValueModel
091: * returned by <code>ConverterFactory.createDoubleToIntegerConverter</code>.
092: */
093: public void testAccessToDoubleToIntegerConverter() {
094: ValueModel doubleConverter = ConverterFactory
095: .createDoubleToIntegerConverter(new ValueHolder(1d));
096: checkAccess(doubleConverter, new Integer(2),
097: "ConverterFactory#createDoubleToIntegerConverter");
098: }
099:
100: /**
101: * Checks reflection access to the converting ValueModel
102: * returned by <code>ConverterFactory.createFloatToIntegerConverter</code>.
103: */
104: public void testAccessToFloatToIntegerConverter() {
105: ValueModel floatConverter = ConverterFactory
106: .createFloatToIntegerConverter(new ValueHolder(1f));
107: checkAccess(floatConverter, new Integer(2),
108: "ConverterFactory#createFloatToIntegerConverter");
109: }
110:
111: /**
112: * Checks reflection access to the converting ValueModel
113: * returned by <code>ConverterFactory.createLongToIntegerConverter</code>.
114: */
115: public void testAccessToLongToIntegerConverter() {
116: ValueModel longConverter = ConverterFactory
117: .createLongToIntegerConverter(new ValueHolder(new Long(
118: 42)));
119: checkAccess(longConverter, new Integer(2),
120: "ConverterFactory#createLongToIntegerConverter");
121: }
122:
123: /**
124: * Checks reflection access to the converting ValueModel
125: * returned by <code>ConverterFactory.createStringConverter</code>.
126: */
127: public void testAccessToStringConverter() {
128: ValueModel stringConverter = ConverterFactory
129: .createStringConverter(new ValueHolder(1967),
130: NumberFormat.getIntegerInstance());
131: checkAccess(stringConverter, "512",
132: "ConverterFactory#createStringConverter");
133: }
134:
135: // Helper Code ************************************************************
136:
137: /**
138: * Checks read-write-access to the given ValueModel using reflection.
139: *
140: * @param valueModel the ValueModel that implements the setter
141: * @param newValue the test value for the setter
142: * @param description used in failure message to describe the model
143: */
144: private static void checkAccess(ValueModel valueModel,
145: Object newValue, String description) {
146: checkReadAccess(valueModel, description + "#getValue()");
147: checkWriteAccess(valueModel, newValue, description
148: + "#setValue(Object)");
149: }
150:
151: /**
152: * Checks read-access to the given ValueModel using reflection.
153: *
154: * @param valueModel the ValueModel that implements the getter
155: * @param description used in failure message to describe the model
156: */
157: private static void checkReadAccess(ValueModel valueModel,
158: String description) {
159: try {
160: Method getter = valueModel.getClass().getMethod("getValue",
161: new Class[] {});
162: getter.invoke(valueModel, new Object[] {});
163: } catch (NoSuchMethodException e) {
164: fail("Inaccessible " + description);
165: } catch (IllegalArgumentException e) {
166: fail("IllegalArgumentException in " + description);
167: } catch (IllegalAccessException e) {
168: fail("IllegalAccessException in " + description);
169: } catch (InvocationTargetException e) {
170: fail("InvocationTargetException in " + description);
171: }
172: }
173:
174: /**
175: * Checks write-access to the given ValueModel using reflection.
176: *
177: * @param valueModel the ValueModel that implements the setter
178: * @param newValue the value to be set
179: * @param description used in failure message to describe the model
180: */
181: private static void checkWriteAccess(ValueModel valueModel,
182: Object newValue, String description) {
183: try {
184: Method setter = valueModel.getClass().getMethod("setValue",
185: new Class[] { Object.class });
186: setter.invoke(valueModel, new Object[] { newValue });
187: } catch (NoSuchMethodException e) {
188: fail("Inaccessible " + description);
189: } catch (IllegalArgumentException e) {
190: fail("IllegalArgumentException in " + description);
191: } catch (IllegalAccessException e) {
192: fail("IllegalAccessException in " + description);
193: } catch (InvocationTargetException e) {
194: fail("InvocationTargetException in " + description);
195: }
196: }
197:
198: }
|