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.lang.reflect.Constructor;
022 import java.math.BigDecimal;
023 import java.net.URL;
024 import java.util.AbstractMap;
025 import java.util.ArrayList;
026 import java.util.Arrays;
027 import java.util.HashMap;
028 import java.util.List;
029 import java.util.Map;
030 import java.util.SortedMap;
031
032 import junit.framework.TestCase;
033
034 public class TestMisc extends TestCase {
035
036 public static <S> void assertPassThru(Object wanted, Class<S> sourceClass) throws Exception {
037 assertPassThru(wanted, sourceClass, sourceClass);
038 }
039
040 public static <S> void assertPassThru(Object wanted, Class<S> sourceClass, Class<? super S> targetClass) throws Exception {
041 Converter<S, ? super S> converter = Converters.getConverter(sourceClass, targetClass);
042 Object result = converter.convert(Util.<S>cast(wanted));
043 assertEquals("pass thru convert", wanted, result);
044 assertSame("pass thru exact equals", wanted, result);
045 assertTrue("pass thru can convert wanted", converter.canConvert(wanted.getClass(), targetClass));
046 assertTrue("pass thru can convert source", converter.canConvert(sourceClass, targetClass));
047 assertEquals("pass thru source class", wanted.getClass(), converter.getSourceClass());
048 assertEquals("pass thru target class", targetClass, converter.getTargetClass());
049 }
050
051 public static void assertStaticHelperClass(Class<?> clz) throws Exception {
052 Constructor<?>[] constructors = clz.getDeclaredConstructors();
053 assertEquals(clz.getName() + " constructor count", 1, constructors.length);
054 assertEquals(clz.getName() + " private declared constructor", 1 << Constructor.DECLARED, constructors[0].getModifiers() & ~(1 << Constructor.PUBLIC) & (1 << Constructor.DECLARED));
055 constructors[0].setAccessible(true);
056 constructors[0].newInstance();
057 }
058
059 public void testConversionException() {
060 java.util.Date nullDate = null;
061 Converter<String, java.util.Date> converter = new DateTimeConverters.StringToDate();
062 try {
063 nullDate = converter.convert("");
064 } catch (ConversionException e) {
065 @SuppressWarnings("unused")
066 Exception ex = new ConversionException("Test case");
067 ex = new ConversionException("Test case", e);
068 }
069 assertEquals("ConversionException", null, nullDate);
070 }
071
072 public void testConverterFactory() {
073 Converter<BigDecimal, URL> notFound = null;
074 try {
075 notFound = Converters.getConverter(BigDecimal.class, URL.class);
076 } catch (ClassNotFoundException e) {}
077 assertEquals("Converter not found", null, notFound);
078 try {
079 notFound = Converters.getConverter(BigDecimal.class, URL.class);
080 } catch (ClassNotFoundException e) {}
081 assertEquals("Converter not found", null, notFound);
082 }
083
084 public void testLoadContainedConvertersIgnoresException() {
085 Converters.loadContainedConverters(TestMisc.class);
086 }
087
088 public void testPassthru() throws Exception {
089 String string = "convert";
090 BigDecimal bigDecimal = new BigDecimal("1.234");
091 URL url = new URL("http://www.apache.org");
092 List<String> baseList = Arrays.asList(new String[]{"a", "1"});
093 ArrayList<String> arrayList = new ArrayList<String>(baseList);
094 HashMap<String, String> hashMap = new HashMap<String, String>();
095 hashMap.put("a", "1");
096 Object[] testObjects = new Object[] {
097 string,
098 bigDecimal,
099 url,
100 arrayList,
101 hashMap,
102 };
103 for (Object testObject: testObjects) {
104 assertPassThru(testObject, testObject.getClass());
105 }
106 assertPassThru(arrayList, arrayList.getClass(), List.class);
107 assertPassThru(hashMap, hashMap.getClass(), Map.class);
108 }
109
110 public void testStaticHelperClass() throws Exception {
111 assertStaticHelperClass(Converters.class);
112 assertStaticHelperClass(Util.class);
113 }
114
115 public void testUtil() throws Exception {
116 assertTrue("Is instance of", Util.instanceOf(SortedMap.class, Map.class));
117 assertTrue("Is instance of", Util.instanceOf(Map.class, AbstractMap.class));
118 assertTrue("Is instance of", Util.instanceOf(HashMap.class, Map.class));
119 assertTrue("Is instance of", Util.instanceOf(HashMap.class, AbstractMap.class));
120 assertFalse("Is not instance of", Util.instanceOf(URL.class, Map.class));
121 assertFalse("Is not instance of", Util.instanceOf(List.class, Map.class));
122 assertTrue("Is empty (null)", Util.isEmpty(null));
123 assertTrue("Is empty (empty String)", Util.isEmpty(""));
124 assertFalse("Is not empty", Util.isEmpty("convert"));
125 }
126
127 public static class ConverterLoaderImpl implements ConverterLoader {
128 public void loadConverters() {
129 throw new RuntimeException();
130 }
131 }
132 }