Skip to content

Commit 8cf89fc

Browse files
Add LDAP tests
1 parent 6fb2706 commit 8cf89fc

File tree

4 files changed

+581
-7
lines changed

4 files changed

+581
-7
lines changed

pom.xml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,17 @@
8787
<arg>-Xlint:-processing</arg>
8888
<arg>-Xlint:-serial</arg>
8989
</compilerArgs>
90+
<!-- LDAP URL tests require the java.naming module -->
91+
<!-- Maven seems to ignore the forceJavacCompilerUse.
92+
This results in an error when passing the addModules option to
93+
the javax.tools API. For this reason, Oracle R2DBC's module-info is
94+
declaring a depency on the java.naming module.
95+
TODO: Figure out how to make maven compile tests correctly, and
96+
remove the java.naming depenency from Oracle R2DBC.
97+
<fork>true</fork>
98+
<forceJavacCompilerUse>true</forceJavacCompilerUse>
99+
<testCompilerArgument>- -add-modules java.naming</testCompilerArgument>
100+
-->
90101
<showWarnings>true</showWarnings>
91102
<release>${java.version}</release>
92103
</configuration>
@@ -150,6 +161,7 @@
150161
<artifactId>maven-surefire-plugin</artifactId>
151162
<version>3.0.0-M5</version>
152163
<configuration>
164+
<!-- Include tests in the oracle.r2dbc.test package -->
153165
<includes>
154166
<include>**/*Test.java</include>
155167
<include>**/*TestKit.java</include>
@@ -278,7 +290,6 @@
278290
<artifactId>reactor-test</artifactId>
279291
<scope>test</scope>
280292
</dependency>
281-
282293
</dependencies>
283294

284295
<profiles>

src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
with oracle.r2dbc.impl.OracleConnectionFactoryProviderImpl;
3131

3232
requires java.sql;
33+
requires java.naming;
3334
requires com.oracle.database.jdbc;
3435
requires reactor.core;
3536
requires transitive org.reactivestreams;

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

Lines changed: 100 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import io.r2dbc.spi.Connection;
2525
import io.r2dbc.spi.ConnectionFactories;
26+
import io.r2dbc.spi.ConnectionFactory;
2627
import io.r2dbc.spi.ConnectionFactoryOptions;
2728
import io.r2dbc.spi.Option;
2829
import io.r2dbc.spi.R2dbcTimeoutException;
@@ -31,10 +32,12 @@
3132
import oracle.jdbc.datasource.OracleDataSource;
3233
import oracle.r2dbc.OracleR2dbcOptions;
3334
import oracle.r2dbc.test.DatabaseConfig;
35+
import oracle.r2dbc.util.TestContextFactory;
3436
import org.junit.jupiter.api.Test;
3537
import reactor.core.publisher.Flux;
3638
import reactor.core.publisher.Mono;
3739

40+
import javax.naming.spi.NamingManager;
3841
import java.io.IOException;
3942
import java.nio.channels.ServerSocketChannel;
4043
import java.nio.channels.SocketChannel;
@@ -216,12 +219,7 @@ public void testCreateDataSource() throws SQLException {
216219
public void testTnsAdmin() throws IOException {
217220

218221
// Create an Oracle Net Descriptor
219-
String descriptor = format(
220-
"(DESCRIPTION=(ADDRESS=(HOST=%s)(PORT=%d)(PROTOCOL=%s))" +
221-
"(CONNECT_DATA=(SERVICE_NAME=%s)))",
222-
host(), port(),
223-
Objects.requireNonNullElse(protocol(), "tcp"),
224-
serviceName());
222+
String descriptor = createDescriptor();
225223

226224
// Create a tnsnames.ora file with an alias for the descriptor
227225
Files.writeString(Path.of("tnsnames.ora"),
@@ -525,6 +523,102 @@ public void testVSessionOptions() {
525523
tryAwaitNone(connection.close());
526524
}
527525
}
526+
/**
527+
* Verifies the use of the LDAP protocol in an r2dbc:oracle URL.
528+
*/
529+
@Test
530+
public void testLdapUrl() throws Exception {
531+
532+
// Configure Oracle R2DBC with an R2DBC URL having the LDAP protocol and the
533+
// given path.
534+
String ldapPath = "sales,cn=OracleContext,dc=com";
535+
ConnectionFactory ldapConnectionFactory = ConnectionFactories.get(
536+
ConnectionFactoryOptions.parse(format(
537+
"r2dbc:oracle:ldap://ldap.example.com:9999/%s", ldapPath))
538+
.mutate()
539+
.option(ConnectionFactoryOptions.USER, DatabaseConfig.user())
540+
.option(ConnectionFactoryOptions.PASSWORD, DatabaseConfig.password())
541+
.build());
542+
543+
// Set up the mock LDAP context factory. See JavaDoc of TestContextFactory
544+
// for details about this.
545+
NamingManager.setInitialContextFactoryBuilder(environment ->
546+
new TestContextFactory());
547+
TestContextFactory.bind(ldapPath, createDescriptor());
548+
549+
// Now verify that the LDAP URL is resolved to the descriptor
550+
Connection ldapConnection = awaitOne(ldapConnectionFactory.create());
551+
try {
552+
assertEquals(
553+
"Hello, LDAP",
554+
awaitOne(
555+
awaitOne(ldapConnection.createStatement(
556+
"SELECT 'Hello, LDAP' FROM sys.dual")
557+
.execute())
558+
.map(row -> row.get(0))));
559+
}
560+
finally {
561+
tryAwaitNone(ldapConnection.close());
562+
}
563+
}
564+
565+
/**
566+
* Verifies the use of the LDAP protocol in an r2dbc:oracle URL having
567+
* multiple LDAP endpoints
568+
*/
569+
@Test
570+
public void testMultiLdapUrl() throws Exception {
571+
572+
// Configure Oracle R2DBC with an R2DBC URL having the LDAP protocol and
573+
// multiple LDAP endpoints. Only the last endpoint will contain the given
574+
// path, and so the previous endpoints are invalid.
575+
String ldapPath = "cn=salesdept,cn=OracleContext,dc=com/salesdb";
576+
ConnectionFactory ldapConnectionFactory = ConnectionFactories.get(
577+
ConnectionFactoryOptions.parse(format(
578+
"r2dbc:oracle:" +
579+
"ldap://ldap1.example.com:7777/cn=salesdept0,cn=OracleContext,dc=com/salesdb" +
580+
"%%20ldap://ldap1.example.com:7777/cn=salesdept1,cn=OracleContext,dc=com/salesdb" +
581+
"%%20ldap://ldap3.example.com:7777/%s", ldapPath))
582+
.mutate()
583+
.option(ConnectionFactoryOptions.USER, DatabaseConfig.user())
584+
.option(ConnectionFactoryOptions.PASSWORD, DatabaseConfig.password())
585+
.build());
586+
587+
// Set up the mock LDAP context factory. A descriptor is bound to the last
588+
// endpoint only. See JavaDoc of TestContextFactory for details about this.
589+
NamingManager.setInitialContextFactoryBuilder(environment ->
590+
new TestContextFactory());
591+
TestContextFactory.bind("salesdb", createDescriptor());
592+
593+
// Now verify that the LDAP URL is resolved to the descriptor
594+
Connection ldapConnection = awaitOne(ldapConnectionFactory.create());
595+
try {
596+
assertEquals(
597+
"Hello, LDAP",
598+
awaitOne(
599+
awaitOne(ldapConnection.createStatement(
600+
"SELECT 'Hello, LDAP' FROM sys.dual")
601+
.execute())
602+
.map(row -> row.get(0))));
603+
}
604+
finally {
605+
tryAwaitNone(ldapConnection.close());
606+
}
607+
}
608+
609+
/**
610+
* Returns an Oracle Net Descriptor having the values configured by
611+
* {@link DatabaseConfig}
612+
* @return An Oracle Net Descriptor for the test database.
613+
*/
614+
private static String createDescriptor() {
615+
return format(
616+
"(DESCRIPTION=(ADDRESS=(HOST=%s)(PORT=%d)(PROTOCOL=%s))" +
617+
"(CONNECT_DATA=(SERVICE_NAME=%s)))",
618+
host(), port(),
619+
Objects.requireNonNullElse(protocol(), "tcp"),
620+
serviceName());
621+
}
528622

529623
/**
530624
* Verifies that an attempt to connect with a {@code listeningChannel}

0 commit comments

Comments
 (0)