Skip to content

Commit 92da61e

Browse files
Throwing IllegalArgumentException
1 parent b333e4a commit 92da61e

File tree

4 files changed

+89
-32
lines changed

4 files changed

+89
-32
lines changed

src/main/java/oracle/r2dbc/impl/OracleConnectionFactoryImpl.java

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import reactor.core.publisher.Mono;
3232

3333
import javax.sql.DataSource;
34+
import java.time.Duration;
3435

3536
/**
3637
* <p>
@@ -48,30 +49,43 @@
4849
* </p>
4950
* <h3 id="required_options">Required Options</h3><p>
5051
* This implementation requires the following options for connection creation:
51-
* </p><ul>
52-
* <li>{@link ConnectionFactoryOptions#DRIVER}</li>
53-
* <li>{@link ConnectionFactoryOptions#HOST}</li>
54-
* </ul>
52+
* </p><dl>
53+
* <dt>{@link ConnectionFactoryOptions#DRIVER}</dt>
54+
* <dd>Must have the value "oracle"</dd>
55+
* <dt>{@link ConnectionFactoryOptions#HOST}</dt>
56+
* <dd>IP address or hostname of an Oracle Database</dd>
57+
* </dl>
5558
* <h3 id="supported_options">Supported Options</h3><p>
5659
* This implementation supports the following options for connection creation:
57-
* </p><ul>
58-
* <li>{@link ConnectionFactoryOptions#PORT}</li>
59-
* <li>{@link ConnectionFactoryOptions#DATABASE}</li>
60-
* <li>{@link ConnectionFactoryOptions#USER}</li>
61-
* <li>{@link ConnectionFactoryOptions#PASSWORD}</li>
62-
* <li>{@link ConnectionFactoryOptions#CONNECT_TIMEOUT}</li>
63-
* <li>{@link ConnectionFactoryOptions#SSL}</li>
64-
* </ul><p>
65-
* A value set for {@code CONNECT_TIMEOUT} will be rounded up to the nearest
66-
* whole second. When a value is set, any connection request that exceeds the
67-
* specified duration of seconds will automatically be cancelled. The
68-
* cancellation will result in an {@code onError} signal delivering an
69-
* {@link io.r2dbc.spi.R2dbcTimeoutException} to a connection {@code
70-
* Subscriber}.
71-
* </p><p>
72-
* A value of {@code true} set for {@code SSL} will configure the Oracle
73-
* JDBC Driver to connect using the TCPS protocol (ie: SSL/TLS).
74-
* </p>
60+
* </p><dl>
61+
* <dt>{@link ConnectionFactoryOptions#PORT}</dt>
62+
* <dd>Port number of an Oracle Database</dd>
63+
* <dt>{@link ConnectionFactoryOptions#DATABASE}</dt>
64+
* <dd>Service name (not an SID) of an Oracle Database</dd>
65+
* <dt>{@link ConnectionFactoryOptions#USER}</dt>
66+
* <dd>Name of an Oracle Database user</dd>
67+
* <dt>{@link ConnectionFactoryOptions#PASSWORD}</dt>
68+
* <dd>
69+
* Password of an Oracle Database user. The value may be an instance
70+
* of a mutable {@link CharSequence}, such {@link java.nio.CharBuffer},
71+
* that may be cleared after creating an instance of
72+
* {@code OracleConnectionFactoryImpl}.
73+
* </dd>
74+
* <dt>{@link ConnectionFactoryOptions#CONNECT_TIMEOUT}</dt>
75+
* <dd>
76+
* Maximum wait time when requesting a {@code Connection}. If the
77+
* duration expires, a {@code Connection} {@code Subscriber} receives
78+
* {@code onError} with an {@link io.r2dbc.spi.R2dbcTimeoutException}.
79+
* The duration is rounded up to the nearest whole second. The query
80+
* section of an R2DBC URL may provide a value in the format specified by
81+
* {@link Duration#parse(CharSequence)}.
82+
* </dd>
83+
* <dt>{@link ConnectionFactoryOptions#SSL}</dt>
84+
* <dd>
85+
* If set to {@code true}, the driver connects to Oracle Database using
86+
* TCPS (ie: SSL/TLS).
87+
* </dd>
88+
* </dl>
7589
*
7690
* @author harayuanwang, michael-a-mcmahon
7791
* @since 0.1.0
@@ -121,14 +135,19 @@ final class OracleConnectionFactoryImpl implements ConnectionFactory {
121135
* </p>
122136
*
123137
* @param options Options applied when opening a connection to a database.
138+
*
124139
* @throws IllegalArgumentException If the value of a required option is
125140
* null.
141+
*
126142
* @throws IllegalStateException If the value of a required option is not
127143
* specified.
144+
*
145+
* @throws IllegalArgumentException If the {@code oracle-net-descriptor}
146+
* {@code Option} is provided with any other options that might have
147+
* conflicting values, such as {@link ConnectionFactoryOptions#HOST}.
128148
*/
129149
OracleConnectionFactoryImpl(ConnectionFactoryOptions options) {
130150
OracleR2dbcExceptions.requireNonNull(options, "options is null.");
131-
132151
adapter = ReactiveJdbcAdapter.getOracleAdapter();
133152
dataSource = adapter.createDataSource(options);
134153
}

src/main/java/oracle/r2dbc/impl/OracleConnectionFactoryProviderImpl.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import java.util.ServiceLoader;
2929

30+
import static io.r2dbc.spi.ConnectionFactoryOptions.DRIVER;
3031
import static oracle.r2dbc.impl.OracleR2dbcExceptions.requireNonNull;
3132

3233
/**
@@ -83,8 +84,13 @@ public OracleConnectionFactoryProviderImpl() { }
8384
* {@code ConnectionFactory} that applies values specified by the {@code
8485
* options} parameter, when opening connections to an Oracle Database.
8586
* </p>
87+
*
8688
* @throws IllegalStateException If any option required by
8789
* {@link OracleConnectionFactoryImpl} is not specified by {@code options}.
90+
*
91+
* @throws IllegalArgumentException If the {@code oracle-net-descriptor}
92+
* {@code Option} is provided with any other options that might have
93+
* conflicting values, such as {@link ConnectionFactoryOptions#HOST}.
8894
*/
8995
@Override
9096
public ConnectionFactory create(ConnectionFactoryOptions options) {
@@ -103,8 +109,7 @@ public ConnectionFactory create(ConnectionFactoryOptions options) {
103109
@Override
104110
public boolean supports(ConnectionFactoryOptions options) {
105111
requireNonNull(options, "options must not be null.");
106-
return DRIVER_IDENTIFIER.equals(
107-
options.getValue(ConnectionFactoryOptions.DRIVER));
112+
return DRIVER_IDENTIFIER.equals(options.getValue(DRIVER));
108113
}
109114

110115
/**

src/main/java/oracle/r2dbc/impl/OracleReactiveJdbcAdapter.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,10 @@ static OracleReactiveJdbcAdapter getInstance() {
420420
* properly encoded by Oracle JDBC. If the data source is not configured
421421
* this way, the Oracle JDBC Driver uses the default character set of the
422422
* database, which may not support Unicode characters.
423+
*
424+
* @throws IllegalArgumentException If the {@code oracle-net-descriptor}
425+
* {@code Option} is provided with any other options that might have
426+
* conflicting values, such as {@link ConnectionFactoryOptions#HOST}.
423427
*/
424428
@Override
425429
public DataSource createDataSource(ConnectionFactoryOptions options) {
@@ -442,6 +446,9 @@ public DataSource createDataSource(ConnectionFactoryOptions options) {
442446
* {@link #createDataSource(ConnectionFactoryOptions)}
443447
* @param options R2DBC options. Not null.
444448
* @return An Oracle JDBC URL composed from R2DBC options
449+
* @throws IllegalArgumentException If the {@code oracle-net-descriptor}
450+
* {@code Option} is provided with any other options that might have
451+
* conflicting values, such as {@link ConnectionFactoryOptions#HOST}.
445452
*/
446453
private static String composeJdbcUrl(ConnectionFactoryOptions options) {
447454
Object descriptor = options.getValue(DESCRIPTOR);
@@ -468,10 +475,10 @@ private static String composeJdbcUrl(ConnectionFactoryOptions options) {
468475
/**
469476
* Validates {@code options} when the {@link #DESCRIPTOR} {@code Option} is
470477
* present. It is invalid to specify any other options having information
471-
* that overlaps with information in the descriptor, such as
478+
* that potentially conflicts with information in the descriptor, such as
472479
* {@link ConnectionFactoryOptions#HOST}.
473480
* @param options Options to validate
474-
* @throws IllegalStateException If {@code options} are invalid
481+
* @throws IllegalArgumentException If {@code options} are invalid
475482
*/
476483
private static void validateDescriptorOptions(
477484
ConnectionFactoryOptions options) {
@@ -488,8 +495,8 @@ private static void validateDescriptorOptions(
488495
.toArray(Option[]::new);
489496

490497
if (overlappingOptions.length != 0) {
491-
throw new IllegalStateException(DESCRIPTOR.name()
492-
+ " Option has been specified with overlapping Options: "
498+
throw new IllegalArgumentException(DESCRIPTOR.name()
499+
+ " Option has been specified with potentially conflicting Options: "
493500
+ Arrays.toString(overlappingOptions));
494501
}
495502
}

src/test/java/oracle/r2dbc/impl/OracleReactiveJdbcAdapterTest.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import static oracle.r2dbc.util.Awaits.awaitNone;
6262
import static oracle.r2dbc.util.Awaits.awaitOne;
6363
import static org.junit.jupiter.api.Assertions.assertEquals;
64+
import static org.junit.jupiter.api.Assertions.assertThrows;
6465

6566
/**
6667
* Verifies that
@@ -173,10 +174,11 @@ public void testCreateDataSource() throws SQLException {
173174
/**
174175
* Verifies that
175176
* {@link OracleReactiveJdbcAdapter#createDataSource(ConnectionFactoryOptions)}
176-
* handles the {@link OracleConnection#CONNECTION_PROPERTY_TNS_ADMIN} property
177-
* correctly. This property configures a file system path to a
177+
* handles Oracle Net Descriptors and the
178+
* {@link OracleConnection#CONNECTION_PROPERTY_TNS_ADMIN} property
179+
* correctly. The TNS ADMIN property configures a file system path to a
178180
* directory containing tnsnames.ora and ojdbc.properties files. If the TNS
179-
* admin property is handled correctly, then TNS descriptors should be read
181+
* admin property is handled correctly, then descriptors should be read
180182
* from tnsnames.ora, and connection properties should be read from
181183
* ojdbc.properties.
182184
*/
@@ -231,6 +233,30 @@ public void testTnsAdmin() throws IOException {
231233
.create())
232234
.close());
233235

236+
// Expect IllegalArgumentException if HOST, PORT, DATABASE, or SSL options
237+
// are provided with a descriptor
238+
assertThrows(IllegalArgumentException.class, () ->
239+
ConnectionFactories.get(
240+
"r2dbc:oracle://"+host()+"?oracle-net-descriptor="+descriptor));
241+
assertThrows(IllegalArgumentException.class, () ->
242+
ConnectionFactories.get("r2dbc:oracle://"
243+
+host()+":"+port()+"?oracle-net-descriptor="+descriptor));
244+
assertThrows(IllegalArgumentException.class, () ->
245+
ConnectionFactories.get("r2dbc:oracle://"+host()+":"+port()+"/"
246+
+serviceName()+"?oracle-net-descriptor="+descriptor));
247+
assertThrows(IllegalArgumentException.class, () ->
248+
ConnectionFactories.get("r2dbc:oracle://"+host()+"/"
249+
+serviceName()+"?oracle-net-descriptor="+descriptor));
250+
assertThrows(IllegalArgumentException.class, () ->
251+
ConnectionFactories.get("r2dbc:oracle:///"
252+
+serviceName()+"?oracle-net-descriptor="+descriptor));
253+
assertThrows(IllegalArgumentException.class, () ->
254+
ConnectionFactories.get("r2dbcs:oracle://" + // r2dbcs is SSL=true
255+
"?oracle-net-descriptor="+descriptor));
256+
assertThrows(IllegalArgumentException.class, () ->
257+
ConnectionFactories.get(
258+
"r2dbc:oracle://?oracle-net-descriptor="+descriptor+"&ssl=true"));
259+
234260
// Create an ojdbc.properties file containing the user name
235261
Files.writeString(Path.of("ojdbc.properties"),
236262
String.format("user=%s", user()),

0 commit comments

Comments
 (0)