-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add OffsetTime codec
- Loading branch information
Showing
4 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |