Skip to content

Commit fbef5e2

Browse files
Subtyping OracleR2dbcWarning as a Message
1 parent f05443c commit fbef5e2

File tree

3 files changed

+81
-52
lines changed

3 files changed

+81
-52
lines changed

src/main/java/oracle/r2dbc/OracleR2dbcWarning.java

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -53,29 +53,6 @@
5353
* }</pre>
5454
* @since 1.1.0
5555
*/
56-
public interface OracleR2dbcWarning extends Result.Segment {
56+
public interface OracleR2dbcWarning extends Result.Message {
5757

58-
/**
59-
* Returns the warning as an {@link R2dbcException}.
60-
* @return The warning as an {@link R2dbcException}. Not null.
61-
*/
62-
R2dbcException exception();
63-
64-
/**
65-
* Returns the error code of the warning.
66-
* @return The error code of the warning.
67-
*/
68-
int errorCode();
69-
70-
/**
71-
* Returns the SQLState of the warning.
72-
* @return The SQLState of the warning. Not null.
73-
*/
74-
String sqlState();
75-
76-
/**
77-
* Returns the text of the warning message.
78-
* @return The text of the warning message. Not null.
79-
*/
80-
String message();
8158
}

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

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ private <T extends Segment, U> Publisher<U> publishSegments(
151151
Flux.from(publishSegments(segment -> {
152152
if (type.isInstance(segment))
153153
return mappingFunction.apply(type.cast(segment));
154+
else if (segment instanceof OracleR2dbcWarning)
155+
return (U)FILTERED;
154156
else if (segment instanceof Message)
155157
throw ((Message)segment).exception();
156158
else
@@ -784,7 +786,7 @@ public long value() {
784786
/**
785787
* Implementation of {@link Message}.
786788
*/
787-
private static final class MessageImpl implements Message {
789+
private static class MessageImpl implements Message {
788790

789791
private final R2dbcException exception;
790792

@@ -811,38 +813,24 @@ public String sqlState() {
811813
public String message() {
812814
return exception.getMessage();
813815
}
816+
817+
@Override
818+
public String toString() {
819+
return exception.toString();
820+
}
814821
}
815822

816823
/**
817824
* Implementation of {@link OracleR2dbcWarning}.
818825
*/
819-
private static final class WarningImpl implements OracleR2dbcWarning {
820-
821-
private final R2dbcException warning;
826+
private static final class WarningImpl
827+
extends MessageImpl
828+
implements OracleR2dbcWarning {
822829

823830
private WarningImpl(R2dbcException exception) {
824-
this.warning = exception;
825-
}
826-
827-
@Override
828-
public R2dbcException exception() {
829-
return warning;
830-
}
831-
832-
@Override
833-
public int errorCode() {
834-
return warning.getErrorCode();
835-
}
836-
837-
@Override
838-
public String sqlState() {
839-
return warning.getSqlState();
831+
super(exception);
840832
}
841833

842-
@Override
843-
public String message() {
844-
return warning.getMessage();
845-
}
846834
}
847835

848836
/**

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

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@
5454
import static oracle.r2dbc.util.Awaits.awaitMany;
5555
import static oracle.r2dbc.util.Awaits.awaitNone;
5656
import static oracle.r2dbc.util.Awaits.awaitOne;
57+
import static oracle.r2dbc.util.Awaits.awaitUpdate;
5758
import static oracle.r2dbc.util.Awaits.consumeOne;
5859
import static oracle.r2dbc.util.Awaits.tryAwaitExecution;
5960
import static oracle.r2dbc.util.Awaits.tryAwaitNone;
6061
import static org.junit.jupiter.api.Assertions.assertEquals;
6162
import static org.junit.jupiter.api.Assertions.assertFalse;
6263
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
63-
import static org.junit.jupiter.api.Assertions.assertSame;
6464
import static org.junit.jupiter.api.Assertions.assertThrows;
6565
import static org.junit.jupiter.api.Assertions.assertTrue;
6666
import static org.junit.jupiter.api.Assertions.fail;
@@ -631,8 +631,8 @@ else if (index == 1) {
631631
}
632632

633633
/**
634-
* Verifies that a warnings are emitted as {@code Message} segments with an
635-
* {@link oracle.r2dbc.OracleR2dbcWarning}.
634+
* Verifies that a warnings are emitted as
635+
* {@link oracle.r2dbc.OracleR2dbcWarning} segments.
636636
*/
637637
@Test
638638
public void testOracleR2dbcWarning() {
@@ -642,7 +642,7 @@ public void testOracleR2dbcWarning() {
642642
// Expect a warning for forcing a view that references a non-existent
643643
// table
644644
String sql = "CREATE OR REPLACE FORCE VIEW testOracleR2dbcWarning AS" +
645-
" SELECT x FROM thisdoesnotexist";
645+
" SELECT x FROM thisdoesnotexist";
646646
Statement warningStatement = connection.createStatement(sql);
647647

648648
// Collect the segments
@@ -684,4 +684,68 @@ public void testOracleR2dbcWarning() {
684684
}
685685
}
686686

687+
/**
688+
* Verifies that a warnings are not emitted as onError signals
689+
*/
690+
@Test
691+
public void testOracleR2dbcWarningIgnored() {
692+
Connection connection = awaitOne(sharedConnection());
693+
try {
694+
695+
// Expect a warning for forcing a view that references a non-existent
696+
// table
697+
String sql =
698+
"CREATE OR REPLACE FORCE VIEW testOracleR2dbcWarningIgnored AS" +
699+
" SELECT x FROM thisdoesnotexist";
700+
Statement warningStatement = connection.createStatement(sql);
701+
702+
// Verify that an update count of 0 is returned.
703+
awaitUpdate(0, warningStatement);
704+
705+
// Verify that no rows are returned
706+
awaitNone(
707+
awaitOne(warningStatement.execute())
708+
.map(row -> "UNEXPECTED ROW"));
709+
710+
// Verify that no rows are returned
711+
awaitNone(
712+
awaitOne(warningStatement.execute())
713+
.map((row, metadata) -> "UNEXPECTED ROW WITH METADATA"));
714+
}
715+
finally {
716+
tryAwaitExecution(
717+
connection.createStatement("DROP VIEW testOracleR2dbcWarningIgnored"));
718+
tryAwaitNone(connection.close());
719+
}
720+
}
721+
722+
/**
723+
* Verifies that {@link Result#flatMap(Function)} may be used to convert
724+
* warnings into onError signals
725+
*/
726+
@Test
727+
public void testOracleR2dbcWarningNotIgnored() {
728+
Connection connection = awaitOne(sharedConnection());
729+
try {
730+
731+
// Expect a warning for forcing a view that references a non-existent
732+
// table
733+
String sql =
734+
"CREATE OR REPLACE FORCE VIEW testOracleR2dbcWarningIgnored AS" +
735+
" SELECT x FROM thisdoesnotexist";
736+
Statement warningStatement = connection.createStatement(sql);
737+
awaitError(
738+
R2dbcException.class,
739+
awaitOne(warningStatement.execute())
740+
.flatMap(segment ->
741+
Mono.error(
742+
assertInstanceOf(OracleR2dbcWarning.class, segment).exception())));
743+
}
744+
finally {
745+
tryAwaitExecution(
746+
connection.createStatement("DROP VIEW testOracleR2dbcWarningIgnored"));
747+
tryAwaitNone(connection.close());
748+
}
749+
}
750+
687751
}

0 commit comments

Comments
 (0)