001: package jaxx.junit;
002:
003: import junit.framework.TestCase;
004:
005: import jaxx.types.*;
006:
007: public class PrimitiveConverterTest extends TestCase {
008: private PrimitiveConverter converter;
009:
010: public void setUp() {
011: converter = new PrimitiveConverter();
012: }
013:
014: public void testBoolean() {
015: Boolean value = (Boolean) converter.convertFromString("true",
016: Boolean.class);
017: assertTrue(value.booleanValue());
018:
019: value = (Boolean) converter.convertFromString("false",
020: Boolean.class);
021: assertFalse(value.booleanValue());
022:
023: try {
024: value = (Boolean) converter.convertFromString("yes",
025: Boolean.class);
026: fail("Expected IllegalArgumentException");
027: } catch (IllegalArgumentException e) {
028: }
029: }
030:
031: public void testByte() {
032: Byte value = (Byte) converter.convertFromString(String
033: .valueOf(Byte.MAX_VALUE), Byte.class);
034: assertEquals(value.byteValue(), Byte.MAX_VALUE);
035:
036: try {
037: value = (Byte) converter.convertFromString(String
038: .valueOf(1 + Byte.MAX_VALUE), Byte.class);
039: fail("Expected NumberFormatException");
040: } catch (NumberFormatException e) {
041: }
042: }
043:
044: public void testShort() {
045: Short value = (Short) converter.convertFromString(String
046: .valueOf(Short.MAX_VALUE), Short.class);
047: assertEquals(value.shortValue(), Short.MAX_VALUE);
048:
049: try {
050: value = (Short) converter.convertFromString(String
051: .valueOf(1 + Short.MAX_VALUE), Short.class);
052: fail("Expected NumberFormatException");
053: } catch (NumberFormatException e) {
054: }
055: }
056:
057: public void testInteger() {
058: Integer value = (Integer) converter.convertFromString(String
059: .valueOf(Integer.MAX_VALUE), Integer.class);
060: assertEquals(value.intValue(), Integer.MAX_VALUE);
061:
062: try {
063: value = (Integer) converter.convertFromString(String
064: .valueOf(1L + Integer.MAX_VALUE), Integer.class);
065: fail("Expected NumberFormatException");
066: } catch (NumberFormatException e) {
067: }
068: }
069:
070: public void testLong() {
071: Long value = (Long) converter.convertFromString(String
072: .valueOf(Long.MAX_VALUE), Long.class);
073: assertEquals(value.longValue(), Long.MAX_VALUE);
074: }
075:
076: public void testFloat() {
077: Float value = (Float) converter.convertFromString("3.1415",
078: Float.class);
079: assertTrue(value.floatValue() == 3.1415f);
080: }
081:
082: public void testDouble() {
083: Double value = (Double) converter.convertFromString("3.1415",
084: Double.class);
085: assertTrue(value.doubleValue() == 3.1415);
086: }
087:
088: public void testCharacter() {
089: Character value = (Character) converter.convertFromString("A",
090: Character.class);
091: assertEquals(value.charValue(), 'A');
092:
093: try {
094: value = (Character) converter.convertFromString("12",
095: Character.class);
096: fail("Expected IllegalArgumentException");
097: } catch (IllegalArgumentException e) {
098: }
099:
100: try {
101: value = (Character) converter.convertFromString("",
102: Character.class);
103: fail("Expected IllegalArgumentException");
104: } catch (IllegalArgumentException e) {
105: }
106: }
107:
108: public void testString() {
109: String value = (String) converter.convertFromString("Test",
110: String.class);
111: assertEquals(value, "Test");
112: }
113: }
|