From 0ddd0c841f719792325d15bf458d9d8d22667738 Mon Sep 17 00:00:00 2001 From: David Syer Date: Wed, 28 Apr 2010 13:45:45 +0000 Subject: [PATCH] Document and test UUIDConventer --- .../integration/util/UUIDConverter.java | 53 ++++++++------ .../integration/util/UUIDConverterTests.java | 70 +++++++++++++++++++ 2 files changed, 102 insertions(+), 21 deletions(-) create mode 100644 org.springframework.integration/src/test/java/org/springframework/integration/util/UUIDConverterTests.java diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/util/UUIDConverter.java b/org.springframework.integration/src/main/java/org/springframework/integration/util/UUIDConverter.java index cc27e7f44e..2012d4ac0d 100644 --- a/org.springframework.integration/src/main/java/org/springframework/integration/util/UUIDConverter.java +++ b/org.springframework.integration/src/main/java/org/springframework/integration/util/UUIDConverter.java @@ -28,31 +28,42 @@ import org.springframework.util.ClassUtils; * Utility to help generate UUID instances from generic objects. * * @author Dave Syer - * + * */ public class UUIDConverter implements Converter { public static final String DEFAULT_CHARSET = "UTF-8"; - private final String charset; - - public UUIDConverter() { - this(DEFAULT_CHARSET); - } - - public UUIDConverter(String charset) { - this.charset = charset; - } - + /** + * Convert the input to a UUID using the convenience method + * {@link #getUUID(Object)}. + * + * @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object) + */ public UUID convert(Object source) { - return getUUID(source, charset); + return getUUID(source); } + /** + * Convenient utility to convert an object to a UUID. If the input is + * + * If none of the above applies there will be an exception trying to serialize. + * + * @param input an Object + * @return a UUID constructed from the input + */ public static UUID getUUID(Object input) { - return getUUID(input, DEFAULT_CHARSET); - } - - public static UUID getUUID(Object input, String charset) { if (input == null) { return null; @@ -66,22 +77,22 @@ public class UUIDConverter implements Converter { try { return UUID.fromString((String) input); } - catch (IllegalArgumentException e) { + catch (Exception e) { try { - return UUID.nameUUIDFromBytes(((String) input).getBytes(charset)); + return UUID.nameUUIDFromBytes(((String) input).getBytes(DEFAULT_CHARSET)); } catch (UnsupportedEncodingException ex) { - throw new IllegalStateException("Cannot convert String using charset=" + charset, ex); + throw new IllegalStateException("Cannot convert String using charset=" + DEFAULT_CHARSET, ex); } } } if (ClassUtils.isPrimitiveOrWrapper(input.getClass())) { try { - return UUID.nameUUIDFromBytes(input.toString().getBytes(charset)); + return UUID.nameUUIDFromBytes(input.toString().getBytes(DEFAULT_CHARSET)); } catch (UnsupportedEncodingException e) { - throw new IllegalStateException("Cannot convert primitive using charset=" + charset, e); + throw new IllegalStateException("Cannot convert primitive using charset=" + DEFAULT_CHARSET, e); } } diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/util/UUIDConverterTests.java b/org.springframework.integration/src/test/java/org/springframework/integration/util/UUIDConverterTests.java new file mode 100644 index 0000000000..ac8b37c223 --- /dev/null +++ b/org.springframework.integration/src/test/java/org/springframework/integration/util/UUIDConverterTests.java @@ -0,0 +1,70 @@ +package org.springframework.integration.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.Date; +import java.util.UUID; + +import org.junit.Test; + +/** + * @author Dave Syer + * + */ +public class UUIDConverterTests { + + @Test + public void testConvertNull() throws Exception { + assertNull(UUIDConverter.getUUID(null)); + } + + @Test + public void testConvertUUID() throws Exception { + UUID uuid = UUID.randomUUID(); + assertEquals(uuid, UUIDConverter.getUUID(uuid)); + } + + @Test + public void testConvertUUIDString() throws Exception { + UUID uuid = UUID.randomUUID(); + assertEquals(uuid, UUIDConverter.getUUID(uuid.toString())); + } + + @Test + public void testConvertAlmostUUIDString() throws Exception { + String name = "1-2-3-4"; + try { + UUID.fromString(name); + fail(); + } catch (IllegalArgumentException e) { + String message = e.getMessage(); + assertTrue("Wrong message: "+message, message.contains("Invalid UUID string")); + } + assertNotNull(UUIDConverter.getUUID(name)); + } + + @Test + public void testConvertRandomString() throws Exception { + assertNotNull(UUIDConverter.getUUID("foo")); + } + + @Test + public void testConvertPrimitive() throws Exception { + assertNotNull(UUIDConverter.getUUID(1L)); + } + + @Test + public void testConvertSerializable() throws Exception { + assertNotNull(UUIDConverter.getUUID(new Date())); + } + + @Test(expected=IllegalArgumentException.class) + public void testConvertNonSerializable() throws Exception { + assertNotNull(UUIDConverter.getUUID(new Object())); + } + +}