19
19
import java .sql .Connection ;
20
20
import java .sql .DatabaseMetaData ;
21
21
import java .sql .SQLException ;
22
- import java .util .Arrays ;
23
22
24
23
import javax .sql .DataSource ;
25
24
41
40
* @author Thomas Risberg
42
41
* @author Stephane Nicoll
43
42
* @author Juergen Hoeller
43
+ * @author Sam Brannen
44
44
*/
45
- public class SQLErrorCodesFactoryTests {
45
+ class SQLErrorCodesFactoryTests {
46
46
47
47
/**
48
48
* Check that a default instance returns empty error codes for an unknown database.
49
49
*/
50
50
@ Test
51
- public void testDefaultInstanceWithNoSuchDatabase () {
51
+ void defaultInstanceWithNoSuchDatabase () {
52
52
SQLErrorCodes sec = SQLErrorCodesFactory .getInstance ().getErrorCodes ("xx" );
53
- assertThat (sec .getBadSqlGrammarCodes (). length ). isEqualTo ( 0 );
54
- assertThat (sec .getDataIntegrityViolationCodes (). length ). isEqualTo ( 0 );
53
+ assertThat (sec .getBadSqlGrammarCodes ()). isEmpty ( );
54
+ assertThat (sec .getDataIntegrityViolationCodes ()). isEmpty ( );
55
55
}
56
56
57
57
/**
58
58
* Check that a known database produces recognizable codes.
59
59
*/
60
60
@ Test
61
- public void testDefaultInstanceWithOracle () {
61
+ void defaultInstanceWithOracle () {
62
62
SQLErrorCodes sec = SQLErrorCodesFactory .getInstance ().getErrorCodes ("Oracle" );
63
63
assertIsOracle (sec );
64
64
}
65
65
66
66
private void assertIsOracle (SQLErrorCodes sec ) {
67
- assertThat (sec .getBadSqlGrammarCodes (). length ). isGreaterThan ( 0 );
68
- assertThat (sec .getDataIntegrityViolationCodes (). length ). isGreaterThan ( 0 );
67
+ assertThat (sec .getBadSqlGrammarCodes ()). isNotEmpty ( );
68
+ assertThat (sec .getDataIntegrityViolationCodes ()). isNotEmpty ( );
69
69
// These had better be a Bad SQL Grammar code
70
- assertThat (Arrays . binarySearch ( sec .getBadSqlGrammarCodes (), "942" )). isGreaterThanOrEqualTo ( 0 );
71
- assertThat (Arrays . binarySearch ( sec .getBadSqlGrammarCodes (), "6550" )). isGreaterThanOrEqualTo ( 0 );
70
+ assertThat (sec .getBadSqlGrammarCodes ()). contains ( "942" );
71
+ assertThat (sec .getBadSqlGrammarCodes ()). contains ( "6550" );
72
72
// This had better NOT be
73
- assertThat (Arrays . binarySearch ( sec .getBadSqlGrammarCodes (), "9xx42" )). isLessThan ( 0 );
73
+ assertThat (sec .getBadSqlGrammarCodes ()). doesNotContain ( "9xx42" );
74
74
}
75
75
76
76
private void assertIsSQLServer (SQLErrorCodes sec ) {
77
77
assertThat (sec .getDatabaseProductName ()).isEqualTo ("Microsoft SQL Server" );
78
78
79
- assertThat (sec .getBadSqlGrammarCodes (). length ). isGreaterThan ( 0 );
79
+ assertThat (sec .getBadSqlGrammarCodes ()). isNotEmpty ( );
80
80
81
- assertThat (Arrays . binarySearch ( sec .getBadSqlGrammarCodes (), "156" )). isGreaterThanOrEqualTo ( 0 );
82
- assertThat (Arrays . binarySearch ( sec .getBadSqlGrammarCodes (), "170" )). isGreaterThanOrEqualTo ( 0 );
83
- assertThat (Arrays . binarySearch ( sec .getBadSqlGrammarCodes (), "207" )). isGreaterThanOrEqualTo ( 0 );
84
- assertThat (Arrays . binarySearch ( sec .getBadSqlGrammarCodes (), "208" )). isGreaterThanOrEqualTo ( 0 );
85
- assertThat (Arrays . binarySearch ( sec .getBadSqlGrammarCodes (), "209" )). isGreaterThanOrEqualTo ( 0 );
86
- assertThat (Arrays . binarySearch ( sec .getBadSqlGrammarCodes (), "9xx42" )). isLessThan ( 0 );
81
+ assertThat (sec .getBadSqlGrammarCodes ()). contains ( "156" );
82
+ assertThat (sec .getBadSqlGrammarCodes ()). contains ( "170" );
83
+ assertThat (sec .getBadSqlGrammarCodes ()). contains ( "207" );
84
+ assertThat (sec .getBadSqlGrammarCodes ()). contains ( "208" );
85
+ assertThat (sec .getBadSqlGrammarCodes ()). contains ( "209" );
86
+ assertThat (sec .getBadSqlGrammarCodes ()). doesNotContain ( "9xx42" );
87
87
88
- assertThat (sec .getPermissionDeniedCodes (). length ). isGreaterThan ( 0 );
89
- assertThat (Arrays . binarySearch ( sec .getPermissionDeniedCodes (), "229" )). isGreaterThanOrEqualTo ( 0 );
88
+ assertThat (sec .getPermissionDeniedCodes ()). isNotEmpty ( );
89
+ assertThat (sec .getPermissionDeniedCodes ()). contains ( "229" );
90
90
91
- assertThat (sec .getDuplicateKeyCodes (). length ). isGreaterThan ( 0 );
92
- assertThat (Arrays . binarySearch ( sec .getDuplicateKeyCodes (), "2601" )). isGreaterThanOrEqualTo ( 0 );
93
- assertThat (Arrays . binarySearch ( sec .getDuplicateKeyCodes (), "2627" )). isGreaterThanOrEqualTo ( 0 );
91
+ assertThat (sec .getDuplicateKeyCodes ()). isNotEmpty ( );
92
+ assertThat (sec .getDuplicateKeyCodes ()). contains ( "2601" );
93
+ assertThat (sec .getDuplicateKeyCodes ()). contains ( "2627" );
94
94
95
- assertThat (sec .getDataIntegrityViolationCodes (). length ). isGreaterThan ( 0 );
96
- assertThat (Arrays . binarySearch ( sec .getDataIntegrityViolationCodes (), "544" )). isGreaterThanOrEqualTo ( 0 );
97
- assertThat (Arrays . binarySearch ( sec .getDataIntegrityViolationCodes (), "8114" )). isGreaterThanOrEqualTo ( 0 );
98
- assertThat (Arrays . binarySearch ( sec .getDataIntegrityViolationCodes (), "8115" )). isGreaterThanOrEqualTo ( 0 );
95
+ assertThat (sec .getDataIntegrityViolationCodes ()). isNotEmpty ( );
96
+ assertThat (sec .getDataIntegrityViolationCodes ()). contains ( "544" );
97
+ assertThat (sec .getDataIntegrityViolationCodes ()). contains ( "8114" );
98
+ assertThat (sec .getDataIntegrityViolationCodes ()). contains ( "8115" );
99
99
100
- assertThat (sec .getDataAccessResourceFailureCodes (). length ). isGreaterThan ( 0 );
101
- assertThat (Arrays . binarySearch ( sec .getDataAccessResourceFailureCodes (), "4060" )). isGreaterThanOrEqualTo ( 0 );
100
+ assertThat (sec .getDataAccessResourceFailureCodes ()). isNotEmpty ( );
101
+ assertThat (sec .getDataAccessResourceFailureCodes ()). contains ( "4060" );
102
102
103
- assertThat (sec .getCannotAcquireLockCodes (). length ). isGreaterThan ( 0 );
104
- assertThat (Arrays . binarySearch ( sec .getCannotAcquireLockCodes (), "1222" )). isGreaterThanOrEqualTo ( 0 );
103
+ assertThat (sec .getCannotAcquireLockCodes ()). isNotEmpty ( );
104
+ assertThat (sec .getCannotAcquireLockCodes ()). contains ( "1222" );
105
105
106
- assertThat (sec .getDeadlockLoserCodes (). length ). isGreaterThan ( 0 );
107
- assertThat (Arrays . binarySearch ( sec .getDeadlockLoserCodes (), "1205" )). isGreaterThanOrEqualTo ( 0 );
106
+ assertThat (sec .getDeadlockLoserCodes ()). isNotEmpty ( );
107
+ assertThat (sec .getDeadlockLoserCodes ()). contains ( "1205" );
108
108
}
109
109
110
110
private void assertIsHsql (SQLErrorCodes sec ) {
111
- assertThat (sec .getBadSqlGrammarCodes (). length ). isGreaterThan ( 0 );
112
- assertThat (sec .getDataIntegrityViolationCodes (). length ). isGreaterThan ( 0 );
111
+ assertThat (sec .getBadSqlGrammarCodes ()). isNotEmpty ( );
112
+ assertThat (sec .getDataIntegrityViolationCodes ()). isNotEmpty ( );
113
113
// This had better be a Bad SQL Grammar code
114
- assertThat (Arrays . binarySearch ( sec .getBadSqlGrammarCodes (), "-22" )). isGreaterThanOrEqualTo ( 0 );
114
+ assertThat (sec .getBadSqlGrammarCodes ()). contains ( "-22" );
115
115
// This had better NOT be
116
- assertThat (Arrays . binarySearch ( sec .getBadSqlGrammarCodes (), "-9" )). isLessThan ( 0 );
116
+ assertThat (sec .getBadSqlGrammarCodes ()). doesNotContain ( "-9" );
117
117
}
118
118
119
119
private void assertIsDB2 (SQLErrorCodes sec ) {
120
- assertThat (sec .getBadSqlGrammarCodes (). length ). isGreaterThan ( 0 );
121
- assertThat (sec .getDataIntegrityViolationCodes (). length ). isGreaterThan ( 0 );
120
+ assertThat (sec .getBadSqlGrammarCodes ()). isNotEmpty ( );
121
+ assertThat (sec .getDataIntegrityViolationCodes ()). isNotEmpty ( );
122
122
123
- assertThat (Arrays . binarySearch ( sec .getBadSqlGrammarCodes (), "942" )). isLessThan ( 0 );
123
+ assertThat (sec .getBadSqlGrammarCodes ()). doesNotContain ( "942" );
124
124
// This had better NOT be
125
- assertThat (Arrays . binarySearch ( sec .getBadSqlGrammarCodes (), "-204" )). isGreaterThanOrEqualTo ( 0 );
125
+ assertThat (sec .getBadSqlGrammarCodes ()). contains ( "-204" );
126
126
}
127
127
128
128
private void assertIsHana (SQLErrorCodes sec ) {
129
- assertThat (sec .getBadSqlGrammarCodes ().length ).isGreaterThan (0 );
130
- assertThat (sec .getDataIntegrityViolationCodes ().length ).isGreaterThan (0 );
131
-
132
- assertThat (Arrays .binarySearch (sec .getBadSqlGrammarCodes (), "368" )).isGreaterThanOrEqualTo (0 );
133
- assertThat (Arrays .binarySearch (sec .getPermissionDeniedCodes (), "10" )).isGreaterThanOrEqualTo (0 );
134
- assertThat (Arrays .binarySearch (sec .getDuplicateKeyCodes (), "301" )).isGreaterThanOrEqualTo (0 );
135
- assertThat (Arrays .binarySearch (sec .getDataIntegrityViolationCodes (), "461" )).isGreaterThanOrEqualTo (0 );
136
- assertThat (Arrays .binarySearch (sec .getDataAccessResourceFailureCodes (), "-813" )).isGreaterThanOrEqualTo (0 );
137
- assertThat (Arrays .binarySearch (sec .getInvalidResultSetAccessCodes (), "582" )).isGreaterThanOrEqualTo (0 );
138
- assertThat (Arrays .binarySearch (sec .getCannotAcquireLockCodes (), "131" )).isGreaterThanOrEqualTo (0 );
139
- assertThat (Arrays .binarySearch (sec .getCannotSerializeTransactionCodes (), "138" )).isGreaterThanOrEqualTo (0 );
140
- assertThat (Arrays .binarySearch (sec .getDeadlockLoserCodes (), "133" )).isGreaterThanOrEqualTo (0 );
141
-
129
+ assertThat (sec .getBadSqlGrammarCodes ()).isNotEmpty ();
130
+ assertThat (sec .getDataIntegrityViolationCodes ()).isNotEmpty ();
131
+
132
+ assertThat (sec .getBadSqlGrammarCodes ()).contains ("368" );
133
+ assertThat (sec .getPermissionDeniedCodes ()).contains ("10" );
134
+ assertThat (sec .getDuplicateKeyCodes ()).contains ("301" );
135
+ assertThat (sec .getDataIntegrityViolationCodes ()).contains ("461" );
136
+ assertThat (sec .getDataAccessResourceFailureCodes ()).contains ("-813" );
137
+ assertThat (sec .getInvalidResultSetAccessCodes ()).contains ("582" );
138
+ assertThat (sec .getCannotAcquireLockCodes ()).contains ("131" );
139
+ assertThat (sec .getCannotSerializeTransactionCodes ()).contains ("138" );
140
+ assertThat (sec .getDeadlockLoserCodes ()).contains ("133" );
142
141
}
143
142
144
143
@ Test
145
- public void testLookupOrder () {
144
+ void lookupOrder () {
146
145
class TestSQLErrorCodesFactory extends SQLErrorCodesFactory {
147
146
private int lookups = 0 ;
148
147
@ Override
@@ -163,15 +162,15 @@ protected Resource loadResource(String path) {
163
162
164
163
// Should have failed to load without error
165
164
TestSQLErrorCodesFactory sf = new TestSQLErrorCodesFactory ();
166
- assertThat (sf .getErrorCodes ("XX" ).getBadSqlGrammarCodes (). length ). isEqualTo ( 0 );
167
- assertThat (sf .getErrorCodes ("Oracle" ).getDataIntegrityViolationCodes (). length ). isEqualTo ( 0 );
165
+ assertThat (sf .getErrorCodes ("XX" ).getBadSqlGrammarCodes ()). isEmpty ( );
166
+ assertThat (sf .getErrorCodes ("Oracle" ).getDataIntegrityViolationCodes ()). isEmpty ( );
168
167
}
169
168
170
169
/**
171
170
* Check that user defined error codes take precedence.
172
171
*/
173
172
@ Test
174
- public void testFindUserDefinedCodes () {
173
+ void findUserDefinedCodes () {
175
174
class TestSQLErrorCodesFactory extends SQLErrorCodesFactory {
176
175
@ Override
177
176
protected Resource loadResource (String path ) {
@@ -184,14 +183,12 @@ protected Resource loadResource(String path) {
184
183
185
184
// Should have loaded without error
186
185
TestSQLErrorCodesFactory sf = new TestSQLErrorCodesFactory ();
187
- assertThat (sf .getErrorCodes ("XX" ).getBadSqlGrammarCodes ().length ).isEqualTo (0 );
188
- assertThat (sf .getErrorCodes ("Oracle" ).getBadSqlGrammarCodes ()).hasSize (2 );
189
- assertThat (sf .getErrorCodes ("Oracle" ).getBadSqlGrammarCodes ()[0 ]).isEqualTo ("1" );
190
- assertThat (sf .getErrorCodes ("Oracle" ).getBadSqlGrammarCodes ()[1 ]).isEqualTo ("2" );
186
+ assertThat (sf .getErrorCodes ("XX" ).getBadSqlGrammarCodes ()).isEmpty ();
187
+ assertThat (sf .getErrorCodes ("Oracle" ).getBadSqlGrammarCodes ()).containsExactly ("1" , "2" );
191
188
}
192
189
193
190
@ Test
194
- public void testInvalidUserDefinedCodeFormat () {
191
+ void invalidUserDefinedCodeFormat () {
195
192
class TestSQLErrorCodesFactory extends SQLErrorCodesFactory {
196
193
@ Override
197
194
protected Resource loadResource (String path ) {
@@ -205,15 +202,15 @@ protected Resource loadResource(String path) {
205
202
206
203
// Should have failed to load without error
207
204
TestSQLErrorCodesFactory sf = new TestSQLErrorCodesFactory ();
208
- assertThat (sf .getErrorCodes ("XX" ).getBadSqlGrammarCodes (). length ). isEqualTo ( 0 );
205
+ assertThat (sf .getErrorCodes ("XX" ).getBadSqlGrammarCodes ()). isEmpty ( );
209
206
assertThat (sf .getErrorCodes ("Oracle" ).getBadSqlGrammarCodes ()).isEmpty ();
210
207
}
211
208
212
209
/**
213
210
* Check that custom error codes take precedence.
214
211
*/
215
212
@ Test
216
- public void testFindCustomCodes () {
213
+ void findCustomCodes () {
217
214
class TestSQLErrorCodesFactory extends SQLErrorCodesFactory {
218
215
@ Override
219
216
protected Resource loadResource (String path ) {
@@ -227,14 +224,13 @@ protected Resource loadResource(String path) {
227
224
// Should have loaded without error
228
225
TestSQLErrorCodesFactory sf = new TestSQLErrorCodesFactory ();
229
226
assertThat (sf .getErrorCodes ("Oracle" ).getCustomTranslations ()).hasSize (1 );
230
- CustomSQLErrorCodesTranslation translation =
231
- sf .getErrorCodes ("Oracle" ).getCustomTranslations ()[0 ];
227
+ CustomSQLErrorCodesTranslation translation = sf .getErrorCodes ("Oracle" ).getCustomTranslations ()[0 ];
232
228
assertThat (translation .getExceptionClass ()).isEqualTo (CustomErrorCodeException .class );
233
229
assertThat (translation .getErrorCodes ()).hasSize (1 );
234
230
}
235
231
236
232
@ Test
237
- public void testDataSourceWithNullMetadata () throws Exception {
233
+ void dataSourceWithNullMetadata () throws Exception {
238
234
Connection connection = mock ();
239
235
DataSource dataSource = mock ();
240
236
given (dataSource .getConnection ()).willReturn (connection );
@@ -250,7 +246,7 @@ public void testDataSourceWithNullMetadata() throws Exception {
250
246
}
251
247
252
248
@ Test
253
- public void testGetFromDataSourceWithSQLException () throws Exception {
249
+ void getFromDataSourceWithSQLException () throws Exception {
254
250
SQLException expectedSQLException = new SQLException ();
255
251
256
252
DataSource dataSource = mock ();
@@ -284,25 +280,25 @@ private SQLErrorCodes getErrorCodesFromDataSource(String productName, SQLErrorCo
284
280
}
285
281
286
282
@ Test
287
- public void testSQLServerRecognizedFromMetadata () throws Exception {
283
+ void sqlServerRecognizedFromMetadata () throws Exception {
288
284
SQLErrorCodes sec = getErrorCodesFromDataSource ("MS-SQL" , null );
289
285
assertIsSQLServer (sec );
290
286
}
291
287
292
288
@ Test
293
- public void testOracleRecognizedFromMetadata () throws Exception {
289
+ void oracleRecognizedFromMetadata () throws Exception {
294
290
SQLErrorCodes sec = getErrorCodesFromDataSource ("Oracle" , null );
295
291
assertIsOracle (sec );
296
292
}
297
293
298
294
@ Test
299
- public void testHsqlRecognizedFromMetadata () throws Exception {
295
+ void hsqlRecognizedFromMetadata () throws Exception {
300
296
SQLErrorCodes sec = getErrorCodesFromDataSource ("HSQL Database Engine" , null );
301
297
assertIsHsql (sec );
302
298
}
303
299
304
300
@ Test
305
- public void testDB2RecognizedFromMetadata () throws Exception {
301
+ void dB2RecognizedFromMetadata () throws Exception {
306
302
SQLErrorCodes sec = getErrorCodesFromDataSource ("DB2" , null );
307
303
assertIsDB2 (sec );
308
304
sec = getErrorCodesFromDataSource ("DB2/" , null );
@@ -312,7 +308,7 @@ public void testDB2RecognizedFromMetadata() throws Exception {
312
308
}
313
309
314
310
@ Test
315
- public void testHanaIsRecognizedFromMetadata () throws Exception {
311
+ void hanaIsRecognizedFromMetadata () throws Exception {
316
312
SQLErrorCodes sec = getErrorCodesFromDataSource ("SAP DB" , null );
317
313
assertIsHana (sec );
318
314
}
@@ -321,7 +317,7 @@ public void testHanaIsRecognizedFromMetadata() throws Exception {
321
317
* Check that wild card database name works.
322
318
*/
323
319
@ Test
324
- public void testWildCardNameRecognized () throws Exception {
320
+ void wildCardNameRecognized () throws Exception {
325
321
class WildcardSQLErrorCodesFactory extends SQLErrorCodesFactory {
326
322
@ Override
327
323
protected Resource loadResource (String path ) {
0 commit comments