001    /*******************************************************************************
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     *******************************************************************************/
019    package org.apache.commons.convert;
020    
021    import java.math.BigDecimal;
022    import java.math.BigInteger;
023    import java.util.Collection;
024    import java.util.List;
025    import java.util.Locale;
026    import java.util.Set;
027    
028    import junit.framework.TestCase;
029    
030    public class TestNumberConverters extends TestCase {
031    
032        protected static final Class<?>[] classArray = {BigDecimal.class, BigInteger.class, Byte.class, Double.class, Integer.class, Float.class, Long.class, Short.class, String.class};
033    
034        @SuppressWarnings("unchecked")
035        public static <S> void assertConversion(S source) throws Exception {
036            Class<?> sourceClass = source.getClass();
037            for (Class<?> targetClass : classArray) {
038                Converter<S, ?> converter = ( Converter<S, ?>) Converters.getConverter(sourceClass, targetClass);
039                String label = converter.getClass().getSimpleName();
040                assertTrue(label + " can convert", converter.canConvert(sourceClass, targetClass));
041                Object result = converter.convert(source);
042                assertEquals(label + " converted", targetClass, result.getClass());
043                try {
044                    LocalizedConverter<S, Object> localizedConverter = (LocalizedConverter) converter;
045                    Object localizedResult = localizedConverter.convert(source, Locale.getDefault(), null, null);
046                    Converter<Object, S> reflectiveConverter = null;
047                    try {
048                        reflectiveConverter = (Converter<Object, S>) Converters.getConverter(targetClass, sourceClass);
049                        assertEquals(label + " reflection converted", source, reflectiveConverter.convert(result));
050                        LocalizedConverter<Object, S> localizedReflectiveConverter = (LocalizedConverter) reflectiveConverter;
051                        assertEquals(label + " localized reflection converted", source, localizedReflectiveConverter.convert(localizedResult, Locale.getDefault(), null));
052                    } catch (ClassNotFoundException e) {
053                        System.out.println(converter.getClass() + " not reflective");
054                    }
055                } catch (ClassCastException e) {}
056                assertToCollection(label, source);
057            }
058        }
059    
060        @SuppressWarnings("unchecked")
061        public static <S> void assertToCollection(String label, S source) throws Exception {
062            Converter<S, ? extends Collection> toList = (Converter<S, ? extends Collection>) Converters.getConverter(source.getClass(), List.class);
063            Collection<S> listResult = toList.convert(source);
064            assertEquals(label + " converted to List", source, listResult.toArray()[0]);
065            Converter<S, ? extends Collection> toSet = (Converter<S, ? extends Collection>) Converters.getConverter(source.getClass(), Set.class);
066            Collection<S> setResult = toSet.convert(source);
067            assertEquals(label + " converted to Set", source, setResult.toArray()[0]);
068        }
069    
070        public TestNumberConverters(String name) {
071            super(name);
072        }
073    
074        public void testNumberConverters() throws Exception {
075            ConverterLoader loader = new NumberConverters();
076            loader.loadConverters();
077            String strDecimal = "1234567.89";
078            String strInteger = "1234567";
079            String strShort = "123";
080            assertConversion(new BigDecimal(strDecimal));
081            assertConversion(new BigInteger(strInteger));
082            assertConversion(new Byte(strShort));
083            assertConversion(new Double(strDecimal));
084            assertConversion(new Integer(strInteger));
085            assertConversion(new Float(strDecimal));
086            assertConversion(new Long(strInteger));
087            assertConversion(new Short(strShort));
088        }
089    }