|
|
|
|
@ -19,73 +19,68 @@ package org.springframework.jdbc.core.namedparam;
@@ -19,73 +19,68 @@ package org.springframework.jdbc.core.namedparam;
|
|
|
|
|
import java.sql.Types; |
|
|
|
|
import java.util.Arrays; |
|
|
|
|
|
|
|
|
|
import junit.framework.TestCase; |
|
|
|
|
import static org.junit.Assert.*; |
|
|
|
|
import org.junit.Test; |
|
|
|
|
|
|
|
|
|
import org.springframework.beans.TestBean; |
|
|
|
|
import org.springframework.test.AssertThrows; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @author Rick Evans |
|
|
|
|
* @author Juergen Hoeller |
|
|
|
|
* @author Arjen Poutsma |
|
|
|
|
*/ |
|
|
|
|
public class BeanPropertySqlParameterSourceTests extends TestCase { |
|
|
|
|
public class BeanPropertySqlParameterSourceTests { |
|
|
|
|
|
|
|
|
|
public void testWithNullBeanPassedToCtor() throws Exception { |
|
|
|
|
new AssertThrows(IllegalArgumentException.class) { |
|
|
|
|
public void test() throws Exception { |
|
|
|
|
new BeanPropertySqlParameterSource(null); |
|
|
|
|
} |
|
|
|
|
}.runTest(); |
|
|
|
|
@Test(expected = IllegalArgumentException.class) |
|
|
|
|
public void withNullBeanPassedToCtor() throws Exception { |
|
|
|
|
new BeanPropertySqlParameterSource(null); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void testGetValueWhereTheUnderlyingBeanHasNoSuchProperty() throws Exception { |
|
|
|
|
new AssertThrows(IllegalArgumentException.class) { |
|
|
|
|
public void test() throws Exception { |
|
|
|
|
BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource(new TestBean()); |
|
|
|
|
source.getValue("thisPropertyDoesNotExist"); |
|
|
|
|
} |
|
|
|
|
}.runTest(); |
|
|
|
|
@Test(expected = IllegalArgumentException.class) |
|
|
|
|
public void getValueWhereTheUnderlyingBeanHasNoSuchProperty() throws Exception { |
|
|
|
|
BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource(new TestBean()); |
|
|
|
|
source.getValue("thisPropertyDoesNotExist"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void testSuccessfulPropertyAccess(){ |
|
|
|
|
@Test |
|
|
|
|
public void successfulPropertyAccess() { |
|
|
|
|
BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource(new TestBean("tb", 99)); |
|
|
|
|
assertTrue(Arrays.asList(source.getReadablePropertyNames()).contains("name")); |
|
|
|
|
assertTrue(Arrays.asList(source.getReadablePropertyNames()).contains("age")); |
|
|
|
|
assertEquals("tb", source.getValue("name")); |
|
|
|
|
assertEquals(new Integer(99), source.getValue("age")); |
|
|
|
|
assertEquals(99, source.getValue("age")); |
|
|
|
|
assertEquals(Types.VARCHAR, source.getSqlType("name")); |
|
|
|
|
assertEquals(Types.INTEGER, source.getSqlType("age")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void testSuccessfulPropertyAccessWithOverriddenSqlType(){ |
|
|
|
|
@Test |
|
|
|
|
public void successfulPropertyAccessWithOverriddenSqlType() { |
|
|
|
|
BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource(new TestBean("tb", 99)); |
|
|
|
|
source.registerSqlType("age", Types.NUMERIC); |
|
|
|
|
assertEquals("tb", source.getValue("name")); |
|
|
|
|
assertEquals(new Integer(99), source.getValue("age")); |
|
|
|
|
assertEquals(99, source.getValue("age")); |
|
|
|
|
assertEquals(Types.VARCHAR, source.getSqlType("name")); |
|
|
|
|
assertEquals(Types.NUMERIC, source.getSqlType("age")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void testHasValueWhereTheUnderlyingBeanHasNoSuchProperty() throws Exception { |
|
|
|
|
@Test |
|
|
|
|
public void hasValueWhereTheUnderlyingBeanHasNoSuchProperty() throws Exception { |
|
|
|
|
BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource(new TestBean()); |
|
|
|
|
assertFalse(source.hasValue("thisPropertyDoesNotExist")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void testGetValueWhereTheUnderlyingBeanPropertyIsNotReadable() throws Exception { |
|
|
|
|
new AssertThrows(IllegalArgumentException.class) { |
|
|
|
|
public void test() throws Exception { |
|
|
|
|
BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource(new NoReadableProperties()); |
|
|
|
|
source.getValue("noOp"); |
|
|
|
|
} |
|
|
|
|
}.runTest(); |
|
|
|
|
@Test(expected = IllegalArgumentException.class) |
|
|
|
|
public void getValueWhereTheUnderlyingBeanPropertyIsNotReadable() throws Exception { |
|
|
|
|
BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource(new NoReadableProperties()); |
|
|
|
|
source.getValue("noOp"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void testHasValueWhereTheUnderlyingBeanPropertyIsNotReadable() throws Exception { |
|
|
|
|
@Test |
|
|
|
|
public void hasValueWhereTheUnderlyingBeanPropertyIsNotReadable() throws Exception { |
|
|
|
|
BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource(new NoReadableProperties()); |
|
|
|
|
assertFalse(source.hasValue("noOp")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final class NoReadableProperties { |
|
|
|
|
|
|
|
|
|
public void setNoOp(String noOp) { |
|
|
|
|
|