forked from pgjdbc/r2dbc-postgresql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding support for MonthDay data type - pgjdbcgh-591
- Loading branch information
1 parent
986ca22
commit f77d357
Showing
3 changed files
with
130 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
31 changes: 31 additions & 0 deletions
31
src/main/java/io/r2dbc/postgresql/codec/MonthDayCodec.java
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,31 @@ | ||
/* | ||
* Copyright 2017 the original author or authors. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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 io.r2dbc.postgresql.codec; | ||
|
||
import io.netty.buffer.ByteBufAllocator; | ||
|
||
import java.time.MonthDay; | ||
import java.time.YearMonth; | ||
|
||
import static java.time.temporal.ChronoField.PROLEPTIC_MONTH; | ||
|
||
final class MonthDayCodec extends StringCodecDelegate<MonthDay> { | ||
|
||
MonthDayCodec(ByteBufAllocator byteBufAllocator) { | ||
super(MonthDay.class, byteBufAllocator, MonthDay::toString, MonthDay::parse); | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
src/test/java/io/r2dbc/postgresql/codec/MonthDayCodecTest.java
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,98 @@ | ||
package io.r2dbc.postgresql.codec; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.r2dbc.postgresql.client.EncodedParameter; | ||
import io.r2dbc.postgresql.client.ParameterAssert; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.nio.charset.Charset; | ||
import java.time.MonthDay; | ||
import java.time.format.DateTimeParseException; | ||
|
||
import static io.r2dbc.postgresql.client.EncodedParameter.NULL_VALUE; | ||
import static io.r2dbc.postgresql.codec.PostgresqlObjectId.BPCHAR; | ||
import static io.r2dbc.postgresql.codec.PostgresqlObjectId.CHAR; | ||
import static io.r2dbc.postgresql.codec.PostgresqlObjectId.NAME; | ||
import static io.r2dbc.postgresql.codec.PostgresqlObjectId.TEXT; | ||
import static io.r2dbc.postgresql.codec.PostgresqlObjectId.VARCHAR; | ||
import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT; | ||
import static io.r2dbc.postgresql.util.TestByteBufAllocator.TEST; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; | ||
|
||
class MonthDayCodecTest { | ||
|
||
@Test | ||
void constructorNoByteBufAllocator() { | ||
assertThatIllegalArgumentException().isThrownBy(() -> new MonthDayCodec(null)) | ||
.withMessage("byteBufAllocator must not be null"); | ||
} | ||
|
||
@Test | ||
void decode() { | ||
final MonthDay monthDay = MonthDay.now(); | ||
|
||
final ByteBuf buffer = TEST.buffer(); | ||
|
||
final int charsWritten = buffer.writeCharSequence(monthDay.toString(), Charset.defaultCharset()); | ||
assertThat(charsWritten).isEqualTo(monthDay.toString().length()); | ||
|
||
assertThat(new MonthDayCodec(TEST) | ||
.decode(buffer, VARCHAR, FORMAT_TEXT, MonthDay.class)) | ||
.isEqualTo(monthDay); | ||
} | ||
|
||
@Test | ||
void decodeJunkString() { | ||
final String junkString = "hello world"; | ||
final ByteBuf buffer = TEST.buffer(); | ||
|
||
final int charsWritten = buffer.writeCharSequence(junkString, Charset.defaultCharset()); | ||
assertThat(charsWritten).isEqualTo(junkString.length()); | ||
|
||
assertThatExceptionOfType(DateTimeParseException.class) | ||
.isThrownBy(() -> new MonthDayCodec(TEST).decode(buffer, VARCHAR, FORMAT_TEXT, MonthDay.class)); | ||
} | ||
|
||
@Test | ||
void decodeNoByteBuf() { | ||
assertThat(new MonthDayCodec(TEST).decode(null, VARCHAR.getObjectId(), FORMAT_TEXT, MonthDay.class)).isNull(); | ||
} | ||
|
||
@Test | ||
void doCanDecode() { | ||
MonthDayCodec codec = new MonthDayCodec(TEST); | ||
|
||
assertThat(codec.doCanDecode(VARCHAR, FORMAT_TEXT)).isTrue(); | ||
assertThat(codec.doCanDecode(CHAR, FORMAT_TEXT)).isTrue(); | ||
assertThat(codec.doCanDecode(BPCHAR, FORMAT_TEXT)).isTrue(); | ||
assertThat(codec.doCanDecode(NAME, FORMAT_TEXT)).isTrue(); | ||
assertThat(codec.doCanDecode(TEXT, FORMAT_TEXT)).isTrue(); | ||
} | ||
|
||
@Test | ||
void doCanDecodeNoType() { | ||
assertThatIllegalArgumentException().isThrownBy(() -> new MonthDayCodec(TEST).doCanDecode(null, FORMAT_TEXT)) | ||
.withMessage("type must not be null"); | ||
} | ||
|
||
@Test | ||
void doEncodeNoValue() { | ||
assertThatIllegalArgumentException().isThrownBy(() -> new MonthDayCodec(TEST).doEncode(null)) | ||
.withMessage("value must not be null"); | ||
} | ||
|
||
@Test | ||
void encodeItemNoValue() { | ||
assertThatIllegalArgumentException().isThrownBy(() -> new MonthDayCodec(TEST).encode(null)) | ||
.withMessage("value must not be null"); | ||
} | ||
|
||
@Test | ||
void encodeNull() { | ||
ParameterAssert.assertThat(new MonthDayCodec(TEST).encodeNull()) | ||
.isEqualTo(new EncodedParameter(FORMAT_TEXT, VARCHAR.getObjectId(), NULL_VALUE)); | ||
} | ||
|
||
} |