1616
1717package org .springframework .r2dbc .connection ;
1818
19+ import java .util .List ;
20+
1921import io .r2dbc .spi .R2dbcBadGrammarException ;
2022import io .r2dbc .spi .R2dbcDataIntegrityViolationException ;
2123import io .r2dbc .spi .R2dbcException ;
2527import io .r2dbc .spi .R2dbcTimeoutException ;
2628import io .r2dbc .spi .R2dbcTransientResourceException ;
2729import org .junit .jupiter .api .Test ;
30+ import org .junit .jupiter .params .ParameterizedTest ;
31+ import org .junit .jupiter .params .provider .Arguments ;
32+ import org .junit .jupiter .params .provider .FieldSource ;
2833
2934import org .springframework .dao .CannotAcquireLockException ;
3035import org .springframework .dao .DataAccessResourceFailureException ;
3843import org .springframework .r2dbc .UncategorizedR2dbcException ;
3944
4045import static org .assertj .core .api .Assertions .assertThat ;
46+ import static org .junit .jupiter .params .provider .Arguments .arguments ;
4147
4248/**
4349 * Tests for {@link ConnectionFactoryUtils}.
@@ -91,30 +97,25 @@ void shouldTranslateIntegrityViolationException() {
9197 Exception exception = ConnectionFactoryUtils .convertR2dbcException ("" , "" ,
9298 new R2dbcDataIntegrityViolationException ());
9399 assertThat (exception ).isExactlyInstanceOf (DataIntegrityViolationException .class );
100+ }
94101
95- exception = ConnectionFactoryUtils .convertR2dbcException ("" , "" ,
96- new R2dbcDataIntegrityViolationException ("reason" , "23505" ));
97- assertThat (exception ).isExactlyInstanceOf (DuplicateKeyException .class );
98-
99- exception = ConnectionFactoryUtils .convertR2dbcException ("" , "" ,
100- new R2dbcDataIntegrityViolationException ("reason" , "23000" , 1 ));
101- assertThat (exception ).as ("Oracle" ).isExactlyInstanceOf (DuplicateKeyException .class );
102-
103- exception = ConnectionFactoryUtils .convertR2dbcException ("" , "" ,
104- new R2dbcDataIntegrityViolationException ("reason" , "23000" , 301 ));
105- assertThat (exception ).as ("SAP HANA" ).isExactlyInstanceOf (DuplicateKeyException .class );
106-
107- exception = ConnectionFactoryUtils .convertR2dbcException ("" , "" ,
108- new R2dbcDataIntegrityViolationException ("reason" , "23000" , 1062 ));
109- assertThat (exception ).as ("MySQL/MariaDB" ).isExactlyInstanceOf (DuplicateKeyException .class );
110-
111- exception = ConnectionFactoryUtils .convertR2dbcException ("" , "" ,
112- new R2dbcDataIntegrityViolationException ("reason" , "23000" , 2601 ));
113- assertThat (exception ).as ("MS SQL Server" ).isExactlyInstanceOf (DuplicateKeyException .class );
114-
115- exception = ConnectionFactoryUtils .convertR2dbcException ("" , "" ,
116- new R2dbcDataIntegrityViolationException ("reason" , "23000" , 2627 ));
117- assertThat (exception ).as ("MS SQL Server" ).isExactlyInstanceOf (DuplicateKeyException .class );
102+ static final List <Arguments > duplicateKeyErrorCodes = List .of (
103+ arguments ("Oracle" , "23505" , 0 ),
104+ arguments ("Oracle" , "23000" , 1 ),
105+ arguments ("SAP HANA" , "23000" , 301 ),
106+ arguments ("MySQL/MariaDB" , "23000" , 1062 ),
107+ arguments ("MS SQL Server" , "23000" , 2601 ),
108+ arguments ("MS SQL Server" , "23000" , 2627 ),
109+ arguments ("Informix" , "23000" , -239 ),
110+ arguments ("Informix" , "23000" , -268 )
111+ );
112+
113+ @ ParameterizedTest
114+ @ FieldSource ("duplicateKeyErrorCodes" )
115+ void shouldTranslateIntegrityViolationExceptionToDuplicateKeyException (String db , String sqlState , int errorCode ) {
116+ Exception exception = ConnectionFactoryUtils .convertR2dbcException ("" , "" ,
117+ new R2dbcDataIntegrityViolationException ("reason" , sqlState , errorCode ));
118+ assertThat (exception ).as (db ).isExactlyInstanceOf (DuplicateKeyException .class );
118119 }
119120
120121 @ Test
@@ -135,24 +136,27 @@ void shouldTranslateBadSqlGrammarException() {
135136 void messageGeneration () {
136137 Exception exception = ConnectionFactoryUtils .convertR2dbcException ("TASK" ,
137138 "SOME-SQL" , new R2dbcTransientResourceException ("MESSAGE" ));
138- assertThat (exception ).isExactlyInstanceOf (
139- TransientDataAccessResourceException .class ).hasMessage ("TASK; SQL [SOME-SQL]; MESSAGE" );
139+ assertThat (exception )
140+ .isExactlyInstanceOf (TransientDataAccessResourceException .class )
141+ .hasMessage ("TASK; SQL [SOME-SQL]; MESSAGE" );
140142 }
141143
142144 @ Test
143145 void messageGenerationNullSQL () {
144146 Exception exception = ConnectionFactoryUtils .convertR2dbcException ("TASK" , null ,
145147 new R2dbcTransientResourceException ("MESSAGE" ));
146- assertThat (exception ).isExactlyInstanceOf (
147- TransientDataAccessResourceException .class ).hasMessage ("TASK; MESSAGE" );
148+ assertThat (exception )
149+ .isExactlyInstanceOf (TransientDataAccessResourceException .class )
150+ .hasMessage ("TASK; MESSAGE" );
148151 }
149152
150153 @ Test
151154 void messageGenerationNullMessage () {
152155 Exception exception = ConnectionFactoryUtils .convertR2dbcException ("TASK" ,
153156 "SOME-SQL" , new R2dbcTransientResourceException ());
154- assertThat (exception ).isExactlyInstanceOf (
155- TransientDataAccessResourceException .class ).hasMessage ("TASK; SQL [SOME-SQL]; null" );
157+ assertThat (exception )
158+ .isExactlyInstanceOf (TransientDataAccessResourceException .class )
159+ .hasMessage ("TASK; SQL [SOME-SQL]; null" );
156160 }
157161
158162
0 commit comments