kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | bc95e11c0ebe32598a444b05960e8f740f5eb6b099edeb5d45fbb49a24efdbe9 |
|---|---|
| Author | Alexandre Dutra <adutra@users.noreply.github.com> · 2016-07-15 19:17 +02:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2016-07-15 10:17 -07:00 |
| Parents | 87e7552e1ac5 |
5 files changed
modified changelog/README.md +1 −0
| @@ -6,6 +6,7 @@ | ||
| 6 | 6 | - [improvement] JAVA-743: Add JSON support to QueryBuilder. |
| 7 | 7 | - [improvement] JAVA-1233: Update HdrHistogram to 2.1.9. |
| 8 | 8 | - [improvement] JAVA-1233: Update Snappy to 1.1.2.6. |
| 9 | +- [bug] JAVA-1161: Preserve full time zone info in ZonedDateTimeCodec and DateTimeCodec. | |
| 9 | 10 | |
| 10 | 11 | Merged from 3.0.x branch: |
| 11 | 12 |
modified driver-extras/src/main/java/com/datastax/driver/extras/codecs/jdk8/ZonedDateTimeCodec.java +79 −36
| @@ -34,14 +34,19 @@ | ||
| 34 | 34 | * Since Cassandra's <code>timestamp</code> type preserves only |
| 35 | 35 | * milliseconds since epoch, any timezone information |
| 36 | 36 | * would normally be lost. By using a |
| 37 | - * <code>tuple<timestamp,varchar></code> a timezone ID can be | |
| 37 | + * <code>tuple<timestamp,varchar></code> a timezone can be | |
| 38 | 38 | * persisted in the <code>varchar</code> field such that when the |
| 39 | - * value is deserialized the timezone is | |
| 40 | - * preserved. | |
| 41 | - * <p/> | |
| 42 | - * <strong>IMPORTANT</strong>: this codec's {@link #format(Object) format} method formats | |
| 43 | - * timestamps using an ISO-8601 format that includes milliseconds. | |
| 44 | - * <strong>This format is incompatible with Cassandra versions < 2.0.9.</strong> | |
| 39 | + * value is deserialized the timezone is preserved. | |
| 40 | + * <p> | |
| 41 | + * <strong>IMPORTANT</strong> | |
| 42 | + * <p> | |
| 43 | + * 1) The default timestamp formatter used by this codec produces CQL literals | |
| 44 | + * that may include milliseconds. | |
| 45 | + * <strong>This literal format is incompatible with Cassandra < 2.0.9.</strong> | |
| 46 | + * <p> | |
| 47 | + * 2) Even if the ISO-8601 standard accepts timestamps with nanosecond precision, | |
| 48 | + * Cassandra timestamps have millisecond precision; therefore, any sub-millisecond | |
| 49 | + * value set on a {@link java.time.ZonedDateTime} will be lost when persisted to Cassandra. | |
| 45 | 50 | * |
| 46 | 51 | * @see <a href="https://cassandra.apache.org/doc/cql3/CQL-2.2.html#usingtimestamps">'Working with timestamps' section of CQL specification</a> |
| 47 | 52 | */ |
| @@ -50,36 +55,74 @@ | ||
| 50 | 55 | public class ZonedDateTimeCodec extends TypeCodec.AbstractTupleCodec<java.time.ZonedDateTime> { |
| 51 | 56 | |
| 52 | 57 | /** |
| 53 | - * A {@link java.time.format.DateTimeFormatter} that parses (most) of | |
| 58 | + * The default {@link java.time.format.DateTimeFormatter} that parses (most) of | |
| 54 | 59 | * the ISO formats accepted in CQL. |
| 55 | 60 | */ |
| 56 | - private static final java.time.format.DateTimeFormatter FORMATTER = new java.time.format.DateTimeFormatterBuilder() | |
| 57 | - .parseCaseSensitive() | |
| 58 | - .parseStrict() | |
| 59 | - .append(java.time.format.DateTimeFormatter.ISO_LOCAL_DATE) | |
| 60 | - .optionalStart() | |
| 61 | - .appendLiteral('T') | |
| 62 | - .appendValue(java.time.temporal.ChronoField.HOUR_OF_DAY, 2) | |
| 63 | - .appendLiteral(':') | |
| 64 | - .appendValue(java.time.temporal.ChronoField.MINUTE_OF_HOUR, 2) | |
| 65 | - .optionalEnd() | |
| 66 | - .optionalStart() | |
| 67 | - .appendLiteral(':') | |
| 68 | - .appendValue(java.time.temporal.ChronoField.SECOND_OF_MINUTE, 2) | |
| 69 | - .optionalEnd() | |
| 70 | - .optionalStart() | |
| 71 | - .appendFraction(java.time.temporal.ChronoField.NANO_OF_SECOND, 0, 9, true) | |
| 72 | - .optionalEnd() | |
| 73 | - .optionalStart() | |
| 61 | + private static final java.time.format.DateTimeFormatter DEFAULT_DATE_TIME_FORMATTER = java.time.format.DateTimeFormatter.ISO_DATE_TIME.withZone(java.time.ZoneOffset.UTC); | |
| 62 | + | |
| 63 | + /** | |
| 64 | + * The default {@link java.time.format.DateTimeFormatter} to parse and format zones. | |
| 65 | + * It will use a time-zone ID, such as {@code Europe/Paris}, or an offset, such as {@code +02:00}, | |
| 66 | + * depending on the best available information. | |
| 67 | + */ | |
| 68 | + private static final java.time.format.DateTimeFormatter DEFAULT_ZONE_FORMATTER = new java.time.format.DateTimeFormatterBuilder() | |
| 74 | 69 | .appendZoneOrOffsetId() |
| 75 | - .optionalEnd() | |
| 76 | - .toFormatter() | |
| 77 | - .withZone(java.time.ZoneOffset.UTC); | |
| 70 | + .toFormatter(); | |
| 78 | 71 | |
| 79 | - private static final java.time.format.DateTimeFormatter ZONE_FORMATTER = java.time.format.DateTimeFormatter.ofPattern("xxx"); | |
| 72 | + private final java.time.format.DateTimeFormatter dateTimeFormatter; | |
| 80 | 73 | |
| 74 | + private final java.time.format.DateTimeFormatter zoneFormatter; | |
| 75 | + | |
| 76 | + /** | |
| 77 | + * Creates a new {@link ZonedDateTimeCodec} for the given tuple | |
| 78 | + * and with default {@link java.time.format.DateTimeFormatter formatters} for | |
| 79 | + * both the timestamp and the zone components. | |
| 80 | + * <p> | |
| 81 | + * The default formatters produce and parse CQL timestamp literals of the following form: | |
| 82 | + * <ol> | |
| 83 | + * <li>Timestamp component: an ISO-8601 full date and time pattern, including at least: year, | |
| 84 | + * month, day, hour and minutes, and optionally, seconds and milliseconds, | |
| 85 | + * followed by the zone ID {@code Z} (UTC), | |
| 86 | + * e.g. {@code 2010-06-30T02:01Z} or {@code 2010-06-30T01:20:47.999Z}; | |
| 87 | + * note that timestamp components are always expressed in UTC time, hence the zone ID {@code Z}.</li> | |
| 88 | + * <li>Zone component: a zone offset such as {@code -07:00}, or a zone ID such as {@code UTC} or {@code Europe/Paris}, | |
| 89 | + * depending on what information is available.</li> | |
| 90 | + * </ol> | |
| 91 | + * | |
| 92 | + * @param tupleType The tuple type this codec should handle. | |
| 93 | + * It must be a {@code tuple<timestamp,varchar>}. | |
| 94 | + * @throws IllegalArgumentException if the provided tuple type is not a {@code tuple<timestamp,varchar>}. | |
| 95 | + */ | |
| 81 | 96 | public ZonedDateTimeCodec(TupleType tupleType) { |
| 97 | + this(tupleType, DEFAULT_DATE_TIME_FORMATTER, DEFAULT_ZONE_FORMATTER); | |
| 98 | + } | |
| 99 | + | |
| 100 | + /** | |
| 101 | + * Creates a new {@link ZonedDateTimeCodec} for the given tuple | |
| 102 | + * and with the provided {@link java.time.format.DateTimeFormatter formatters} for | |
| 103 | + * the timestamp and the zone components of the tuple. | |
| 104 | + * <p> | |
| 105 | + * Use this constructor if you intend to customize the way the codec | |
| 106 | + * parses and formats timestamps and zones. Beware that Cassandra only accepts | |
| 107 | + * timestamp literals in some of the most common ISO-8601 formats; | |
| 108 | + * attempting to use non-standard formats could result in invalid CQL literals. | |
| 109 | + * | |
| 110 | + * @param tupleType The tuple type this codec should handle. | |
| 111 | + * It must be a {@code tuple<timestamp,varchar>}. | |
| 112 | + * @param dateTimeFormatter The {@link java.time.format.DateTimeFormatter DateTimeFormatter} to use | |
| 113 | + * to parse and format the timestamp component of the tuple. | |
| 114 | + * As a parser, it should be lenient enough to accept most of the ISO-8601 formats | |
| 115 | + * accepted by Cassandra as valid CQL literals. | |
| 116 | + * As a formatter, it should be configured to always format timestamps in UTC | |
| 117 | + * (see {@link java.time.format.DateTimeFormatter#withZone(java.time.ZoneId)}. | |
| 118 | + * @param zoneFormatter The {@link java.time.format.DateTimeFormatter DateTimeFormatter} to use | |
| 119 | + * to parse and format the zone component of the tuple. | |
| 120 | + * @throws IllegalArgumentException if the provided tuple type is not a {@code tuple<timestamp,varchar>}. | |
| 121 | + */ | |
| 122 | + public ZonedDateTimeCodec(TupleType tupleType, java.time.format.DateTimeFormatter dateTimeFormatter, java.time.format.DateTimeFormatter zoneFormatter) { | |
| 82 | 123 | super(tupleType, java.time.ZonedDateTime.class); |
| 124 | + this.dateTimeFormatter = dateTimeFormatter; | |
| 125 | + this.zoneFormatter = zoneFormatter; | |
| 83 | 126 | List<DataType> types = tupleType.getComponentTypes(); |
| 84 | 127 | checkArgument( |
| 85 | 128 | types.size() == 2 && types.get(0).equals(DataType.timestamp()) && types.get(1).equals(DataType.varchar()), |
| @@ -99,7 +142,7 @@ | ||
| 99 | 142 | return bigint().serializeNoBoxing(millis, protocolVersion); |
| 100 | 143 | } |
| 101 | 144 | if (index == 1) { |
| 102 | - return varchar().serialize(ZONE_FORMATTER.format(source.getOffset()), protocolVersion); | |
| 145 | + return varchar().serialize(zoneFormatter.format(source), protocolVersion); | |
| 103 | 146 | } |
| 104 | 147 | throw new IndexOutOfBoundsException("Tuple index out of bounds. " + index); |
| 105 | 148 | } |
| @@ -112,7 +155,7 @@ | ||
| 112 | 155 | } |
| 113 | 156 | if (index == 1) { |
| 114 | 157 | String zoneId = varchar().deserialize(input, protocolVersion); |
| 115 | - return target.withZoneSameInstant(java.time.ZoneId.of(zoneId)); | |
| 158 | + return target.withZoneSameInstant(zoneFormatter.parse(zoneId, java.time.temporal.TemporalQueries.zone())); | |
| 116 | 159 | } |
| 117 | 160 | throw new IndexOutOfBoundsException("Tuple index out of bounds. " + index); |
| 118 | 161 | } |
| @@ -120,10 +163,10 @@ | ||
| 120 | 163 | @Override |
| 121 | 164 | protected String formatField(java.time.ZonedDateTime value, int index) { |
| 122 | 165 | if (index == 0) { |
| 123 | - return quote(FORMATTER.format(value)); | |
| 166 | + return quote(dateTimeFormatter.format(value)); | |
| 124 | 167 | } |
| 125 | 168 | if (index == 1) { |
| 126 | - return quote(ZONE_FORMATTER.format(value.getOffset())); | |
| 169 | + return quote(zoneFormatter.format(value)); | |
| 127 | 170 | } |
| 128 | 171 | throw new IndexOutOfBoundsException("Tuple index out of bounds. " + index); |
| 129 | 172 | } |
| @@ -143,14 +186,14 @@ | ||
| 143 | 186 | } |
| 144 | 187 | } |
| 145 | 188 | try { |
| 146 | - return java.time.ZonedDateTime.from(FORMATTER.parse(input)); | |
| 189 | + return java.time.ZonedDateTime.from(dateTimeFormatter.parse(input)); | |
| 147 | 190 | } catch (java.time.format.DateTimeParseException e) { |
| 148 | 191 | throw new InvalidTypeException(String.format("Cannot parse timestamp value from \"%s\"", target)); |
| 149 | 192 | } |
| 150 | 193 | } |
| 151 | 194 | if (index == 1) { |
| 152 | 195 | String zoneId = varchar().parse(input); |
| 153 | - return target.withZoneSameInstant(java.time.ZoneId.of(zoneId)); | |
| 196 | + return target.withZoneSameInstant(zoneFormatter.parse(zoneId, java.time.temporal.TemporalQueries.zone())); | |
| 154 | 197 | } |
| 155 | 198 | throw new IndexOutOfBoundsException("Tuple index out of bounds. " + index); |
| 156 | 199 | } |
modified driver-extras/src/main/java/com/datastax/driver/extras/codecs/joda/DateTimeCodec.java +76 −19
| @@ -19,9 +19,7 @@ | ||
| 19 | 19 | import com.datastax.driver.core.exceptions.InvalidTypeException; |
| 20 | 20 | import org.joda.time.DateTime; |
| 21 | 21 | import org.joda.time.DateTimeZone; |
| 22 | -import org.joda.time.format.DateTimeFormat; | |
| 23 | 22 | import org.joda.time.format.DateTimeFormatter; |
| 24 | -import org.joda.time.format.DateTimeFormatterBuilder; | |
| 25 | 23 | import org.joda.time.format.ISODateTimeFormat; |
| 26 | 24 | |
| 27 | 25 | import java.nio.ByteBuffer; |
| @@ -30,7 +28,6 @@ | ||
| 30 | 28 | import static com.datastax.driver.core.ParseUtils.isLongLiteral; |
| 31 | 29 | import static com.datastax.driver.core.ParseUtils.quote; |
| 32 | 30 | import static com.google.common.base.Preconditions.checkArgument; |
| 33 | -import static org.joda.time.DateTimeZone.UTC; | |
| 34 | 31 | |
| 35 | 32 | /** |
| 36 | 33 | * {@link TypeCodec} that maps |
| @@ -47,7 +44,7 @@ | ||
| 47 | 44 | * preserved. |
| 48 | 45 | * <p/> |
| 49 | 46 | * <strong>IMPORTANT</strong>: this codec's {@link #format(Object) format} method formats |
| 50 | - * timestamps using an ISO-8601 format that includes nanoseconds. | |
| 47 | + * timestamps as CQL literal strings using an ISO-8601 format that includes milliseconds. | |
| 51 | 48 | * <strong>This format is incompatible with Cassandra versions < 2.0.9.</strong> |
| 52 | 49 | * |
| 53 | 50 | * @see <a href="https://cassandra.apache.org/doc/cql3/CQL-2.2.html#usingtimestamps">'Working with timestamps' section of CQL specification</a> |
| @@ -56,21 +53,78 @@ | ||
| 56 | 53 | |
| 57 | 54 | /** |
| 58 | 55 | * A {@link DateTimeFormatter} that parses (most) of |
| 59 | - * the ISO formats accepted in CQL. | |
| 56 | + * the ISO-8601 formats accepted in CQL. | |
| 60 | 57 | */ |
| 61 | - private static final DateTimeFormatter FORMATTER = new DateTimeFormatterBuilder() | |
| 62 | - .append(ISODateTimeFormat.dateOptionalTimeParser().getParser()) | |
| 63 | - .appendOptional( | |
| 64 | - new DateTimeFormatterBuilder() | |
| 65 | - .appendTimeZoneOffset("Z", true, 2, 4) | |
| 66 | - .toParser()) | |
| 67 | - .toFormatter() | |
| 68 | - .withZoneUTC(); | |
| 69 | - | |
| 70 | - private static final DateTimeFormatter ZONE_FORMATTER = DateTimeFormat.forPattern("ZZ"); | |
| 58 | + private static final DateTimeFormatter DEFAULT_PARSER = ISODateTimeFormat.dateOptionalTimeParser(); | |
| 71 | 59 | |
| 60 | + /** | |
| 61 | + * A {@link DateTimeFormatter} that prints timestamps | |
| 62 | + * with a full ISO-8601 date and time format, including the time zone (Z). | |
| 63 | + */ | |
| 64 | + private static final DateTimeFormatter DEFFAULT_PRINTER = ISODateTimeFormat.dateTime().withZoneUTC(); | |
| 65 | + | |
| 66 | + private final DateTimeFormatter parser; | |
| 67 | + | |
| 68 | + private final DateTimeFormatter printer; | |
| 69 | + | |
| 70 | + /** | |
| 71 | + * Creates a new {@link DateTimeCodec} for the given tuple, | |
| 72 | + * using a default parser and a default printer to handle | |
| 73 | + * the timestamp component of the tuple. | |
| 74 | + * <p> | |
| 75 | + * The default formatter and printer produce and parse CQL timestamp literals of the following form: | |
| 76 | + * <ol> | |
| 77 | + * <li>The printer will always produce a full ISO-8601 date and time pattern, including year, | |
| 78 | + * month, day, hour, minutes, seconds and milliseconds, | |
| 79 | + * followed by the zone ID {@code Z} (UTC), e.g. {@code 2010-06-30T01:20:47.999Z}; | |
| 80 | + * note that timestamp components are always printed in UTC time, hence the zone ID {@code Z}.</li> | |
| 81 | + * <li>The parser accepts most ISO-8601 date and time patterns, the time part (minutes, seconds, milliseconds) being optional.</li> | |
| 82 | + * </ol> | |
| 83 | + * <p> | |
| 84 | + * Note that it is not possible to customize the parsing and printing of | |
| 85 | + * the zone component of the tuple. This codec prints either a zone offset such as {@code -07:00}, | |
| 86 | + * or a zone ID such as {@code UTC} or {@code Europe/Paris}, | |
| 87 | + * depending on what is the best information is available. | |
| 88 | + * | |
| 89 | + * @param tupleType The tuple type this codec should handle. | |
| 90 | + * It must be a {@code tuple<timestamp,varchar>}. | |
| 91 | + * @throws IllegalArgumentException if the provided tuple type is not a {@code tuple<timestamp,varchar>}. | |
| 92 | + */ | |
| 72 | 93 | public DateTimeCodec(TupleType tupleType) { |
| 94 | + this(tupleType, DEFAULT_PARSER, DEFFAULT_PRINTER); | |
| 95 | + } | |
| 96 | + | |
| 97 | + /** | |
| 98 | + * Creates a new {@link DateTimeCodec} for the given tuple, | |
| 99 | + * using the provided {@link DateTimeFormatter parser} and {@link DateTimeFormatter printer} | |
| 100 | + * to format and print the timestamp component of the tuple. | |
| 101 | + * <p> | |
| 102 | + * Use this constructor if you intend to customize the way the codec | |
| 103 | + * parses and formats timestamps. Beware that Cassandra only accepts | |
| 104 | + * timestamp literals in some of the most common ISO-8601 formats; | |
| 105 | + * attempting to use non-standard formats could result in invalid CQL literals. | |
| 106 | + * <p> | |
| 107 | + * Note that it is not possible to customize the parsing and printing of | |
| 108 | + * the zone component of the tuple. This codec prints either a zone offset such as {@code -07:00}, | |
| 109 | + * or a zone ID such as {@code UTC} or {@code Europe/Paris}, | |
| 110 | + * depending on what information is available. | |
| 111 | + * | |
| 112 | + * @param tupleType The tuple type this codec should handle. | |
| 113 | + * It must be a {@code tuple<timestamp,varchar>}. | |
| 114 | + * @param parser The {@link DateTimeFormatter parser} to use | |
| 115 | + * to parse the timestamp component of the tuple. | |
| 116 | + * It should be lenient enough to accept most of the ISO-8601 formats | |
| 117 | + * accepted by Cassandra as valid CQL literals. | |
| 118 | + * @param printer The {@link DateTimeFormatter printer} to use | |
| 119 | + * to format the timestamp component of the tuple. | |
| 120 | + * This printer should be configured to always format timestamps in UTC | |
| 121 | + * (see {@link DateTimeFormatter#withZoneUTC()}. | |
| 122 | + * @throws IllegalArgumentException if the provided tuple type is not a {@code tuple<timestamp,varchar>}. | |
| 123 | + */ | |
| 124 | + public DateTimeCodec(TupleType tupleType, DateTimeFormatter parser, DateTimeFormatter printer) { | |
| 73 | 125 | super(tupleType, DateTime.class); |
| 126 | + this.parser = parser; | |
| 127 | + this.printer = printer; | |
| 74 | 128 | List<DataType> types = tupleType.getComponentTypes(); |
| 75 | 129 | checkArgument( |
| 76 | 130 | types.size() == 2 && types.get(0).equals(DataType.timestamp()) && types.get(1).equals(DataType.varchar()), |
| @@ -90,7 +144,7 @@ | ||
| 90 | 144 | return bigint().serializeNoBoxing(millis, protocolVersion); |
| 91 | 145 | } |
| 92 | 146 | if (index == 1) { |
| 93 | - return varchar().serialize(ZONE_FORMATTER.print(source), protocolVersion); | |
| 147 | + return varchar().serialize(source.getZone().getID(), protocolVersion); | |
| 94 | 148 | } |
| 95 | 149 | throw new IndexOutOfBoundsException("Tuple index out of bounds. " + index); |
| 96 | 150 | } |
| @@ -111,10 +165,10 @@ | ||
| 111 | 165 | @Override |
| 112 | 166 | protected String formatField(DateTime value, int index) { |
| 113 | 167 | if (index == 0) { |
| 114 | - return quote(value.withZone(UTC).toString()); | |
| 168 | + return quote(printer.print(value)); | |
| 115 | 169 | } |
| 116 | 170 | if (index == 1) { |
| 117 | - return quote(ZONE_FORMATTER.print(value)); | |
| 171 | + return quote(value.getZone().getID()); | |
| 118 | 172 | } |
| 119 | 173 | throw new IndexOutOfBoundsException("Tuple index out of bounds. " + index); |
| 120 | 174 | } |
| @@ -134,13 +188,16 @@ | ||
| 134 | 188 | } |
| 135 | 189 | } |
| 136 | 190 | try { |
| 137 | - return FORMATTER.parseDateTime(input); | |
| 191 | + return parser.parseDateTime(input); | |
| 138 | 192 | } catch (RuntimeException e) { |
| 139 | 193 | throw new InvalidTypeException(String.format("Cannot parse timestamp value from \"%s\"", target)); |
| 140 | 194 | } |
| 141 | 195 | } |
| 142 | 196 | if (index == 1) { |
| 143 | 197 | String zoneId = varchar().parse(input); |
| 198 | + // Joda time does not recognize "Z" | |
| 199 | + if ("Z".equals(zoneId)) | |
| 200 | + return target.withZone(DateTimeZone.UTC); | |
| 144 | 201 | return target.withZone(DateTimeZone.forID(zoneId)); |
| 145 | 202 | } |
| 146 | 203 | throw new IndexOutOfBoundsException("Tuple index out of bounds. " + index); |
modified driver-extras/src/test/java/com/datastax/driver/extras/codecs/jdk8/ZonedDateTimeCodecTest.java +40 −23
| @@ -16,57 +16,75 @@ | ||
| 16 | 16 | package com.datastax.driver.extras.codecs.jdk8; |
| 17 | 17 | |
| 18 | 18 | import com.datastax.driver.core.Assertions; |
| 19 | +import com.datastax.driver.core.CodecRegistry; | |
| 20 | +import com.datastax.driver.core.ProtocolVersion; | |
| 19 | 21 | import com.datastax.driver.core.TupleType; |
| 20 | 22 | import org.testng.annotations.DataProvider; |
| 21 | 23 | import org.testng.annotations.Test; |
| 22 | 24 | |
| 23 | -import java.time.ZoneOffset; | |
| 24 | 25 | import java.time.ZonedDateTime; |
| 25 | 26 | |
| 26 | 27 | import static com.datastax.driver.core.DataType.timestamp; |
| 27 | 28 | import static com.datastax.driver.core.DataType.varchar; |
| 28 | 29 | import static com.datastax.driver.core.ProtocolVersion.V4; |
| 29 | -import static com.google.common.collect.Lists.newArrayList; | |
| 30 | -import static java.time.Instant.ofEpochMilli; | |
| 31 | -import static java.time.Instant.parse; | |
| 32 | -import static java.time.ZoneOffset.UTC; | |
| 33 | -import static java.time.ZonedDateTime.ofInstant; | |
| 30 | +import static java.time.ZonedDateTime.parse; | |
| 34 | 31 | import static org.assertj.core.api.Assertions.assertThat; |
| 35 | -import static org.mockito.Mockito.mock; | |
| 36 | -import static org.mockito.Mockito.when; | |
| 37 | 32 | |
| 33 | +@SuppressWarnings("Since15") | |
| 38 | 34 | public class ZonedDateTimeCodecTest { |
| 39 | 35 | |
| 40 | - private final TupleType tupleType = mock(TupleType.class); | |
| 36 | + private final TupleType tupleType = TupleType.of(ProtocolVersion.V4, CodecRegistry.DEFAULT_INSTANCE, timestamp(), varchar()); | |
| 41 | 37 | |
| 42 | 38 | @DataProvider(name = "ZonedDateTimeCodecTest.parse") |
| 43 | 39 | public Object[][] parseParameters() { |
| 44 | 40 | return new Object[][]{ |
| 45 | - {null, null}, | |
| 46 | - {"", null}, | |
| 47 | - {"NULL", null}, | |
| 48 | - {"(0 ,'+01:00')", ofInstant(ofEpochMilli(0), ZoneOffset.of("+01:00"))}, | |
| 49 | - {"(1277860847999 ,'+01:00')", ofInstant(parse("2010-06-30T01:20:47.999Z"), ZoneOffset.of("+01:00"))}, | |
| 50 | - {"('2010-06-30T01:20Z' ,'+01:00')", ofInstant(parse("2010-06-30T01:20:00.000Z"), ZoneOffset.of("+01:00"))}, | |
| 51 | - {"('2010-06-30T01:20:47Z' ,'+01:00')", ofInstant(parse("2010-06-30T01:20:47.000Z"), ZoneOffset.of("+01:00"))}, | |
| 52 | - {"('2010-06-30T01:20:47.999Z','+01:00')", ofInstant(parse("2010-06-30T01:20:47.999Z"), ZoneOffset.of("+01:00"))} | |
| 41 | + //@formatter:off | |
| 42 | + {null , null}, | |
| 43 | + {"" , null}, | |
| 44 | + {"NULL" , null}, | |
| 45 | + // timestamps as milliseconds since the Epoch, offsets without zone id | |
| 46 | + {"(0 ,'+00:00')" , parse("1970-01-01T00:00:00.000+00:00")}, | |
| 47 | + {"(0 ,'+01:00')" , parse("1970-01-01T01:00:00.000+01:00")}, | |
| 48 | + {"(1277860847999 ,'+01:00')" , parse("2010-06-30T02:20:47.999+01:00")}, | |
| 49 | + // timestamps as valid CQL literals with different precisions, offsets without zone id | |
| 50 | + {"('2010-06-30T01:20Z' ,'+01:00')" , parse("2010-06-30T02:20:00.000+01:00")}, | |
| 51 | + {"('2010-06-30T01:20:47Z' ,'+01:00')" , parse("2010-06-30T02:20:47.000+01:00")}, | |
| 52 | + {"('2010-06-30T01:20:47.999Z','+01:00')" , parse("2010-06-30T02:20:47.999+01:00")}, | |
| 53 | + // zone ids with different precisions | |
| 54 | + {"('2016-04-06T19:01Z' ,'Z')" , parse("2016-04-06T19:01:00.000+00:00[Z]")}, | |
| 55 | + {"('2016-04-06T19:01Z' ,'UTC')" , parse("2016-04-06T19:01:00.000+00:00[UTC]")}, | |
| 56 | + {"('2016-04-06T19:01Z' ,'GMT')" , parse("2016-04-06T19:01:00.000+00:00[GMT]")}, | |
| 57 | + {"('2016-04-06T19:01Z' ,'Etc/GMT')" , parse("2016-04-06T19:01:00.000+00:00[Etc/GMT]")}, | |
| 58 | + {"('2016-04-06T19:01Z' ,'Asia/Vientiane')" , parse("2016-04-07T02:01:00.000+07:00[Asia/Vientiane]")}, | |
| 59 | + {"('2016-04-06T19:01:32Z' ,'Asia/Vientiane')" , parse("2016-04-07T02:01:32.000+07:00[Asia/Vientiane]")}, | |
| 60 | + {"('2016-04-06T19:01:32.999Z','Asia/Vientiane')" , parse("2016-04-07T02:01:32.999+07:00[Asia/Vientiane]")} | |
| 61 | + //@formatter:on | |
| 53 | 62 | }; |
| 54 | 63 | } |
| 55 | 64 | |
| 56 | 65 | @DataProvider(name = "ZonedDateTimeCodecTest.format") |
| 57 | 66 | public Object[][] formatParameters() { |
| 58 | 67 | return new Object[][]{ |
| 59 | - {null, "NULL"}, | |
| 60 | - {ofInstant(ofEpochMilli(0), UTC), "('1970-01-01T00:00:00Z','+00:00')"}, | |
| 61 | - {ZonedDateTime.parse("2010-06-30T01:20:47.999+01:00"), "('2010-06-30T00:20:47.999Z','+01:00')"} | |
| 68 | + //@formatter:off | |
| 69 | + {null , "NULL"}, | |
| 70 | + {parse("1970-01-01T00:00Z") , "('1970-01-01T00:00:00Z','Z')"}, | |
| 71 | + {parse("1970-01-01T00:00:00Z") , "('1970-01-01T00:00:00Z','Z')"}, | |
| 72 | + {parse("1970-01-01T00:00:00.000Z") , "('1970-01-01T00:00:00Z','Z')"}, | |
| 73 | + {parse("2010-06-30T01:20+01:00") , "('2010-06-30T00:20:00Z','+01:00')"}, | |
| 74 | + {parse("2010-06-30T01:20:47+01:00") , "('2010-06-30T00:20:47Z','+01:00')"}, | |
| 75 | + {parse("2010-06-30T01:20:47+01:00") , "('2010-06-30T00:20:47Z','+01:00')"}, | |
| 76 | + {parse("2010-06-30T01:20:47.999+01:00") , "('2010-06-30T00:20:47.999Z','+01:00')"}, | |
| 77 | + {parse("2010-06-30T01:20:47.999+00:00[UTC]") , "('2010-06-30T01:20:47.999Z','UTC')"}, | |
| 78 | + {parse("2010-06-30T01:20:47.999+00:00[GMT]") , "('2010-06-30T01:20:47.999Z','GMT')"}, | |
| 79 | + {parse("2010-06-30T01:20:47.999+00:00[Etc/GMT]"), "('2010-06-30T01:20:47.999Z','Etc/GMT')"}, | |
| 80 | + {parse("2016-04-07T02:01+07:00[Asia/Vientiane]"), "('2016-04-06T19:01:00Z','Asia/Vientiane')"} | |
| 81 | + //@formatter:on | |
| 62 | 82 | }; |
| 63 | 83 | } |
| 64 | 84 | |
| 65 | 85 | @Test(groups = "unit", dataProvider = "ZonedDateTimeCodecTest.parse") |
| 66 | 86 | public void should_parse_valid_formats(String input, ZonedDateTime expected) { |
| 67 | 87 | // given |
| 68 | - TupleType tupleType = mock(TupleType.class); | |
| 69 | - when(tupleType.getComponentTypes()).thenReturn(newArrayList(timestamp(), varchar())); | |
| 70 | 88 | ZonedDateTimeCodec codec = new ZonedDateTimeCodec(tupleType); |
| 71 | 89 | // when |
| 72 | 90 | ZonedDateTime actual = codec.parse(input); |
| @@ -77,7 +95,6 @@ | ||
| 77 | 95 | @Test(groups = "unit", dataProvider = "ZonedDateTimeCodecTest.format") |
| 78 | 96 | public void should_serialize_and_format_valid_object(ZonedDateTime input, String expected) { |
| 79 | 97 | // given |
| 80 | - when(tupleType.getComponentTypes()).thenReturn(newArrayList(timestamp(), varchar())); | |
| 81 | 98 | ZonedDateTimeCodec codec = new ZonedDateTimeCodec(tupleType); |
| 82 | 99 | // when |
| 83 | 100 | String actual = codec.format(input); |
modified driver-extras/src/test/java/com/datastax/driver/extras/codecs/joda/DateTimeCodecTest.java +35 −17
| @@ -16,6 +16,8 @@ | ||
| 16 | 16 | package com.datastax.driver.extras.codecs.joda; |
| 17 | 17 | |
| 18 | 18 | import com.datastax.driver.core.Assertions; |
| 19 | +import com.datastax.driver.core.CodecRegistry; | |
| 20 | +import com.datastax.driver.core.ProtocolVersion; | |
| 19 | 21 | import com.datastax.driver.core.TupleType; |
| 20 | 22 | import org.joda.time.DateTime; |
| 21 | 23 | import org.testng.annotations.DataProvider; |
| @@ -24,28 +26,37 @@ | ||
| 24 | 26 | import static com.datastax.driver.core.DataType.timestamp; |
| 25 | 27 | import static com.datastax.driver.core.DataType.varchar; |
| 26 | 28 | import static com.datastax.driver.core.ProtocolVersion.V4; |
| 27 | -import static com.google.common.collect.Lists.newArrayList; | |
| 28 | 29 | import static org.assertj.core.api.Assertions.assertThat; |
| 29 | -import static org.joda.time.DateTimeZone.UTC; | |
| 30 | 30 | import static org.joda.time.DateTimeZone.forID; |
| 31 | -import static org.mockito.Mockito.mock; | |
| 32 | -import static org.mockito.Mockito.when; | |
| 33 | 31 | |
| 34 | 32 | public class DateTimeCodecTest { |
| 35 | 33 | |
| 36 | - private final TupleType tupleType = mock(TupleType.class); | |
| 34 | + private final TupleType tupleType = TupleType.of(ProtocolVersion.V4, CodecRegistry.DEFAULT_INSTANCE, timestamp(), varchar()); | |
| 37 | 35 | |
| 38 | 36 | @DataProvider(name = "DateTimeCodecTest.parse") |
| 39 | 37 | public Object[][] parseParameters() { |
| 40 | 38 | return new Object[][]{ |
| 41 | - {null, null}, | |
| 42 | - {"", null}, | |
| 43 | - {"NULL", null}, | |
| 44 | - {"(0 ,'UTC')", new DateTime(0, forID("UTC"))}, | |
| 45 | - {"(1277860847999 ,'+01:00')", new DateTime("2010-06-30T02:20:47.999+01:00", forID("+01:00"))}, | |
| 46 | - {"('2010-06-30T01:20Z' ,'+01:00')", new DateTime("2010-06-30T02:20:00.000+01:00", forID("+01:00"))}, | |
| 47 | - {"('2010-06-30T01:20:47Z' ,'+01:00')", new DateTime("2010-06-30T02:20:47.000+01:00", forID("+01:00"))}, | |
| 48 | - {"('2010-06-30T01:20:47.999Z','+01:00')", new DateTime("2010-06-30T02:20:47.999+01:00", forID("+01:00"))} | |
| 39 | + //@formatter:off | |
| 40 | + {null , null}, | |
| 41 | + {"" , null}, | |
| 42 | + {"NULL" , null}, | |
| 43 | + // timestamps as milliseconds since the Epoch, offsets without zone id | |
| 44 | + {"(0 ,'+00:00')" , new DateTime("1970-01-01T00:00:00.000+00:00", forID("+00:00"))}, | |
| 45 | + {"(0 ,'+01:00')" , new DateTime("1970-01-01T01:00:00.000+01:00", forID("+01:00"))}, | |
| 46 | + {"(1277860847999 ,'+01:00')" , new DateTime("2010-06-30T02:20:47.999+01:00", forID("+01:00"))}, | |
| 47 | + // timestamps as valid CQL literals with different precisions, offsets without zone id | |
| 48 | + {"('2010-06-30T01:20Z' ,'+01:00')" , new DateTime("2010-06-30T02:20:00.000+01:00", forID("+01:00"))}, | |
| 49 | + {"('2010-06-30T01:20:47Z' ,'+01:00')" , new DateTime("2010-06-30T02:20:47.000+01:00", forID("+01:00"))}, | |
| 50 | + {"('2010-06-30T01:20:47.999Z','+01:00')" , new DateTime("2010-06-30T02:20:47.999+01:00", forID("+01:00"))}, | |
| 51 | + // zone ids with different precisions | |
| 52 | + {"('2016-04-06T19:01Z' ,'Z')" , new DateTime("2016-04-06T19:01:00.000+00:00", forID("UTC"))}, | |
| 53 | + {"('2016-04-06T19:01Z' ,'UTC')" , new DateTime("2016-04-06T19:01:00.000+00:00", forID("UTC"))}, | |
| 54 | + {"('2016-04-06T19:01Z' ,'GMT')" , new DateTime("2016-04-06T19:01:00.000+00:00", forID("GMT"))}, | |
| 55 | + {"('2016-04-06T19:01Z' ,'Etc/GMT')" , new DateTime("2016-04-06T19:01:00.000+00:00", forID("GMT"))}, | |
| 56 | + {"('2016-04-06T19:01Z' ,'Asia/Vientiane')" , new DateTime("2016-04-07T02:01:00.000+07:00", forID("Asia/Vientiane"))}, | |
| 57 | + {"('2016-04-06T19:01:32Z' ,'Asia/Vientiane')" , new DateTime("2016-04-07T02:01:32.000+07:00", forID("Asia/Vientiane"))}, | |
| 58 | + {"('2016-04-06T19:01:32.999Z','Asia/Vientiane')" , new DateTime("2016-04-07T02:01:32.999+07:00", forID("Asia/Vientiane"))}, | |
| 59 | + //@formatter:on | |
| 49 | 60 | }; |
| 50 | 61 | } |
| 51 | 62 | |
| @@ -53,15 +64,23 @@ | ||
| 53 | 64 | public Object[][] formatParameters() { |
| 54 | 65 | return new Object[][]{ |
| 55 | 66 | {null, "NULL"}, |
| 56 | - {new DateTime(0).withZone(UTC), "('1970-01-01T00:00:00.000Z','+00:00')"}, | |
| 57 | - {new DateTime("2010-06-30T01:20:47.999+01:00", forID("+01:00")), "('2010-06-30T00:20:47.999Z','+01:00')"} | |
| 67 | + //@formatter:off | |
| 68 | + {new DateTime("1970-01-01T00:00Z" , forID("+00:00")) , "('1970-01-01T00:00:00.000Z','UTC')"}, | |
| 69 | + {new DateTime("1970-01-01T00:00:00Z" , forID("+00:00")) , "('1970-01-01T00:00:00.000Z','UTC')"}, | |
| 70 | + {new DateTime("1970-01-01T00:00:00.000Z" , forID("+00:00")) , "('1970-01-01T00:00:00.000Z','UTC')"}, | |
| 71 | + {new DateTime("2010-06-30T01:20+01:00" , forID("+01:00")) , "('2010-06-30T00:20:00.000Z','+01:00')"}, | |
| 72 | + {new DateTime("2010-06-30T01:20:47+01:00" , forID("+01:00")) , "('2010-06-30T00:20:47.000Z','+01:00')"}, | |
| 73 | + {new DateTime("2010-06-30T01:20:47.999+01:00" , forID("+01:00")) , "('2010-06-30T00:20:47.999Z','+01:00')"}, | |
| 74 | + {new DateTime("2016-04-07T02:01Z" , forID("UTC")) , "('2016-04-07T02:01:00.000Z','UTC')"}, | |
| 75 | + {new DateTime("2016-04-07T02:01Z" , forID("GMT")) , "('2016-04-07T02:01:00.000Z','Etc/GMT')"}, | |
| 76 | + {new DateTime("2016-04-07T02:01+07:00" , forID("Asia/Vientiane")), "('2016-04-06T19:01:00.000Z','Asia/Vientiane')"} | |
| 77 | + //@formatter:on | |
| 58 | 78 | }; |
| 59 | 79 | } |
| 60 | 80 | |
| 61 | 81 | @Test(groups = "unit", dataProvider = "DateTimeCodecTest.parse") |
| 62 | 82 | public void should_parse_valid_formats(String input, DateTime expected) { |
| 63 | 83 | // given |
| 64 | - when(tupleType.getComponentTypes()).thenReturn(newArrayList(timestamp(), varchar())); | |
| 65 | 84 | DateTimeCodec codec = new DateTimeCodec(tupleType); |
| 66 | 85 | // when |
| 67 | 86 | DateTime actual = codec.parse(input); |
| @@ -72,7 +91,6 @@ | ||
| 72 | 91 | @Test(groups = "unit", dataProvider = "DateTimeCodecTest.format") |
| 73 | 92 | public void should_serialize_and_format_valid_object(DateTime input, String expected) { |
| 74 | 93 | // given |
| 75 | - when(tupleType.getComponentTypes()).thenReturn(newArrayList(timestamp(), varchar())); | |
| 76 | 94 | DateTimeCodec codec = new DateTimeCodec(tupleType); |
| 77 | 95 | // when |
| 78 | 96 | String actual = codec.format(input); |