001: // Modified or written by Object Mentor, Inc. for inclusion with FitNesse.
002: // Copyright (c) 2002 Cunningham & Cunningham, Inc.
003: // Released under the terms of the GNU General Public License version 2 or later.
004: package fit;
005:
006: import junit.framework.TestCase;
007: import java.util.*;
008: import java.text.DateFormat;
009: import java.text.ParseException;
010: import java.text.SimpleDateFormat;
011:
012: public class TypeAdapterTest extends TestCase {
013: private TestFixture f = new TestFixture();
014: private TypeAdapter adapter;
015: private static final String dateFormat = "MMM dd yyyy";
016:
017: public void testTypeAdapter() throws Exception {
018:
019: adapter = TypeAdapter.on(f, f.getClass().getField("sampleInt"));
020: adapter.set(adapter.parse("123456"));
021: assertEquals(123456, f.sampleInt);
022: assertEquals("-234567", adapter.parse("-234567").toString());
023:
024: adapter = TypeAdapter.on(f, f.getClass().getField(
025: "sampleInteger"));
026: adapter.set(adapter.parse("54321"));
027: assertEquals("54321", f.sampleInteger.toString());
028:
029: adapter = TypeAdapter.on(f, f.getClass().getMethod("pi",
030: new Class[] {}));
031: assertEquals(3.14159,
032: ((Double) adapter.invoke()).doubleValue(), 0.00001);
033: assertEquals(new Double(3.141592653), adapter.invoke());
034:
035: adapter = TypeAdapter.on(f, f.getClass().getField("ch"));
036: adapter.set(adapter.parse("abc"));
037: assertEquals('a', f.ch);
038:
039: adapter = TypeAdapter.on(f, f.getClass().getField("name"));
040: adapter.set(adapter.parse("xyzzy"));
041: assertEquals("xyzzy", f.name);
042:
043: adapter = TypeAdapter.on(f, f.getClass()
044: .getField("sampleFloat"));
045: adapter.set(adapter.parse("6.02e23"));
046: assertEquals(6.02e23, f.sampleFloat, 1e17);
047:
048: adapter = TypeAdapter.on(f, f.getClass()
049: .getField("sampleArray"));
050: adapter.set(adapter.parse("1,2,3"));
051: assertEquals(1, f.sampleArray[0]);
052: assertEquals(2, f.sampleArray[1]);
053: assertEquals(3, f.sampleArray[2]);
054: assertEquals("1, 2, 3", adapter.toString(f.sampleArray));
055: assertTrue(adapter.equals(new int[] { 1, 2, 3 }, f.sampleArray));
056:
057: adapter = TypeAdapter
058: .on(f, f.getClass().getField("sampleDate"));
059: Date date = new GregorianCalendar(49 + 1900, 4, 26).getTime();
060: String format = DateFormat.getDateInstance(DateFormat.SHORT)
061: .format(date);
062: adapter.set(adapter.parse(format));
063: assertEquals(date, f.sampleDate);
064:
065: adapter = TypeAdapter
066: .on(f, f.getClass().getField("sampleByte"));
067: adapter.set(adapter.parse("123"));
068: assertEquals(123, f.sampleByte);
069:
070: adapter = TypeAdapter.on(f, f.getClass()
071: .getField("sampleShort"));
072: adapter.set(adapter.parse("12345"));
073: assertEquals(12345, f.sampleShort);
074: }
075:
076: static class TestFixture extends ColumnFixture {
077: public byte sampleByte;
078: public short sampleShort;
079: public int sampleInt;
080: public Integer sampleInteger;
081: public float sampleFloat;
082: public char ch;
083: public String name;
084: public int[] sampleArray;
085: public Date sampleDate;
086:
087: public double pi() {
088: return 3.141592653;
089: }
090: }
091:
092: public void testBooleanTypeAdapter() throws Exception {
093: assertBooleanTypeAdapterParses("true", true);
094: assertBooleanTypeAdapterParses("yes", true);
095: assertBooleanTypeAdapterParses("y", true);
096: assertBooleanTypeAdapterParses("+", true);
097: assertBooleanTypeAdapterParses("1", true);
098: assertBooleanTypeAdapterParses("True", true);
099: assertBooleanTypeAdapterParses("YES", true);
100: assertBooleanTypeAdapterParses("Y", true);
101:
102: assertBooleanTypeAdapterParses("N", false);
103: assertBooleanTypeAdapterParses("No", false);
104: assertBooleanTypeAdapterParses("false", false);
105: assertBooleanTypeAdapterParses("0", false);
106: assertBooleanTypeAdapterParses("-", false);
107: assertBooleanTypeAdapterParses("whatever", false);
108: }
109:
110: private void assertBooleanTypeAdapterParses(String booleanString,
111: boolean assertedValue) throws Exception {
112: TypeAdapter booleanAdapter = TypeAdapter
113: .adapterFor(Boolean.class);
114: Boolean result = (Boolean) booleanAdapter.parse(booleanString);
115: assertTrue(result.booleanValue() == assertedValue);
116: }
117:
118: public void testParseDelegateObjectMethod() throws Exception {
119: Date april26Of1949 = new GregorianCalendar(49 + 1900, 4, 26)
120: .getTime();
121: String format = new SimpleDateFormat(dateFormat)
122: .format(april26Of1949);
123:
124: TypeAdapter.registerParseDelegate(Date.class,
125: new SimpleDateFormat(dateFormat));
126:
127: adapter = TypeAdapter
128: .on(f, f.getClass().getField("sampleDate"));
129: adapter.set(adapter.parse(format));
130: assertEquals(april26Of1949, f.sampleDate);
131: }
132:
133: public void testParseDelegateClassMethod() throws Exception {
134: Date april26Of1949 = new GregorianCalendar(49 + 1900, 4, 26)
135: .getTime();
136: String format = new SimpleDateFormat(dateFormat)
137: .format(april26Of1949);
138:
139: TypeAdapter.registerParseDelegate(Date.class,
140: DateFormater.class);
141:
142: adapter = TypeAdapter
143: .on(f, f.getClass().getField("sampleDate"));
144: adapter.set(adapter.parse(format));
145: assertEquals(april26Of1949, f.sampleDate);
146: }
147:
148: public void testParsedelegateClassShouldHavePublicStaticNonVoidParseMethodWithStringParam()
149: throws Exception {
150: TypeAdapter.registerParseDelegate(Class.class,
151: PublicStaticParseMethod.class);
152: }
153:
154: public void testShouldThrowNoSuchMethodExceptionIfGivenParseDelgateClassDoesNotHavePublicParseMethod()
155: throws Exception {
156: try {
157: TypeAdapter.registerParseDelegate(Class.class,
158: ProtectedParseMethod.class);
159: } catch (RuntimeException e) {
160: assertEquals(
161: "Parse delegate class "
162: + ProtectedParseMethod.class.getName()
163: + " does not have a suitable static parse() method.",
164: e.getMessage());
165: }
166: }
167:
168: public void testShouldThrowNoSuchMethodExceptionIfGivenParseDelgateClassDoesNotHaveStaticParseMethod()
169: throws Exception {
170: try {
171: TypeAdapter.registerParseDelegate(Class.class,
172: PublicNonStaticParseMethod.class);
173: } catch (RuntimeException e) {
174: assertEquals(
175: "Parse delegate class "
176: + PublicNonStaticParseMethod.class
177: .getName()
178: + " does not have a suitable static parse() method.",
179: e.getMessage());
180: }
181: }
182:
183: public void testShouldThrowNoSuchMethodExceptionIfGivenParseDelgateClassHasParseMethodReturningVoid()
184: throws Exception {
185: try {
186: TypeAdapter.registerParseDelegate(Class.class,
187: PublicStaticVoidParseMethod.class);
188: } catch (RuntimeException e) {
189: assertEquals(
190: "Parse delegate class "
191: + PublicStaticVoidParseMethod.class
192: .getName()
193: + " does not have a suitable static parse() method.",
194: e.getMessage());
195: }
196: }
197:
198: public void testShouldThrowNoSuchMethodExceptionIfGivenParseDelgateClassDoesNotHaveParseMethodWithStringParam()
199: throws Exception {
200: try {
201: TypeAdapter.registerParseDelegate(Class.class,
202: PublicStaticParseMethodWithoutStringParam.class);
203: } catch (RuntimeException e) {
204: assertEquals(
205: "Parse delegate class "
206: + PublicStaticParseMethodWithoutStringParam.class
207: .getName()
208: + " does not have a suitable static parse() method.",
209: e.getMessage());
210: }
211: }
212:
213: public static class DateFormater {
214: public static Date parse(String date) throws ParseException {
215: return new SimpleDateFormat(dateFormat).parse(date);
216: }
217: }
218:
219: public static class ProtectedParseMethod {
220: protected static ProtectedParseMethod parse(String a) {
221: return null;
222: }
223: }
224:
225: public static class PublicNonStaticParseMethod {
226: public ProtectedParseMethod parse(String a) {
227: return null;
228: }
229: }
230:
231: public static class PublicStaticVoidParseMethod {
232: public static void parse(String a) {
233: }
234: }
235:
236: public static class PublicStaticParseMethod {
237: public static ProtectedParseMethod parse(String a) {
238: return new ProtectedParseMethod();
239: }
240: }
241:
242: public static class PublicStaticParseMethodWithoutStringParam {
243: public static PublicStaticParseMethodWithoutStringParam parse() {
244: return null;
245: }
246: }
247:
248: @Override
249: protected void tearDown() throws Exception {
250: TypeAdapter.clearDelegatesForNextTest();
251: }
252: }
|