1 10 package org.nanocontainer.reflection; 11 12 import junit.framework.TestCase; 13 14 import java.io.File ; 15 16 public class StringToObjectConverterTestCase extends TestCase { 17 private StringToObjectConverter converter = new StringToObjectConverter(); 18 19 public void testConvertsToString() { 20 assertEquals("hello", converter.convertTo(String .class, "hello")); 21 assertEquals("", converter.convertTo(String .class, "")); 22 } 23 24 public void testConvertsToInts() { 25 assertEquals(new Integer (22), converter.convertTo(Integer .class, "22")); 26 assertEquals(new Integer (-9), converter.convertTo(Integer .class, "-9")); 27 } 28 29 public void testConvertsToLong() { 30 assertEquals(new Long (123456789012L), converter.convertTo(Long .class, "123456789012")); 31 assertEquals(new Long (-123456789012L), converter.convertTo(Long .class, "-123456789012")); 32 assertEquals(new Long (0), converter.convertTo(Long .class, "0")); 33 } 34 35 public void testConvertsToBooleanUsingBestGuess() { 36 assertEquals(Boolean.TRUE, converter.convertTo(Boolean .class, "t")); 37 assertEquals(Boolean.TRUE, converter.convertTo(Boolean .class, "true")); 38 assertEquals(Boolean.TRUE, converter.convertTo(Boolean .class, "T")); 39 assertEquals(Boolean.TRUE, converter.convertTo(Boolean .class, "TRUE")); 40 assertEquals(Boolean.TRUE, converter.convertTo(Boolean .class, "1")); 41 assertEquals(Boolean.TRUE, converter.convertTo(Boolean .class, "yes")); 42 assertEquals(Boolean.TRUE, converter.convertTo(Boolean .class, "Yo!")); 43 44 assertEquals(Boolean.FALSE, converter.convertTo(Boolean .class, "f")); 45 assertEquals(Boolean.FALSE, converter.convertTo(Boolean .class, "false")); 46 assertEquals(Boolean.FALSE, converter.convertTo(Boolean .class, "FALSE")); 47 assertEquals(Boolean.FALSE, converter.convertTo(Boolean .class, "0")); 48 assertEquals(Boolean.FALSE, converter.convertTo(Boolean .class, "no")); 49 assertEquals(Boolean.FALSE, converter.convertTo(Boolean .class, "nada!")); 50 assertEquals(Boolean.FALSE, converter.convertTo(Boolean .class, "")); 51 assertEquals(Boolean.FALSE, converter.convertTo(Boolean .class, "I'm a lumberjack and I'm okay")); 52 } 53 54 public void testCustomConversionsCanBeRegistered() { 55 converter.register(File .class, new Converter() { 56 public Object convert(String in) { 57 return new File (in); 58 } 59 }); 60 assertEquals("hello", converter.convertTo(String .class, "hello")); 61 assertEquals(new File ("hello"), converter.convertTo(File .class, "hello")); 62 } 63 64 public void testNullsMapToDefaultValues() { 65 assertNull(converter.convertTo(String .class, null)); 66 assertEquals(new Integer (0), converter.convertTo(Integer .class, null)); 67 assertEquals(new Long (0), converter.convertTo(Long .class, null)); 68 assertEquals(Boolean.FALSE, converter.convertTo(Boolean .class, null)); 69 } 70 71 public void testExceptionThrownIfConverterNotRegistered() { 72 try { 73 converter.convertTo(File .class, "hello"); 74 fail("Should have thrown exception"); 75 } catch (InvalidConversionException e) { 76 } 78 } 79 80 public void testDodgyFormatThrowExceptions() { 81 try { 82 converter.convertTo(Integer .class, "fooo"); 83 fail("Should have thrown exception"); 84 } catch (NumberFormatException e) { 85 } 87 } 88 89 } 90 | Popular Tags |