001 /***************************************************************************** 002 * Copyright (C) NanoContainer Organization. All rights reserved. * 003 * ------------------------------------------------------------------------- * 004 * The software in this package is published under the terms of the BSD * 005 * style license a copy of which has been included with this distribution in * 006 * the LICENSE.txt file. * 007 * * 008 * Original code by * 009 *****************************************************************************/ 010 package org.nanocontainer.reflection; 011 012 import junit.framework.TestCase; 013 014 import java.io.File; 015 016 public class StringToObjectConverterTestCase extends TestCase { 017 private StringToObjectConverter converter = new StringToObjectConverter(); 018 019 public void testConvertsToString() { 020 assertEquals("hello", converter.convertTo(String.class, "hello")); 021 assertEquals("", converter.convertTo(String.class, "")); 022 } 023 024 public void testConvertsToInts() { 025 assertEquals(new Integer(22), converter.convertTo(Integer.class, "22")); 026 assertEquals(new Integer(-9), converter.convertTo(Integer.class, "-9")); 027 } 028 029 public void testConvertsToLong() { 030 assertEquals(new Long(123456789012L), converter.convertTo(Long.class, "123456789012")); 031 assertEquals(new Long(-123456789012L), converter.convertTo(Long.class, "-123456789012")); 032 assertEquals(new Long(0), converter.convertTo(Long.class, "0")); 033 } 034 035 public void testConvertsToBooleanUsingBestGuess() { 036 assertEquals(Boolean.TRUE, converter.convertTo(Boolean.class, "t")); 037 assertEquals(Boolean.TRUE, converter.convertTo(Boolean.class, "true")); 038 assertEquals(Boolean.TRUE, converter.convertTo(Boolean.class, "T")); 039 assertEquals(Boolean.TRUE, converter.convertTo(Boolean.class, "TRUE")); 040 assertEquals(Boolean.TRUE, converter.convertTo(Boolean.class, "1")); 041 assertEquals(Boolean.TRUE, converter.convertTo(Boolean.class, "yes")); 042 assertEquals(Boolean.TRUE, converter.convertTo(Boolean.class, "Yo!")); 043 044 assertEquals(Boolean.FALSE, converter.convertTo(Boolean.class, "f")); 045 assertEquals(Boolean.FALSE, converter.convertTo(Boolean.class, "false")); 046 assertEquals(Boolean.FALSE, converter.convertTo(Boolean.class, "FALSE")); 047 assertEquals(Boolean.FALSE, converter.convertTo(Boolean.class, "0")); 048 assertEquals(Boolean.FALSE, converter.convertTo(Boolean.class, "no")); 049 assertEquals(Boolean.FALSE, converter.convertTo(Boolean.class, "nada!")); 050 assertEquals(Boolean.FALSE, converter.convertTo(Boolean.class, "")); 051 assertEquals(Boolean.FALSE, converter.convertTo(Boolean.class, "I'm a lumberjack and I'm okay")); 052 } 053 054 public void testCustomConversionsCanBeRegistered() { 055 converter.register(File.class, new Converter() { 056 public Object convert(String in) { 057 return new File(in); 058 } 059 }); 060 assertEquals("hello", converter.convertTo(String.class, "hello")); 061 assertEquals(new File("hello"), converter.convertTo(File.class, "hello")); 062 } 063 064 public void testNullsMapToDefaultValues() { 065 assertNull(converter.convertTo(String.class, null)); 066 assertEquals(new Integer(0), converter.convertTo(Integer.class, null)); 067 assertEquals(new Long(0), converter.convertTo(Long.class, null)); 068 assertEquals(Boolean.FALSE, converter.convertTo(Boolean.class, null)); 069 } 070 071 public void testExceptionThrownIfConverterNotRegistered() { 072 try { 073 converter.convertTo(File.class, "hello"); 074 fail("Should have thrown exception"); 075 } catch (InvalidConversionException e) { 076 // good 077 } 078 } 079 080 public void testDodgyFormatThrowExceptions() { 081 try { 082 converter.convertTo(Integer.class, "fooo"); 083 fail("Should have thrown exception"); 084 } catch (NumberFormatException e) { 085 // good 086 } 087 } 088 089 }