Browse Source

Polishing

pull/1884/head
Juergen Hoeller 7 years ago
parent
commit
c04c8a2472
  1. 4
      spring-core/src/main/java/org/springframework/util/LinkedCaseInsensitiveMap.java
  2. 11
      spring-jdbc/src/main/java/org/springframework/jdbc/core/ColumnMapRowMapper.java
  3. 1
      spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/TableMetaDataContext.java
  4. 3
      spring-jdbc/src/main/java/org/springframework/jdbc/support/JdbcUtils.java
  5. 211
      spring-jdbc/src/test/java/org/springframework/jdbc/core/JdbcTemplateTests.java
  6. 2
      spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompHeaders.java
  7. 4
      spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/DefaultPersistenceUnitManager.java

4
spring-core/src/main/java/org/springframework/util/LinkedCaseInsensitiveMap.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -151,6 +151,7 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable @@ -151,6 +151,7 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable
}
@Override
@Nullable
public V getOrDefault(Object key, V defaultValue) {
if (key instanceof String) {
String caseInsensitiveKey = this.caseInsensitiveKeys.get(convertKey((String) key));
@ -162,6 +163,7 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable @@ -162,6 +163,7 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable
}
@Override
@Nullable
public V put(String key, @Nullable V value) {
String oldKey = this.caseInsensitiveKeys.put(convertKey(key), key);
if (oldKey != null && !oldKey.equals(key)) {

11
spring-jdbc/src/main/java/org/springframework/jdbc/core/ColumnMapRowMapper.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -52,13 +52,12 @@ public class ColumnMapRowMapper implements RowMapper<Map<String, Object>> { @@ -52,13 +52,12 @@ public class ColumnMapRowMapper implements RowMapper<Map<String, Object>> {
public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
Map<String, Object> mapOfColValues = createColumnMap(columnCount);
Map<String, Object> mapOfColumnValues = createColumnMap(columnCount);
for (int i = 1; i <= columnCount; i++) {
String key = getColumnKey(JdbcUtils.lookupColumnName(rsmd, i));
Object obj = getColumnValue(rs, i);
mapOfColValues.put(key, obj);
String column = JdbcUtils.lookupColumnName(rsmd, i);
mapOfColumnValues.put(getColumnKey(column), getColumnValue(rs, i));
}
return mapOfColValues;
return mapOfColumnValues;
}
/**

1
spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/TableMetaDataContext.java

@ -253,7 +253,6 @@ public class TableMetaDataContext { @@ -253,7 +253,6 @@ public class TableMetaDataContext {
for (Map.Entry<String, ?> entry : inParameters.entrySet()) {
if (column.equalsIgnoreCase(entry.getKey())) {
value = entry.getValue();
// TODO: break;
}
}
}

3
spring-jdbc/src/main/java/org/springframework/jdbc/support/JdbcUtils.java

@ -37,6 +37,7 @@ import org.springframework.jdbc.CannotGetJdbcConnectionException; @@ -37,6 +37,7 @@ import org.springframework.jdbc.CannotGetJdbcConnectionException;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.NumberUtils;
import org.springframework.util.StringUtils;
/**
* Generic utility methods for working with JDBC. Mainly for internal use
@ -452,7 +453,7 @@ public abstract class JdbcUtils { @@ -452,7 +453,7 @@ public abstract class JdbcUtils {
*/
public static String lookupColumnName(ResultSetMetaData resultSetMetaData, int columnIndex) throws SQLException {
String name = resultSetMetaData.getColumnLabel(columnIndex);
if (name == null || name.length() < 1) {
if (!StringUtils.hasLength(name)) {
name = resultSetMetaData.getColumnName(columnIndex);
}
return name;

211
spring-jdbc/src/test/java/org/springframework/jdbc/core/JdbcTemplateTests.java

@ -28,6 +28,7 @@ import java.sql.Statement; @@ -28,6 +28,7 @@ import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@ -151,69 +152,37 @@ public class JdbcTemplateTests { @@ -151,69 +152,37 @@ public class JdbcTemplateTests {
@Test
public void testStringsWithStaticSql() throws Exception {
doTestStrings(null, null, null, null, new JdbcTemplateCallback() {
@Override
public void doInJdbcTemplate(JdbcTemplate template, String sql, RowCallbackHandler rch) {
template.query(sql, rch);
}
});
doTestStrings(null, null, null, null, (template, sql, rch) -> template.query(sql, rch));
}
@Test
public void testStringsWithStaticSqlAndFetchSizeAndMaxRows() throws Exception {
doTestStrings(10, 20, 30, null, new JdbcTemplateCallback() {
@Override
public void doInJdbcTemplate(JdbcTemplate template, String sql, RowCallbackHandler rch) {
template.query(sql, rch);
}
});
doTestStrings(10, 20, 30, null, (template, sql, rch) -> template.query(sql, rch));
}
@Test
public void testStringsWithEmptyPreparedStatementSetter() throws Exception {
doTestStrings(null, null, null, null, new JdbcTemplateCallback() {
@Override
public void doInJdbcTemplate(JdbcTemplate template, String sql, RowCallbackHandler rch) {
template.query(sql, (PreparedStatementSetter) null, rch);
}
});
doTestStrings(null, null, null, null, (template, sql, rch) ->
template.query(sql, (PreparedStatementSetter) null, rch));
}
@Test
public void testStringsWithPreparedStatementSetter() throws Exception {
final Integer argument = 99;
doTestStrings(null, null, null, argument, new JdbcTemplateCallback() {
@Override
public void doInJdbcTemplate(JdbcTemplate template, String sql, RowCallbackHandler rch) {
template.query(sql, new PreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps) throws SQLException {
ps.setObject(1, argument);
}
}, rch);
}
});
doTestStrings(null, null, null, argument, (template, sql, rch) -> template.query(sql, ps -> {
ps.setObject(1, argument);
}, rch));
}
@Test
public void testStringsWithEmptyPreparedStatementArgs() throws Exception {
doTestStrings(null, null, null, null, new JdbcTemplateCallback() {
@Override
public void doInJdbcTemplate(JdbcTemplate template, String sql, RowCallbackHandler rch) {
template.query(sql, (Object[]) null, rch);
}
});
doTestStrings(null, null, null, null, (template, sql, rch) -> template.query(sql, (Object[]) null, rch));
}
@Test
public void testStringsWithPreparedStatementArgs() throws Exception {
final Integer argument = 99;
doTestStrings(null, null, null, argument, new JdbcTemplateCallback() {
@Override
public void doInJdbcTemplate(JdbcTemplate template, String sql, RowCallbackHandler rch) {
template.query(sql, new Object[] { argument }, rch);
}
});
doTestStrings(null, null, null, argument, (template, sql, rch) -> template.query(sql, new Object[] { argument }, rch));
}
private void doTestStrings(Integer fetchSize, Integer maxRows, Integer queryTimeout,
@ -355,11 +324,8 @@ public class JdbcTemplateTests { @@ -355,11 +324,8 @@ public class JdbcTemplateTests {
this.thrown.expect(sameInstance(runtimeException));
try {
this.template.query(sql, new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) {
throw runtimeException;
}
this.template.query(sql, (RowCallbackHandler) rs -> {
throw runtimeException;
});
}
finally {
@ -462,7 +428,7 @@ public class JdbcTemplateTests { @@ -462,7 +428,7 @@ public class JdbcTemplateTests {
final String[] sql = {"A", "B", "C", "D"};
given(this.statement.executeBatch()).willThrow(
new BatchUpdateException(new int[] { 1, Statement.EXECUTE_FAILED, 1,
Statement.EXECUTE_FAILED }));
Statement.EXECUTE_FAILED }));
mockDatabaseMetaData(true);
given(this.connection.createStatement()).willReturn(this.statement);
@ -530,17 +496,17 @@ public class JdbcTemplateTests { @@ -530,17 +496,17 @@ public class JdbcTemplateTests {
mockDatabaseMetaData(true);
BatchPreparedStatementSetter setter =
new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i)
throws SQLException {
ps.setInt(1, ids[i]);
}
@Override
public int getBatchSize() {
return ids.length;
}
};
new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i)
throws SQLException {
ps.setInt(1, ids[i]);
}
@Override
public int getBatchSize() {
return ids.length;
}
};
JdbcTemplate template = new JdbcTemplate(this.dataSource, false);
@ -739,10 +705,10 @@ public class JdbcTemplateTests { @@ -739,10 +705,10 @@ public class JdbcTemplateTests {
@Test
public void testBatchUpdateWithListOfObjectArrays() throws Exception {
final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?";
final List<Object[]> ids = new ArrayList<>();
final List<Object[]> ids = new ArrayList<>(2);
ids.add(new Object[] {100});
ids.add(new Object[] {200});
final int[] rowsAffected = new int[] { 1, 2 };
final int[] rowsAffected = new int[] {1, 2};
given(this.preparedStatement.executeBatch()).willReturn(rowsAffected);
mockDatabaseMetaData(true);
@ -765,11 +731,11 @@ public class JdbcTemplateTests { @@ -765,11 +731,11 @@ public class JdbcTemplateTests {
@Test
public void testBatchUpdateWithListOfObjectArraysPlusTypeInfo() throws Exception {
final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?";
final List<Object[]> ids = new ArrayList<>();
final List<Object[]> ids = new ArrayList<>(2);
ids.add(new Object[] {100});
ids.add(new Object[] {200});
final int[] sqlTypes = new int[] {Types.NUMERIC};
final int[] rowsAffected = new int[] { 1, 2 };
final int[] rowsAffected = new int[] {1, 2};
given(this.preparedStatement.executeBatch()).willReturn(rowsAffected);
mockDatabaseMetaData(true);
@ -797,17 +763,11 @@ public class JdbcTemplateTests { @@ -797,17 +763,11 @@ public class JdbcTemplateTests {
given(this.preparedStatement.executeBatch()).willReturn(rowsAffected1, rowsAffected2);
mockDatabaseMetaData(true);
ParameterizedPreparedStatementSetter<Integer> setter = new ParameterizedPreparedStatementSetter<Integer>() {
@Override
public void setValues(PreparedStatement ps, Integer argument) throws SQLException {
ps.setInt(1, argument.intValue());
}
};
ParameterizedPreparedStatementSetter<Integer> setter = (ps, argument) -> ps.setInt(1, argument.intValue());
JdbcTemplate template = new JdbcTemplate(this.dataSource, false);
int[][] actualRowsAffected = template.batchUpdate(sql, ids, 2, setter);
assertTrue("executed 2 updates", actualRowsAffected[0].length == 2);
assertEquals("executed 2 updates", 2, actualRowsAffected[0].length);
assertEquals(rowsAffected1[0], actualRowsAffected[0][0]);
assertEquals(rowsAffected1[1], actualRowsAffected[0][1]);
assertEquals(rowsAffected2[0], actualRowsAffected[1][0]);
@ -895,14 +855,9 @@ public class JdbcTemplateTests { @@ -895,14 +855,9 @@ public class JdbcTemplateTests {
given(this.preparedStatement.executeUpdate()).willReturn(expectedRowsUpdated);
PreparedStatementSetter pss = new PreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps) throws SQLException {
ps.setString(1, name);
}
};
PreparedStatementSetter pss = ps -> ps.setString(1, name);
int actualRowsUpdated = new JdbcTemplate(this.dataSource).update(sql, pss);
assertTrue("updated correct # of rows", actualRowsUpdated == expectedRowsUpdated);
assertEquals("updated correct # of rows", actualRowsUpdated, expectedRowsUpdated);
verify(this.preparedStatement).setString(1, name);
verify(this.preparedStatement).close();
verify(this.connection).close();
@ -915,12 +870,7 @@ public class JdbcTemplateTests { @@ -915,12 +870,7 @@ public class JdbcTemplateTests {
SQLException sqlException = new SQLException();
given(this.preparedStatement.executeUpdate()).willThrow(sqlException);
PreparedStatementSetter pss = new PreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps) throws SQLException {
ps.setString(1, name);
}
};
PreparedStatementSetter pss = ps -> ps.setString(1, name);
this.thrown.expect(DataAccessException.class);
this.thrown.expect(exceptionCause(sameInstance(sqlException)));
try {
@ -964,11 +914,8 @@ public class JdbcTemplateTests { @@ -964,11 +914,8 @@ public class JdbcTemplateTests {
this.thrown.expect(SQLWarningException.class);
this.thrown.expect(exceptionCause(sameInstance(warnings)));
try {
t.query(sql, new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
rs.getByte(1);
}
t.query(sql, rs -> {
rs.getByte(1);
});
}
finally {
@ -990,11 +937,8 @@ public class JdbcTemplateTests { @@ -990,11 +937,8 @@ public class JdbcTemplateTests {
// Too long: truncation
this.template.setIgnoreWarnings(true);
this.template.query(sql, new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws java.sql.SQLException {
rs.getByte(1);
}
this.template.query(sql, rs -> {
rs.getByte(1);
});
verify(this.resultSet).close();
@ -1014,11 +958,8 @@ public class JdbcTemplateTests { @@ -1014,11 +958,8 @@ public class JdbcTemplateTests {
this.thrown.expect(BadSqlGrammarException.class);
this.thrown.expect(exceptionCause(sameInstance(sqlException)));
try {
this.template.query(sql, new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
throw sqlException;
}
this.template.query(sql, (RowCallbackHandler) rs -> {
throw sqlException;
});
fail("Should have thrown BadSqlGrammarException");
}
@ -1045,11 +986,8 @@ public class JdbcTemplateTests { @@ -1045,11 +986,8 @@ public class JdbcTemplateTests {
this.thrown.expect(BadSqlGrammarException.class);
this.thrown.expect(exceptionCause(sameInstance(sqlException)));
try {
template.query(sql, new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
throw sqlException;
}
template.query(sql, (RowCallbackHandler) rs -> {
throw sqlException;
});
}
finally {
@ -1082,11 +1020,8 @@ public class JdbcTemplateTests { @@ -1082,11 +1020,8 @@ public class JdbcTemplateTests {
this.thrown.expect(BadSqlGrammarException.class);
this.thrown.expect(exceptionCause(sameInstance(sqlException)));
try {
template.query(sql, new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
throw sqlException;
}
template.query(sql, (RowCallbackHandler) rs -> {
throw sqlException;
});
}
finally {
@ -1104,34 +1039,23 @@ public class JdbcTemplateTests { @@ -1104,34 +1039,23 @@ public class JdbcTemplateTests {
given(this.connection.createStatement()).willReturn(this.statement);
try {
this.template.query("my query", new ResultSetExtractor<Object>() {
@Override
public Object extractData(ResultSet rs) {
throw new InvalidDataAccessApiUsageException("");
}
this.template.query("my query", (ResultSetExtractor<Object>) rs -> {
throw new InvalidDataAccessApiUsageException("");
});
fail("Should have thrown InvalidDataAccessApiUsageException");
}
catch (InvalidDataAccessApiUsageException idaauex) {
catch (InvalidDataAccessApiUsageException ex) {
// ok
}
try {
this.template.query(new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection con)
throws SQLException {
return con.prepareStatement("my query");
}
}, new ResultSetExtractor<Object>() {
@Override
public Object extractData(ResultSet rs2) {
throw new InvalidDataAccessApiUsageException("");
}
});
this.template.query(con -> con.prepareStatement("my query"),
(ResultSetExtractor<Object>) rs2 -> {
throw new InvalidDataAccessApiUsageException("");
} );
fail("Should have thrown InvalidDataAccessApiUsageException");
}
catch (InvalidDataAccessApiUsageException idaauex) {
catch (InvalidDataAccessApiUsageException ex) {
// ok
}
@ -1147,24 +1071,13 @@ public class JdbcTemplateTests { @@ -1147,24 +1071,13 @@ public class JdbcTemplateTests {
given(this.callableStatement.execute()).willReturn(true);
given(this.callableStatement.getUpdateCount()).willReturn(-1);
List<SqlParameter> params = new ArrayList<>();
params.add(new SqlReturnResultSet("", new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) {
throw new InvalidDataAccessApiUsageException("");
}
}));
SqlParameter param = new SqlReturnResultSet("", (RowCallbackHandler) rs -> {
throw new InvalidDataAccessApiUsageException("");
});
this.thrown.expect(InvalidDataAccessApiUsageException.class);
try {
this.template.call(new CallableStatementCreator() {
@Override
public CallableStatement createCallableStatement(Connection conn)
throws SQLException {
return conn.prepareCall("my query");
}
}, params);
this.template.call(conn -> conn.prepareCall("my query"), Collections.singletonList(param));
}
finally {
verify(this.resultSet).close();
@ -1175,7 +1088,6 @@ public class JdbcTemplateTests { @@ -1175,7 +1088,6 @@ public class JdbcTemplateTests {
@Test
public void testCaseInsensitiveResultsMap() throws Exception {
given(this.callableStatement.execute()).willReturn(false);
given(this.callableStatement.getUpdateCount()).willReturn(-1);
given(this.callableStatement.getObject(1)).willReturn("X");
@ -1187,16 +1099,8 @@ public class JdbcTemplateTests { @@ -1187,16 +1099,8 @@ public class JdbcTemplateTests {
assertTrue("now it should have been set to case insensitive",
this.template.isResultsMapCaseInsensitive());
List<SqlParameter> params = new ArrayList<>();
params.add(new SqlOutParameter("a", 12));
Map<String, Object> out = this.template.call(new CallableStatementCreator() {
@Override
public CallableStatement createCallableStatement(Connection conn)
throws SQLException {
return conn.prepareCall("my query");
}
}, params);
Map<String, Object> out = this.template.call(
conn -> conn.prepareCall("my query"), Collections.singletonList(new SqlOutParameter("a", 12)));
assertThat(out, instanceOf(LinkedCaseInsensitiveMap.class));
assertNotNull("we should have gotten the result with upper case", out.get("A"));
@ -1205,6 +1109,7 @@ public class JdbcTemplateTests { @@ -1205,6 +1109,7 @@ public class JdbcTemplateTests {
verify(this.connection).close();
}
private void mockDatabaseMetaData(boolean supportsBatchUpdates) throws SQLException {
DatabaseMetaData databaseMetaData = mock(DatabaseMetaData.class);
given(databaseMetaData.getDatabaseProductName()).willReturn("MySQL");

2
spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompHeaders.java

@ -203,7 +203,7 @@ public class StompHeaders implements MultiValueMap<String, String>, Serializable @@ -203,7 +203,7 @@ public class StompHeaders implements MultiValueMap<String, String>, Serializable
* Applies to the CONNECT frame.
* @since 5.0.7
*/
public void setAcceptVersion(@Nullable String[] acceptVersions) {
public void setAcceptVersion(@Nullable String... acceptVersions) {
if (ObjectUtils.isEmpty(acceptVersions)) {
set(ACCEPT_VERSION, null);
return;

4
spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/DefaultPersistenceUnitManager.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -115,7 +115,7 @@ public class DefaultPersistenceUnitManager @@ -115,7 +115,7 @@ public class DefaultPersistenceUnitManager
private static final Set<AnnotationTypeFilter> entityTypeFilters;
static {
entityTypeFilters = new LinkedHashSet<>(4);
entityTypeFilters = new LinkedHashSet<>(8);
entityTypeFilters.add(new AnnotationTypeFilter(Entity.class, false));
entityTypeFilters.add(new AnnotationTypeFilter(Embeddable.class, false));
entityTypeFilters.add(new AnnotationTypeFilter(MappedSuperclass.class, false));

Loading…
Cancel
Save