commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adri...@apache.org
Subject svn commit: r932941 - in /commons/sandbox/convert/trunk/src: main/java/org/apache/commons/convert/NetConverters.java test/java/org/apache/commons/convert/TestNetConverters.java
Date Sun, 11 Apr 2010 17:19:41 GMT
Author: adrianc
Date: Sun Apr 11 17:19:40 2010
New Revision: 932941

URL: http://svn.apache.org/viewvc?rev=932941&view=rev
Log:
Expanded net converters, added unit tests.

Added:
    commons/sandbox/convert/trunk/src/test/java/org/apache/commons/convert/TestNetConverters.java
  (with props)
Modified:
    commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/NetConverters.java

Modified: commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/NetConverters.java
URL: http://svn.apache.org/viewvc/commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/NetConverters.java?rev=932941&r1=932940&r2=932941&view=diff
==============================================================================
--- commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/NetConverters.java
(original)
+++ commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/NetConverters.java
Sun Apr 11 17:19:40 2010
@@ -28,18 +28,16 @@ import java.net.URL;
 /** Net Converter classes. */
 public class NetConverters implements ConverterLoader {
 
-    public static class StringToInetAddress extends AbstractConverter<String, InetAddress>
{
-        public StringToInetAddress() {
-            super(String.class, InetAddress.class);
-        }
-
-        public InetAddress convert(String obj) throws ConversionException {
-            try {
-                return InetAddress.getByName(obj);
-            } catch (IOException e) {
-                throw (ConversionException) new ConversionException(e.getMessage()).initCause(e);
-            }
-        }
+    public void loadConverters() {
+        Converters.loadContainedConverters(NetConverters.class);
+        Converters.registerConverter(new GenericToStringConverter<URI>(URI.class));
+        Converters.registerConverter(new GenericToStringConverter<URL>(URL.class));
+        Converters.registerConverter(new GenericSingletonToList<InetAddress>(InetAddress.class));
+        Converters.registerConverter(new GenericSingletonToList<URI>(URI.class));
+        Converters.registerConverter(new GenericSingletonToList<URL>(URL.class));
+        Converters.registerConverter(new GenericSingletonToSet<InetAddress>(InetAddress.class));
+        Converters.registerConverter(new GenericSingletonToSet<URI>(URI.class));
+        Converters.registerConverter(new GenericSingletonToSet<URL>(URL.class));
     }
 
     public static class InetAddressToString extends AbstractConverter<InetAddress, String>
{
@@ -54,6 +52,20 @@ public class NetConverters implements Co
         }
     }
 
+    public static class StringToInetAddress extends AbstractConverter<String, InetAddress>
{
+        public StringToInetAddress() {
+            super(String.class, InetAddress.class);
+        }
+
+        public InetAddress convert(String obj) throws ConversionException {
+            try {
+                return InetAddress.getByName(obj);
+            } catch (IOException e) {
+                throw (ConversionException) new ConversionException(e.getMessage()).initCause(e);
+            }
+        }
+    }
+
     public static class StringToURI extends AbstractConverter<String, URI> {
         public StringToURI() {
             super(String.class, URI.class);
@@ -109,10 +121,4 @@ public class NetConverters implements Co
             }
         }
     }
-
-    public void loadConverters() {
-        Converters.loadContainedConverters(NetConverters.class);
-        Converters.registerConverter(new GenericToStringConverter<URI>(URI.class));
-        Converters.registerConverter(new GenericToStringConverter<URL>(URL.class));
-    }
 }

Added: commons/sandbox/convert/trunk/src/test/java/org/apache/commons/convert/TestNetConverters.java
URL: http://svn.apache.org/viewvc/commons/sandbox/convert/trunk/src/test/java/org/apache/commons/convert/TestNetConverters.java?rev=932941&view=auto
==============================================================================
--- commons/sandbox/convert/trunk/src/test/java/org/apache/commons/convert/TestNetConverters.java
(added)
+++ commons/sandbox/convert/trunk/src/test/java/org/apache/commons/convert/TestNetConverters.java
Sun Apr 11 17:19:40 2010
@@ -0,0 +1,81 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *******************************************************************************/
+package org.apache.commons.convert;
+
+import java.net.InetAddress;
+import java.net.URI;
+import java.net.URL;
+import java.util.Collection;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.commons.convert.Converter;
+import org.apache.commons.convert.Converters;
+import org.apache.commons.convert.NetConverters;
+
+import junit.framework.TestCase;
+
+public class TestNetConverters extends TestCase {
+
+    @SuppressWarnings("unchecked")
+    public static <S, T> void assertConversion(String label, Converter<S, T>
converter, S source, T target) throws Exception {
+        assertTrue(label + " can convert", converter.canConvert(source.getClass(), target.getClass()));
+        assertEquals(label + " registered", converter.getClass(), Converters.getConverter(source.getClass(),
target.getClass()).getClass());
+        assertEquals(label + " converted", target, converter.convert(source));
+        Converter<T, S> reflectiveConverter = null;
+        try {
+            reflectiveConverter = (Converter<T, S>) Converters.getConverter(target.getClass(),
source.getClass());
+            assertEquals(label + " reflection converted", source, reflectiveConverter.convert(target));
+        } catch (ClassNotFoundException e) {
+            System.out.println(converter.getClass() + " not reflective");
+        }
+    }
+
+    @SuppressWarnings("unchecked")
+    public static <S> void assertToCollection(String label, S source) throws Exception
{
+        Converter<S, ? extends Collection> toList = (Converter<S, ? extends Collection>)
Converters.getConverter(source.getClass(), List.class);
+        Collection<S> listResult = toList.convert(source);
+        assertEquals(label + " converted to List", source, listResult.toArray()[0]);
+        Converter<S, ? extends Collection> toSet = (Converter<S, ? extends Collection>)
Converters.getConverter(source.getClass(), Set.class);
+        Collection<S> setResult = toSet.convert(source);
+        assertEquals(label + " converted to Set", source, setResult.toArray()[0]);
+    }
+
+    public TestNetConverters(String name) {
+        super(name);
+    }
+
+    public void testNetConverters() throws Exception {
+        ConverterLoader loader = new NetConverters();
+        loader.loadConverters();
+        InetAddress address = InetAddress.getLocalHost();
+        String strAddress = address.getHostName();
+        String strURI = "mailto:dev@commons.apache.org";
+        URI uri = new URI(strURI);
+        String strURL = "http://www.apache.org/index.html";
+        URL url = new URL(strURL);
+        assertConversion("InetAddressToString", new NetConverters.InetAddressToString(),
address, strAddress);
+        assertToCollection("InetAddressToCollection", address);
+        assertConversion("UriToString", new GenericToStringConverter<URI>(URI.class),
uri, strURI);
+        assertToCollection("UriToCollection", uri);
+        assertConversion("UrlToString", new GenericToStringConverter<URL>(URL.class),
url, strURL);
+        assertToCollection("UrlToCollection", url);
+        assertConversion("UriToUrl", new NetConverters.URIToURL(), url.toURI(), url);
+    }
+}

Propchange: commons/sandbox/convert/trunk/src/test/java/org/apache/commons/convert/TestNetConverters.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/convert/trunk/src/test/java/org/apache/commons/convert/TestNetConverters.java
------------------------------------------------------------------------------
    svn:keywords = Date Rev Author URL Id

Propchange: commons/sandbox/convert/trunk/src/test/java/org/apache/commons/convert/TestNetConverters.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message