Repository: cayenne
Updated Branches:
  refs/heads/master e616dc140 -> 63518b81c


CAY-2447 Added crypto support for java.time


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

Branch: refs/heads/master
Commit: 097276917ca17d8d114a7a47d0655e7f8e504ac1
Parents: e616dc1
Author: kkomyak <const1...@gmail.com>
Authored: Tue Aug 7 17:30:46 2018 +0300
Committer: kkomyak <const1...@gmail.com>
Committed: Wed Aug 8 09:58:17 2018 +0300

----------------------------------------------------------------------
 RELEASE-NOTES.txt                               |  1 +
 .../org/apache/cayenne/crypto/CryptoModule.java | 11 ++++++
 .../transformer/value/LocalDateConverter.java   | 28 +++++++++++++++
 .../value/LocalDateTimeConverter.java           | 37 ++++++++++++++++++++
 .../transformer/value/LocalTimeConverter.java   | 27 ++++++++++++++
 .../value/LocalDateConverterTest.java           | 27 ++++++++++++++
 .../value/LocalDateTimeConverterTest.java       | 29 +++++++++++++++
 .../value/LocalTimeConverterTest.java           | 28 +++++++++++++++
 8 files changed, 188 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/09727691/RELEASE-NOTES.txt
----------------------------------------------------------------------
diff --git a/RELEASE-NOTES.txt b/RELEASE-NOTES.txt
index 8db9264..fff3063 100644
--- a/RELEASE-NOTES.txt
+++ b/RELEASE-NOTES.txt
@@ -19,6 +19,7 @@ Bug Fixes:
 
 CAY-2444 Change URI from http:// to https:// in xsi:schemaLocation
 CAY-2445 Oracle: Problem with ExpressionFactory.notInExp()
+CAY-2447 Crypto support for LocalDateTime
 
 ----------------------------------
 Release: 4.1.M2

http://git-wip-us.apache.org/repos/asf/cayenne/blob/09727691/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/CryptoModule.java
----------------------------------------------------------------------
diff --git 
a/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/CryptoModule.java 
b/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/CryptoModule.java
index f12f22a..5c75ad5 100644
--- a/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/CryptoModule.java
+++ b/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/CryptoModule.java
@@ -45,6 +45,9 @@ import 
org.apache.cayenne.crypto.transformer.value.DoubleConverter;
 import org.apache.cayenne.crypto.transformer.value.FloatConverter;
 import org.apache.cayenne.crypto.transformer.value.IntegerConverter;
 import org.apache.cayenne.crypto.transformer.value.LazyValueTransformerFactory;
+import org.apache.cayenne.crypto.transformer.value.LocalDateConverter;
+import org.apache.cayenne.crypto.transformer.value.LocalDateTimeConverter;
+import org.apache.cayenne.crypto.transformer.value.LocalTimeConverter;
 import org.apache.cayenne.crypto.transformer.value.LongConverter;
 import org.apache.cayenne.crypto.transformer.value.ShortConverter;
 import org.apache.cayenne.crypto.transformer.value.Utf8StringConverter;
@@ -57,6 +60,9 @@ import org.apache.cayenne.di.Module;
 import java.math.BigDecimal;
 import java.math.BigInteger;
 import java.sql.Types;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
 import java.util.Date;
 
 /**
@@ -183,5 +189,10 @@ public class CryptoModule implements Module {
         mapBuilder.put(Date.class.getName(), UtilDateConverter.INSTANCE);
         mapBuilder.put(BigInteger.class.getName(), 
BigIntegerConverter.INSTANCE);
         mapBuilder.put(BigDecimal.class.getName(), 
BigDecimalConverter.INSTANCE);
+
+        mapBuilder.put(LocalDate.class.getName(), LocalDateConverter.INSTANCE);
+        mapBuilder.put(LocalTime.class.getName(), LocalTimeConverter.INSTANCE);
+        mapBuilder.put(LocalDateTime.class.getName(), 
LocalDateTimeConverter.INSTANCE);
+
     }
 }

http://git-wip-us.apache.org/repos/asf/cayenne/blob/09727691/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/LocalDateConverter.java
----------------------------------------------------------------------
diff --git 
a/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/LocalDateConverter.java
 
b/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/LocalDateConverter.java
new file mode 100644
index 0000000..d584034
--- /dev/null
+++ 
b/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/LocalDateConverter.java
@@ -0,0 +1,28 @@
+package org.apache.cayenne.crypto.transformer.value;
+
+import java.time.LocalDate;
+import java.util.Objects;
+
+/**
+ * @since 4.1.M3
+ */
+public class LocalDateConverter implements BytesConverter<LocalDate> {
+
+    public static final BytesConverter<LocalDate> INSTANCE = new 
LocalDateConverter(LongConverter.INSTANCE);
+
+    private BytesConverter<Long> longConverter;
+
+    public LocalDateConverter(BytesConverter<Long> longConverter) {
+        this.longConverter = Objects.requireNonNull(longConverter);
+    }
+
+    @Override
+    public LocalDate fromBytes(byte[] bytes) {
+        return LocalDate.ofEpochDay(longConverter.fromBytes(bytes));
+    }
+
+    @Override
+    public byte[] toBytes(LocalDate value) {
+        return longConverter.toBytes(value.toEpochDay());
+    }
+}

http://git-wip-us.apache.org/repos/asf/cayenne/blob/09727691/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/LocalDateTimeConverter.java
----------------------------------------------------------------------
diff --git 
a/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/LocalDateTimeConverter.java
 
b/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/LocalDateTimeConverter.java
new file mode 100644
index 0000000..9e98632
--- /dev/null
+++ 
b/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/LocalDateTimeConverter.java
@@ -0,0 +1,37 @@
+package org.apache.cayenne.crypto.transformer.value;
+
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.util.Objects;
+import java.util.TimeZone;
+
+/**
+ * @since 4.1.M3
+ */
+public class LocalDateTimeConverter implements BytesConverter<LocalDateTime> {
+    public static final BytesConverter<LocalDateTime> INSTANCE = new 
LocalDateTimeConverter(LongConverter.INSTANCE);
+
+    private BytesConverter<Long> longConverter;
+
+    public LocalDateTimeConverter(BytesConverter<Long> longConverter) {
+        this.longConverter = Objects.requireNonNull(longConverter);
+    }
+
+
+    @Override
+    public LocalDateTime fromBytes(byte[] bytes) {
+
+        return 
LocalDateTime.ofInstant(Instant.ofEpochMilli(longConverter.fromBytes(bytes)),
+                        TimeZone.getDefault().toZoneId());
+    }
+
+
+    @Override
+    public byte[] toBytes(LocalDateTime value) {
+
+        long epochMilli = 
value.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
+
+        return longConverter.toBytes(epochMilli);
+    }
+}

http://git-wip-us.apache.org/repos/asf/cayenne/blob/09727691/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/LocalTimeConverter.java
----------------------------------------------------------------------
diff --git 
a/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/LocalTimeConverter.java
 
b/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/LocalTimeConverter.java
new file mode 100644
index 0000000..a1fa9bf
--- /dev/null
+++ 
b/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/LocalTimeConverter.java
@@ -0,0 +1,27 @@
+package org.apache.cayenne.crypto.transformer.value;
+
+import java.time.LocalTime;
+import java.util.Objects;
+
+/**
+ * @since 4.1.M3
+ */
+public class LocalTimeConverter implements BytesConverter<LocalTime> {
+    public static final BytesConverter<LocalTime> INSTANCE = new 
LocalTimeConverter(LongConverter.INSTANCE);
+
+    private BytesConverter<Long> longConverter;
+
+    public LocalTimeConverter(BytesConverter<Long> longConverter) {
+        this.longConverter = Objects.requireNonNull(longConverter);
+    }
+
+    @Override
+    public LocalTime fromBytes(byte[] bytes) {
+        return LocalTime.ofNanoOfDay(longConverter.fromBytes(bytes));
+    }
+
+    @Override
+    public byte[] toBytes(LocalTime value) {
+        return longConverter.toBytes(value.toNanoOfDay());
+    }
+}

http://git-wip-us.apache.org/repos/asf/cayenne/blob/09727691/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/LocalDateConverterTest.java
----------------------------------------------------------------------
diff --git 
a/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/LocalDateConverterTest.java
 
b/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/LocalDateConverterTest.java
new file mode 100644
index 0000000..9b5458a
--- /dev/null
+++ 
b/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/LocalDateConverterTest.java
@@ -0,0 +1,27 @@
+package org.apache.cayenne.crypto.transformer.value;
+
+import org.junit.Test;
+
+import java.text.ParseException;
+import java.time.LocalDate;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+public class LocalDateConverterTest {
+
+    private LocalDate localDate(String dateString) {
+        return LocalDate.parse(dateString);
+    }
+
+    @Test
+    public void testFromBytes() throws ParseException {
+        assertEquals(localDate("2015-01-07"), 
LocalDateConverter.INSTANCE.fromBytes(new byte[]{64, 58}));
+    }
+
+    @Test
+    public void testToBytes() throws ParseException {
+        assertArrayEquals(new byte[]{64, 58},
+                LocalDateConverter.INSTANCE.toBytes(localDate("2015-01-07")));
+    }
+}

http://git-wip-us.apache.org/repos/asf/cayenne/blob/09727691/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/LocalDateTimeConverterTest.java
----------------------------------------------------------------------
diff --git 
a/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/LocalDateTimeConverterTest.java
 
b/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/LocalDateTimeConverterTest.java
new file mode 100644
index 0000000..9b0e5f2
--- /dev/null
+++ 
b/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/LocalDateTimeConverterTest.java
@@ -0,0 +1,29 @@
+package org.apache.cayenne.crypto.transformer.value;
+
+import org.junit.Test;
+
+import java.text.ParseException;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+public class LocalDateTimeConverterTest {
+    private LocalDateTime localDate(String dateString) {
+
+        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd 
HH:mm:ss");
+        return LocalDateTime.parse(dateString, formatter);
+    }
+
+    @Test
+    public void testFromBytes() throws ParseException {
+        assertEquals(localDate("2015-01-07 11:00:02"), 
LocalDateTimeConverter.INSTANCE.fromBytes(new byte[]{0, 0, 1, 74, -61, 104, 83, 
-48}));
+    }
+
+    @Test
+    public void testToBytes() throws ParseException {
+        assertArrayEquals(new byte[]{0, 0, 1, 74, -61, 104, 83, -48},
+                LocalDateTimeConverter.INSTANCE.toBytes(localDate("2015-01-07 
11:00:02")));
+    }
+}

http://git-wip-us.apache.org/repos/asf/cayenne/blob/09727691/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/LocalTimeConverterTest.java
----------------------------------------------------------------------
diff --git 
a/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/LocalTimeConverterTest.java
 
b/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/LocalTimeConverterTest.java
new file mode 100644
index 0000000..4743217
--- /dev/null
+++ 
b/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/LocalTimeConverterTest.java
@@ -0,0 +1,28 @@
+package org.apache.cayenne.crypto.transformer.value;
+
+import org.junit.Test;
+
+import java.text.ParseException;
+import java.time.LocalTime;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+public class LocalTimeConverterTest {
+
+    private LocalTime localTime(String dateString) {
+        return LocalTime.parse(dateString);
+    }
+
+    @Test
+    public void testFromBytes() throws ParseException {
+        assertEquals(localTime("11:00:02"), 
LocalTimeConverter.INSTANCE.fromBytes(new byte[]{0, 0, 36, 4, -113, 36, 116, 
0}));
+    }
+
+    @Test
+    public void testToBytes() throws ParseException {
+        assertArrayEquals(new byte[]{0, 0, 36, 4, -113, 36, 116, 0},
+                LocalTimeConverter.INSTANCE.toBytes(localTime("11:00:02")));
+    }
+
+}

Reply via email to