You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2019/10/21 23:40:58 UTC
[commons-beanutils] branch master updated: BEANUTILS-528: New
converters for UUID, URI, and Path. (#10)
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-beanutils.git
The following commit(s) were added to refs/heads/master by this push:
new 7493ae6 BEANUTILS-528: New converters for UUID, URI, and Path. (#10)
7493ae6 is described below
commit 7493ae6f11fdbd41d33a50c5e5c7088baf896f91
Author: Melloware <me...@gmail.com>
AuthorDate: Mon Oct 21 19:40:52 2019 -0400
BEANUTILS-528: New converters for UUID, URI, and Path. (#10)
* BEANUTILS-528: New converters for UUID, URI, and Path.
* BEANUTILS-528: New converters for UUID, URI, and Path.
* BEANUTILS-528: Change PathConverter to use isAssignableFrom.
* BEANUTILS-528: Fixed type in Javadoc.
---
.../commons/beanutils2/ConvertUtilsBean.java | 18 +++
.../beanutils2/converters/PathConverter.java | 83 ++++++++++++++
.../beanutils2/converters/URIConverter.java | 82 +++++++++++++
.../beanutils2/converters/UUIDConverter.java | 82 +++++++++++++
.../converters/PathConverterTestCase.java | 122 ++++++++++++++++++++
.../converters/URIConverterTestCase.java | 127 +++++++++++++++++++++
.../converters/UUIDConverterTestCase.java | 116 +++++++++++++++++++
7 files changed, 630 insertions(+)
diff --git a/src/main/java/org/apache/commons/beanutils2/ConvertUtilsBean.java b/src/main/java/org/apache/commons/beanutils2/ConvertUtilsBean.java
index c793a9e..8197c6f 100644
--- a/src/main/java/org/apache/commons/beanutils2/ConvertUtilsBean.java
+++ b/src/main/java/org/apache/commons/beanutils2/ConvertUtilsBean.java
@@ -23,10 +23,13 @@ import java.io.File;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
+import java.net.URI;
import java.net.URL;
+import java.nio.file.Path;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Collection;
+import java.util.UUID;
import org.apache.commons.beanutils2.converters.ArrayConverter;
import org.apache.commons.beanutils2.converters.BigDecimalConverter;
@@ -43,12 +46,15 @@ import org.apache.commons.beanutils2.converters.FileConverter;
import org.apache.commons.beanutils2.converters.FloatConverter;
import org.apache.commons.beanutils2.converters.IntegerConverter;
import org.apache.commons.beanutils2.converters.LongConverter;
+import org.apache.commons.beanutils2.converters.PathConverter;
import org.apache.commons.beanutils2.converters.ShortConverter;
import org.apache.commons.beanutils2.converters.SqlDateConverter;
import org.apache.commons.beanutils2.converters.SqlTimeConverter;
import org.apache.commons.beanutils2.converters.SqlTimestampConverter;
import org.apache.commons.beanutils2.converters.StringConverter;
+import org.apache.commons.beanutils2.converters.URIConverter;
import org.apache.commons.beanutils2.converters.URLConverter;
+import org.apache.commons.beanutils2.converters.UUIDConverter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -74,7 +80,10 @@ import org.apache.commons.logging.LogFactory;
* <li>short and java.lang.Short (default to zero)</li>
* <li>java.lang.String (default to null)</li>
* <li>java.io.File (no default value)</li>
+ * <li>java.nio.file.Path (no default value)</li>
* <li>java.net.URL (no default value)</li>
+ * <li>java.net.URI (no default value)</li>
+ * <li>java.util.UUID (no default value)</li>
* <li>java.sql.Date (no default value)</li>
* <li>java.sql.Time (no default value)</li>
* <li>java.sql.Timestamp (no default value)</li>
@@ -444,10 +453,13 @@ public class ConvertUtilsBean {
* <li><code>java.util.Date.class</code> - {@link DateConverter}</li>
* <li><code>java.util.Calendar.class</code> - {@link CalendarConverter}</li>
* <li><code>File.class</code> - {@link FileConverter}</li>
+ * <li><code>Path.class</code> - {@link PathConverter}</li>
* <li><code>java.sql.Date.class</code> - {@link SqlDateConverter}</li>
* <li><code>java.sql.Time.class</code> - {@link SqlTimeConverter}</li>
* <li><code>java.sql.Timestamp.class</code> - {@link SqlTimestampConverter}</li>
* <li><code>URL.class</code> - {@link URLConverter}</li>
+ * <li><code>URI.class</code> - {@link URIConverter}</li>
+ * <li><code>UUID.class</code> - {@link UUIDConverter}</li>
* </ul>
* @param throwException <code>true</code> if the converters should
* throw an exception when a conversion error occurs, otherwise <code>
@@ -458,10 +470,13 @@ public class ConvertUtilsBean {
register(java.util.Date.class, throwException ? new DateConverter() : new DateConverter(null));
register(Calendar.class, throwException ? new CalendarConverter() : new CalendarConverter(null));
register(File.class, throwException ? new FileConverter() : new FileConverter(null));
+ register(Path.class, throwException ? new PathConverter() : new PathConverter(null));
register(java.sql.Date.class, throwException ? new SqlDateConverter() : new SqlDateConverter(null));
register(java.sql.Time.class, throwException ? new SqlTimeConverter() : new SqlTimeConverter(null));
register(Timestamp.class, throwException ? new SqlTimestampConverter() : new SqlTimestampConverter(null));
register(URL.class, throwException ? new URLConverter() : new URLConverter(null));
+ register(URI.class, throwException ? new URIConverter() : new URIConverter(null));
+ register(UUID.class, throwException ? new UUIDConverter() : new UUIDConverter(null));
}
/**
@@ -505,10 +520,13 @@ public class ConvertUtilsBean {
registerArrayConverter(java.util.Date.class, new DateConverter(), throwException, defaultArraySize);
registerArrayConverter(Calendar.class, new DateConverter(), throwException, defaultArraySize);
registerArrayConverter(File.class, new FileConverter(), throwException, defaultArraySize);
+ registerArrayConverter(Path.class, new PathConverter(), throwException, defaultArraySize);
registerArrayConverter(java.sql.Date.class, new SqlDateConverter(), throwException, defaultArraySize);
registerArrayConverter(java.sql.Time.class, new SqlTimeConverter(), throwException, defaultArraySize);
registerArrayConverter(Timestamp.class, new SqlTimestampConverter(), throwException, defaultArraySize);
registerArrayConverter(URL.class, new URLConverter(), throwException, defaultArraySize);
+ registerArrayConverter(URI.class, new URIConverter(), throwException, defaultArraySize);
+ registerArrayConverter(UUID.class, new UUIDConverter(), throwException, defaultArraySize);
}
diff --git a/src/main/java/org/apache/commons/beanutils2/converters/PathConverter.java b/src/main/java/org/apache/commons/beanutils2/converters/PathConverter.java
new file mode 100644
index 0000000..55b4f5c
--- /dev/null
+++ b/src/main/java/org/apache/commons/beanutils2/converters/PathConverter.java
@@ -0,0 +1,83 @@
+/*
+ * 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.beanutils2.converters;
+
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+/**
+ * {@link org.apache.commons.beanutils2.Converter} implementation that handles conversion
+ * to and from <b>java.nio.file.Path</b> objects.
+ * <p>
+ * Can be configured to either return a <i>default value</i> or throw a
+ * <code>ConversionException</code> if a conversion error occurs.
+ *
+ * @since 2.0
+ */
+public final class PathConverter extends AbstractConverter {
+
+ /**
+ * Construct a <b>java.nio.file.Path</b> <i>Converter</i> that throws
+ * a <code>ConversionException</code> if an error occurs.
+ */
+ public PathConverter() {
+ super();
+ }
+
+ /**
+ * Construct a <b>java.nio.file.Path</b> <i>Converter</i> that returns
+ * a default value if an error occurs.
+ *
+ * @param defaultValue The default value to be returned
+ * if the value to be converted is missing or an error
+ * occurs converting the value.
+ */
+ public PathConverter(final Object defaultValue) {
+ super(defaultValue);
+ }
+
+ /**
+ * Return the default type this <code>Converter</code> handles.
+ *
+ * @return The default type this <code>Converter</code> handles.
+ * @since 2.0
+ */
+ @Override
+ protected Class<?> getDefaultType() {
+ return Path.class;
+ }
+
+ /**
+ * <p>Convert a java.nio.file.Path or object into a String.</p>
+ *
+ * @param <T> Target type of the conversion.
+ * @param type Data type to which this value should be converted.
+ * @param value The input value to be converted.
+ * @return The converted value.
+ * @throws Throwable if an error occurs converting to the specified type
+ * @since 2.0
+ */
+ @Override
+ protected <T> T convertToType(final Class<T> type, final Object value) throws Throwable {
+ if (Path.class.isAssignableFrom(type)) {
+ return type.cast(Paths.get(String.valueOf(value)));
+ }
+
+ throw conversionException(type, value);
+ }
+
+}
diff --git a/src/main/java/org/apache/commons/beanutils2/converters/URIConverter.java b/src/main/java/org/apache/commons/beanutils2/converters/URIConverter.java
new file mode 100644
index 0000000..e508e4b
--- /dev/null
+++ b/src/main/java/org/apache/commons/beanutils2/converters/URIConverter.java
@@ -0,0 +1,82 @@
+/*
+ * 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.beanutils2.converters;
+
+import java.net.URI;
+
+/**
+ * {@link org.apache.commons.beanutils2.Converter} implementation that handles conversion
+ * to and from <b>java.net.URI</b> objects.
+ * <p>
+ * Can be configured to either return a <i>default value</i> or throw a
+ * <code>ConversionException</code> if a conversion error occurs.
+ *
+ * @since 2.0
+ */
+public final class URIConverter extends AbstractConverter {
+
+ /**
+ * Construct a <b>java.net.URI</b> <i>Converter</i> that throws
+ * a <code>ConversionException</code> if an error occurs.
+ */
+ public URIConverter() {
+ super();
+ }
+
+ /**
+ * Construct a <b>java.net.URI</b> <i>Converter</i> that returns
+ * a default value if an error occurs.
+ *
+ * @param defaultValue The default value to be returned
+ * if the value to be converted is missing or an error
+ * occurs converting the value.
+ */
+ public URIConverter(final Object defaultValue) {
+ super(defaultValue);
+ }
+
+ /**
+ * Return the default type this <code>Converter</code> handles.
+ *
+ * @return The default type this <code>Converter</code> handles.
+ * @since 2.0
+ */
+ @Override
+ protected Class<?> getDefaultType() {
+ return URI.class;
+ }
+
+ /**
+ * <p>Convert a java.net.URI or object into a String.</p>
+ *
+ * @param <T> Target type of the conversion.
+ * @param type Data type to which this value should be converted.
+ * @param value The input value to be converted.
+ * @return The converted value.
+ * @throws Throwable if an error occurs converting to the specified type
+ * @since 2.0
+ */
+ @Override
+ protected <T> T convertToType(final Class<T> type, final Object value) throws Throwable {
+ if (URI.class.equals(type)) {
+ return type.cast(new URI(value.toString()));
+ }
+
+ throw conversionException(type, value);
+ }
+
+}
diff --git a/src/main/java/org/apache/commons/beanutils2/converters/UUIDConverter.java b/src/main/java/org/apache/commons/beanutils2/converters/UUIDConverter.java
new file mode 100644
index 0000000..415d49c
--- /dev/null
+++ b/src/main/java/org/apache/commons/beanutils2/converters/UUIDConverter.java
@@ -0,0 +1,82 @@
+/*
+ * 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.beanutils2.converters;
+
+import java.util.UUID;
+
+/**
+ * {@link org.apache.commons.beanutils2.Converter} implementation that handles conversion
+ * to and from <b>java.util.UUID</b> objects.
+ * <p>
+ * Can be configured to either return a <i>default value</i> or throw a
+ * <code>ConversionException</code> if a conversion error occurs.
+ *
+ * @since 2.0
+ */
+public final class UUIDConverter extends AbstractConverter {
+
+ /**
+ * Construct a <b>java.util.UUID</b> <i>Converter</i> that throws
+ * a <code>ConversionException</code> if an error occurs.
+ */
+ public UUIDConverter() {
+ super();
+ }
+
+ /**
+ * Construct a <b>java.util.UUID</b> <i>Converter</i> that returns
+ * a default value if an error occurs.
+ *
+ * @param defaultValue The default value to be returned
+ * if the value to be converted is missing or an error
+ * occurs converting the value.
+ */
+ public UUIDConverter(final Object defaultValue) {
+ super(defaultValue);
+ }
+
+ /**
+ * Return the default type this <code>Converter</code> handles.
+ *
+ * @return The default type this <code>Converter</code> handles.
+ * @since 2.0
+ */
+ @Override
+ protected Class<?> getDefaultType() {
+ return UUID.class;
+ }
+
+ /**
+ * <p>Convert a java.util.UUID or object into a String.</p>
+ *
+ * @param <T> Target type of the conversion.
+ * @param type Data type to which this value should be converted.
+ * @param value The input value to be converted.
+ * @return The converted value.
+ * @throws Throwable if an error occurs converting to the specified type
+ * @since 2.0
+ */
+ @Override
+ protected <T> T convertToType(final Class<T> type, final Object value) throws Throwable {
+ if (UUID.class.equals(type)) {
+ return type.cast(UUID.fromString(String.valueOf(value)));
+ }
+
+ throw conversionException(type, value);
+ }
+
+}
diff --git a/src/test/java/org/apache/commons/beanutils2/converters/PathConverterTestCase.java b/src/test/java/org/apache/commons/beanutils2/converters/PathConverterTestCase.java
new file mode 100644
index 0000000..5c53e60
--- /dev/null
+++ b/src/test/java/org/apache/commons/beanutils2/converters/PathConverterTestCase.java
@@ -0,0 +1,122 @@
+/*
+ * 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.beanutils2.converters;
+
+
+
+import java.io.File;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import org.apache.commons.beanutils2.ConversionException;
+import org.apache.commons.beanutils2.Converter;
+
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+
+/**
+ * Test Case for the UUIDConverter class.
+ *
+ */
+public class PathConverterTestCase extends TestCase {
+
+ private Converter converter = null;
+
+ // ------------------------------------------------------------------------
+
+ public PathConverterTestCase(final String name) {
+ super(name);
+ }
+
+ // ------------------------------------------------------------------------
+
+ @Override
+ public void setUp() throws Exception {
+ converter = makeConverter();
+ }
+
+ public static TestSuite suite() {
+ return new TestSuite(PathConverterTestCase.class);
+ }
+
+ @Override
+ public void tearDown() throws Exception {
+ converter = null;
+ }
+
+ // ------------------------------------------------------------------------
+
+ protected Converter makeConverter() {
+ return new PathConverter();
+ }
+
+ protected Class<?> getExpectedType() {
+ return Path.class;
+ }
+
+ // ------------------------------------------------------------------------
+
+ public void testSimpleConversion() throws Exception {
+ final String[] message= {
+ "from String",
+ "from String",
+ "from String",
+ "from String",
+ "from String",
+ "from String",
+ "from String",
+ "from String",
+ };
+
+ // get the separator so test works on Windows or *nix
+ String separator = File.pathSeparator;
+
+ final Object[] input = {
+ separator + "foo"+separator+"bar"+separator+"baz",
+ separator
+ };
+
+ final Path[] expected = {
+ Paths.get(separator + "foo"+separator+"bar"+separator+"baz"),
+ Paths.get(separator)
+ };
+
+ for(int i=0;i<expected.length;i++) {
+ assertEquals(message[i] + " to URI",expected[i],converter.convert(Path.class,input[i]));
+ assertEquals(message[i] + " to null type",expected[i],converter.convert(null,input[i]));
+ }
+
+ for(int i=0;i<expected.length;i++) {
+ assertEquals(input[i] + " to String", input[i], converter.convert(String.class, expected[i]));
+ }
+ }
+
+ /**
+ * Tests a conversion to an unsupported type.
+ */
+ public void testUnsupportedType() {
+ try {
+ converter.convert(Integer.class, "http://www.apache.org");
+ fail("Unsupported type could be converted!");
+ } catch (final ConversionException cex) {
+ // expected result
+ }
+ }
+}
+
diff --git a/src/test/java/org/apache/commons/beanutils2/converters/URIConverterTestCase.java b/src/test/java/org/apache/commons/beanutils2/converters/URIConverterTestCase.java
new file mode 100644
index 0000000..faaef5a
--- /dev/null
+++ b/src/test/java/org/apache/commons/beanutils2/converters/URIConverterTestCase.java
@@ -0,0 +1,127 @@
+/*
+ * 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.beanutils2.converters;
+
+import java.net.URI;
+
+import org.apache.commons.beanutils2.ConversionException;
+import org.apache.commons.beanutils2.Converter;
+
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+
+/**
+ * Test Case for the URIConverter class.
+ *
+ */
+public class URIConverterTestCase extends TestCase {
+
+ private Converter converter = null;
+
+ // ------------------------------------------------------------------------
+
+ public URIConverterTestCase(final String name) {
+ super(name);
+ }
+
+ // ------------------------------------------------------------------------
+
+ @Override
+ public void setUp() throws Exception {
+ converter = makeConverter();
+ }
+
+ public static TestSuite suite() {
+ return new TestSuite(URIConverterTestCase.class);
+ }
+
+ @Override
+ public void tearDown() throws Exception {
+ converter = null;
+ }
+
+ // ------------------------------------------------------------------------
+
+ protected Converter makeConverter() {
+ return new URIConverter();
+ }
+
+ protected Class<?> getExpectedType() {
+ return URI.class;
+ }
+
+ // ------------------------------------------------------------------------
+
+ public void testSimpleConversion() throws Exception {
+ final String[] message= {
+ "from String",
+ "from String",
+ "from String",
+ "from String",
+ "from String",
+ "from String",
+ "from String",
+ "from String",
+ };
+
+ final Object[] input = {
+ "http://www.apache.org",
+ "http://www.apache.org/",
+ "ftp://cvs.apache.org",
+ "file://project.xml",
+ "http://208.185.179.12",
+ "http://www.apache.org:9999/test/thing",
+ "http://user:admin@www.apache.org:50/one/two.three",
+ "http://notreal.apache.org",
+ };
+
+ final URI[] expected = {
+ new URI("http://www.apache.org"),
+ new URI("http://www.apache.org/"),
+ new URI("ftp://cvs.apache.org"),
+ new URI("file://project.xml"),
+ new URI("http://208.185.179.12"),
+ new URI("http://www.apache.org:9999/test/thing"),
+ new URI("http://user:admin@www.apache.org:50/one/two.three"),
+ new URI("http://notreal.apache.org")
+ };
+
+ for(int i=0;i<expected.length;i++) {
+ assertEquals(message[i] + " to URI",expected[i],converter.convert(URI.class,input[i]));
+ assertEquals(message[i] + " to null type",expected[i],converter.convert(null,input[i]));
+ }
+
+ for(int i=0;i<expected.length;i++) {
+ assertEquals(input[i] + " to String", input[i], converter.convert(String.class, expected[i]));
+ }
+ }
+
+ /**
+ * Tests a conversion to an unsupported type.
+ */
+ public void testUnsupportedType() {
+ try {
+ converter.convert(Integer.class, "http://www.apache.org");
+ fail("Unsupported type could be converted!");
+ } catch (final ConversionException cex) {
+ // expected result
+ }
+ }
+}
+
diff --git a/src/test/java/org/apache/commons/beanutils2/converters/UUIDConverterTestCase.java b/src/test/java/org/apache/commons/beanutils2/converters/UUIDConverterTestCase.java
new file mode 100644
index 0000000..019b2fe
--- /dev/null
+++ b/src/test/java/org/apache/commons/beanutils2/converters/UUIDConverterTestCase.java
@@ -0,0 +1,116 @@
+/*
+ * 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.beanutils2.converters;
+
+
+import java.util.UUID;
+
+import org.apache.commons.beanutils2.ConversionException;
+import org.apache.commons.beanutils2.Converter;
+
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+
+/**
+ * Test Case for the UUIDConverter class.
+ *
+ */
+public class UUIDConverterTestCase extends TestCase {
+
+ private Converter converter = null;
+
+ // ------------------------------------------------------------------------
+
+ public UUIDConverterTestCase(final String name) {
+ super(name);
+ }
+
+ // ------------------------------------------------------------------------
+
+ @Override
+ public void setUp() throws Exception {
+ converter = makeConverter();
+ }
+
+ public static TestSuite suite() {
+ return new TestSuite(UUIDConverterTestCase.class);
+ }
+
+ @Override
+ public void tearDown() throws Exception {
+ converter = null;
+ }
+
+ // ------------------------------------------------------------------------
+
+ protected Converter makeConverter() {
+ return new UUIDConverter();
+ }
+
+ protected Class<?> getExpectedType() {
+ return UUID.class;
+ }
+
+ // ------------------------------------------------------------------------
+
+ public void testSimpleConversion() throws Exception {
+ final String[] message= {
+ "from String",
+ "from String",
+ "from String",
+ "from String",
+ "from String",
+ "from String",
+ "from String",
+ "from String",
+ };
+
+ final Object[] input = {
+ "123e4567-e89b-12d3-a456-556642440000",
+ "7dc53df5-703e-49b3-8670-b1c468f47f1f"
+ };
+
+ final UUID[] expected = {
+ UUID.fromString("123e4567-e89b-12d3-a456-556642440000"),
+ UUID.fromString("7dc53df5-703e-49b3-8670-b1c468f47f1f")
+ };
+
+ for(int i=0;i<expected.length;i++) {
+ assertEquals(message[i] + " to URI",expected[i],converter.convert(UUID.class,input[i]));
+ assertEquals(message[i] + " to null type",expected[i],converter.convert(null,input[i]));
+ }
+
+ for(int i=0;i<expected.length;i++) {
+ assertEquals(input[i] + " to String", input[i], converter.convert(String.class, expected[i]));
+ }
+ }
+
+ /**
+ * Tests a conversion to an unsupported type.
+ */
+ public void testUnsupportedType() {
+ try {
+ converter.convert(Integer.class, "http://www.apache.org");
+ fail("Unsupported type could be converted!");
+ } catch (final ConversionException cex) {
+ // expected result
+ }
+ }
+}
+