Skip to content

Commit 33c069e

Browse files
committed
Align Statement.bind(…) and Readable.get(…) exceptions with specification.
We now throw the correct exception type according to the spec. [resolves #456][r2dbc/r2dbc-spi#184] Signed-off-by: Mark Paluch <[email protected]>
1 parent 5a5ac38 commit 33c069e

7 files changed

+24
-14
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
<postgresql.version>42.2.24</postgresql.version>
4848
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
4949
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
50-
<r2dbc-spi.version>0.9.0.M2</r2dbc-spi.version>
50+
<r2dbc-spi.version>0.9.0.BUILD-SNAPSHOT</r2dbc-spi.version>
5151
<reactor.version>2020.0.11</reactor.version>
5252
<scram-client.version>2.1</scram-client.version>
5353
<spring-framework.version>5.3.10</spring-framework.version>

src/main/java/io/r2dbc/postgresql/ExtendedQueryPostgresqlStatement.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.HashSet;
3737
import java.util.Iterator;
3838
import java.util.List;
39+
import java.util.NoSuchElementException;
3940
import java.util.Set;
4041
import java.util.concurrent.atomic.AtomicBoolean;
4142
import java.util.regex.Matcher;
@@ -252,7 +253,7 @@ private int getIndex(String identifier) {
252253
Matcher matcher = PARAMETER_SYMBOL.matcher(identifier);
253254

254255
if (!matcher.find()) {
255-
throw new IllegalArgumentException(String.format("Identifier '%s' is not a valid identifier. Should be of the pattern '%s'.", identifier, PARAMETER_SYMBOL.pattern()));
256+
throw new NoSuchElementException(String.format("Identifier '%s' is not a valid identifier. Should be of the pattern '%s'.", identifier, PARAMETER_SYMBOL.pattern()));
256257
}
257258

258259
return Integer.parseInt(matcher.group(1)) - 1;

src/main/java/io/r2dbc/postgresql/PostgresqlRow.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import java.util.ArrayList;
3131
import java.util.List;
32+
import java.util.NoSuchElementException;
3233
import java.util.Objects;
3334

3435
/**
@@ -172,7 +173,7 @@ private int getColumn(String name) {
172173
}
173174
}
174175

175-
throw new IllegalArgumentException(String.format("Column name '%s' does not exist in column names %s", name, toColumnNames()));
176+
throw new NoSuchElementException(String.format("Column name '%s' does not exist in column names %s", name, toColumnNames()));
176177
}
177178

178179
private List<String> toColumnNames() {
@@ -187,7 +188,7 @@ private List<String> toColumnNames() {
187188

188189
private int getColumn(int index) {
189190
if (index >= this.fields.size()) {
190-
throw new IllegalArgumentException(String.format("Column index %d is larger than the number of columns %d", index, this.fields.size()));
191+
throw new IndexOutOfBoundsException(String.format("Column index %d is larger than the number of columns %d", index, this.fields.size()));
191192
}
192193

193194
return index;

src/main/java/io/r2dbc/postgresql/PostgresqlRowMetadata.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.LinkedHashMap;
3131
import java.util.List;
3232
import java.util.Map;
33+
import java.util.NoSuchElementException;
3334
import java.util.Objects;
3435

3536
/**
@@ -55,7 +56,7 @@ final class PostgresqlRowMetadata extends AbstractCollection<String> implements
5556
@Override
5657
public PostgresqlColumnMetadata getColumnMetadata(int index) {
5758
if (index >= this.columnMetadatas.size()) {
58-
throw new IllegalArgumentException(String.format("Column index %d is larger than the number of columns %d", index, this.columnMetadatas.size()));
59+
throw new IndexOutOfBoundsException(String.format("Column index %d is larger than the number of columns %d", index, this.columnMetadatas.size()));
5960
}
6061

6162
return this.columnMetadatas.get(index);
@@ -72,7 +73,7 @@ public PostgresqlColumnMetadata getColumnMetadata(String name) {
7273
}
7374
}
7475

75-
throw new IllegalArgumentException(String.format("Column name '%s' does not exist in column names %s", name, getColumnNames()));
76+
throw new NoSuchElementException(String.format("Column name '%s' does not exist in column names %s", name, getColumnNames()));
7677
}
7778

7879
@Override

src/test/java/io/r2dbc/postgresql/ExtendedQueryPostgresqlStatementUnitTests.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.util.Arrays;
4545
import java.util.Collections;
4646
import java.util.LinkedList;
47+
import java.util.NoSuchElementException;
4748
import java.util.concurrent.atomic.AtomicBoolean;
4849
import java.util.logging.Level;
4950

@@ -52,6 +53,7 @@
5253
import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY;
5354
import static io.r2dbc.postgresql.util.TestByteBufAllocator.TEST;
5455
import static org.assertj.core.api.Assertions.assertThat;
56+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
5557
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
5658
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
5759
import static org.mockito.ArgumentMatchers.any;
@@ -128,13 +130,13 @@ void bindNullNoType() {
128130

129131
@Test
130132
void bindNullWrongIdentifierFormat() {
131-
assertThatIllegalArgumentException().isThrownBy(() -> this.statement.bindNull("foo", Integer.class))
133+
assertThatExceptionOfType(NoSuchElementException.class).isThrownBy(() -> this.statement.bindNull("foo", Integer.class))
132134
.withMessage("Identifier 'foo' is not a valid identifier. Should be of the pattern '\\$([\\d]+)'.");
133135
}
134136

135137
@Test
136138
void bindWrongIdentifierFormat() {
137-
assertThatIllegalArgumentException().isThrownBy(() -> this.statement.bind("foo", ""))
139+
assertThatExceptionOfType(NoSuchElementException.class).isThrownBy(() -> this.statement.bind("foo", ""))
138140
.withMessage("Identifier 'foo' is not a valid identifier. Should be of the pattern '\\$([\\d]+)'.");
139141
}
140142

src/test/java/io/r2dbc/postgresql/PostgresqlRowMetadataUnitTests.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Collection;
2626
import java.util.Collections;
2727
import java.util.List;
28+
import java.util.NoSuchElementException;
2829

2930
import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT;
3031
import static org.assertj.core.api.Assertions.assertThat;
@@ -55,13 +56,13 @@ void getColumnMetadataIndex() {
5556

5657
@Test
5758
void getColumnMetadataInvalidIndex() {
58-
assertThatIllegalArgumentException().isThrownBy(() -> new PostgresqlRowMetadata(this.columnMetadatas).getColumnMetadata(2))
59+
assertThatExceptionOfType(IndexOutOfBoundsException.class).isThrownBy(() -> new PostgresqlRowMetadata(this.columnMetadatas).getColumnMetadata(2))
5960
.withMessage("Column index 2 is larger than the number of columns 2");
6061
}
6162

6263
@Test
6364
void getColumnMetadataInvalidName() {
64-
assertThatIllegalArgumentException().isThrownBy(() -> new PostgresqlRowMetadata(this.columnMetadatas).getColumnMetadata("test-name-3"))
65+
assertThatExceptionOfType(NoSuchElementException.class).isThrownBy(() -> new PostgresqlRowMetadata(this.columnMetadatas).getColumnMetadata("test-name-3"))
6566
.withMessage("Column name 'test-name-3' does not exist in column names [test-name-2, test-name-1]");
6667
}
6768

src/test/java/io/r2dbc/postgresql/PostgresqlRowUnitTests.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
2525
import java.util.Arrays;
2626
import java.util.Collections;
2727
import java.util.List;
28+
import java.util.NoSuchElementException;
2829

2930
import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY;
3031
import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT;
3132
import static io.r2dbc.postgresql.util.TestByteBufAllocator.TEST;
3233
import static org.assertj.core.api.Assertions.assertThat;
34+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
3335
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
3436
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
3537
import static org.mockito.Mockito.mock;
@@ -98,15 +100,17 @@ void getIndex() {
98100

99101
@Test
100102
void getInvalidIndex() {
101-
assertThatIllegalArgumentException().isThrownBy(() -> new PostgresqlRow(MockContext.empty(), new PostgresqlRowMetadata(Collections.emptyList()), this.columns, new ByteBuf[0]).get(3,
102-
Object.class))
103+
assertThatExceptionOfType(IndexOutOfBoundsException.class).isThrownBy(() -> new PostgresqlRow(MockContext.empty(), new PostgresqlRowMetadata(Collections.emptyList()), this.columns,
104+
new ByteBuf[0]).get(3,
105+
Object.class))
103106
.withMessage("Column index 3 is larger than the number of columns 3");
104107
}
105108

106109
@Test
107110
void getInvalidName() {
108-
assertThatIllegalArgumentException().isThrownBy(() -> new PostgresqlRow(MockContext.empty(), new PostgresqlRowMetadata(Collections.emptyList()), this.columns, new ByteBuf[0]).get("test-name" +
109-
"-4", Object.class))
111+
assertThatExceptionOfType(NoSuchElementException.class).isThrownBy(() -> new PostgresqlRow(MockContext.empty(), new PostgresqlRowMetadata(Collections.emptyList()), this.columns,
112+
new ByteBuf[0]).get("test-name" +
113+
"-4", Object.class))
110114
.withMessageMatching("Column name 'test-name-4' does not exist in column names \\[test-name-[\\d], test-name-[\\d], test-name-[\\d]\\]");
111115
}
112116

0 commit comments

Comments
 (0)