INT-4291 Add ExprEvalSqlPSF.setSqlParameterTypes

JIRA: https://jira.spring.io/browse/INT-4291

* Fix checkstyle trailing space
*Use correct way to constants in spring xml and fix test case

* Polishing - Update javadoc and rename method name to relate more to the behavior
This commit is contained in:
Meherzad Lahewala
2017-11-04 19:32:44 +05:30
committed by Artem Bilan
parent d76af8c2aa
commit 854dba1366
5 changed files with 107 additions and 10 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -20,6 +20,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import java.sql.Types;
import java.util.Arrays;
import java.util.Collections;
@@ -27,10 +28,11 @@ import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.JdbcUtils;
/**
* @author Dave Syer
*
* @author Meherzad Lahewala
*/
public class ExpressionEvaluatingSqlParameterSourceFactoryTests {
@@ -44,6 +46,7 @@ public class ExpressionEvaluatingSqlParameterSourceFactoryTests {
SqlParameterSource source = factory.createParameterSource(null);
assertTrue(source.hasValue("foo"));
assertEquals("bar", source.getValue("foo"));
assertEquals(JdbcUtils.TYPE_UNKNOWN, source.getSqlType("foo"));
}
@Test
@@ -53,18 +56,19 @@ public class ExpressionEvaluatingSqlParameterSourceFactoryTests {
SqlParameterSource source = factory.createParameterSource(Collections.singletonMap("foo", "bar"));
assertTrue(source.hasValue("foo"));
assertEquals("bar", source.getValue("foo"));
assertEquals(JdbcUtils.TYPE_UNKNOWN, source.getSqlType("foo"));
}
@Test
public void testListOfMapsInput() throws Exception {
factory.setBeanFactory(mock(BeanFactory.class));
factory.afterPropertiesSet();
@SuppressWarnings("unchecked")
SqlParameterSource source = factory.createParameterSource(Arrays.asList(Collections.singletonMap("foo", "bar"),
Collections.singletonMap("foo", "bucket")));
String expression = "foo";
assertTrue(source.hasValue(expression));
assertEquals("[bar, bucket]", source.getValue(expression).toString());
assertEquals(JdbcUtils.TYPE_UNKNOWN, source.getSqlType(expression));
}
@Test
@@ -75,6 +79,7 @@ public class ExpressionEvaluatingSqlParameterSourceFactoryTests {
// This is an illegal parameter name in Spring JDBC so we'd never get this as input
assertTrue(source.hasValue("foo.toUpperCase()"));
assertEquals("BAR", source.getValue("foo.toUpperCase()"));
assertEquals(JdbcUtils.TYPE_UNKNOWN, source.getSqlType("food"));
}
@Test
@@ -85,6 +90,7 @@ public class ExpressionEvaluatingSqlParameterSourceFactoryTests {
SqlParameterSource source = factory.createParameterSource(Collections.singletonMap("foo", "bar"));
assertTrue(source.hasValue("spam"));
assertEquals("BAR", source.getValue("spam"));
assertEquals(JdbcUtils.TYPE_UNKNOWN, source.getSqlType("spam"));
}
@Test
@@ -96,6 +102,7 @@ public class ExpressionEvaluatingSqlParameterSourceFactoryTests {
SqlParameterSource source = factory.createParameterSource(Collections.singletonMap("crap", "bucket"));
assertTrue(source.hasValue("spam"));
assertEquals("BAR", source.getValue("spam"));
assertEquals(JdbcUtils.TYPE_UNKNOWN, source.getSqlType("spam"));
}
@Test
@@ -103,12 +110,39 @@ public class ExpressionEvaluatingSqlParameterSourceFactoryTests {
factory.setParameterExpressions(Collections.singletonMap("spam", "foo.toUpperCase()"));
factory.setBeanFactory(mock(BeanFactory.class));
factory.afterPropertiesSet();
@SuppressWarnings("unchecked")
SqlParameterSource source = factory.createParameterSource(Arrays.asList(Collections.singletonMap("foo", "bar"),
Collections.singletonMap("foo", "bucket")));
String expression = "spam";
assertTrue(source.hasValue(expression));
assertEquals("[BAR, BUCKET]", source.getValue(expression).toString());
assertEquals(JdbcUtils.TYPE_UNKNOWN, source.getSqlType("foo"));
}
@Test
public void testListOfMapsInputWithExpressionAndTypes() throws Exception {
factory.setParameterExpressions(Collections.singletonMap("spam", "foo.toUpperCase()"));
factory.setBeanFactory(mock(BeanFactory.class));
factory.setSqlParameterTypes(Collections.singletonMap("spam", Types.SQLXML));
factory.afterPropertiesSet();
SqlParameterSource source = factory.createParameterSource(Arrays.asList(Collections.singletonMap("foo", "bar"),
Collections.singletonMap("foo", "bucket")));
String expression = "spam";
assertTrue(source.hasValue(expression));
assertEquals("[BAR, BUCKET]", source.getValue(expression).toString());
assertEquals(Types.SQLXML, source.getSqlType("spam"));
}
@Test
public void testListOfMapsInputWithExpressionAndEmptyTypes() throws Exception {
factory.setParameterExpressions(Collections.singletonMap("spam", "foo.toUpperCase()"));
factory.setBeanFactory(mock(BeanFactory.class));
factory.setSqlParameterTypes(Collections.emptyMap());
factory.afterPropertiesSet();
SqlParameterSource source = factory.createParameterSource(Arrays.asList(Collections.singletonMap("foo", "bar"),
Collections.singletonMap("foo", "bucket")));
String expression = "spam";
assertTrue(source.hasValue(expression));
assertEquals("[BAR, BUCKET]", source.getValue(expression).toString());
assertEquals(JdbcUtils.TYPE_UNKNOWN, source.getSqlType("spam"));
}
}

View File

@@ -15,17 +15,22 @@
update="delete from item" />
<beans:import resource="jdbcInboundChannelAdapterCommonConfig.xml" />
<beans:bean id="parameterSource" factory-bean="parameterSourceFactory" factory-method="createParameterSourceNoCache">
<beans:constructor-arg value="" />
</beans:bean>
<beans:bean id="parameterSourceFactory" class="org.springframework.integration.jdbc.ExpressionEvaluatingSqlParameterSourceFactory">
<beans:property name="parameterExpressions">
<beans:map>
<beans:entry key="status" value="@statusBean.which()" />
</beans:map>
</beans:property>
<beans:property name="sqlParameterTypes">
<beans:map>
<beans:entry key="status" value="#{ T(java.sql.Types).INTEGER}" />
</beans:map>
</beans:property>
</beans:bean>
<beans:bean id="statusBean" class="org.springframework.integration.jdbc.config.JdbcPollingChannelAdapterParserTests$Status" />