You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tamaya.apache.org by pl...@apache.org on 2015/04/13 21:21:06 UTC

[2/6] incubator-tamaya git commit: TAMAYA-72 Added converter for DateTimeZone

TAMAYA-72 Added converter for DateTimeZone


Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/d42c5e75
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/d42c5e75
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/d42c5e75

Branch: refs/heads/master
Commit: d42c5e75d58cc0a3727f506f7cb4101bd279d689
Parents: bffd9ea
Author: Oliver B. Fischer <pl...@apache.org>
Authored: Mon Apr 6 08:40:52 2015 +0200
Committer: Oliver B. Fischer <pl...@apache.org>
Committed: Mon Apr 6 08:40:52 2015 +0200

----------------------------------------------------------------------
 sandbox/jodatime/pom.xml                        |  1 +
 .../tamaya/jodatime/DateTimeZoneConverter.java  | 57 ++++++++++++++++++++
 .../tamaya/jodatime/DateTimeConverterTest.java  |  6 +--
 .../jodatime/DateTimeZoneConverterTest.java     | 57 ++++++++++++++++++++
 4 files changed, 116 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d42c5e75/sandbox/jodatime/pom.xml
----------------------------------------------------------------------
diff --git a/sandbox/jodatime/pom.xml b/sandbox/jodatime/pom.xml
index c6a729a..08448ab 100644
--- a/sandbox/jodatime/pom.xml
+++ b/sandbox/jodatime/pom.xml
@@ -30,6 +30,7 @@ under the License.
     </parent>
 
     <artifactId>tamaya-jodatime</artifactId>
+    <name>Apache Tamaya Joda-Time Support</name>
 
     <inceptionYear>2015</inceptionYear>
 

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d42c5e75/sandbox/jodatime/src/main/java/org/apache/tamaya/jodatime/DateTimeZoneConverter.java
----------------------------------------------------------------------
diff --git a/sandbox/jodatime/src/main/java/org/apache/tamaya/jodatime/DateTimeZoneConverter.java b/sandbox/jodatime/src/main/java/org/apache/tamaya/jodatime/DateTimeZoneConverter.java
new file mode 100644
index 0000000..09daebb
--- /dev/null
+++ b/sandbox/jodatime/src/main/java/org/apache/tamaya/jodatime/DateTimeZoneConverter.java
@@ -0,0 +1,57 @@
+/*
+ * 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.tamaya.jodatime;
+
+import org.apache.tamaya.PropertyConverter;
+import org.joda.time.DateTimeZone;
+
+import java.util.regex.Pattern;
+
+import static java.util.Objects.requireNonNull;
+
+public class DateTimeZoneConverter implements PropertyConverter<DateTimeZone> {
+    private static final Pattern IS_INTEGER_VALUE = Pattern.compile("(\\+|-)?\\d+");
+
+    @Override
+    public DateTimeZone convert(String value) {
+        String trimmed = requireNonNull(value).trim();
+
+        DateTimeZone result = null;
+
+        try {
+            if (isSingleIntegerValue(trimmed)) {
+                int offset = Integer.parseInt(trimmed);
+                result = DateTimeZone.forOffsetHours(offset);
+            } else { // Let us assume a string id
+                result = DateTimeZone.forID(trimmed);
+            }
+
+        } catch (Exception e) {
+            result = null; // Give the next converter a change. Read the JavaDoc of convert
+        }
+
+        return result;
+    }
+
+    private boolean isSingleIntegerValue(String value) {
+        boolean match = IS_INTEGER_VALUE.matcher(value).matches();
+
+        return match;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d42c5e75/sandbox/jodatime/src/test/java/org/apache/tamaya/jodatime/DateTimeConverterTest.java
----------------------------------------------------------------------
diff --git a/sandbox/jodatime/src/test/java/org/apache/tamaya/jodatime/DateTimeConverterTest.java b/sandbox/jodatime/src/test/java/org/apache/tamaya/jodatime/DateTimeConverterTest.java
index 084a202..6a1803a 100644
--- a/sandbox/jodatime/src/test/java/org/apache/tamaya/jodatime/DateTimeConverterTest.java
+++ b/sandbox/jodatime/src/test/java/org/apache/tamaya/jodatime/DateTimeConverterTest.java
@@ -73,11 +73,7 @@ public class DateTimeConverterTest {
     @Test
     public void invalidInputValuesResultInReturningNull() {
         String[] inputValues = {
-             "2007-08-01T12:34:45.000+0:0",
-             "2007-08-01T12:34:45.000+00:0",
-             "2007-08-01T12:34:45.000+00:0",
-             "2007-08-01T+00:00",
-             "2007-08-01+00:00"
+             "00:00", "a", "-", "+ :00", "+00:"
         };
 
         for (String input : inputValues) {

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/d42c5e75/sandbox/jodatime/src/test/java/org/apache/tamaya/jodatime/DateTimeZoneConverterTest.java
----------------------------------------------------------------------
diff --git a/sandbox/jodatime/src/test/java/org/apache/tamaya/jodatime/DateTimeZoneConverterTest.java b/sandbox/jodatime/src/test/java/org/apache/tamaya/jodatime/DateTimeZoneConverterTest.java
index 8a083b8..0077c25 100644
--- a/sandbox/jodatime/src/test/java/org/apache/tamaya/jodatime/DateTimeZoneConverterTest.java
+++ b/sandbox/jodatime/src/test/java/org/apache/tamaya/jodatime/DateTimeZoneConverterTest.java
@@ -18,5 +18,62 @@
  */
 package org.apache.tamaya.jodatime;
 
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.fail;
+
 public class DateTimeZoneConverterTest {
+    private DateTimeZoneConverter converter = new DateTimeZoneConverter();
+
+    @Test
+    public void canConvertDateTimeZoneInformation() {
+        Object[][] inputResultPairs = {
+             // Valid input for DateTimeZone.forOffsetHours
+             {"1", DateTimeZone.forOffsetHours(1)},
+             {"12", DateTimeZone.forOffsetHours(12)},
+             {"13", DateTimeZone.forOffsetHours(13)},
+             {"0", DateTimeZone.forOffsetHours(0)},
+             {"-1 ", DateTimeZone.forOffsetHours(-1)},
+
+             // Valid input for DateTimeZone.forID()
+             {"Chile/EasterIsland", DateTimeZone.forID("Chile/EasterIsland")},
+             {"UTC", DateTimeZone.forID("UTC")},
+             {"+00", DateTimeZone.forID("+00:00")},
+             {"+00:00", DateTimeZone.forID("+00:00")},
+             {"+00:00 ", DateTimeZone.forID("+00:00")},
+             {" +00:00 ", DateTimeZone.forID("+00:00")},
+             {"+04:00", DateTimeZone.forID("+04:00")},
+        };
+
+        for (Object[] pair : inputResultPairs) {
+            DateTimeZone zone = converter.convert((String) pair[0]);
+
+            assertThat("Converter failed to convert input value " + pair[0], zone, notNullValue());
+            assertThat(zone, equalTo((DateTimeZone)pair[1]));
+        }
+    }
+
+    @Test
+    public void invalidInputValuesResultInReturningNull() {
+        String[] inputValues = {
+             "2007-08-01T12:34:45.000+0:0",
+             "2007-08-01T12:34:45.000+00:0",
+             "2007-08-01T12:34:45.000+00:0",
+             "2007-08-01T+00:00",
+             "2007-08-01+00:00"
+        };
+
+        for (String input : inputValues) {
+            DateTimeZone date = converter.convert(input);
+
+            assertThat(date, nullValue());
+        }
+    }
 }