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.util.Collection;
022    import java.util.List;
023    import java.util.Set;
024    
025    import org.apache.commons.convert.Converter;
026    import org.apache.commons.convert.Converters;
027    import org.apache.commons.convert.BooleanConverters;
028    
029    import junit.framework.TestCase;
030    
031    public class TestBooleanConverters extends TestCase {
032    
033        public TestBooleanConverters(String name) {
034            super(name);
035        }
036    
037        public static <T> void assertFromBoolean(String label, Converter<Boolean, T> converter, T trueResult, T falseResult) throws Exception {
038            assertTrue(label + " can convert", converter.canConvert(Boolean.class, trueResult.getClass()));
039            assertEquals(label + " registered", converter.getClass(), Converters.getConverter(Boolean.class, trueResult.getClass()).getClass());
040            assertEquals(label + " converted", trueResult, converter.convert(true));
041            assertEquals(label + " converted", falseResult, converter.convert(false));
042        }
043    
044        public static <S> void assertToBoolean(String label, Converter<S, Boolean> converter, S trueSource, S falseSource) throws Exception {
045            assertTrue(label + " can convert", converter.canConvert(trueSource.getClass(), Boolean.class));
046            assertEquals(label + " registered", converter.getClass(), Converters.getConverter(trueSource.getClass(), Boolean.class).getClass());
047            assertEquals(label + " converted", Boolean.TRUE, converter.convert(trueSource));
048            assertEquals(label + " converted", Boolean.FALSE, converter.convert(falseSource));
049        }
050    
051        @SuppressWarnings("unchecked")
052        public static <S> void assertToCollection(String label, S source) throws Exception {
053            Converter<S, ? extends Collection> toList = (Converter<S, ? extends Collection>) Converters.getConverter(source.getClass(), List.class);
054            Collection<S> listResult = toList.convert(source);
055            assertEquals(label + " converted to List", source, listResult.toArray()[0]);
056            Converter<S, ? extends Collection> toSet = (Converter<S, ? extends Collection>) Converters.getConverter(source.getClass(), Set.class);
057            Collection<S> setResult = toSet.convert(source);
058            assertEquals(label + " converted to Set", source, setResult.toArray()[0]);
059        }
060    
061        public void testBooleanConverters() throws Exception {
062            ConverterLoader loader = new BooleanConverters();
063            loader.loadConverters();
064            assertFromBoolean("BooleanToInteger", new BooleanConverters.BooleanToInteger(), 1, 0);
065            assertFromBoolean("BooleanToString", new GenericToStringConverter<Boolean>(Boolean.class), "true", "false");
066            assertToBoolean("IntegerToBoolean", new BooleanConverters.IntegerToBoolean(), 1, 0);
067            assertToBoolean("StringToBoolean", new BooleanConverters.StringToBoolean(), "true", "false");
068            assertToCollection("BooleanToCollection", Boolean.TRUE);
069        }
070    }