Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toString for AbstractSqlParameterSource #2080

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

import java.util.HashMap;
import java.util.Map;
import java.util.StringJoiner;

import org.springframework.jdbc.core.SqlParameterValue;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

Expand All @@ -27,6 +29,7 @@
* Provides registration of SQL types per parameter.
*
* @author Juergen Hoeller
* @author Jens Schauder
* @since 2.0
*/
public abstract class AbstractSqlParameterSource implements SqlParameterSource {
Expand Down Expand Up @@ -81,4 +84,21 @@ public String getTypeName(String paramName) {
return this.typeNames.get(paramName);
}

@Override
public String toString() {
String prefix = this.getClass().getSimpleName() + "[";
StringJoiner stringJoiner = new StringJoiner(", ", prefix, "]");
String[] parameterNames = this.getParameterNames();
if (parameterNames != null) {
for (String name : parameterNames) {
String typeName = this.getTypeName(name);
Object value = this.getValue(name);
if (value instanceof SqlParameterValue) {
value = ((SqlParameterValue) value).getValue();
}
stringJoiner.add(name + "=" + value + " (sqlType: " + (typeName == null ? this.getSqlType(name) : typeName) + ")");
}
}
return stringJoiner.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2019 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.
Expand All @@ -19,6 +19,7 @@
import java.sql.Types;
import java.util.Arrays;

import org.hamcrest.Matchers;
import org.junit.Test;

import org.springframework.tests.sample.beans.TestBean;
Expand All @@ -29,6 +30,7 @@
* @author Rick Evans
* @author Juergen Hoeller
* @author Arjen Poutsma
* @author Jens Schauder
*/
public class BeanPropertySqlParameterSourceTests {

Expand Down Expand Up @@ -82,6 +84,15 @@ public void hasValueWhereTheUnderlyingBeanPropertyIsNotReadable() throws Excepti
assertFalse(source.hasValue("noOp"));
}

@Test
public void toStringShowsParameterDetails() {
BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource(new TestBean("tb", 99));
assertThat(source.toString(), Matchers.allOf(
Matchers.containsString("BeanPropertySqlParameterSource"),
Matchers.containsString("name=tb (sqlType: 12)"),
Matchers.containsString("age=99 (sqlType: 4)"))
);
}

@SuppressWarnings("unused")
private static final class NoReadableProperties {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@

package org.springframework.jdbc.core.namedparam;

import org.hamcrest.Matchers;
import org.junit.Test;

import org.springframework.jdbc.core.SqlParameterValue;
import org.springframework.tests.sample.beans.TestBean;

import static org.junit.Assert.*;

/**
* @author Rick Evans
* @author Arjen Poutsma
* @author Jens Schauder
*/
public class MapSqlParameterSourceTests {

Expand All @@ -48,4 +51,12 @@ public void sqlParameterValueRegistersSqlType() throws Exception {
assertEquals("Correct SQL Type not registered", 2, msps2.getSqlType("FOO"));
}

@Test
public void toStringShowsParameterDetails() {
MapSqlParameterSource source = new MapSqlParameterSource("FOO", new SqlParameterValue(2, "Foo"));
assertThat(source.toString(), Matchers.allOf(
Matchers.containsString("MapSqlParameterSource"),
Matchers.containsString("FOO=Foo (sqlType: 2)"))
);
}
}