001    /*****************************************************************************
002     * Copyright (C) PicoContainer 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.picocontainer.script;
011    
012    import static org.junit.Assert.assertEquals;
013    import static org.junit.Assert.assertNull;
014    import static org.junit.Assert.fail;
015    
016    import java.io.File;
017    
018    import org.junit.Test;
019    import org.picocontainer.script.util.StringConversions;
020    
021    public final class StringConversionsTestCase {
022        private final StringConversions converter = new StringConversions();
023    
024        @Test public void testConversionToString() {
025            assertEquals("hello", converter.convertTo(String.class, "hello"));
026            assertEquals("", converter.convertTo(String.class, ""));
027        }
028    
029        @Test public void testConversionToInts() {
030            assertEquals(22, converter.convertTo(Integer.class, "22"));
031            assertEquals(-9, converter.convertTo(Integer.class, "-9"));
032        }
033    
034        @Test public void testConversionToLong() {
035            assertEquals(123456789012L, converter.convertTo(Long.class, "123456789012"));
036            assertEquals(-123456789012L, converter.convertTo(Long.class, "-123456789012"));
037            assertEquals((long)0, converter.convertTo(Long.class, "0"));
038        }
039    
040        @Test public void testConversionToBooleanUsingBestGuess() {
041            assertEquals(Boolean.TRUE, converter.convertTo(Boolean.class, "t"));
042            assertEquals(Boolean.TRUE, converter.convertTo(Boolean.class, "true"));
043            assertEquals(Boolean.TRUE, converter.convertTo(Boolean.class, "T"));
044            assertEquals(Boolean.TRUE, converter.convertTo(Boolean.class, "TRUE"));
045            assertEquals(Boolean.TRUE, converter.convertTo(Boolean.class, "1"));
046            assertEquals(Boolean.TRUE, converter.convertTo(Boolean.class, "yes"));
047            assertEquals(Boolean.TRUE, converter.convertTo(Boolean.class, "Yo!"));
048    
049            assertEquals(Boolean.FALSE, converter.convertTo(Boolean.class, "f"));
050            assertEquals(Boolean.FALSE, converter.convertTo(Boolean.class, "false"));
051            assertEquals(Boolean.FALSE, converter.convertTo(Boolean.class, "FALSE"));
052            assertEquals(Boolean.FALSE, converter.convertTo(Boolean.class, "0"));
053            assertEquals(Boolean.FALSE, converter.convertTo(Boolean.class, "no"));
054            assertEquals(Boolean.FALSE, converter.convertTo(Boolean.class, "nada!"));
055            assertEquals(Boolean.FALSE, converter.convertTo(Boolean.class, ""));
056            assertEquals(Boolean.FALSE, converter.convertTo(Boolean.class, "I'm a lumberjack and I'm okay"));
057        }
058    
059        @SuppressWarnings("unchecked")
060        @Test public void testCustomConversionsCanBeRegistered() {
061            converter.register(File.class, new StringConversions.StringConverter() {
062                public Object convert(String in) {
063                    return new File(in);
064                }
065            });
066            assertEquals("hello", converter.convertTo(String.class, "hello"));
067            assertEquals(new File("hello"), converter.convertTo(File.class, "hello"));
068        }
069    
070        @Test public void testNullsMapToDefaultValues() {
071            assertNull(converter.convertTo(String.class, null));
072            assertEquals(0, converter.convertTo(Integer.class, null));
073            assertEquals((long)0, converter.convertTo(Long.class, null));
074            assertEquals(Boolean.FALSE, converter.convertTo(Boolean.class, null));
075        }
076    
077        @Test public void testExceptionThrownIfConverterNotRegistered() {
078            try {
079                converter.convertTo(File.class, "hello");
080                fail("Should have thrown exception");
081            } catch (StringConversions.InvalidConversionException e) {
082                // good
083            }
084        }
085    
086        @Test public void testDodgyFormatThrowExceptions() {
087            try {
088                converter.convertTo(Integer.class, "fooo");
089                fail("Should have thrown exception");
090            } catch (NumberFormatException e) {
091                // good
092            }
093        }
094    
095    }