forked from pgjdbc/r2dbc-postgresql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongCodecUnitTests.java
118 lines (98 loc) · 4.22 KB
/
LongCodecUnitTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* 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.r2dbc.postgresql.client.EncodedParameter;
import org.junit.jupiter.api.Test;
import static io.r2dbc.postgresql.client.EncodedParameter.NULL_VALUE;
import static io.r2dbc.postgresql.client.ParameterAssert.assertThat;
import static io.r2dbc.postgresql.codec.PostgresqlObjectId.*;
import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY;
import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT;
import static io.r2dbc.postgresql.util.ByteBufUtils.encode;
import static io.r2dbc.postgresql.util.TestByteBufAllocator.TEST;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* Unit tests for {@link LongCodec}.
*/
final class LongCodecUnitTests {
private static final int dataType = INT8.getObjectId();
@Test
void constructorNoByteBufAllocator() {
assertThatIllegalArgumentException().isThrownBy(() -> new LongCodec(null))
.withMessage("byteBufAllocator must not be null");
}
@Test
void decode() {
LongCodec codec = new LongCodec(TEST);
assertThat(codec.decode(TEST.buffer(8).writeLong(100L), dataType, FORMAT_BINARY, Long.class)).isEqualTo(100L);
assertThat(codec.decode(encode(TEST, "100"), dataType, FORMAT_TEXT, Long.class)).isEqualTo(100L);
/* According to documentation: "The oid type is currently implemented as an unsigned four-byte integer"
* (https://www.postgresql.org/docs/12/datatype-oid.html).
* There is no "unsigned four-byte integer" common type in java, so the closest type to hold all possible oid
* values is long. 2314556683 is a valid oid for example.
*/
assertThat(codec.decode(encode(TEST, "2314556683"), OID.getObjectId(), FORMAT_TEXT, Long.class))
.isEqualTo(2314556683L);
}
@Test
void decodeNoByteBuf() {
assertThat(new LongCodec(TEST).decode(null, dataType, FORMAT_BINARY, Long.class)).isNull();
}
@Test
void decodeNoFormat() {
assertThatIllegalArgumentException().isThrownBy(() -> new LongCodec(TEST).decode(TEST.buffer(0), dataType, null, Long.class))
.withMessage("format must not be null");
}
@Test
void doCanDecode() {
LongCodec codec = new LongCodec(TEST);
assertThat(codec.doCanDecode(VARCHAR, FORMAT_BINARY)).isFalse();
assertThat(codec.doCanDecode(INT8, FORMAT_BINARY)).isTrue();
assertThat(codec.doCanDecode(INT8, FORMAT_TEXT)).isTrue();
}
@Test
void doCanDecodeNoType() {
assertThatIllegalArgumentException().isThrownBy(() -> new LongCodec(TEST).doCanDecode(null, null))
.withMessage("type must not be null");
}
@Test
void doEncode() {
assertThat(new LongCodec(TEST).doEncode(100L))
.hasFormat(FORMAT_BINARY)
.hasType(INT8.getObjectId())
.hasValue(TEST.buffer(8).writeLong(100));
}
@Test
void doEncodeNoValue() {
assertThatIllegalArgumentException().isThrownBy(() -> new LongCodec(TEST).doEncode(null))
.withMessage("value must not be null");
}
@Test
void encodeItem() {
assertThat(new LongCodec(TEST).encodeToText(100L)).isEqualTo("100");
}
@Test
void encodeItemNoValue() {
assertThatIllegalArgumentException().isThrownBy(() -> new LongCodec(TEST).encodeToText(null))
.withMessage("value must not be null");
}
@Test
void encodeNull() {
assertThat(new LongCodec(TEST).encodeNull())
.isEqualTo(new EncodedParameter(FORMAT_BINARY, INT8.getObjectId(), NULL_VALUE));
}
}