001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003: package fit;
004:
005: import junit.framework.TestCase;
006: import java.util.*;
007: import java.text.DateFormat;
008:
009: public class TypeAdapterTest extends TestCase {
010: public void testTypeAdapter() throws Exception {
011: TestFixture f = new TestFixture();
012: TypeAdapter a = TypeAdapter.on(f, f.getClass().getField(
013: "sampleInt"));
014: a.set(a.parse("123456"));
015: assertEquals(123456, f.sampleInt);
016: assertEquals("-234567", a.parse("-234567").toString());
017: a = TypeAdapter.on(f, f.getClass().getField("sampleInteger"));
018: a.set(a.parse("54321"));
019: assertEquals("54321", f.sampleInteger.toString());
020: a = TypeAdapter.on(f, f.getClass().getMethod("pi",
021: new Class[] {}));
022: assertEquals(3.14159, ((Double) a.invoke()).doubleValue(),
023: 0.00001);
024: assertEquals(new Double(3.141592653), a.invoke());
025: a = TypeAdapter.on(f, f.getClass().getField("ch"));
026: a.set(a.parse("abc"));
027: assertEquals('a', f.ch);
028: a = TypeAdapter.on(f, f.getClass().getField("name"));
029: a.set(a.parse("xyzzy"));
030: assertEquals("xyzzy", f.name);
031: a = TypeAdapter.on(f, f.getClass().getField("sampleFloat"));
032: a.set(a.parse("6.02e23"));
033: assertEquals(6.02e23, f.sampleFloat, 1e17);
034: a = TypeAdapter.on(f, f.getClass().getField("sampleArray"));
035: a.set(a.parse("1,2,3"));
036: assertEquals(1, f.sampleArray[0]);
037: assertEquals(2, f.sampleArray[1]);
038: assertEquals(3, f.sampleArray[2]);
039: assertEquals("1, 2, 3", a.toString(f.sampleArray));
040: assertTrue(a.equals(new int[] { 1, 2, 3 }, f.sampleArray));
041: a = TypeAdapter.on(f, f.getClass().getField("sampleDate"));
042: Date date = new GregorianCalendar(49 + 1900, 4, 26).getTime();
043: a.set(a.parse(DateFormat.getDateInstance(DateFormat.SHORT)
044: .format(date)));
045: assertEquals(date, f.sampleDate);
046: a = TypeAdapter.on(f, f.getClass().getField("sampleByte"));
047: a.set(a.parse("123"));
048: assertEquals(123, f.sampleByte);
049: a = TypeAdapter.on(f, f.getClass().getField("sampleShort"));
050: a.set(a.parse("12345"));
051: assertEquals(12345, f.sampleShort);
052: }
053:
054: static class TestFixture extends ColumnFixture {
055: public byte sampleByte;
056:
057: public short sampleShort;
058:
059: public int sampleInt;
060:
061: public Integer sampleInteger;
062:
063: public float sampleFloat;
064:
065: public char ch;
066:
067: public String name;
068:
069: public int[] sampleArray;
070:
071: public Date sampleDate;
072:
073: public double pi() {
074: return 3.141592653;
075: }
076: }
077:
078: public void testBooleanTypeAdapter() throws Exception {
079: assertBooleanTypeAdapterParses("true", true);
080: assertBooleanTypeAdapterParses("yes", true);
081: assertBooleanTypeAdapterParses("y", true);
082: assertBooleanTypeAdapterParses("+", true);
083: assertBooleanTypeAdapterParses("1", true);
084: assertBooleanTypeAdapterParses("True", true);
085: assertBooleanTypeAdapterParses("YES", true);
086: assertBooleanTypeAdapterParses("Y", true);
087:
088: assertBooleanTypeAdapterParses("N", false);
089: assertBooleanTypeAdapterParses("No", false);
090: assertBooleanTypeAdapterParses("false", false);
091: assertBooleanTypeAdapterParses("0", false);
092: assertBooleanTypeAdapterParses("-", false);
093: assertBooleanTypeAdapterParses("whatever", false);
094: }
095:
096: private void assertBooleanTypeAdapterParses(String booleanString,
097: boolean assertedValue) throws Exception {
098: TypeAdapter booleanAdapter = TypeAdapter
099: .adapterFor(Boolean.class);
100: Boolean result = (Boolean) booleanAdapter.parse(booleanString);
101: assertTrue(result.booleanValue() == assertedValue);
102: }
103: }
|