Moved tests from testsuite to jdbc

This commit is contained in:
Arjen Poutsma
2008-10-31 10:02:48 +00:00
parent d66956b117
commit 97e1cdd5fc
3 changed files with 0 additions and 0 deletions

View File

@@ -1,90 +0,0 @@
/*
* Copyright 2002-2008 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.jdbc.core.namedparam;
import java.sql.Types;
import java.util.Arrays;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.TestBean;
/**
* @author Rick Evans
* @author Juergen Hoeller
* @author Arjen Poutsma
*/
public class BeanPropertySqlParameterSourceTests {
@Test(expected = IllegalArgumentException.class)
public void withNullBeanPassedToCtor() throws Exception {
new BeanPropertySqlParameterSource(null);
}
@Test(expected = IllegalArgumentException.class)
public void getValueWhereTheUnderlyingBeanHasNoSuchProperty() throws Exception {
BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource(new TestBean());
source.getValue("thisPropertyDoesNotExist");
}
@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(99, source.getValue("age"));
assertEquals(Types.VARCHAR, source.getSqlType("name"));
assertEquals(Types.INTEGER, source.getSqlType("age"));
}
@Test
public void successfulPropertyAccessWithOverriddenSqlType() {
BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource(new TestBean("tb", 99));
source.registerSqlType("age", Types.NUMERIC);
assertEquals("tb", source.getValue("name"));
assertEquals(99, source.getValue("age"));
assertEquals(Types.VARCHAR, source.getSqlType("name"));
assertEquals(Types.NUMERIC, source.getSqlType("age"));
}
@Test
public void hasValueWhereTheUnderlyingBeanHasNoSuchProperty() throws Exception {
BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource(new TestBean());
assertFalse(source.hasValue("thisPropertyDoesNotExist"));
}
@Test(expected = IllegalArgumentException.class)
public void getValueWhereTheUnderlyingBeanPropertyIsNotReadable() throws Exception {
BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource(new NoReadableProperties());
source.getValue("noOp");
}
@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) {
}
}
}

View File

@@ -1,50 +0,0 @@
/*
* Copyright 2002-2006 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.jdbc.core.namedparam;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.jdbc.core.SqlParameterValue;
/**
* @author Rick Evans
* @author Arjen Poutsma
*/
public final class MapSqlParameterSourceTests {
@Test
public void nullParameterValuesPassedToCtorIsOk() throws Exception {
new MapSqlParameterSource(null);
}
@Test(expected = IllegalArgumentException.class)
public void getValueChokesIfParameterIsNotPresent() throws Exception {
MapSqlParameterSource source = new MapSqlParameterSource();
source.getValue("pechorin was right!");
}
@Test
public void sqlParameterValueRegistersSqlType() throws Exception {
MapSqlParameterSource msps = new MapSqlParameterSource("FOO", new SqlParameterValue(2, "Foo"));
assertEquals("Correct SQL Type not registered", 2, msps.getSqlType("FOO"));
MapSqlParameterSource msps2 = new MapSqlParameterSource();
msps2.addValues(msps.getValues());
assertEquals("Correct SQL Type not registered", 2, msps2.getSqlType("FOO"));
}
}

View File

@@ -1,202 +0,0 @@
/*
* Copyright 2002-2008 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.jdbc.core.namedparam;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.dao.InvalidDataAccessApiUsageException;
/**
* @author Thomas Risberg
* @author Juergen Hoeller
* @author Rick Evans
*/
public class NamedParameterUtilsTests {
@Test
public void parseSql() {
String sql = "xxx :a yyyy :b :c :a zzzzz";
ParsedSql psql = NamedParameterUtils.parseSqlStatement(sql);
assertEquals("xxx ? yyyy ? ? ? zzzzz", NamedParameterUtils.substituteNamedParameters(psql, null));
assertEquals("a", psql.getParameterNames().get(0));
assertEquals("c", psql.getParameterNames().get(2));
assertEquals("a", psql.getParameterNames().get(3));
assertEquals(4, psql.getTotalParameterCount());
assertEquals(3, psql.getNamedParameterCount());
String sql2 = "xxx &a yyyy ? zzzzz";
ParsedSql psql2 = NamedParameterUtils.parseSqlStatement(sql2);
assertEquals("xxx ? yyyy ? zzzzz", NamedParameterUtils.substituteNamedParameters(psql2, null));
assertEquals("a", psql2.getParameterNames().get(0));
assertEquals(2, psql2.getTotalParameterCount());
assertEquals(1, psql2.getNamedParameterCount());
String sql3 = "xxx &a+:b" + '\t' + ":c%10 yyyy ? zzzzz";
ParsedSql psql3 = NamedParameterUtils.parseSqlStatement(sql3);
assertEquals("a", psql3.getParameterNames().get(0));
assertEquals("b", psql3.getParameterNames().get(1));
assertEquals("c", psql3.getParameterNames().get(2));
}
@Test
public void substituteNamedParameters() {
MapSqlParameterSource namedParams = new MapSqlParameterSource();
namedParams.addValue("a", "a").addValue("b", "b").addValue("c", "c");
assertEquals("xxx ? ? ?", NamedParameterUtils.substituteNamedParameters("xxx :a :b :c", namedParams));
assertEquals("xxx ? ? ? xx ? ?",
NamedParameterUtils.substituteNamedParameters("xxx :a :b :c xx :a :a", namedParams));
}
@Test
public void convertParamMapToArray() {
Map<String, String> paramMap = new HashMap<String, String>();
paramMap.put("a", "a");
paramMap.put("b", "b");
paramMap.put("c", "c");
assertSame(3, NamedParameterUtils.buildValueArray("xxx :a :b :c", paramMap).length);
assertSame(5, NamedParameterUtils.buildValueArray("xxx :a :b :c xx :a :b", paramMap).length);
assertSame(5, NamedParameterUtils.buildValueArray("xxx :a :a :a xx :a :a", paramMap).length);
assertEquals("b", NamedParameterUtils.buildValueArray("xxx :a :b :c xx :a :b", paramMap)[4]);
try {
NamedParameterUtils.buildValueArray("xxx :a :b ?", paramMap);
fail("mixed named parameters and ? placeholders not detected");
}
catch (InvalidDataAccessApiUsageException expected) {
}
}
@Test
public void convertTypeMapToArray() {
MapSqlParameterSource namedParams = new MapSqlParameterSource();
namedParams.addValue("a", "a", 1).addValue("b", "b", 2).addValue("c", "c", 3);
assertSame(3, NamedParameterUtils
.buildSqlTypeArray(NamedParameterUtils.parseSqlStatement("xxx :a :b :c"), namedParams).length);
assertSame(5, NamedParameterUtils
.buildSqlTypeArray(NamedParameterUtils.parseSqlStatement("xxx :a :b :c xx :a :b"), namedParams).length);
assertSame(5, NamedParameterUtils
.buildSqlTypeArray(NamedParameterUtils.parseSqlStatement("xxx :a :a :a xx :a :a"), namedParams).length);
assertEquals(2, NamedParameterUtils
.buildSqlTypeArray(NamedParameterUtils.parseSqlStatement("xxx :a :b :c xx :a :b"), namedParams)[4]);
}
@Test(expected = InvalidDataAccessApiUsageException.class)
public void buildValueArrayWithMissingParameterValue() throws Exception {
String sql = "select count(0) from foo where id = :id";
NamedParameterUtils.buildValueArray(sql, new HashMap());
}
@Test
public void substituteNamedParametersWithStringContainingQuotes() throws Exception {
String expectedSql = "select 'first name' from artists where id = ? and quote = 'exsqueeze me?'";
String sql = "select 'first name' from artists where id = :id and quote = 'exsqueeze me?'";
String newSql = NamedParameterUtils.substituteNamedParameters(sql, new MapSqlParameterSource());
assertEquals(expectedSql, newSql);
}
@Test
public void testParseSqlStatementWithStringContainingQuotes() throws Exception {
String expectedSql = "select 'first name' from artists where id = ? and quote = 'exsqueeze me?'";
String sql = "select 'first name' from artists where id = :id and quote = 'exsqueeze me?'";
ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
assertEquals(expectedSql, NamedParameterUtils.substituteNamedParameters(parsedSql, null));
}
/*
* SPR-4789
*/
@Test
public void parseSqlContainingComments() {
String sql1 = "/*+ HINT */ xxx /* comment ? */ :a yyyy :b :c :a zzzzz -- :xx XX\n";
ParsedSql psql1 = NamedParameterUtils.parseSqlStatement(sql1);
assertEquals("/*+ HINT */ xxx /* comment ? */ ? yyyy ? ? ? zzzzz -- :xx XX\n",
NamedParameterUtils.substituteNamedParameters(psql1, null));
MapSqlParameterSource paramMap = new MapSqlParameterSource();
paramMap.addValue("a", "a");
paramMap.addValue("b", "b");
paramMap.addValue("c", "c");
Object[] params = NamedParameterUtils.buildValueArray(psql1, paramMap, null);
assertEquals(4, params.length);
assertEquals("a", params[0]);
assertEquals("b", params[1]);
assertEquals("c", params[2]);
assertEquals("a", params[3]);
String sql2 = "/*+ HINT */ xxx /* comment ? */ :a yyyy :b :c :a zzzzz -- :xx XX";
ParsedSql psql2 = NamedParameterUtils.parseSqlStatement(sql2);
assertEquals("/*+ HINT */ xxx /* comment ? */ ? yyyy ? ? ? zzzzz -- :xx XX",
NamedParameterUtils.substituteNamedParameters(psql2, null));
String sql3 = "/*+ HINT */ xxx /* comment ? */ :a yyyy :b :c :a zzzzz /* :xx XX*";
ParsedSql psql3 = NamedParameterUtils.parseSqlStatement(sql3);
assertEquals("/*+ HINT */ xxx /* comment ? */ ? yyyy ? ? ? zzzzz /* :xx XX*",
NamedParameterUtils.substituteNamedParameters(psql3, null));
String sql4 = "/*+ HINT */ xxx /* comment :a ? */ :a yyyy :b :c :a zzzzz /* :xx XX*";
ParsedSql psql4 = NamedParameterUtils.parseSqlStatement(sql4);
Map parameters = Collections.singletonMap("a", "0");
assertEquals("/*+ HINT */ xxx /* comment :a ? */ ? yyyy ? ? ? zzzzz /* :xx XX*",
NamedParameterUtils.substituteNamedParameters(psql4, new MapSqlParameterSource(parameters)));
}
/*
* SPR-4612
*/
@Test
public void parseSqlStatementWithPostgresCasting() throws Exception {
String expectedSql = "select 'first name' from artists where id = ? and birth_date=?::timestamp";
String sql = "select 'first name' from artists where id = :id and birth_date=:birthDate::timestamp";
ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
assertEquals(expectedSql, NamedParameterUtils.substituteNamedParameters(parsedSql, null));
}
/*
* SPR-2544
*/
@Test
public void parseSqlStatementWithLogicalAnd() {
String expectedSql = "xxx & yyyy";
ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(expectedSql);
assertEquals(expectedSql, NamedParameterUtils.substituteNamedParameters(parsedSql, null));
}
/*
* SPR-2544
*/
@Test
public void substituteNamedParametersWithLogicalAnd() throws Exception {
String expectedSql = "xxx & yyyy";
String newSql = NamedParameterUtils.substituteNamedParameters(expectedSql, new MapSqlParameterSource());
assertEquals(expectedSql, newSql);
}
/*
* SPR-3173
*/
@Test
public void variableAssignmentOperator() throws Exception {
String expectedSql = "x := 1";
String newSql = NamedParameterUtils.substituteNamedParameters(expectedSql, new MapSqlParameterSource());
assertEquals(expectedSql, newSql);
}
}