Skip to content

Commit

Permalink
Merge pull request #168 from anshlykov/gh-118
Browse files Browse the repository at this point in the history
Add OffsetTime codec
  • Loading branch information
gregturn committed Jun 9, 2021
2 parents 7e9d60e + 6713726 commit 4863ba0
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/java/io/r2dbc/h2/codecs/DefaultCodecs.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ static List<Codec<?>> createCodecs(Client client, ClassLoader classLoader, Codec
new LocalTimeCodec(),
new LongCodec(),
new OffsetDateTimeCodec(client),
new OffsetTimeCodec(client),
new ShortCodec(),
new StringCodec(),
new UuidCodec(),
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/io/r2dbc/h2/codecs/OffsetTimeCodec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.r2dbc.h2.codecs;

import io.r2dbc.h2.client.Client;
import io.r2dbc.h2.util.Assert;
import org.h2.engine.CastDataProvider;
import org.h2.util.JSR310Utils;
import org.h2.value.Value;

import java.time.OffsetTime;

final class OffsetTimeCodec extends AbstractCodec<OffsetTime> {

private final Client client;

OffsetTimeCodec(Client client) {
super(OffsetTime.class);
this.client = client;
}

@Override
boolean doCanDecode(int dataType) {
return dataType == Value.TIME_TZ;
}

@Override
OffsetTime doDecode(Value value, Class<? extends OffsetTime> type) {
Assert.requireType(this.client.getSession(), CastDataProvider.class, "The session must implement CastDataProvider.");
return (OffsetTime) JSR310Utils.valueToOffsetTime(value, (CastDataProvider) this.client.getSession());
}

@Override
Value doEncode(OffsetTime value) {
return JSR310Utils.offsetTimeToValue(Assert.requireNonNull(value, "value must not be null"));
}
}
6 changes: 6 additions & 0 deletions src/test/java/io/r2dbc/h2/codecs/CodecIntegrationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;
Expand Down Expand Up @@ -294,6 +295,11 @@ void shouldEncodeZonedDateTimeAsTimestampWithTimeZone() {
testType(connection, "TIMESTAMP(1) WITH TIME ZONE", value, value.toOffsetDateTime());
}

@Test
void shouldEncodeOffsetTimeAsTimeWithTimeZone() {
testType(connection, "TIME WITH TIME ZONE", OffsetTime.parse("10:20:30+04:50"));
}

@Test
void shouldDecodeScalarNull() {
createTable(connection, "VARCHAR");
Expand Down
66 changes: 66 additions & 0 deletions src/test/java/io/r2dbc/h2/codecs/OffsetTimeCodecTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package io.r2dbc.h2.codecs;

import io.r2dbc.h2.client.Client;
import org.h2.engine.Session;
import org.h2.value.Value;
import org.h2.value.ValueNull;
import org.h2.value.ValueTimeTimeZone;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.time.OffsetTime;
import java.time.ZoneOffset;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

final class OffsetTimeCodecTest {

private Client client;

@BeforeEach
void setUp() {
this.client = mock(Client.class);

when(this.client.getSession()).thenReturn(mock(Session.class));
}

@Test
void decode() {
assertThat(new OffsetTimeCodec(client).decode(ValueTimeTimeZone.parse("23:59:59.999999999Z"), OffsetTime.class))
.isEqualTo(OffsetTime.of(23, 59, 59, 999999999, ZoneOffset.UTC));
assertThat(new OffsetTimeCodec(client).decode(ValueTimeTimeZone.parse("10:20:30+02"), OffsetTime.class))
.isEqualTo(OffsetTime.of(10, 20, 30, 0, ZoneOffset.ofHours(2)));
}

@Test
void doCanDecode() {
OffsetTimeCodec codec = new OffsetTimeCodec(client);

assertThat(codec.doCanDecode(Value.TIME_TZ)).isTrue();
assertThat(codec.doCanDecode(Value.TIME)).isFalse();
assertThat(codec.doCanDecode(Value.UNKNOWN)).isFalse();
assertThat(codec.doCanDecode(Value.INT)).isFalse();
}

@Test
void doEncode() {
assertThat(new OffsetTimeCodec(client).doEncode(OffsetTime.of(23, 59, 59, 999999999, ZoneOffset.UTC)))
.isEqualTo(ValueTimeTimeZone.parse("23:59:59.999999999Z"));
assertThat(new OffsetTimeCodec(client).doEncode(OffsetTime.of(10, 20, 30, 0, ZoneOffset.ofHours(2))))
.isEqualTo(ValueTimeTimeZone.parse("10:20:30+02"));
}

@Test
void doEncodeNoValue() {
assertThatIllegalArgumentException().isThrownBy(() -> new OffsetTimeCodec(client).doEncode(null))
.withMessage("value must not be null");
}

@Test
void encodeNull() {
assertThat(new OffsetTimeCodec(client).encodeNull()).isEqualTo(ValueNull.INSTANCE);
}
}

0 comments on commit 4863ba0

Please sign in to comment.