qpid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kw...@apache.org
Subject svn commit: r1738935 - in /qpid/java/trunk/broker-core/src: main/java/org/apache/qpid/server/model/AttributeValueConverter.java test/java/org/apache/qpid/server/model/AttributeValueConverterTest.java
Date Wed, 13 Apr 2016 13:11:41 GMT
Author: kwall
Date: Wed Apr 13 13:11:41 2016
New Revision: 1738935

URL: http://svn.apache.org/viewvc?rev=1738935&view=rev
Log:
QPID-7158: [Java Broker] Support attribute values of type java.util.Date

Modified:
    qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/AttributeValueConverter.java
    qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/model/AttributeValueConverterTest.java

Modified: qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/AttributeValueConverter.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/AttributeValueConverter.java?rev=1738935&r1=1738934&r2=1738935&view=diff
==============================================================================
--- qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/AttributeValueConverter.java
(original)
+++ qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/AttributeValueConverter.java
Wed Apr 13 13:11:41 2016
@@ -36,6 +36,7 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.LinkedHashMap;
@@ -507,6 +508,53 @@ abstract class AttributeValueConverter<T
 
     };
 
+    static final AttributeValueConverter<Date> DATE_CONVERTER = new AttributeValueConverter<Date>()
+    {
+
+        @Override
+        public Date convert(final Object value, final ConfiguredObject object)
+        {
+            if(value instanceof Date)
+            {
+                return (Date) value;
+            }
+            else if(value instanceof Number)
+            {
+                return new Date(((Number) value).longValue());
+            }
+            else if(value instanceof String)
+            {
+                String interpolated = AbstractConfiguredObject.interpolate(object, (String)
value);
+
+                try
+                {
+                    return new Date(Long.valueOf(interpolated));
+                }
+                catch(NumberFormatException e)
+                {
+                    try
+                    {
+                        return DatatypeConverter.parseDateTime(interpolated).getTime();
+                    }
+                    catch (IllegalArgumentException e1)
+                    {
+                        throw new IllegalArgumentException("Cannot convert string '" + interpolated
+ "' to a Date."
+                                                           + " It is neither a ISO-8601 date
or date time nor a string"
+                                                           + " containing a numeric value.");
+                    }
+                }
+            }
+            else if(value == null)
+            {
+                return null;
+            }
+            else
+            {
+                throw new IllegalArgumentException("Cannot convert type " + value.getClass()
+ " to a Date");
+            }
+        }
+    };
+
     private static <T> T convertFromJson(final String value, final ConfiguredObject
object, final Class<T> valueType)
     {
         String interpolated = AbstractConfiguredObject.interpolate(object, value);
@@ -551,6 +599,10 @@ abstract class AttributeValueConverter<T
         {
             return (AttributeValueConverter<X>) BOOLEAN_CONVERTER;
         }
+        else if(type == Date.class)
+        {
+            return (AttributeValueConverter<X>) DATE_CONVERTER;
+        }
         else if(type == UUID.class)
         {
             return (AttributeValueConverter<X>) UUID_CONVERTER;

Modified: qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/model/AttributeValueConverterTest.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/model/AttributeValueConverterTest.java?rev=1738935&r1=1738934&r2=1738935&view=diff
==============================================================================
--- qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/model/AttributeValueConverterTest.java
(original)
+++ qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/model/AttributeValueConverterTest.java
Wed Apr 13 13:11:41 2016
@@ -32,11 +32,13 @@ import java.security.cert.Certificate;
 import java.security.cert.X509Certificate;
 import java.text.ParseException;
 import java.util.Collection;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.TimeUnit;
 
 import org.apache.qpid.server.model.testmodels.hierarchy.TestModel;
 import org.apache.qpid.server.model.testmodels.hierarchy.TestCar;
@@ -94,6 +96,40 @@ public class AttributeValueConverterTest
         }
     }
 
+    public void testDateConverter() throws Exception
+    {
+        final long nowMillis = System.currentTimeMillis();
+        final Date now = new Date(nowMillis);
+
+        ConfiguredObject object = _objectFactory.create(TestCar.class, _attributes);
+
+        AttributeValueConverter<Date> converter = getConverter(Date.class, Date.class);
+
+        assertNull("Cannot convert null", converter.convert(null, object));
+
+        assertEquals("Cannot convert date expressed as Date", now, converter.convert(now,
object));
+
+        assertEquals("Cannot convert date expressed as Number", new Date(nowMillis), converter.convert(nowMillis,
object));
+        assertEquals("Cannot convert date expressed as String containing Number", new Date(nowMillis),
converter.convert(""+nowMillis, object));
+
+        final String iso8601DateTime = "1970-01-01T00:00:01Z";
+        assertEquals("Cannot convert date expressed as ISO8601 date time", new Date(1000),
converter.convert(iso8601DateTime, object));
+
+        final String iso8601Date = "1970-01-02Z";
+        assertEquals("Cannot convert date expressed as ISO8601 date", new Date(TimeUnit.MILLISECONDS.convert(1,
TimeUnit.DAYS)), converter.convert(iso8601Date, object));
+
+        try
+        {
+            converter.convert("elephant", object);
+            fail("Exception not thrown");
+        }
+        catch (IllegalArgumentException e)
+        {
+            // PASS
+        }
+
+    }
+
     public void testNonGenericCollectionConverter()
     {
         _context.put("simpleCollection", "[\"a\", \"b\"]");



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org


Mime
View raw message