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.net.InetAddress;
022 import java.net.URI;
023 import java.net.URL;
024 import java.util.Collection;
025 import java.util.List;
026 import java.util.Set;
027
028 import org.apache.commons.convert.Converter;
029 import org.apache.commons.convert.Converters;
030 import org.apache.commons.convert.NetConverters;
031
032 import junit.framework.TestCase;
033
034 public class TestNetConverters extends TestCase {
035
036 @SuppressWarnings("unchecked")
037 public static <S, T> void assertConversion(String label, Converter<S, T> converter, S source, T target) throws Exception {
038 assertTrue(label + " can convert", converter.canConvert(source.getClass(), target.getClass()));
039 assertEquals(label + " registered", converter.getClass(), Converters.getConverter(source.getClass(), target.getClass()).getClass());
040 assertEquals(label + " converted", target, converter.convert(source));
041 Converter<T, S> reflectiveConverter = null;
042 try {
043 reflectiveConverter = (Converter<T, S>) Converters.getConverter(target.getClass(), source.getClass());
044 assertEquals(label + " reflection converted", source, reflectiveConverter.convert(target));
045 } catch (ClassNotFoundException e) {
046 System.out.println(converter.getClass() + " not reflective");
047 }
048 }
049
050 @SuppressWarnings("unchecked")
051 public static <S> void assertToCollection(String label, S source) throws Exception {
052 Converter<S, ? extends Collection> toList = (Converter<S, ? extends Collection>) Converters.getConverter(source.getClass(), List.class);
053 Collection<S> listResult = toList.convert(source);
054 assertEquals(label + " converted to List", source, listResult.toArray()[0]);
055 Converter<S, ? extends Collection> toSet = (Converter<S, ? extends Collection>) Converters.getConverter(source.getClass(), Set.class);
056 Collection<S> setResult = toSet.convert(source);
057 assertEquals(label + " converted to Set", source, setResult.toArray()[0]);
058 }
059
060 public TestNetConverters(String name) {
061 super(name);
062 }
063
064 public void testNetConverters() throws Exception {
065 ConverterLoader loader = new NetConverters();
066 loader.loadConverters();
067 InetAddress address = InetAddress.getLocalHost();
068 String strAddress = address.getHostName();
069 String strURI = "mailto:dev@commons.apache.org";
070 URI uri = new URI(strURI);
071 String strURL = "http://www.apache.org/index.html";
072 URL url = new URL(strURL);
073 assertConversion("InetAddressToString", new NetConverters.InetAddressToString(), address, strAddress);
074 assertToCollection("InetAddressToCollection", address);
075 assertConversion("UriToString", new GenericToStringConverter<URI>(URI.class), uri, strURI);
076 assertToCollection("UriToCollection", uri);
077 assertConversion("UrlToString", new GenericToStringConverter<URL>(URL.class), url, strURL);
078 assertToCollection("UrlToCollection", url);
079 assertConversion("UriToUrl", new NetConverters.URIToURL(), url.toURI(), url);
080 }
081 }