Skip to content

Commit 5752e03

Browse files
committed
Polishing
1 parent dc26d3b commit 5752e03

File tree

4 files changed

+131
-137
lines changed

4 files changed

+131
-137
lines changed

spring-core/src/test/java/org/springframework/core/AttributeAccessorSupportTests.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.core;
1818

19-
import java.util.Arrays;
2019
import java.util.concurrent.atomic.AtomicInteger;
2120
import java.util.function.Function;
2221

@@ -82,10 +81,7 @@ void remove() {
8281
void attributeNames() {
8382
this.attributeAccessor.setAttribute(NAME, VALUE);
8483
this.attributeAccessor.setAttribute("abc", "123");
85-
String[] attributeNames = this.attributeAccessor.attributeNames();
86-
Arrays.sort(attributeNames);
87-
assertThat(Arrays.binarySearch(attributeNames, "abc")).isEqualTo(0);
88-
assertThat(Arrays.binarySearch(attributeNames, NAME)).isEqualTo(1);
84+
assertThat(this.attributeAccessor.attributeNames()).contains("abc", NAME);
8985
}
9086

9187
@SuppressWarnings("serial")

spring-jdbc/src/test/java/org/springframework/jdbc/support/SQLErrorCodesFactoryTests.java

Lines changed: 71 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.sql.Connection;
2020
import java.sql.DatabaseMetaData;
2121
import java.sql.SQLException;
22-
import java.util.Arrays;
2322

2423
import javax.sql.DataSource;
2524

@@ -41,108 +40,108 @@
4140
* @author Thomas Risberg
4241
* @author Stephane Nicoll
4342
* @author Juergen Hoeller
43+
* @author Sam Brannen
4444
*/
45-
public class SQLErrorCodesFactoryTests {
45+
class SQLErrorCodesFactoryTests {
4646

4747
/**
4848
* Check that a default instance returns empty error codes for an unknown database.
4949
*/
5050
@Test
51-
public void testDefaultInstanceWithNoSuchDatabase() {
51+
void defaultInstanceWithNoSuchDatabase() {
5252
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();
5555
}
5656

5757
/**
5858
* Check that a known database produces recognizable codes.
5959
*/
6060
@Test
61-
public void testDefaultInstanceWithOracle() {
61+
void defaultInstanceWithOracle() {
6262
SQLErrorCodes sec = SQLErrorCodesFactory.getInstance().getErrorCodes("Oracle");
6363
assertIsOracle(sec);
6464
}
6565

6666
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();
6969
// 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");
7272
// This had better NOT be
73-
assertThat(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "9xx42")).isLessThan(0);
73+
assertThat(sec.getBadSqlGrammarCodes()).doesNotContain("9xx42");
7474
}
7575

7676
private void assertIsSQLServer(SQLErrorCodes sec) {
7777
assertThat(sec.getDatabaseProductName()).isEqualTo("Microsoft SQL Server");
7878

79-
assertThat(sec.getBadSqlGrammarCodes().length).isGreaterThan(0);
79+
assertThat(sec.getBadSqlGrammarCodes()).isNotEmpty();
8080

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");
8787

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");
9090

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");
9494

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");
9999

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");
102102

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");
105105

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");
108108
}
109109

110110
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();
113113
// This had better be a Bad SQL Grammar code
114-
assertThat(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "-22")).isGreaterThanOrEqualTo(0);
114+
assertThat(sec.getBadSqlGrammarCodes()).contains("-22");
115115
// This had better NOT be
116-
assertThat(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "-9")).isLessThan(0);
116+
assertThat(sec.getBadSqlGrammarCodes()).doesNotContain("-9");
117117
}
118118

119119
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();
122122

123-
assertThat(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "942")).isLessThan(0);
123+
assertThat(sec.getBadSqlGrammarCodes()).doesNotContain("942");
124124
// This had better NOT be
125-
assertThat(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "-204")).isGreaterThanOrEqualTo(0);
125+
assertThat(sec.getBadSqlGrammarCodes()).contains("-204");
126126
}
127127

128128
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");
142141
}
143142

144143
@Test
145-
public void testLookupOrder() {
144+
void lookupOrder() {
146145
class TestSQLErrorCodesFactory extends SQLErrorCodesFactory {
147146
private int lookups = 0;
148147
@Override
@@ -163,15 +162,15 @@ protected Resource loadResource(String path) {
163162

164163
// Should have failed to load without error
165164
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();
168167
}
169168

170169
/**
171170
* Check that user defined error codes take precedence.
172171
*/
173172
@Test
174-
public void testFindUserDefinedCodes() {
173+
void findUserDefinedCodes() {
175174
class TestSQLErrorCodesFactory extends SQLErrorCodesFactory {
176175
@Override
177176
protected Resource loadResource(String path) {
@@ -184,14 +183,12 @@ protected Resource loadResource(String path) {
184183

185184
// Should have loaded without error
186185
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");
191188
}
192189

193190
@Test
194-
public void testInvalidUserDefinedCodeFormat() {
191+
void invalidUserDefinedCodeFormat() {
195192
class TestSQLErrorCodesFactory extends SQLErrorCodesFactory {
196193
@Override
197194
protected Resource loadResource(String path) {
@@ -205,15 +202,15 @@ protected Resource loadResource(String path) {
205202

206203
// Should have failed to load without error
207204
TestSQLErrorCodesFactory sf = new TestSQLErrorCodesFactory();
208-
assertThat(sf.getErrorCodes("XX").getBadSqlGrammarCodes().length).isEqualTo(0);
205+
assertThat(sf.getErrorCodes("XX").getBadSqlGrammarCodes()).isEmpty();
209206
assertThat(sf.getErrorCodes("Oracle").getBadSqlGrammarCodes()).isEmpty();
210207
}
211208

212209
/**
213210
* Check that custom error codes take precedence.
214211
*/
215212
@Test
216-
public void testFindCustomCodes() {
213+
void findCustomCodes() {
217214
class TestSQLErrorCodesFactory extends SQLErrorCodesFactory {
218215
@Override
219216
protected Resource loadResource(String path) {
@@ -227,14 +224,13 @@ protected Resource loadResource(String path) {
227224
// Should have loaded without error
228225
TestSQLErrorCodesFactory sf = new TestSQLErrorCodesFactory();
229226
assertThat(sf.getErrorCodes("Oracle").getCustomTranslations()).hasSize(1);
230-
CustomSQLErrorCodesTranslation translation =
231-
sf.getErrorCodes("Oracle").getCustomTranslations()[0];
227+
CustomSQLErrorCodesTranslation translation = sf.getErrorCodes("Oracle").getCustomTranslations()[0];
232228
assertThat(translation.getExceptionClass()).isEqualTo(CustomErrorCodeException.class);
233229
assertThat(translation.getErrorCodes()).hasSize(1);
234230
}
235231

236232
@Test
237-
public void testDataSourceWithNullMetadata() throws Exception {
233+
void dataSourceWithNullMetadata() throws Exception {
238234
Connection connection = mock();
239235
DataSource dataSource = mock();
240236
given(dataSource.getConnection()).willReturn(connection);
@@ -250,7 +246,7 @@ public void testDataSourceWithNullMetadata() throws Exception {
250246
}
251247

252248
@Test
253-
public void testGetFromDataSourceWithSQLException() throws Exception {
249+
void getFromDataSourceWithSQLException() throws Exception {
254250
SQLException expectedSQLException = new SQLException();
255251

256252
DataSource dataSource = mock();
@@ -284,25 +280,25 @@ private SQLErrorCodes getErrorCodesFromDataSource(String productName, SQLErrorCo
284280
}
285281

286282
@Test
287-
public void testSQLServerRecognizedFromMetadata() throws Exception {
283+
void sqlServerRecognizedFromMetadata() throws Exception {
288284
SQLErrorCodes sec = getErrorCodesFromDataSource("MS-SQL", null);
289285
assertIsSQLServer(sec);
290286
}
291287

292288
@Test
293-
public void testOracleRecognizedFromMetadata() throws Exception {
289+
void oracleRecognizedFromMetadata() throws Exception {
294290
SQLErrorCodes sec = getErrorCodesFromDataSource("Oracle", null);
295291
assertIsOracle(sec);
296292
}
297293

298294
@Test
299-
public void testHsqlRecognizedFromMetadata() throws Exception {
295+
void hsqlRecognizedFromMetadata() throws Exception {
300296
SQLErrorCodes sec = getErrorCodesFromDataSource("HSQL Database Engine", null);
301297
assertIsHsql(sec);
302298
}
303299

304300
@Test
305-
public void testDB2RecognizedFromMetadata() throws Exception {
301+
void dB2RecognizedFromMetadata() throws Exception {
306302
SQLErrorCodes sec = getErrorCodesFromDataSource("DB2", null);
307303
assertIsDB2(sec);
308304
sec = getErrorCodesFromDataSource("DB2/", null);
@@ -312,7 +308,7 @@ public void testDB2RecognizedFromMetadata() throws Exception {
312308
}
313309

314310
@Test
315-
public void testHanaIsRecognizedFromMetadata() throws Exception {
311+
void hanaIsRecognizedFromMetadata() throws Exception {
316312
SQLErrorCodes sec = getErrorCodesFromDataSource("SAP DB", null);
317313
assertIsHana(sec);
318314
}
@@ -321,7 +317,7 @@ public void testHanaIsRecognizedFromMetadata() throws Exception {
321317
* Check that wild card database name works.
322318
*/
323319
@Test
324-
public void testWildCardNameRecognized() throws Exception {
320+
void wildCardNameRecognized() throws Exception {
325321
class WildcardSQLErrorCodesFactory extends SQLErrorCodesFactory {
326322
@Override
327323
protected Resource loadResource(String path) {

0 commit comments

Comments
 (0)