Moved tests from testsuite to jdbc
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright 2002-2007 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.beans;
|
||||
|
||||
import org.springframework.core.enums.ShortCodedLabeledEnum;
|
||||
|
||||
/**
|
||||
* @author Rob Harrop
|
||||
*/
|
||||
public class Colour extends ShortCodedLabeledEnum {
|
||||
|
||||
public static final Colour RED = new Colour(0, "RED");
|
||||
public static final Colour BLUE = new Colour(1, "BLUE");
|
||||
public static final Colour GREEN = new Colour(2, "GREEN");
|
||||
public static final Colour PURPLE = new Colour(3, "PURPLE");
|
||||
|
||||
private Colour(int code, String label) {
|
||||
super(code, label);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright 2002-2005 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.beans;
|
||||
|
||||
public interface INestedTestBean {
|
||||
|
||||
public String getCompany();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
|
||||
/*
|
||||
* Copyright 2002-2005 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.beans;
|
||||
|
||||
public interface IOther {
|
||||
|
||||
void absquatulate();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright 2002-2007 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.beans;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Interface used for {@link org.springframework.beans.TestBean}.
|
||||
*
|
||||
* <p>Two methods are the same as on Person, but if this
|
||||
* extends person it breaks quite a few tests..
|
||||
*
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public interface ITestBean {
|
||||
|
||||
int getAge();
|
||||
|
||||
void setAge(int age);
|
||||
|
||||
String getName();
|
||||
|
||||
void setName(String name);
|
||||
|
||||
ITestBean getSpouse();
|
||||
|
||||
void setSpouse(ITestBean spouse);
|
||||
|
||||
ITestBean[] getSpouses();
|
||||
|
||||
String[] getStringArray();
|
||||
|
||||
void setStringArray(String[] stringArray);
|
||||
|
||||
/**
|
||||
* Throws a given (non-null) exception.
|
||||
*/
|
||||
void exceptional(Throwable t) throws Throwable;
|
||||
|
||||
Object returnsThis();
|
||||
|
||||
INestedTestBean getDoctor();
|
||||
|
||||
INestedTestBean getLawyer();
|
||||
|
||||
IndexedTestBean getNestedIndexedBean();
|
||||
|
||||
/**
|
||||
* Increment the age by one.
|
||||
* @return the previous age
|
||||
*/
|
||||
int haveBirthday();
|
||||
|
||||
void unreliableFileOperation() throws IOException;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
/*
|
||||
* 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.beans;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.SortedMap;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 11.11.2003
|
||||
*/
|
||||
public class IndexedTestBean {
|
||||
|
||||
private TestBean[] array;
|
||||
|
||||
private Collection collection;
|
||||
|
||||
private List list;
|
||||
|
||||
private Set set;
|
||||
|
||||
private SortedSet sortedSet;
|
||||
|
||||
private Map map;
|
||||
|
||||
private SortedMap sortedMap;
|
||||
|
||||
|
||||
public IndexedTestBean() {
|
||||
this(true);
|
||||
}
|
||||
|
||||
public IndexedTestBean(boolean populate) {
|
||||
if (populate) {
|
||||
populate();
|
||||
}
|
||||
}
|
||||
|
||||
public void populate() {
|
||||
TestBean tb0 = new TestBean("name0", 0);
|
||||
TestBean tb1 = new TestBean("name1", 0);
|
||||
TestBean tb2 = new TestBean("name2", 0);
|
||||
TestBean tb3 = new TestBean("name3", 0);
|
||||
TestBean tb4 = new TestBean("name4", 0);
|
||||
TestBean tb5 = new TestBean("name5", 0);
|
||||
TestBean tb6 = new TestBean("name6", 0);
|
||||
TestBean tb7 = new TestBean("name7", 0);
|
||||
TestBean tbX = new TestBean("nameX", 0);
|
||||
TestBean tbY = new TestBean("nameY", 0);
|
||||
this.array = new TestBean[] {tb0, tb1};
|
||||
this.list = new ArrayList();
|
||||
this.list.add(tb2);
|
||||
this.list.add(tb3);
|
||||
this.set = new TreeSet();
|
||||
this.set.add(tb6);
|
||||
this.set.add(tb7);
|
||||
this.map = new HashMap();
|
||||
this.map.put("key1", tb4);
|
||||
this.map.put("key2", tb5);
|
||||
this.map.put("key.3", tb5);
|
||||
List list = new ArrayList();
|
||||
list.add(tbX);
|
||||
list.add(tbY);
|
||||
this.map.put("key4", list);
|
||||
}
|
||||
|
||||
|
||||
public TestBean[] getArray() {
|
||||
return array;
|
||||
}
|
||||
|
||||
public void setArray(TestBean[] array) {
|
||||
this.array = array;
|
||||
}
|
||||
|
||||
public Collection getCollection() {
|
||||
return collection;
|
||||
}
|
||||
|
||||
public void setCollection(Collection collection) {
|
||||
this.collection = collection;
|
||||
}
|
||||
|
||||
public List getList() {
|
||||
return list;
|
||||
}
|
||||
|
||||
public void setList(List list) {
|
||||
this.list = list;
|
||||
}
|
||||
|
||||
public Set getSet() {
|
||||
return set;
|
||||
}
|
||||
|
||||
public void setSet(Set set) {
|
||||
this.set = set;
|
||||
}
|
||||
|
||||
public SortedSet getSortedSet() {
|
||||
return sortedSet;
|
||||
}
|
||||
|
||||
public void setSortedSet(SortedSet sortedSet) {
|
||||
this.sortedSet = sortedSet;
|
||||
}
|
||||
|
||||
public Map getMap() {
|
||||
return map;
|
||||
}
|
||||
|
||||
public void setMap(Map map) {
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
public SortedMap getSortedMap() {
|
||||
return sortedMap;
|
||||
}
|
||||
|
||||
public void setSortedMap(SortedMap sortedMap) {
|
||||
this.sortedMap = sortedMap;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright 2002-2005 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.beans;
|
||||
|
||||
/**
|
||||
* Simple nested test bean used for testing bean factories, AOP framework etc.
|
||||
*
|
||||
* @author Trevor D. Cook
|
||||
* @since 30.09.2003
|
||||
*/
|
||||
public class NestedTestBean implements INestedTestBean {
|
||||
|
||||
private String company = "";
|
||||
|
||||
public NestedTestBean() {
|
||||
}
|
||||
|
||||
public NestedTestBean(String company) {
|
||||
setCompany(company);
|
||||
}
|
||||
|
||||
public void setCompany(String company) {
|
||||
this.company = (company != null ? company : "");
|
||||
}
|
||||
|
||||
public String getCompany() {
|
||||
return company;
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
if (!(obj instanceof NestedTestBean)) {
|
||||
return false;
|
||||
}
|
||||
NestedTestBean ntb = (NestedTestBean) obj;
|
||||
return this.company.equals(ntb.company);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return this.company.hashCode();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "NestedTestBean: " + this.company;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,437 @@
|
||||
/*
|
||||
* 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.beans;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.beans.factory.BeanNameAware;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* Simple test bean used for testing bean factories, the AOP framework etc.
|
||||
*
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
* @since 15 April 2001
|
||||
*/
|
||||
public class TestBean implements BeanNameAware, BeanFactoryAware, ITestBean, IOther, Comparable {
|
||||
|
||||
private String beanName;
|
||||
|
||||
private String country;
|
||||
|
||||
private BeanFactory beanFactory;
|
||||
|
||||
private boolean postProcessed;
|
||||
|
||||
private String name;
|
||||
|
||||
private String sex;
|
||||
|
||||
private int age;
|
||||
|
||||
private boolean jedi;
|
||||
|
||||
private ITestBean[] spouses;
|
||||
|
||||
private String touchy;
|
||||
|
||||
private String[] stringArray;
|
||||
|
||||
private Integer[] someIntegerArray;
|
||||
|
||||
private Date date = new Date();
|
||||
|
||||
private Float myFloat = new Float(0.0);
|
||||
|
||||
private Collection friends = new LinkedList();
|
||||
|
||||
private Set someSet = new HashSet();
|
||||
|
||||
private Map someMap = new HashMap();
|
||||
|
||||
private List someList = new ArrayList();
|
||||
|
||||
private Properties someProperties = new Properties();
|
||||
|
||||
private INestedTestBean doctor = new NestedTestBean();
|
||||
|
||||
private INestedTestBean lawyer = new NestedTestBean();
|
||||
|
||||
private IndexedTestBean nestedIndexedBean;
|
||||
|
||||
private boolean destroyed;
|
||||
|
||||
private Number someNumber;
|
||||
|
||||
private Colour favouriteColour;
|
||||
|
||||
private Boolean someBoolean;
|
||||
|
||||
private List otherColours;
|
||||
|
||||
private List pets;
|
||||
|
||||
|
||||
public TestBean() {
|
||||
}
|
||||
|
||||
public TestBean(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public TestBean(ITestBean spouse) {
|
||||
this.spouses = new ITestBean[] {spouse};
|
||||
}
|
||||
|
||||
public TestBean(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public TestBean(ITestBean spouse, Properties someProperties) {
|
||||
this.spouses = new ITestBean[] {spouse};
|
||||
this.someProperties = someProperties;
|
||||
}
|
||||
|
||||
public TestBean(List someList) {
|
||||
this.someList = someList;
|
||||
}
|
||||
|
||||
public TestBean(Set someSet) {
|
||||
this.someSet = someSet;
|
||||
}
|
||||
|
||||
public TestBean(Map someMap) {
|
||||
this.someMap = someMap;
|
||||
}
|
||||
|
||||
public TestBean(Properties someProperties) {
|
||||
this.someProperties = someProperties;
|
||||
}
|
||||
|
||||
|
||||
public void setBeanName(String beanName) {
|
||||
this.beanName = beanName;
|
||||
}
|
||||
|
||||
public String getBeanName() {
|
||||
return beanName;
|
||||
}
|
||||
|
||||
public void setBeanFactory(BeanFactory beanFactory) {
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
public BeanFactory getBeanFactory() {
|
||||
return beanFactory;
|
||||
}
|
||||
|
||||
public void setPostProcessed(boolean postProcessed) {
|
||||
this.postProcessed = postProcessed;
|
||||
}
|
||||
|
||||
public boolean isPostProcessed() {
|
||||
return postProcessed;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getSex() {
|
||||
return sex;
|
||||
}
|
||||
|
||||
public void setSex(String sex) {
|
||||
this.sex = sex;
|
||||
if (this.name == null) {
|
||||
this.name = sex;
|
||||
}
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public boolean isJedi() {
|
||||
return jedi;
|
||||
}
|
||||
|
||||
public void setJedi(boolean jedi) {
|
||||
this.jedi = jedi;
|
||||
}
|
||||
|
||||
public ITestBean getSpouse() {
|
||||
return (spouses != null ? spouses[0] : null);
|
||||
}
|
||||
|
||||
public void setSpouse(ITestBean spouse) {
|
||||
this.spouses = new ITestBean[] {spouse};
|
||||
}
|
||||
|
||||
public ITestBean[] getSpouses() {
|
||||
return spouses;
|
||||
}
|
||||
|
||||
public String getTouchy() {
|
||||
return touchy;
|
||||
}
|
||||
|
||||
public void setTouchy(String touchy) throws Exception {
|
||||
if (touchy.indexOf('.') != -1) {
|
||||
throw new Exception("Can't contain a .");
|
||||
}
|
||||
if (touchy.indexOf(',') != -1) {
|
||||
throw new NumberFormatException("Number format exception: contains a ,");
|
||||
}
|
||||
this.touchy = touchy;
|
||||
}
|
||||
|
||||
public String getCountry() {
|
||||
return country;
|
||||
}
|
||||
|
||||
public void setCountry(String country) {
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
public String[] getStringArray() {
|
||||
return stringArray;
|
||||
}
|
||||
|
||||
public void setStringArray(String[] stringArray) {
|
||||
this.stringArray = stringArray;
|
||||
}
|
||||
|
||||
public Integer[] getSomeIntegerArray() {
|
||||
return someIntegerArray;
|
||||
}
|
||||
|
||||
public void setSomeIntegerArray(Integer[] someIntegerArray) {
|
||||
this.someIntegerArray = someIntegerArray;
|
||||
}
|
||||
|
||||
public Date getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(Date date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public Float getMyFloat() {
|
||||
return myFloat;
|
||||
}
|
||||
|
||||
public void setMyFloat(Float myFloat) {
|
||||
this.myFloat = myFloat;
|
||||
}
|
||||
|
||||
public Collection getFriends() {
|
||||
return friends;
|
||||
}
|
||||
|
||||
public void setFriends(Collection friends) {
|
||||
this.friends = friends;
|
||||
}
|
||||
|
||||
public Set getSomeSet() {
|
||||
return someSet;
|
||||
}
|
||||
|
||||
public void setSomeSet(Set someSet) {
|
||||
this.someSet = someSet;
|
||||
}
|
||||
|
||||
public Map getSomeMap() {
|
||||
return someMap;
|
||||
}
|
||||
|
||||
public void setSomeMap(Map someMap) {
|
||||
this.someMap = someMap;
|
||||
}
|
||||
|
||||
public List getSomeList() {
|
||||
return someList;
|
||||
}
|
||||
|
||||
public void setSomeList(List someList) {
|
||||
this.someList = someList;
|
||||
}
|
||||
|
||||
public Properties getSomeProperties() {
|
||||
return someProperties;
|
||||
}
|
||||
|
||||
public void setSomeProperties(Properties someProperties) {
|
||||
this.someProperties = someProperties;
|
||||
}
|
||||
|
||||
public INestedTestBean getDoctor() {
|
||||
return doctor;
|
||||
}
|
||||
|
||||
public void setDoctor(INestedTestBean doctor) {
|
||||
this.doctor = doctor;
|
||||
}
|
||||
|
||||
public INestedTestBean getLawyer() {
|
||||
return lawyer;
|
||||
}
|
||||
|
||||
public void setLawyer(INestedTestBean lawyer) {
|
||||
this.lawyer = lawyer;
|
||||
}
|
||||
|
||||
public Number getSomeNumber() {
|
||||
return someNumber;
|
||||
}
|
||||
|
||||
public void setSomeNumber(Number someNumber) {
|
||||
this.someNumber = someNumber;
|
||||
}
|
||||
|
||||
public Colour getFavouriteColour() {
|
||||
return favouriteColour;
|
||||
}
|
||||
|
||||
public void setFavouriteColour(Colour favouriteColour) {
|
||||
this.favouriteColour = favouriteColour;
|
||||
}
|
||||
|
||||
public Boolean getSomeBoolean() {
|
||||
return someBoolean;
|
||||
}
|
||||
|
||||
public void setSomeBoolean(Boolean someBoolean) {
|
||||
this.someBoolean = someBoolean;
|
||||
}
|
||||
|
||||
public IndexedTestBean getNestedIndexedBean() {
|
||||
return nestedIndexedBean;
|
||||
}
|
||||
|
||||
public void setNestedIndexedBean(IndexedTestBean nestedIndexedBean) {
|
||||
this.nestedIndexedBean = nestedIndexedBean;
|
||||
}
|
||||
|
||||
public List getOtherColours() {
|
||||
return otherColours;
|
||||
}
|
||||
|
||||
public void setOtherColours(List otherColours) {
|
||||
this.otherColours = otherColours;
|
||||
}
|
||||
|
||||
public List getPets() {
|
||||
return pets;
|
||||
}
|
||||
|
||||
public void setPets(List pets) {
|
||||
this.pets = pets;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @see org.springframework.beans.ITestBean#exceptional(Throwable)
|
||||
*/
|
||||
public void exceptional(Throwable t) throws Throwable {
|
||||
if (t != null) {
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
|
||||
public void unreliableFileOperation() throws IOException {
|
||||
throw new IOException();
|
||||
}
|
||||
/**
|
||||
* @see org.springframework.beans.ITestBean#returnsThis()
|
||||
*/
|
||||
public Object returnsThis() {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.springframework.beans.IOther#absquatulate()
|
||||
*/
|
||||
public void absquatulate() {
|
||||
}
|
||||
|
||||
public int haveBirthday() {
|
||||
return age++;
|
||||
}
|
||||
|
||||
|
||||
public void destroy() {
|
||||
this.destroyed = true;
|
||||
}
|
||||
|
||||
public boolean wasDestroyed() {
|
||||
return destroyed;
|
||||
}
|
||||
|
||||
|
||||
public boolean equals(Object other) {
|
||||
if (this == other) {
|
||||
return true;
|
||||
}
|
||||
if (other == null || !(other instanceof TestBean)) {
|
||||
return false;
|
||||
}
|
||||
TestBean tb2 = (TestBean) other;
|
||||
return (ObjectUtils.nullSafeEquals(this.name, tb2.name) && this.age == tb2.age);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return this.age;
|
||||
}
|
||||
|
||||
public int compareTo(Object other) {
|
||||
if (this.name != null && other instanceof TestBean) {
|
||||
return this.name.compareTo(((TestBean) other).getName());
|
||||
}
|
||||
else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* AbstractJdbcTests.java
|
||||
*
|
||||
* Copyright (C) 2002 by Interprise Software. All rights reserved.
|
||||
*/
|
||||
/*
|
||||
* Copyright 2002-2005 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;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.easymock.MockControl;
|
||||
|
||||
/**
|
||||
* @author Trevor D. Cook
|
||||
*/
|
||||
public abstract class AbstractJdbcTests extends TestCase {
|
||||
|
||||
protected MockControl ctrlDataSource;
|
||||
protected DataSource mockDataSource;
|
||||
protected MockControl ctrlConnection;
|
||||
protected Connection mockConnection;
|
||||
|
||||
/**
|
||||
* Set to true if the user wants verification, indicated
|
||||
* by a call to replay(). We need to make this optional,
|
||||
* otherwise we setUp() will always result in verification failures
|
||||
*/
|
||||
private boolean shouldVerify;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
this.shouldVerify = false;
|
||||
super.setUp();
|
||||
|
||||
ctrlConnection = MockControl.createControl(Connection.class);
|
||||
mockConnection = (Connection) ctrlConnection.getMock();
|
||||
mockConnection.getMetaData();
|
||||
ctrlConnection.setDefaultReturnValue(null);
|
||||
mockConnection.close();
|
||||
ctrlConnection.setDefaultVoidCallable();
|
||||
|
||||
ctrlDataSource = MockControl.createControl(DataSource.class);
|
||||
mockDataSource = (DataSource) ctrlDataSource.getMock();
|
||||
mockDataSource.getConnection();
|
||||
ctrlDataSource.setDefaultReturnValue(mockConnection);
|
||||
}
|
||||
|
||||
protected void replay() {
|
||||
ctrlDataSource.replay();
|
||||
ctrlConnection.replay();
|
||||
this.shouldVerify = true;
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
|
||||
// we shouldn't verify unless the user called replay()
|
||||
if (shouldVerify()) {
|
||||
ctrlDataSource.verify();
|
||||
//ctrlConnection.verify();
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean shouldVerify() {
|
||||
return this.shouldVerify;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.sql.Timestamp;
|
||||
import java.sql.Types;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.Assert;
|
||||
import org.easymock.MockControl;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.jdbc.core.test.ConcretePerson;
|
||||
import org.springframework.jdbc.core.test.Person;
|
||||
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
|
||||
import org.springframework.jdbc.support.SQLStateSQLExceptionTranslator;
|
||||
|
||||
/**
|
||||
* Mock object based abstract class for RowMapper tests.
|
||||
* Initializes mock objects and verifies results.
|
||||
*
|
||||
* @author Thomas Risberg
|
||||
*/
|
||||
public abstract class AbstractRowMapperTests extends TestCase {
|
||||
|
||||
private final boolean debugEnabled = LogFactory.getLog(JdbcTemplate.class).isDebugEnabled();
|
||||
|
||||
protected MockControl conControl;
|
||||
protected Connection con;
|
||||
protected MockControl rsmdControl;
|
||||
protected ResultSetMetaData rsmd;
|
||||
protected MockControl rsControl;
|
||||
protected ResultSet rs;
|
||||
protected MockControl stmtControl;
|
||||
protected Statement stmt;
|
||||
protected JdbcTemplate jdbcTemplate;
|
||||
|
||||
protected void setUp() throws SQLException {
|
||||
conControl = MockControl.createControl(Connection.class);
|
||||
con = (Connection) conControl.getMock();
|
||||
con.isClosed();
|
||||
conControl.setDefaultReturnValue(false);
|
||||
|
||||
rsmdControl = MockControl.createControl(ResultSetMetaData.class);
|
||||
rsmd = (ResultSetMetaData)rsmdControl.getMock();
|
||||
rsmd.getColumnCount();
|
||||
rsmdControl.setReturnValue(4, 1);
|
||||
rsmd.getColumnLabel(1);
|
||||
rsmdControl.setReturnValue("name", 1);
|
||||
rsmd.getColumnLabel(2);
|
||||
rsmdControl.setReturnValue("age", 1);
|
||||
rsmd.getColumnLabel(3);
|
||||
rsmdControl.setReturnValue("birth_date", 1);
|
||||
rsmd.getColumnLabel(4);
|
||||
rsmdControl.setReturnValue("balance", 1);
|
||||
rsmdControl.replay();
|
||||
|
||||
rsControl = MockControl.createControl(ResultSet.class);
|
||||
rs = (ResultSet) rsControl.getMock();
|
||||
rs.getMetaData();
|
||||
rsControl.setReturnValue(rsmd, 1);
|
||||
rs.next();
|
||||
rsControl.setReturnValue(true, 1);
|
||||
rs.getString(1);
|
||||
rsControl.setReturnValue("Bubba", 1);
|
||||
rs.wasNull();
|
||||
rsControl.setReturnValue(false, 1);
|
||||
rs.getLong(2);
|
||||
rsControl.setReturnValue(22, 1);
|
||||
rs.getTimestamp(3);
|
||||
rsControl.setReturnValue(new Timestamp(1221222L), 1);
|
||||
rs.getBigDecimal(4);
|
||||
rsControl.setReturnValue(new BigDecimal("1234.56"), 1);
|
||||
rs.next();
|
||||
rsControl.setReturnValue(false, 1);
|
||||
rs.close();
|
||||
rsControl.setVoidCallable(1);
|
||||
rsControl.replay();
|
||||
|
||||
stmtControl = MockControl.createControl(Statement.class);
|
||||
stmt = (Statement) stmtControl.getMock();
|
||||
|
||||
con.createStatement();
|
||||
conControl.setReturnValue(stmt, 1);
|
||||
stmt.executeQuery("select name, age, birth_date, balance from people");
|
||||
stmtControl.setReturnValue(rs, 1);
|
||||
if (debugEnabled) {
|
||||
stmt.getWarnings();
|
||||
stmtControl.setReturnValue(null, 1);
|
||||
}
|
||||
stmt.close();
|
||||
stmtControl.setVoidCallable(1);
|
||||
|
||||
conControl.replay();
|
||||
stmtControl.replay();
|
||||
|
||||
jdbcTemplate = new JdbcTemplate();
|
||||
jdbcTemplate.setDataSource(new SingleConnectionDataSource(con, false));
|
||||
jdbcTemplate.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
|
||||
jdbcTemplate.afterPropertiesSet();
|
||||
}
|
||||
|
||||
protected void verifyPerson(Person bean) {
|
||||
verify();
|
||||
Assert.assertEquals("Bubba", bean.getName());
|
||||
Assert.assertEquals(22L, bean.getAge());
|
||||
Assert.assertEquals(new java.util.Date(1221222L), bean.getBirth_date());
|
||||
Assert.assertEquals(new BigDecimal("1234.56"), bean.getBalance());
|
||||
}
|
||||
|
||||
protected void verifyConcretePerson(ConcretePerson bean) {
|
||||
verify();
|
||||
Assert.assertEquals("Bubba", bean.getName());
|
||||
Assert.assertEquals(22L, bean.getAge());
|
||||
Assert.assertEquals(new java.util.Date(1221222L), bean.getBirth_date());
|
||||
Assert.assertEquals(new BigDecimal("1234.56"), bean.getBalance());
|
||||
}
|
||||
|
||||
private void verify() {
|
||||
conControl.verify();
|
||||
rsControl.verify();
|
||||
rsmdControl.verify();
|
||||
stmtControl.verify();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
import org.springframework.jdbc.core.test.ConcretePerson;
|
||||
import org.springframework.jdbc.core.test.ExtendedPerson;
|
||||
import org.springframework.jdbc.core.test.Person;
|
||||
|
||||
/**
|
||||
* @author Thomas Risberg
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class BeanPropertyRowMapperTests extends AbstractRowMapperTests {
|
||||
|
||||
public void testOverridingClassDefinedForMapping() {
|
||||
BeanPropertyRowMapper mapper = new BeanPropertyRowMapper(Person.class);
|
||||
try {
|
||||
mapper.setMappedClass(Long.class);
|
||||
fail("Setting new class should have thrown InvalidDataAccessApiUsageException");
|
||||
}
|
||||
catch (InvalidDataAccessApiUsageException ex) {
|
||||
}
|
||||
try {
|
||||
mapper.setMappedClass(Person.class);
|
||||
}
|
||||
catch (InvalidDataAccessApiUsageException ex) {
|
||||
fail("Setting same class should not have thrown InvalidDataAccessApiUsageException");
|
||||
}
|
||||
}
|
||||
|
||||
public void testStaticQueryWithRowMapper() throws SQLException {
|
||||
List result = jdbcTemplate.query("select name, age, birth_date, balance from people",
|
||||
new BeanPropertyRowMapper(Person.class));
|
||||
assertEquals(1, result.size());
|
||||
Person bean = (Person) result.get(0);
|
||||
verifyPerson(bean);
|
||||
}
|
||||
|
||||
public void testMappingWithInheritance() throws SQLException {
|
||||
List result = jdbcTemplate.query("select name, age, birth_date, balance from people",
|
||||
new BeanPropertyRowMapper(ConcretePerson.class));
|
||||
assertEquals(1, result.size());
|
||||
ConcretePerson bean = (ConcretePerson) result.get(0);
|
||||
verifyConcretePerson(bean);
|
||||
}
|
||||
|
||||
public void testMappingWithNoUnpopulatedFieldsFound() throws SQLException {
|
||||
List result = jdbcTemplate.query("select name, age, birth_date, balance from people",
|
||||
new BeanPropertyRowMapper(ConcretePerson.class, true));
|
||||
assertEquals(1, result.size());
|
||||
ConcretePerson bean = (ConcretePerson) result.get(0);
|
||||
verifyConcretePerson(bean);
|
||||
}
|
||||
|
||||
public void testMappingWithUnpopulatedFieldsNotChecked() throws SQLException {
|
||||
List result = jdbcTemplate.query("select name, age, birth_date, balance from people",
|
||||
new BeanPropertyRowMapper(ExtendedPerson.class));
|
||||
assertEquals(1, result.size());
|
||||
ExtendedPerson bean = (ExtendedPerson) result.get(0);
|
||||
verifyConcretePerson(bean);
|
||||
}
|
||||
|
||||
public void testMappingWithUnpopulatedFieldsNotAccepted() throws SQLException {
|
||||
try {
|
||||
List result = jdbcTemplate.query("select name, age, birth_date, balance from people",
|
||||
new BeanPropertyRowMapper(ExtendedPerson.class, true));
|
||||
fail("Should have thrown InvalidDataAccessApiUsageException because of missing field");
|
||||
}
|
||||
catch (InvalidDataAccessApiUsageException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,983 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.dao.IncorrectResultSizeDataAccessException;
|
||||
import org.springframework.jdbc.AbstractJdbcTests;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 19.12.2004
|
||||
*/
|
||||
public class JdbcTemplateQueryTests extends AbstractJdbcTests {
|
||||
|
||||
private final boolean debugEnabled = LogFactory.getLog(JdbcTemplate.class).isDebugEnabled();
|
||||
|
||||
private MockControl ctrlStatement;
|
||||
private Statement mockStatement;
|
||||
private MockControl ctrlPreparedStatement;
|
||||
private PreparedStatement mockPreparedStatement;
|
||||
private MockControl ctrlResultSet;
|
||||
private ResultSet mockResultSet;
|
||||
private MockControl ctrlResultSetMetaData;
|
||||
private ResultSetMetaData mockResultSetMetaData;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
||||
ctrlStatement = MockControl.createControl(Statement.class);
|
||||
mockStatement = (Statement) ctrlStatement.getMock();
|
||||
ctrlPreparedStatement = MockControl.createControl(PreparedStatement.class);
|
||||
mockPreparedStatement = (PreparedStatement) ctrlPreparedStatement.getMock();
|
||||
ctrlResultSet = MockControl.createControl(ResultSet.class);
|
||||
mockResultSet = (ResultSet) ctrlResultSet.getMock();
|
||||
ctrlResultSetMetaData = MockControl.createControl(ResultSetMetaData.class);
|
||||
mockResultSetMetaData = (ResultSetMetaData) ctrlResultSetMetaData.getMock();
|
||||
}
|
||||
|
||||
protected void replay() {
|
||||
super.replay();
|
||||
ctrlStatement.replay();
|
||||
ctrlPreparedStatement.replay();
|
||||
ctrlResultSet.replay();
|
||||
ctrlResultSetMetaData.replay();
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
if (shouldVerify()) {
|
||||
ctrlStatement.verify();
|
||||
ctrlPreparedStatement.verify();
|
||||
ctrlResultSet.verify();
|
||||
ctrlResultSetMetaData.verify();
|
||||
}
|
||||
}
|
||||
|
||||
public void testQueryForList() throws Exception {
|
||||
String sql = "SELECT AGE FROM CUSTMR WHERE ID < 3";
|
||||
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1, 2);
|
||||
mockResultSetMetaData.getColumnLabel(1);
|
||||
ctrlResultSetMetaData.setReturnValue("age", 2);
|
||||
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData, 2);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getObject(1);
|
||||
ctrlResultSet.setReturnValue(new Integer(11));
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getObject(1);
|
||||
ctrlResultSet.setReturnValue(new Integer(12));
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockStatement.executeQuery(sql);
|
||||
ctrlStatement.setReturnValue(mockResultSet);
|
||||
if (debugEnabled) {
|
||||
mockStatement.getWarnings();
|
||||
ctrlStatement.setReturnValue(null);
|
||||
}
|
||||
mockStatement.close();
|
||||
ctrlStatement.setVoidCallable();
|
||||
|
||||
mockConnection.createStatement();
|
||||
ctrlConnection.setReturnValue(mockStatement);
|
||||
|
||||
replay();
|
||||
|
||||
JdbcTemplate template = new JdbcTemplate(mockDataSource);
|
||||
|
||||
List li = template.queryForList(sql);
|
||||
assertEquals("All rows returned", 2, li.size());
|
||||
assertEquals("First row is Integer", 11, ((Integer)((Map)li.get(0)).get("age")).intValue());
|
||||
assertEquals("Second row is Integer", 12, ((Integer)((Map)li.get(1)).get("age")).intValue());
|
||||
}
|
||||
|
||||
public void testQueryForListWithEmptyResult() throws Exception {
|
||||
String sql = "SELECT AGE FROM CUSTMR WHERE ID < 3";
|
||||
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockStatement.executeQuery(sql);
|
||||
ctrlStatement.setReturnValue(mockResultSet);
|
||||
if (debugEnabled) {
|
||||
mockStatement.getWarnings();
|
||||
ctrlStatement.setReturnValue(null);
|
||||
}
|
||||
mockStatement.close();
|
||||
ctrlStatement.setVoidCallable();
|
||||
|
||||
mockConnection.createStatement();
|
||||
ctrlConnection.setReturnValue(mockStatement);
|
||||
|
||||
replay();
|
||||
|
||||
JdbcTemplate template = new JdbcTemplate(mockDataSource);
|
||||
List li = template.queryForList(sql);
|
||||
assertEquals("All rows returned", 0, li.size());
|
||||
}
|
||||
|
||||
public void testQueryForListWithSingleRowAndColumn() throws Exception {
|
||||
String sql = "SELECT AGE FROM CUSTMR WHERE ID < 3";
|
||||
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1);
|
||||
mockResultSetMetaData.getColumnLabel(1);
|
||||
ctrlResultSetMetaData.setReturnValue("age", 1);
|
||||
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getObject(1);
|
||||
ctrlResultSet.setReturnValue(new Integer(11));
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockStatement.executeQuery(sql);
|
||||
ctrlStatement.setReturnValue(mockResultSet);
|
||||
if (debugEnabled) {
|
||||
mockStatement.getWarnings();
|
||||
ctrlStatement.setReturnValue(null);
|
||||
}
|
||||
mockStatement.close();
|
||||
ctrlStatement.setVoidCallable();
|
||||
|
||||
mockConnection.createStatement();
|
||||
ctrlConnection.setReturnValue(mockStatement);
|
||||
|
||||
replay();
|
||||
|
||||
JdbcTemplate template = new JdbcTemplate(mockDataSource);
|
||||
|
||||
List li = template.queryForList(sql);
|
||||
assertEquals("All rows returned", 1, li.size());
|
||||
assertEquals("First row is Integer", 11, ((Integer)((Map)li.get(0)).get("age")).intValue());
|
||||
}
|
||||
|
||||
public void testQueryForListWithIntegerElement() throws Exception {
|
||||
String sql = "SELECT AGE FROM CUSTMR WHERE ID < 3";
|
||||
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1);
|
||||
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getInt(1);
|
||||
ctrlResultSet.setReturnValue(11);
|
||||
mockResultSet.wasNull();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockStatement.executeQuery(sql);
|
||||
ctrlStatement.setReturnValue(mockResultSet);
|
||||
if (debugEnabled) {
|
||||
mockStatement.getWarnings();
|
||||
ctrlStatement.setReturnValue(null);
|
||||
}
|
||||
mockStatement.close();
|
||||
ctrlStatement.setVoidCallable();
|
||||
|
||||
mockConnection.createStatement();
|
||||
ctrlConnection.setReturnValue(mockStatement);
|
||||
|
||||
replay();
|
||||
|
||||
JdbcTemplate template = new JdbcTemplate(mockDataSource);
|
||||
|
||||
List li = template.queryForList(sql, Integer.class);
|
||||
assertEquals("All rows returned", 1, li.size());
|
||||
assertEquals("Element is Integer", 11, ((Integer) li.get(0)).intValue());
|
||||
}
|
||||
|
||||
public void testQueryForMapWithSingleRowAndColumn() throws Exception {
|
||||
String sql = "SELECT AGE FROM CUSTMR WHERE ID < 3";
|
||||
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1);
|
||||
mockResultSetMetaData.getColumnLabel(1);
|
||||
ctrlResultSetMetaData.setReturnValue("age", 1);
|
||||
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getObject(1);
|
||||
ctrlResultSet.setReturnValue(new Integer(11));
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockStatement.executeQuery(sql);
|
||||
ctrlStatement.setReturnValue(mockResultSet);
|
||||
if (debugEnabled) {
|
||||
mockStatement.getWarnings();
|
||||
ctrlStatement.setReturnValue(null);
|
||||
}
|
||||
mockStatement.close();
|
||||
ctrlStatement.setVoidCallable();
|
||||
|
||||
mockConnection.createStatement();
|
||||
ctrlConnection.setReturnValue(mockStatement);
|
||||
|
||||
replay();
|
||||
|
||||
JdbcTemplate template = new JdbcTemplate(mockDataSource);
|
||||
|
||||
Map map = template.queryForMap(sql);
|
||||
assertEquals("Wow is Integer", 11, ((Integer) map.get("age")).intValue());
|
||||
}
|
||||
|
||||
public void testQueryForObjectThrowsIncorrectResultSizeForMoreThanOneRow() throws Exception {
|
||||
String sql = "select pass from t_account where first_name='Alef'";
|
||||
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1);
|
||||
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getString(1);
|
||||
ctrlResultSet.setReturnValue("pass");
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData);
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1);
|
||||
mockResultSet.getString(1);
|
||||
ctrlResultSet.setReturnValue("pass");
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockStatement.executeQuery(sql);
|
||||
ctrlStatement.setReturnValue(mockResultSet);
|
||||
if (debugEnabled) {
|
||||
mockStatement.getWarnings();
|
||||
ctrlStatement.setReturnValue(null);
|
||||
}
|
||||
mockStatement.close();
|
||||
ctrlStatement.setVoidCallable();
|
||||
|
||||
mockConnection.createStatement();
|
||||
ctrlConnection.setReturnValue(mockStatement);
|
||||
|
||||
replay();
|
||||
|
||||
JdbcTemplate template = new JdbcTemplate(mockDataSource);
|
||||
try {
|
||||
template.queryForObject(sql, String.class);
|
||||
fail("Should have thrown IncorrectResultSizeDataAccessException");
|
||||
}
|
||||
catch (IncorrectResultSizeDataAccessException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testQueryForObjectWithRowMapper() throws Exception {
|
||||
String sql = "SELECT AGE FROM CUSTMR WHERE ID = 3";
|
||||
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getInt(1);
|
||||
ctrlResultSet.setReturnValue(22);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockStatement.executeQuery(sql);
|
||||
ctrlStatement.setReturnValue(mockResultSet);
|
||||
if (debugEnabled) {
|
||||
mockStatement.getWarnings();
|
||||
ctrlStatement.setReturnValue(null);
|
||||
}
|
||||
mockStatement.close();
|
||||
ctrlStatement.setVoidCallable();
|
||||
|
||||
mockConnection.createStatement();
|
||||
ctrlConnection.setReturnValue(mockStatement);
|
||||
|
||||
replay();
|
||||
|
||||
JdbcTemplate template = new JdbcTemplate(mockDataSource);
|
||||
|
||||
Object o = template.queryForObject(sql, new RowMapper() {
|
||||
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
return new Integer(rs.getInt(1));
|
||||
}
|
||||
});
|
||||
assertTrue("Correct result type", o instanceof Integer);
|
||||
}
|
||||
|
||||
public void testQueryForObjectWithString() throws Exception {
|
||||
String sql = "SELECT AGE FROM CUSTMR WHERE ID = 3";
|
||||
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1);
|
||||
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getString(1);
|
||||
ctrlResultSet.setReturnValue("myvalue");
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockStatement.executeQuery(sql);
|
||||
ctrlStatement.setReturnValue(mockResultSet);
|
||||
if (debugEnabled) {
|
||||
mockStatement.getWarnings();
|
||||
ctrlStatement.setReturnValue(null);
|
||||
}
|
||||
mockStatement.close();
|
||||
ctrlStatement.setVoidCallable();
|
||||
|
||||
mockConnection.createStatement();
|
||||
ctrlConnection.setReturnValue(mockStatement);
|
||||
|
||||
replay();
|
||||
|
||||
JdbcTemplate template = new JdbcTemplate(mockDataSource);
|
||||
assertEquals("myvalue", template.queryForObject(sql, String.class));
|
||||
}
|
||||
|
||||
public void testQueryForObjectWithBigInteger() throws Exception {
|
||||
String sql = "SELECT AGE FROM CUSTMR WHERE ID = 3";
|
||||
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1);
|
||||
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getObject(1);
|
||||
ctrlResultSet.setReturnValue("22");
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockStatement.executeQuery(sql);
|
||||
ctrlStatement.setReturnValue(mockResultSet);
|
||||
if (debugEnabled) {
|
||||
mockStatement.getWarnings();
|
||||
ctrlStatement.setReturnValue(null);
|
||||
}
|
||||
mockStatement.close();
|
||||
ctrlStatement.setVoidCallable();
|
||||
|
||||
mockConnection.createStatement();
|
||||
ctrlConnection.setReturnValue(mockStatement);
|
||||
|
||||
replay();
|
||||
|
||||
JdbcTemplate template = new JdbcTemplate(mockDataSource);
|
||||
assertEquals(new BigInteger("22"), template.queryForObject(sql, BigInteger.class));
|
||||
}
|
||||
|
||||
public void testQueryForObjectWithBigDecimal() throws Exception {
|
||||
String sql = "SELECT AGE FROM CUSTMR WHERE ID = 3";
|
||||
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1);
|
||||
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getBigDecimal(1);
|
||||
ctrlResultSet.setReturnValue(new BigDecimal(22.5));
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockStatement.executeQuery(sql);
|
||||
ctrlStatement.setReturnValue(mockResultSet);
|
||||
if (debugEnabled) {
|
||||
mockStatement.getWarnings();
|
||||
ctrlStatement.setReturnValue(null);
|
||||
}
|
||||
mockStatement.close();
|
||||
ctrlStatement.setVoidCallable();
|
||||
|
||||
mockConnection.createStatement();
|
||||
ctrlConnection.setReturnValue(mockStatement);
|
||||
|
||||
replay();
|
||||
|
||||
JdbcTemplate template = new JdbcTemplate(mockDataSource);
|
||||
assertEquals(new BigDecimal(22.5), template.queryForObject(sql, BigDecimal.class));
|
||||
}
|
||||
|
||||
public void testQueryForObjectWithInteger() throws Exception {
|
||||
String sql = "SELECT AGE FROM CUSTMR WHERE ID = 3";
|
||||
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1);
|
||||
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getInt(1);
|
||||
ctrlResultSet.setReturnValue(22);
|
||||
mockResultSet.wasNull();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockStatement.executeQuery(sql);
|
||||
ctrlStatement.setReturnValue(mockResultSet);
|
||||
if (debugEnabled) {
|
||||
mockStatement.getWarnings();
|
||||
ctrlStatement.setReturnValue(null);
|
||||
}
|
||||
mockStatement.close();
|
||||
ctrlStatement.setVoidCallable();
|
||||
|
||||
mockConnection.createStatement();
|
||||
ctrlConnection.setReturnValue(mockStatement);
|
||||
|
||||
replay();
|
||||
|
||||
JdbcTemplate template = new JdbcTemplate(mockDataSource);
|
||||
assertEquals(new Integer(22), template.queryForObject(sql, Integer.class));
|
||||
}
|
||||
|
||||
public void testQueryForObjectWithIntegerAndNull() throws Exception {
|
||||
String sql = "SELECT AGE FROM CUSTMR WHERE ID = 3";
|
||||
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1);
|
||||
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getInt(1);
|
||||
ctrlResultSet.setReturnValue(0);
|
||||
mockResultSet.wasNull();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockStatement.executeQuery(sql);
|
||||
ctrlStatement.setReturnValue(mockResultSet);
|
||||
if (debugEnabled) {
|
||||
mockStatement.getWarnings();
|
||||
ctrlStatement.setReturnValue(null);
|
||||
}
|
||||
mockStatement.close();
|
||||
ctrlStatement.setVoidCallable();
|
||||
|
||||
mockConnection.createStatement();
|
||||
ctrlConnection.setReturnValue(mockStatement);
|
||||
|
||||
replay();
|
||||
|
||||
JdbcTemplate template = new JdbcTemplate(mockDataSource);
|
||||
assertNull(template.queryForObject(sql, Integer.class));
|
||||
}
|
||||
|
||||
public void testQueryForInt() throws Exception {
|
||||
String sql = "SELECT AGE FROM CUSTMR WHERE ID = 3";
|
||||
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1);
|
||||
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getInt(1);
|
||||
ctrlResultSet.setReturnValue(22);
|
||||
mockResultSet.wasNull();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockStatement.executeQuery(sql);
|
||||
ctrlStatement.setReturnValue(mockResultSet);
|
||||
if (debugEnabled) {
|
||||
mockStatement.getWarnings();
|
||||
ctrlStatement.setReturnValue(null);
|
||||
}
|
||||
mockStatement.close();
|
||||
ctrlStatement.setVoidCallable();
|
||||
|
||||
mockConnection.createStatement();
|
||||
ctrlConnection.setReturnValue(mockStatement);
|
||||
|
||||
replay();
|
||||
|
||||
JdbcTemplate template = new JdbcTemplate(mockDataSource);
|
||||
int i = template.queryForInt(sql);
|
||||
assertEquals("Return of an int", 22, i);
|
||||
}
|
||||
|
||||
public void testQueryForLong() throws Exception {
|
||||
String sql = "SELECT AGE FROM CUSTMR WHERE ID = 3";
|
||||
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1);
|
||||
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getLong(1);
|
||||
ctrlResultSet.setReturnValue(87);
|
||||
mockResultSet.wasNull();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockStatement.executeQuery(sql);
|
||||
ctrlStatement.setReturnValue(mockResultSet);
|
||||
if (debugEnabled) {
|
||||
mockStatement.getWarnings();
|
||||
ctrlStatement.setReturnValue(null);
|
||||
}
|
||||
mockStatement.close();
|
||||
ctrlStatement.setVoidCallable();
|
||||
|
||||
mockConnection.createStatement();
|
||||
ctrlConnection.setReturnValue(mockStatement);
|
||||
|
||||
replay();
|
||||
|
||||
JdbcTemplate template = new JdbcTemplate(mockDataSource);
|
||||
long l = template.queryForLong(sql);
|
||||
assertEquals("Return of a long", 87, l);
|
||||
}
|
||||
|
||||
public void testQueryForListWithArgs() throws Exception {
|
||||
doTestQueryForListWithArgs("SELECT AGE FROM CUSTMR WHERE ID < ?");
|
||||
}
|
||||
|
||||
public void testQueryForListIsNotConfusedByNamedParameterPrefix() throws Exception {
|
||||
doTestQueryForListWithArgs("SELECT AGE FROM PREFIX:CUSTMR WHERE ID < ?");
|
||||
}
|
||||
|
||||
private void doTestQueryForListWithArgs(String sql) throws Exception {
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1, 2);
|
||||
mockResultSetMetaData.getColumnLabel(1);
|
||||
ctrlResultSetMetaData.setReturnValue("age", 2);
|
||||
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData, 2);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getObject(1);
|
||||
ctrlResultSet.setReturnValue(new Integer(11));
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getObject(1);
|
||||
ctrlResultSet.setReturnValue(new Integer(12));
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockPreparedStatement.setObject(1, new Integer(3));
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.executeQuery();
|
||||
ctrlPreparedStatement.setReturnValue(mockResultSet);
|
||||
if (debugEnabled) {
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
}
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(sql);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
JdbcTemplate template = new JdbcTemplate(mockDataSource);
|
||||
|
||||
List li = template.queryForList(sql, new Object[] {new Integer(3)});
|
||||
assertEquals("All rows returned", 2, li.size());
|
||||
assertEquals("First row is Integer", 11, ((Integer)((Map)li.get(0)).get("age")).intValue());
|
||||
assertEquals("Second row is Integer", 12, ((Integer)((Map)li.get(1)).get("age")).intValue());
|
||||
}
|
||||
|
||||
public void testQueryForListWithArgsAndEmptyResult() throws Exception {
|
||||
String sql = "SELECT AGE FROM CUSTMR WHERE ID < ?";
|
||||
|
||||
ctrlResultSet = MockControl.createControl(ResultSet.class);
|
||||
mockResultSet = (ResultSet) ctrlResultSet.getMock();
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockPreparedStatement.setObject(1, new Integer(3));
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.executeQuery();
|
||||
ctrlPreparedStatement.setReturnValue(mockResultSet);
|
||||
if (debugEnabled) {
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
}
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(sql);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
JdbcTemplate template = new JdbcTemplate(mockDataSource);
|
||||
|
||||
List li = template.queryForList(sql, new Object[] {new Integer(3)});
|
||||
assertEquals("All rows returned", 0, li.size());
|
||||
}
|
||||
|
||||
public void testQueryForListWithArgsAndSingleRowAndColumn() throws Exception {
|
||||
String sql = "SELECT AGE FROM CUSTMR WHERE ID < ?";
|
||||
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1);
|
||||
mockResultSetMetaData.getColumnLabel(1);
|
||||
ctrlResultSetMetaData.setReturnValue("age", 1);
|
||||
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getObject(1);
|
||||
ctrlResultSet.setReturnValue(new Integer(11));
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockPreparedStatement.setObject(1, new Integer(3));
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.executeQuery();
|
||||
ctrlPreparedStatement.setReturnValue(mockResultSet);
|
||||
if (debugEnabled) {
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
}
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(sql);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
JdbcTemplate template = new JdbcTemplate(mockDataSource);
|
||||
|
||||
List li = template.queryForList(sql, new Object[] {new Integer(3)});
|
||||
assertEquals("All rows returned", 1, li.size());
|
||||
assertEquals("First row is Integer", 11, ((Integer)((Map)li.get(0)).get("age")).intValue());
|
||||
}
|
||||
|
||||
public void testQueryForListWithArgsAndIntegerElementAndSingleRowAndColumn() throws Exception {
|
||||
String sql = "SELECT AGE FROM CUSTMR WHERE ID < ?";
|
||||
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1);
|
||||
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getInt(1);
|
||||
ctrlResultSet.setReturnValue(11);
|
||||
mockResultSet.wasNull();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockPreparedStatement.setObject(1, new Integer(3));
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.executeQuery();
|
||||
ctrlPreparedStatement.setReturnValue(mockResultSet);
|
||||
if (debugEnabled) {
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
}
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(sql);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
JdbcTemplate template = new JdbcTemplate(mockDataSource);
|
||||
|
||||
List li = template.queryForList(sql, new Object[] {new Integer(3)}, Integer.class);
|
||||
assertEquals("All rows returned", 1, li.size());
|
||||
assertEquals("First row is Integer", 11, ((Integer) li.get(0)).intValue());
|
||||
}
|
||||
|
||||
public void testQueryForMapWithArgsAndSingleRowAndColumn() throws Exception {
|
||||
String sql = "SELECT AGE FROM CUSTMR WHERE ID < ?";
|
||||
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1);
|
||||
mockResultSetMetaData.getColumnLabel(1);
|
||||
ctrlResultSetMetaData.setReturnValue("age", 1);
|
||||
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getObject(1);
|
||||
ctrlResultSet.setReturnValue(new Integer(11));
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockPreparedStatement.setObject(1, new Integer(3));
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.executeQuery();
|
||||
ctrlPreparedStatement.setReturnValue(mockResultSet);
|
||||
if (debugEnabled) {
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
}
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(sql);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
JdbcTemplate template = new JdbcTemplate(mockDataSource);
|
||||
|
||||
Map map = template.queryForMap(sql, new Object[] {new Integer(3)});
|
||||
assertEquals("Row is Integer", 11, ((Integer) map.get("age")).intValue());
|
||||
}
|
||||
|
||||
public void testQueryForObjectWithArgsAndRowMapper() throws Exception {
|
||||
String sql = "SELECT AGE FROM CUSTMR WHERE ID = ?";
|
||||
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getInt(1);
|
||||
ctrlResultSet.setReturnValue(22);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockPreparedStatement.setObject(1, new Integer(3));
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.executeQuery();
|
||||
ctrlPreparedStatement.setReturnValue(mockResultSet);
|
||||
if (debugEnabled) {
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
}
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(sql);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
JdbcTemplate template = new JdbcTemplate(mockDataSource);
|
||||
|
||||
Object o = template.queryForObject(sql, new Object[] {new Integer(3)}, new RowMapper() {
|
||||
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
return new Integer(rs.getInt(1));
|
||||
}
|
||||
});
|
||||
assertTrue("Correct result type", o instanceof Integer);
|
||||
}
|
||||
|
||||
public void testQueryForObjectWithArgsAndInteger() throws Exception {
|
||||
String sql = "SELECT AGE FROM CUSTMR WHERE ID = ?";
|
||||
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1);
|
||||
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getInt(1);
|
||||
ctrlResultSet.setReturnValue(22);
|
||||
mockResultSet.wasNull();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockPreparedStatement.setObject(1, new Integer(3));
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.executeQuery();
|
||||
ctrlPreparedStatement.setReturnValue(mockResultSet);
|
||||
if (debugEnabled) {
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
}
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(sql);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
JdbcTemplate template = new JdbcTemplate(mockDataSource);
|
||||
|
||||
Object o = template.queryForObject(sql, new Object[] {new Integer(3)}, Integer.class);
|
||||
assertTrue("Correct result type", o instanceof Integer);
|
||||
}
|
||||
|
||||
public void testQueryForIntWithArgs() throws Exception {
|
||||
String sql = "SELECT AGE FROM CUSTMR WHERE ID = ?";
|
||||
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1);
|
||||
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getInt(1);
|
||||
ctrlResultSet.setReturnValue(22);
|
||||
mockResultSet.wasNull();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockPreparedStatement.setObject(1, new Integer(3));
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.executeQuery();
|
||||
ctrlPreparedStatement.setReturnValue(mockResultSet);
|
||||
if (debugEnabled) {
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
}
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(sql);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
JdbcTemplate template = new JdbcTemplate(mockDataSource);
|
||||
int i = template.queryForInt(sql, new Object[] {new Integer(3)});
|
||||
assertEquals("Return of an int", 22, i);
|
||||
}
|
||||
|
||||
public void testQueryForLongWithArgs() throws Exception {
|
||||
String sql = "SELECT AGE FROM CUSTMR WHERE ID = ?";
|
||||
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1);
|
||||
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getLong(1);
|
||||
ctrlResultSet.setReturnValue(87);
|
||||
mockResultSet.wasNull();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockPreparedStatement.setObject(1, new Integer(3));
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.executeQuery();
|
||||
ctrlPreparedStatement.setReturnValue(mockResultSet);
|
||||
if (debugEnabled) {
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
}
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(sql);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
JdbcTemplate template = new JdbcTemplate(mockDataSource);
|
||||
long l = template.queryForLong(sql, new Object[] {new Integer(3)});
|
||||
assertEquals("Return of a long", 87, l);
|
||||
}
|
||||
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,248 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.sql.Types;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.easymock.MockControl;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
|
||||
import org.springframework.jdbc.support.SQLStateSQLExceptionTranslator;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 02.08.2004
|
||||
*/
|
||||
public class RowMapperTests extends TestCase {
|
||||
|
||||
private final boolean debugEnabled = LogFactory.getLog(JdbcTemplate.class).isDebugEnabled();
|
||||
|
||||
private MockControl conControl;
|
||||
private Connection con;
|
||||
private MockControl rsControl;
|
||||
private ResultSet rs;
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
private List result;
|
||||
|
||||
protected void setUp() throws SQLException {
|
||||
conControl = MockControl.createControl(Connection.class);
|
||||
con = (Connection) conControl.getMock();
|
||||
con.isClosed();
|
||||
conControl.setDefaultReturnValue(false);
|
||||
|
||||
rsControl = MockControl.createControl(ResultSet.class);
|
||||
rs = (ResultSet) rsControl.getMock();
|
||||
rs.next();
|
||||
rsControl.setReturnValue(true, 1);
|
||||
rs.getString(1);
|
||||
rsControl.setReturnValue("tb1", 1);
|
||||
rs.getInt(2);
|
||||
rsControl.setReturnValue(1, 1);
|
||||
rs.next();
|
||||
rsControl.setReturnValue(true, 1);
|
||||
rs.getString(1);
|
||||
rsControl.setReturnValue("tb2", 1);
|
||||
rs.getInt(2);
|
||||
rsControl.setReturnValue(2, 1);
|
||||
rs.next();
|
||||
rsControl.setReturnValue(false, 1);
|
||||
rs.close();
|
||||
rsControl.setVoidCallable(1);
|
||||
rsControl.replay();
|
||||
|
||||
jdbcTemplate = new JdbcTemplate();
|
||||
jdbcTemplate.setDataSource(new SingleConnectionDataSource(con, false));
|
||||
jdbcTemplate.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
|
||||
jdbcTemplate.afterPropertiesSet();
|
||||
}
|
||||
|
||||
public void testStaticQueryWithRowMapper() throws SQLException {
|
||||
MockControl stmtControl = MockControl.createControl(Statement.class);
|
||||
Statement stmt = (Statement) stmtControl.getMock();
|
||||
|
||||
con.createStatement();
|
||||
conControl.setReturnValue(stmt, 1);
|
||||
stmt.executeQuery("some SQL");
|
||||
stmtControl.setReturnValue(rs, 1);
|
||||
if (debugEnabled) {
|
||||
stmt.getWarnings();
|
||||
stmtControl.setReturnValue(null, 1);
|
||||
}
|
||||
stmt.close();
|
||||
stmtControl.setVoidCallable(1);
|
||||
|
||||
conControl.replay();
|
||||
stmtControl.replay();
|
||||
|
||||
result = jdbcTemplate.query("some SQL", new TestRowMapper());
|
||||
|
||||
stmtControl.verify();
|
||||
verify();
|
||||
}
|
||||
|
||||
public void testPreparedStatementCreatorWithRowMapper() throws SQLException {
|
||||
MockControl psControl = MockControl.createControl(PreparedStatement.class);
|
||||
final PreparedStatement ps = (PreparedStatement) psControl.getMock();
|
||||
|
||||
ps.executeQuery();
|
||||
psControl.setReturnValue(rs, 1);
|
||||
if (debugEnabled) {
|
||||
ps.getWarnings();
|
||||
psControl.setReturnValue(null, 1);
|
||||
}
|
||||
ps.close();
|
||||
psControl.setVoidCallable(1);
|
||||
|
||||
conControl.replay();
|
||||
psControl.replay();
|
||||
|
||||
result = jdbcTemplate.query(
|
||||
new PreparedStatementCreator() {
|
||||
public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
|
||||
return ps;
|
||||
}
|
||||
}, new TestRowMapper());
|
||||
|
||||
psControl.verify();
|
||||
verify();
|
||||
}
|
||||
|
||||
public void testPreparedStatementSetterWithRowMapper() throws SQLException {
|
||||
MockControl psControl = MockControl.createControl(PreparedStatement.class);
|
||||
final PreparedStatement ps = (PreparedStatement) psControl.getMock();
|
||||
|
||||
con.prepareStatement("some SQL");
|
||||
conControl.setReturnValue(ps, 1);
|
||||
ps.setString(1, "test");
|
||||
psControl.setVoidCallable(1);
|
||||
ps.executeQuery();
|
||||
psControl.setReturnValue(rs, 1);
|
||||
if (debugEnabled) {
|
||||
ps.getWarnings();
|
||||
psControl.setReturnValue(null, 1);
|
||||
}
|
||||
ps.close();
|
||||
psControl.setVoidCallable(1);
|
||||
|
||||
conControl.replay();
|
||||
psControl.replay();
|
||||
|
||||
result = jdbcTemplate.query(
|
||||
"some SQL",
|
||||
new PreparedStatementSetter() {
|
||||
public void setValues(PreparedStatement ps) throws SQLException {
|
||||
ps.setString(1, "test");
|
||||
}
|
||||
}, new TestRowMapper());
|
||||
|
||||
psControl.verify();
|
||||
verify();
|
||||
}
|
||||
|
||||
public void testQueryWithArgsAndRowMapper() throws SQLException {
|
||||
MockControl psControl = MockControl.createControl(PreparedStatement.class);
|
||||
final PreparedStatement ps = (PreparedStatement) psControl.getMock();
|
||||
|
||||
con.prepareStatement("some SQL");
|
||||
conControl.setReturnValue(ps, 1);
|
||||
ps.setString(1, "test1");
|
||||
ps.setString(2, "test2");
|
||||
psControl.setVoidCallable(1);
|
||||
ps.executeQuery();
|
||||
psControl.setReturnValue(rs, 1);
|
||||
if (debugEnabled) {
|
||||
ps.getWarnings();
|
||||
psControl.setReturnValue(null, 1);
|
||||
}
|
||||
ps.close();
|
||||
psControl.setVoidCallable(1);
|
||||
|
||||
conControl.replay();
|
||||
psControl.replay();
|
||||
|
||||
result = jdbcTemplate.query(
|
||||
"some SQL",
|
||||
new Object[] {"test1", "test2"},
|
||||
new TestRowMapper());
|
||||
|
||||
psControl.verify();
|
||||
verify();
|
||||
}
|
||||
|
||||
public void testQueryWithArgsAndTypesAndRowMapper() throws SQLException {
|
||||
MockControl psControl = MockControl.createControl(PreparedStatement.class);
|
||||
final PreparedStatement ps = (PreparedStatement) psControl.getMock();
|
||||
|
||||
con.prepareStatement("some SQL");
|
||||
conControl.setReturnValue(ps, 1);
|
||||
ps.setString(1, "test1");
|
||||
ps.setString(2, "test2");
|
||||
psControl.setVoidCallable(1);
|
||||
ps.executeQuery();
|
||||
psControl.setReturnValue(rs, 1);
|
||||
if (debugEnabled) {
|
||||
ps.getWarnings();
|
||||
psControl.setReturnValue(null, 1);
|
||||
}
|
||||
ps.close();
|
||||
psControl.setVoidCallable(1);
|
||||
|
||||
conControl.replay();
|
||||
psControl.replay();
|
||||
|
||||
result = jdbcTemplate.query(
|
||||
"some SQL",
|
||||
new Object[] {"test1", "test2"},
|
||||
new int[] {Types.VARCHAR, Types.VARCHAR},
|
||||
new TestRowMapper());
|
||||
|
||||
psControl.verify();
|
||||
verify();
|
||||
}
|
||||
|
||||
protected void verify() {
|
||||
conControl.verify();
|
||||
rsControl.verify();
|
||||
|
||||
assertTrue(result != null);
|
||||
assertEquals(2, result.size());
|
||||
TestBean tb1 = (TestBean) result.get(0);
|
||||
TestBean tb2 = (TestBean) result.get(1);
|
||||
assertEquals("tb1", tb1.getName());
|
||||
assertEquals(1, tb1.getAge());
|
||||
assertEquals("tb2", tb2.getName());
|
||||
assertEquals(2, tb2.getAge());
|
||||
}
|
||||
|
||||
|
||||
private static class TestRowMapper implements RowMapper {
|
||||
|
||||
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
return new TestBean(rs.getString(1), rs.getInt(2));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,223 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.easymock.MockControl;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 31.08.2004
|
||||
*/
|
||||
public class StatementCreatorUtilsTests extends TestCase {
|
||||
|
||||
private MockControl psControl;
|
||||
private PreparedStatement ps;
|
||||
|
||||
protected void setUp() {
|
||||
psControl = MockControl.createControl(PreparedStatement.class);
|
||||
ps = (PreparedStatement) psControl.getMock();
|
||||
}
|
||||
|
||||
protected void tearDown() {
|
||||
psControl.verify();
|
||||
}
|
||||
|
||||
public void testSetParameterValueWithNullAndType() throws SQLException {
|
||||
ps.setNull(1, Types.VARCHAR);
|
||||
psControl.setVoidCallable(1);
|
||||
psControl.replay();
|
||||
StatementCreatorUtils.setParameterValue(ps, 1, Types.VARCHAR, null, null);
|
||||
}
|
||||
|
||||
public void testSetParameterValueWithNullAndTypeName() throws SQLException {
|
||||
ps.setNull(1, Types.VARCHAR, "mytype");
|
||||
psControl.setVoidCallable(1);
|
||||
psControl.replay();
|
||||
StatementCreatorUtils.setParameterValue(ps, 1, Types.VARCHAR, "mytype", null);
|
||||
}
|
||||
|
||||
public void testSetParameterValueWithNullAndUnknownType() throws SQLException {
|
||||
ps.setNull(1, Types.NULL);
|
||||
psControl.setVoidCallable(1);
|
||||
psControl.replay();
|
||||
StatementCreatorUtils.setParameterValue(ps, 1, SqlTypeValue.TYPE_UNKNOWN, null, null);
|
||||
}
|
||||
|
||||
public void testSetParameterValueWithNullAndUnknownTypeOnInformix() throws SQLException {
|
||||
MockControl conControl = MockControl.createControl(Connection.class);
|
||||
Connection con = (Connection) conControl.getMock();
|
||||
MockControl metaDataControl = MockControl.createControl(DatabaseMetaData.class);
|
||||
DatabaseMetaData metaData = (DatabaseMetaData) metaDataControl.getMock();
|
||||
ps.getConnection();
|
||||
psControl.setReturnValue(con, 1);
|
||||
con.getMetaData();
|
||||
conControl.setReturnValue(metaData, 1);
|
||||
metaData.getDatabaseProductName();
|
||||
metaDataControl.setReturnValue("Informix Dynamic Server");
|
||||
metaData.getDriverName();
|
||||
metaDataControl.setReturnValue("Informix Driver");
|
||||
ps.setObject(1, null);
|
||||
psControl.setVoidCallable(1);
|
||||
psControl.replay();
|
||||
conControl.replay();
|
||||
metaDataControl.replay();
|
||||
StatementCreatorUtils.setParameterValue(ps, 1, SqlTypeValue.TYPE_UNKNOWN, null, null);
|
||||
conControl.verify();
|
||||
metaDataControl.verify();
|
||||
}
|
||||
|
||||
public void testSetParameterValueWithNullAndUnknownTypeOnDerbyEmbedded() throws SQLException {
|
||||
MockControl conControl = MockControl.createControl(Connection.class);
|
||||
Connection con = (Connection) conControl.getMock();
|
||||
MockControl metaDataControl = MockControl.createControl(DatabaseMetaData.class);
|
||||
DatabaseMetaData metaData = (DatabaseMetaData) metaDataControl.getMock();
|
||||
ps.getConnection();
|
||||
psControl.setReturnValue(con, 1);
|
||||
con.getMetaData();
|
||||
conControl.setReturnValue(metaData, 1);
|
||||
metaData.getDatabaseProductName();
|
||||
metaDataControl.setReturnValue("Apache Derby");
|
||||
metaData.getDriverName();
|
||||
metaDataControl.setReturnValue("Apache Derby Embedded Driver");
|
||||
ps.setNull(1, Types.VARCHAR);
|
||||
psControl.setVoidCallable(1);
|
||||
psControl.replay();
|
||||
conControl.replay();
|
||||
metaDataControl.replay();
|
||||
StatementCreatorUtils.setParameterValue(ps, 1, SqlTypeValue.TYPE_UNKNOWN, null, null);
|
||||
conControl.verify();
|
||||
metaDataControl.verify();
|
||||
}
|
||||
|
||||
public void testSetParameterValueWithString() throws SQLException {
|
||||
ps.setString(1, "test");
|
||||
psControl.setVoidCallable(1);
|
||||
psControl.replay();
|
||||
StatementCreatorUtils.setParameterValue(ps, 1, Types.VARCHAR, null, "test");
|
||||
}
|
||||
|
||||
public void testSetParameterValueWithStringAndSpecialType() throws SQLException {
|
||||
ps.setObject(1, "test", Types.CHAR);
|
||||
psControl.setVoidCallable(1);
|
||||
psControl.replay();
|
||||
StatementCreatorUtils.setParameterValue(ps, 1, Types.CHAR, null, "test");
|
||||
}
|
||||
|
||||
public void testSetParameterValueWithStringAndUnknownType() throws SQLException {
|
||||
ps.setString(1, "test");
|
||||
psControl.setVoidCallable(1);
|
||||
psControl.replay();
|
||||
StatementCreatorUtils.setParameterValue(ps, 1, SqlTypeValue.TYPE_UNKNOWN, null, "test");
|
||||
}
|
||||
|
||||
public void testSetParameterValueWithSqlDate() throws SQLException {
|
||||
java.sql.Date date = new java.sql.Date(1000);
|
||||
ps.setDate(1, date);
|
||||
psControl.setVoidCallable(1);
|
||||
psControl.replay();
|
||||
StatementCreatorUtils.setParameterValue(ps, 1, Types.DATE, null, date);
|
||||
}
|
||||
|
||||
public void testSetParameterValueWithDateAndUtilDate() throws SQLException {
|
||||
java.util.Date date = new java.util.Date(1000);
|
||||
ps.setDate(1, new java.sql.Date(1000));
|
||||
psControl.setVoidCallable(1);
|
||||
psControl.replay();
|
||||
StatementCreatorUtils.setParameterValue(ps, 1, Types.DATE, null, date);
|
||||
}
|
||||
|
||||
public void testSetParameterValueWithDateAndCalendar() throws SQLException {
|
||||
java.util.Calendar cal = new GregorianCalendar();
|
||||
ps.setDate(1, new java.sql.Date(cal.getTime().getTime()), cal);
|
||||
psControl.setVoidCallable(1);
|
||||
psControl.replay();
|
||||
StatementCreatorUtils.setParameterValue(ps, 1, Types.DATE, null, cal);
|
||||
}
|
||||
|
||||
public void testSetParameterValueWithSqlTime() throws SQLException {
|
||||
java.sql.Time time = new java.sql.Time(1000);
|
||||
ps.setTime(1, time);
|
||||
psControl.setVoidCallable(1);
|
||||
psControl.replay();
|
||||
StatementCreatorUtils.setParameterValue(ps, 1, Types.TIME, null, time);
|
||||
}
|
||||
|
||||
public void testSetParameterValueWithTimeAndUtilDate() throws SQLException {
|
||||
java.util.Date date = new java.util.Date(1000);
|
||||
ps.setTime(1, new java.sql.Time(1000));
|
||||
psControl.setVoidCallable(1);
|
||||
psControl.replay();
|
||||
StatementCreatorUtils.setParameterValue(ps, 1, Types.TIME, null, date);
|
||||
}
|
||||
|
||||
public void testSetParameterValueWithTimeAndCalendar() throws SQLException {
|
||||
java.util.Calendar cal = new GregorianCalendar();
|
||||
ps.setTime(1, new java.sql.Time(cal.getTime().getTime()), cal);
|
||||
psControl.setVoidCallable(1);
|
||||
psControl.replay();
|
||||
StatementCreatorUtils.setParameterValue(ps, 1, Types.TIME, null, cal);
|
||||
}
|
||||
|
||||
public void testSetParameterValueWithSqlTimestamp() throws SQLException {
|
||||
java.sql.Timestamp timestamp = new java.sql.Timestamp(1000);
|
||||
ps.setTimestamp(1, timestamp);
|
||||
psControl.setVoidCallable(1);
|
||||
psControl.replay();
|
||||
StatementCreatorUtils.setParameterValue(ps, 1, Types.TIMESTAMP, null, timestamp);
|
||||
}
|
||||
|
||||
public void testSetParameterValueWithTimestampAndUtilDate() throws SQLException {
|
||||
java.util.Date date = new java.util.Date(1000);
|
||||
ps.setTimestamp(1, new java.sql.Timestamp(1000));
|
||||
psControl.setVoidCallable(1);
|
||||
psControl.replay();
|
||||
StatementCreatorUtils.setParameterValue(ps, 1, Types.TIMESTAMP, null, date);
|
||||
}
|
||||
|
||||
public void testSetParameterValueWithTimestampAndCalendar() throws SQLException {
|
||||
java.util.Calendar cal = new GregorianCalendar();
|
||||
ps.setTimestamp(1, new java.sql.Timestamp(cal.getTime().getTime()), cal);
|
||||
psControl.setVoidCallable(1);
|
||||
psControl.replay();
|
||||
StatementCreatorUtils.setParameterValue(ps, 1, Types.TIMESTAMP, null, cal);
|
||||
}
|
||||
|
||||
public void testSetParameterValueWithDateAndUnknownType() throws SQLException {
|
||||
java.util.Date date = new java.util.Date(1000);
|
||||
ps.setTimestamp(1, new java.sql.Timestamp(1000));
|
||||
psControl.setVoidCallable(1);
|
||||
psControl.replay();
|
||||
StatementCreatorUtils.setParameterValue(ps, 1, SqlTypeValue.TYPE_UNKNOWN, null, date);
|
||||
}
|
||||
|
||||
public void testSetParameterValueWithCalendarAndUnknownType() throws SQLException {
|
||||
java.util.Calendar cal = new GregorianCalendar();
|
||||
ps.setTimestamp(1, new java.sql.Timestamp(cal.getTime().getTime()), cal);
|
||||
psControl.setVoidCallable(1);
|
||||
psControl.replay();
|
||||
StatementCreatorUtils.setParameterValue(ps, 1, SqlTypeValue.TYPE_UNKNOWN, null, cal);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,608 @@
|
||||
/*
|
||||
* 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.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
|
||||
import org.springframework.jdbc.AbstractJdbcTests;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
/**
|
||||
* @author Thomas Risberg
|
||||
*/
|
||||
public class NamedParameterQueryTests extends AbstractJdbcTests {
|
||||
|
||||
private MockControl ctrlPreparedStatement;
|
||||
private PreparedStatement mockPreparedStatement;
|
||||
private MockControl ctrlResultSet;
|
||||
private ResultSet mockResultSet;
|
||||
private MockControl ctrlResultSetMetaData;
|
||||
private ResultSetMetaData mockResultSetMetaData;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
ctrlPreparedStatement = MockControl.createControl(PreparedStatement.class);
|
||||
mockPreparedStatement = (PreparedStatement) ctrlPreparedStatement.getMock();
|
||||
ctrlResultSet = MockControl.createControl(ResultSet.class);
|
||||
mockResultSet = (ResultSet) ctrlResultSet.getMock();
|
||||
ctrlResultSetMetaData = MockControl.createControl(ResultSetMetaData.class);
|
||||
mockResultSetMetaData = (ResultSetMetaData) ctrlResultSetMetaData.getMock();
|
||||
}
|
||||
|
||||
protected void replay() {
|
||||
super.replay();
|
||||
ctrlPreparedStatement.replay();
|
||||
ctrlResultSet.replay();
|
||||
ctrlResultSetMetaData.replay();
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
if (false && shouldVerify()) {
|
||||
ctrlPreparedStatement.verify();
|
||||
ctrlResultSet.verify();
|
||||
ctrlResultSetMetaData.verify();
|
||||
}
|
||||
}
|
||||
|
||||
public void testQueryForListWithParamMap() throws Exception {
|
||||
String sql = "SELECT AGE FROM CUSTMR WHERE ID < :id";
|
||||
String sqlToUse = "SELECT AGE FROM CUSTMR WHERE ID < ?";
|
||||
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1, 2);
|
||||
mockResultSetMetaData.getColumnLabel(1);
|
||||
ctrlResultSetMetaData.setReturnValue("age", 2);
|
||||
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData, 2);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getObject(1);
|
||||
ctrlResultSet.setReturnValue(new Integer(11));
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getObject(1);
|
||||
ctrlResultSet.setReturnValue(new Integer(12));
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockPreparedStatement.setObject(1, new Integer(3));
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.executeQuery();
|
||||
ctrlPreparedStatement.setReturnValue(mockResultSet);
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(sqlToUse);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(mockDataSource);
|
||||
|
||||
MapSqlParameterSource parms = new MapSqlParameterSource();
|
||||
parms.addValue("id", new Integer(3));
|
||||
|
||||
List li = template.queryForList(sql, parms);
|
||||
assertEquals("All rows returned", 2, li.size());
|
||||
assertEquals("First row is Integer", 11, ((Integer)((Map)li.get(0)).get("age")).intValue());
|
||||
assertEquals("Second row is Integer", 12, ((Integer)((Map)li.get(1)).get("age")).intValue());
|
||||
}
|
||||
|
||||
public void testQueryForListWithParamMapAndEmptyResult() throws Exception {
|
||||
String sql = "SELECT AGE FROM CUSTMR WHERE ID < :id";
|
||||
String sqlToUse = "SELECT AGE FROM CUSTMR WHERE ID < ?";
|
||||
|
||||
ctrlResultSet = MockControl.createControl(ResultSet.class);
|
||||
mockResultSet = (ResultSet) ctrlResultSet.getMock();
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockPreparedStatement.setObject(1, new Integer(3));
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.executeQuery();
|
||||
ctrlPreparedStatement.setReturnValue(mockResultSet);
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(sqlToUse);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(mockDataSource);
|
||||
|
||||
MapSqlParameterSource parms = new MapSqlParameterSource();
|
||||
parms.addValue("id", new Integer(3));
|
||||
|
||||
List li = template.queryForList(sql, parms);
|
||||
assertEquals("All rows returned", 0, li.size());
|
||||
}
|
||||
|
||||
public void testQueryForListWithParamMapAndSingleRowAndColumn() throws Exception {
|
||||
String sql = "SELECT AGE FROM CUSTMR WHERE ID < :id";
|
||||
String sqlToUse = "SELECT AGE FROM CUSTMR WHERE ID < ?";
|
||||
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1);
|
||||
mockResultSetMetaData.getColumnLabel(1);
|
||||
ctrlResultSetMetaData.setReturnValue("age", 1);
|
||||
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getObject(1);
|
||||
ctrlResultSet.setReturnValue(new Integer(11));
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockPreparedStatement.setObject(1, new Integer(3));
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.executeQuery();
|
||||
ctrlPreparedStatement.setReturnValue(mockResultSet);
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(sqlToUse);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(mockDataSource);
|
||||
|
||||
MapSqlParameterSource parms = new MapSqlParameterSource();
|
||||
parms.addValue("id", new Integer(3));
|
||||
|
||||
List li = template.queryForList(sql, parms);
|
||||
assertEquals("All rows returned", 1, li.size());
|
||||
assertEquals("First row is Integer", 11, ((Integer)((Map)li.get(0)).get("age")).intValue());
|
||||
}
|
||||
|
||||
public void testQueryForListWithParamMapAndIntegerElementAndSingleRowAndColumn() throws Exception {
|
||||
String sql = "SELECT AGE FROM CUSTMR WHERE ID < :id";
|
||||
String sqlToUse = "SELECT AGE FROM CUSTMR WHERE ID < ?";
|
||||
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1);
|
||||
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getInt(1);
|
||||
ctrlResultSet.setReturnValue(11);
|
||||
mockResultSet.wasNull();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockPreparedStatement.setObject(1, new Integer(3));
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.executeQuery();
|
||||
ctrlPreparedStatement.setReturnValue(mockResultSet);
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(sqlToUse);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(mockDataSource);
|
||||
|
||||
MapSqlParameterSource parms = new MapSqlParameterSource();
|
||||
parms.addValue("id", new Integer(3));
|
||||
|
||||
List li = template.queryForList(sql, parms, Integer.class);
|
||||
assertEquals("All rows returned", 1, li.size());
|
||||
assertEquals("First row is Integer", 11, ((Integer) li.get(0)).intValue());
|
||||
}
|
||||
|
||||
public void testQueryForMapWithParamMapAndSingleRowAndColumn() throws Exception {
|
||||
String sql = "SELECT AGE FROM CUSTMR WHERE ID < :id";
|
||||
String sqlToUse = "SELECT AGE FROM CUSTMR WHERE ID < ?";
|
||||
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1);
|
||||
mockResultSetMetaData.getColumnLabel(1);
|
||||
ctrlResultSetMetaData.setReturnValue("age", 1);
|
||||
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getObject(1);
|
||||
ctrlResultSet.setReturnValue(new Integer(11));
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockPreparedStatement.setObject(1, new Integer(3));
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.executeQuery();
|
||||
ctrlPreparedStatement.setReturnValue(mockResultSet);
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(sqlToUse);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(mockDataSource);
|
||||
|
||||
MapSqlParameterSource parms = new MapSqlParameterSource();
|
||||
parms.addValue("id", new Integer(3));
|
||||
|
||||
Map map = template.queryForMap(sql, parms);
|
||||
assertEquals("Row is Integer", 11, ((Integer) map.get("age")).intValue());
|
||||
}
|
||||
|
||||
public void testQueryForObjectWithParamMapAndRowMapper() throws Exception {
|
||||
String sql = "SELECT AGE FROM CUSTMR WHERE ID = :id";
|
||||
String sqlToUse = "SELECT AGE FROM CUSTMR WHERE ID = ?";
|
||||
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getInt(1);
|
||||
ctrlResultSet.setReturnValue(22);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockPreparedStatement.setObject(1, new Integer(3));
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.executeQuery();
|
||||
ctrlPreparedStatement.setReturnValue(mockResultSet);
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(sqlToUse);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(mockDataSource);
|
||||
|
||||
MapSqlParameterSource parms = new MapSqlParameterSource();
|
||||
parms.addValue("id", new Integer(3));
|
||||
|
||||
Object o = template.queryForObject(sql, parms, new RowMapper() {
|
||||
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
return new Integer(rs.getInt(1));
|
||||
}
|
||||
});
|
||||
assertTrue("Correct result type", o instanceof Integer);
|
||||
}
|
||||
|
||||
public void testQueryForObjectWithMapAndInteger() throws Exception {
|
||||
String sql = "SELECT AGE FROM CUSTMR WHERE ID = :id";
|
||||
String sqlToUse = "SELECT AGE FROM CUSTMR WHERE ID = ?";
|
||||
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1);
|
||||
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getInt(1);
|
||||
ctrlResultSet.setReturnValue(22);
|
||||
mockResultSet.wasNull();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockPreparedStatement.setObject(1, new Integer(3));
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.executeQuery();
|
||||
ctrlPreparedStatement.setReturnValue(mockResultSet);
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(sqlToUse);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(mockDataSource);
|
||||
|
||||
Map parms = new HashMap();
|
||||
parms.put("id", new Integer(3));
|
||||
|
||||
Object o = template.queryForObject(sql, parms, Integer.class);
|
||||
assertTrue("Correct result type", o instanceof Integer);
|
||||
}
|
||||
|
||||
public void testQueryForObjectWithParamMapAndInteger() throws Exception {
|
||||
String sql = "SELECT AGE FROM CUSTMR WHERE ID = :id";
|
||||
String sqlToUse = "SELECT AGE FROM CUSTMR WHERE ID = ?";
|
||||
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1);
|
||||
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getInt(1);
|
||||
ctrlResultSet.setReturnValue(22);
|
||||
mockResultSet.wasNull();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockPreparedStatement.setObject(1, new Integer(3));
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.executeQuery();
|
||||
ctrlPreparedStatement.setReturnValue(mockResultSet);
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(sqlToUse);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(mockDataSource);
|
||||
|
||||
MapSqlParameterSource parms = new MapSqlParameterSource();
|
||||
parms.addValue("id", new Integer(3));
|
||||
|
||||
Object o = template.queryForObject(sql, parms, Integer.class);
|
||||
assertTrue("Correct result type", o instanceof Integer);
|
||||
}
|
||||
|
||||
public void testQueryForObjectWithParamMapAndList() throws Exception {
|
||||
String sql = "SELECT AGE FROM CUSTMR WHERE ID IN (:ids)";
|
||||
String sqlToUse = "SELECT AGE FROM CUSTMR WHERE ID IN (?, ?)";
|
||||
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1);
|
||||
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getInt(1);
|
||||
ctrlResultSet.setReturnValue(22);
|
||||
mockResultSet.wasNull();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockPreparedStatement.setObject(1, new Integer(3));
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.setObject(2, new Integer(4));
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.executeQuery();
|
||||
ctrlPreparedStatement.setReturnValue(mockResultSet);
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(sqlToUse);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(mockDataSource);
|
||||
|
||||
MapSqlParameterSource parms = new MapSqlParameterSource();
|
||||
parms.addValue("ids", Arrays.asList(new Object[] {new Integer(3), new Integer(4)}));
|
||||
|
||||
Object o = template.queryForObject(sql, parms, Integer.class);
|
||||
assertTrue("Correct result type", o instanceof Integer);
|
||||
}
|
||||
|
||||
public void testQueryForObjectWithParamMapAndListOfExpressionLists() throws Exception {
|
||||
String sql = "SELECT AGE FROM CUSTMR WHERE (ID, NAME) IN (:multiExpressionList)";
|
||||
String sqlToUse = "SELECT AGE FROM CUSTMR WHERE (ID, NAME) IN ((?, ?), (?, ?))";
|
||||
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1);
|
||||
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getInt(1);
|
||||
ctrlResultSet.setReturnValue(22);
|
||||
mockResultSet.wasNull();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockPreparedStatement.setObject(1, new Integer(3));
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.setString(2, "Rod");
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.setObject(3, new Integer(4));
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.setString(4, "Juergen");
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.executeQuery();
|
||||
ctrlPreparedStatement.setReturnValue(mockResultSet);
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(sqlToUse);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(mockDataSource);
|
||||
|
||||
MapSqlParameterSource parms = new MapSqlParameterSource();
|
||||
List l1 = new ArrayList();
|
||||
l1.add(new Object[] {new Integer(3), "Rod"});
|
||||
l1.add(new Object[] {new Integer(4), "Juergen"});
|
||||
parms.addValue("multiExpressionList", l1);
|
||||
|
||||
Object o = template.queryForObject(sql, parms, Integer.class);
|
||||
assertTrue("Correct result type", o instanceof Integer);
|
||||
}
|
||||
|
||||
public void testQueryForIntWithParamMap() throws Exception {
|
||||
String sql = "SELECT AGE FROM CUSTMR WHERE ID = :id";
|
||||
String sqlToUse = "SELECT AGE FROM CUSTMR WHERE ID = ?";
|
||||
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1);
|
||||
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getDouble(1);
|
||||
ctrlResultSet.setReturnValue(22.0d);
|
||||
mockResultSet.wasNull();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockPreparedStatement.setObject(1, new Integer(3));
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.executeQuery();
|
||||
ctrlPreparedStatement.setReturnValue(mockResultSet);
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(sqlToUse);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(mockDataSource);
|
||||
|
||||
MapSqlParameterSource parms = new MapSqlParameterSource();
|
||||
parms.addValue("id", new Integer(3));
|
||||
|
||||
int i = template.queryForInt(sql, parms);
|
||||
assertEquals("Return of an int", 22, i);
|
||||
}
|
||||
|
||||
public void testQueryForLongWithParamBean() throws Exception {
|
||||
String sql = "SELECT AGE FROM CUSTMR WHERE ID = :id";
|
||||
String sqlToUse = "SELECT AGE FROM CUSTMR WHERE ID = ?";
|
||||
|
||||
mockResultSetMetaData.getColumnCount();
|
||||
ctrlResultSetMetaData.setReturnValue(1);
|
||||
|
||||
mockResultSet.getMetaData();
|
||||
ctrlResultSet.setReturnValue(mockResultSetMetaData);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(true);
|
||||
mockResultSet.getDouble(1);
|
||||
ctrlResultSet.setReturnValue(87.0d);
|
||||
mockResultSet.wasNull();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.next();
|
||||
ctrlResultSet.setReturnValue(false);
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
mockPreparedStatement.setObject(1, new Integer(3), Types.INTEGER);
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.executeQuery();
|
||||
ctrlPreparedStatement.setReturnValue(mockResultSet);
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockConnection.prepareStatement(sqlToUse);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
|
||||
replay();
|
||||
|
||||
NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(mockDataSource);
|
||||
BeanPropertySqlParameterSource parms = new BeanPropertySqlParameterSource(new ParameterBean(3));
|
||||
|
||||
long l = template.queryForLong(sql, parms);
|
||||
assertEquals("Return of a long", 87, l);
|
||||
}
|
||||
|
||||
|
||||
private static class ParameterBean {
|
||||
|
||||
private int id;
|
||||
|
||||
public ParameterBean(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* 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.support;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.jdbc.AbstractJdbcTests;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
/**
|
||||
* @author Rod Johnson
|
||||
*/
|
||||
public class JdbcBeanDefinitionReaderTests extends AbstractJdbcTests {
|
||||
|
||||
private final boolean debugEnabled = LogFactory.getLog(JdbcTemplate.class).isDebugEnabled();
|
||||
|
||||
|
||||
public void testValid() throws Exception {
|
||||
String sql = "SELECT NAME AS NAME, PROPERTY AS PROPERTY, VALUE AS VALUE FROM T";
|
||||
|
||||
MockControl ctrlResultSet = MockControl.createControl(ResultSet.class);
|
||||
ResultSet mockResultSet = (ResultSet) ctrlResultSet.getMock();
|
||||
ctrlResultSet.expectAndReturn(mockResultSet.next(), true, 2);
|
||||
ctrlResultSet.expectAndReturn(mockResultSet.next(), false);
|
||||
|
||||
// first row
|
||||
ctrlResultSet.expectAndReturn(mockResultSet.getString(1), "one");
|
||||
ctrlResultSet.expectAndReturn(mockResultSet.getString(2), "(class)");
|
||||
ctrlResultSet.expectAndReturn(mockResultSet.getString(3), "org.springframework.beans.TestBean");
|
||||
|
||||
// second row
|
||||
ctrlResultSet.expectAndReturn(mockResultSet.getString(1), "one");
|
||||
ctrlResultSet.expectAndReturn(mockResultSet.getString(2), "age");
|
||||
ctrlResultSet.expectAndReturn(mockResultSet.getString(3), "53");
|
||||
|
||||
mockResultSet.close();
|
||||
ctrlResultSet.setVoidCallable();
|
||||
|
||||
MockControl ctrlStatement = MockControl.createControl(Statement.class);
|
||||
Statement mockStatement = (Statement) ctrlStatement.getMock();
|
||||
ctrlStatement.expectAndReturn(mockStatement.executeQuery(sql), mockResultSet);
|
||||
if (debugEnabled) {
|
||||
ctrlStatement.expectAndReturn(mockStatement.getWarnings(), null);
|
||||
}
|
||||
mockStatement.close();
|
||||
ctrlStatement.setVoidCallable();
|
||||
|
||||
mockConnection.createStatement();
|
||||
ctrlConnection.setReturnValue(mockStatement);
|
||||
|
||||
ctrlResultSet.replay();
|
||||
ctrlStatement.replay();
|
||||
replay();
|
||||
|
||||
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||
JdbcBeanDefinitionReader reader = new JdbcBeanDefinitionReader(bf);
|
||||
reader.setDataSource(mockDataSource);
|
||||
reader.loadBeanDefinitions(sql);
|
||||
assertEquals("Incorrect number of bean definitions", 1, bf.getBeanDefinitionCount());
|
||||
TestBean tb = (TestBean) bf.getBean("one");
|
||||
assertEquals("Age in TestBean was wrong.", 53, tb.getAge());
|
||||
|
||||
ctrlResultSet.verify();
|
||||
ctrlStatement.verify();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright 2002-2005 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.support;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.easymock.MockControl;
|
||||
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 30.07.2003
|
||||
*/
|
||||
public class JdbcDaoSupportTests extends TestCase {
|
||||
|
||||
public void testJdbcDaoSupportWithDataSource() throws Exception {
|
||||
MockControl dsControl = MockControl.createControl(DataSource.class);
|
||||
DataSource ds = (DataSource) dsControl.getMock();
|
||||
final List test = new ArrayList();
|
||||
JdbcDaoSupport dao = new JdbcDaoSupport() {
|
||||
protected void initDao() {
|
||||
test.add("test");
|
||||
}
|
||||
};
|
||||
dao.setDataSource(ds);
|
||||
dao.afterPropertiesSet();
|
||||
assertEquals("Correct DataSource", ds, dao.getDataSource());
|
||||
assertEquals("Correct JdbcTemplate", ds, dao.getJdbcTemplate().getDataSource());
|
||||
assertEquals("initDao called", test.size(), 1);
|
||||
}
|
||||
|
||||
public void testJdbcDaoSupportWithJdbcTemplate() throws Exception {
|
||||
JdbcTemplate template = new JdbcTemplate();
|
||||
final List test = new ArrayList();
|
||||
JdbcDaoSupport dao = new JdbcDaoSupport() {
|
||||
protected void initDao() {
|
||||
test.add("test");
|
||||
}
|
||||
};
|
||||
dao.setJdbcTemplate(template);
|
||||
dao.afterPropertiesSet();
|
||||
assertEquals("Correct JdbcTemplate", dao.getJdbcTemplate(), template);
|
||||
assertEquals("initDao called", test.size(), 1);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
/*
|
||||
* Copyright 2002-2005 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.support;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.easymock.MockControl;
|
||||
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.dao.IncorrectResultSizeDataAccessException;
|
||||
import org.springframework.jdbc.LobRetrievalFailureException;
|
||||
import org.springframework.jdbc.support.lob.LobCreator;
|
||||
import org.springframework.jdbc.support.lob.LobHandler;
|
||||
|
||||
/**
|
||||
* @author Alef Arendsen
|
||||
*/
|
||||
public class LobSupportTests extends TestCase {
|
||||
|
||||
public void testCreatingPreparedStatementCallback() throws SQLException {
|
||||
// - return value should match
|
||||
// - lob creator should be closed
|
||||
// - set return value should be called
|
||||
// - execute update should be called
|
||||
|
||||
MockControl lobHandlerControl = MockControl.createControl(LobHandler.class);
|
||||
LobHandler handler = (LobHandler)lobHandlerControl.getMock();
|
||||
|
||||
MockControl lobCreatorControl = MockControl.createControl(LobCreator.class);
|
||||
LobCreator creator = (LobCreator)lobCreatorControl.getMock();
|
||||
|
||||
MockControl psControl = MockControl.createControl(PreparedStatement.class);
|
||||
PreparedStatement ps = (PreparedStatement)psControl.getMock();
|
||||
|
||||
handler.getLobCreator();
|
||||
lobHandlerControl.setReturnValue(creator);
|
||||
ps.executeUpdate();
|
||||
psControl.setReturnValue(3);
|
||||
creator.close();
|
||||
|
||||
lobHandlerControl.replay();
|
||||
lobCreatorControl.replay();
|
||||
psControl.replay();
|
||||
|
||||
class SetValuesCalled {
|
||||
boolean b = false;
|
||||
}
|
||||
|
||||
final SetValuesCalled svc = new SetValuesCalled();
|
||||
|
||||
AbstractLobCreatingPreparedStatementCallback psc =
|
||||
new AbstractLobCreatingPreparedStatementCallback(handler) {
|
||||
|
||||
protected void setValues(PreparedStatement ps, LobCreator lobCreator)
|
||||
throws SQLException, DataAccessException {
|
||||
svc.b = true;
|
||||
}
|
||||
};
|
||||
|
||||
assertEquals(new Integer(3), psc.doInPreparedStatement(ps));
|
||||
|
||||
lobHandlerControl.verify();
|
||||
lobCreatorControl.verify();
|
||||
psControl.verify();
|
||||
assertTrue(svc.b);
|
||||
}
|
||||
|
||||
public void testAbstractLobStreamingResultSetExtractorNoRows() throws SQLException {
|
||||
MockControl rsetControl = MockControl.createControl(ResultSet.class);
|
||||
ResultSet rset = (ResultSet)rsetControl.getMock();
|
||||
rset.next();
|
||||
rsetControl.setReturnValue(false);
|
||||
rsetControl.replay();
|
||||
|
||||
AbstractLobStreamingResultSetExtractor lobRse = getResultSetExtractor(false);
|
||||
try {
|
||||
lobRse.extractData(rset);
|
||||
fail("IncorrectResultSizeDataAccessException should have been thrown");
|
||||
} catch (IncorrectResultSizeDataAccessException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testAbstractLobStreamingResultSetExtractorOneRow() throws SQLException {
|
||||
MockControl rsetControl = MockControl.createControl(ResultSet.class);
|
||||
ResultSet rset = (ResultSet)rsetControl.getMock();
|
||||
rset.next();
|
||||
rsetControl.setReturnValue(true);
|
||||
// see if it's called
|
||||
rset.clearWarnings();
|
||||
rset.next();
|
||||
rsetControl.setReturnValue(false);
|
||||
rsetControl.replay();
|
||||
|
||||
AbstractLobStreamingResultSetExtractor lobRse = getResultSetExtractor(false);
|
||||
lobRse.extractData(rset);
|
||||
rsetControl.verify();
|
||||
}
|
||||
|
||||
public void testAbstractLobStreamingResultSetExtractorMultipleRows() throws SQLException {
|
||||
MockControl rsetControl = MockControl.createControl(ResultSet.class);
|
||||
ResultSet rset = (ResultSet)rsetControl.getMock();
|
||||
rset.next();
|
||||
rsetControl.setReturnValue(true);
|
||||
// see if it's called
|
||||
rset.clearWarnings();
|
||||
rset.next();
|
||||
rsetControl.setReturnValue(true);
|
||||
rsetControl.replay();
|
||||
|
||||
AbstractLobStreamingResultSetExtractor lobRse = getResultSetExtractor(false);
|
||||
try {
|
||||
lobRse.extractData(rset);
|
||||
fail("IncorrectResultSizeDataAccessException should have been thrown");
|
||||
} catch (IncorrectResultSizeDataAccessException e) {
|
||||
// expected
|
||||
}
|
||||
rsetControl.verify();
|
||||
}
|
||||
|
||||
public void testAbstractLobStreamingResultSetExtractorCorrectException() throws SQLException {
|
||||
MockControl rsetControl = MockControl.createControl(ResultSet.class);
|
||||
ResultSet rset = (ResultSet)rsetControl.getMock();
|
||||
rset.next();
|
||||
rsetControl.setReturnValue(true);
|
||||
rsetControl.replay();
|
||||
|
||||
AbstractLobStreamingResultSetExtractor lobRse = getResultSetExtractor(true);
|
||||
try {
|
||||
lobRse.extractData(rset);
|
||||
fail("LobRetrievalFailureException should have been thrown");
|
||||
} catch (LobRetrievalFailureException e) {
|
||||
// expected
|
||||
}
|
||||
rsetControl.verify();
|
||||
}
|
||||
|
||||
private AbstractLobStreamingResultSetExtractor getResultSetExtractor(final boolean ex) {
|
||||
AbstractLobStreamingResultSetExtractor lobRse = new AbstractLobStreamingResultSetExtractor() {
|
||||
protected void streamData(ResultSet rs) throws SQLException, IOException {
|
||||
if (ex) {
|
||||
throw new IOException();
|
||||
}
|
||||
else {
|
||||
rs.clearWarnings();
|
||||
}
|
||||
}
|
||||
};
|
||||
return lobRse;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,274 @@
|
||||
/*
|
||||
* Copyright 2002-2005 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.support;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.easymock.ArgumentsMatcher;
|
||||
import org.easymock.MockControl;
|
||||
|
||||
import org.springframework.jdbc.support.lob.LobCreator;
|
||||
import org.springframework.jdbc.support.lob.LobHandler;
|
||||
|
||||
/**
|
||||
* Test cases for the sql lob value:
|
||||
*
|
||||
* BLOB:
|
||||
* 1. Types.BLOB: setBlobAsBytes (byte[])
|
||||
* 2. String: setBlobAsBytes (byte[])
|
||||
* 3. else: IllegalArgumentException
|
||||
*
|
||||
* CLOB:
|
||||
* 4. String or NULL: setClobAsString (String)
|
||||
* 5. InputStream: setClobAsAsciiStream (InputStream)
|
||||
* 6. Reader: setClobAsCharacterStream (Reader)
|
||||
* 7. else: IllegalArgumentException
|
||||
*
|
||||
* @author Alef Arendsen
|
||||
*/
|
||||
public class SqlLobValueTests extends TestCase {
|
||||
|
||||
private MockControl psControl;
|
||||
private PreparedStatement ps;
|
||||
|
||||
private MockControl lobHandlerControl;
|
||||
private LobHandler handler;
|
||||
|
||||
private MockControl lobCreatorControl;
|
||||
private LobCreator creator;
|
||||
|
||||
public void setUp() {
|
||||
// create preparedstatement
|
||||
psControl = MockControl.createControl(PreparedStatement.class);
|
||||
ps = (PreparedStatement) psControl.getMock();
|
||||
|
||||
// create handler controler
|
||||
lobHandlerControl = MockControl.createControl(LobHandler.class);
|
||||
handler = (LobHandler) lobHandlerControl.getMock();
|
||||
|
||||
// create creator control
|
||||
lobCreatorControl = MockControl.createControl(LobCreator.class);
|
||||
creator = (LobCreator) lobCreatorControl.getMock();
|
||||
|
||||
// set initial state
|
||||
handler.getLobCreator();
|
||||
lobHandlerControl.setReturnValue(creator);
|
||||
}
|
||||
|
||||
private void replay() {
|
||||
psControl.replay();
|
||||
lobHandlerControl.replay();
|
||||
lobCreatorControl.replay();
|
||||
}
|
||||
|
||||
public void test1() throws SQLException {
|
||||
byte[] testBytes = "Bla".getBytes();
|
||||
creator.setBlobAsBytes(ps, 1, testBytes);
|
||||
replay();
|
||||
SqlLobValue lob = new SqlLobValue(testBytes, handler);
|
||||
lob.setTypeValue(ps, 1, Types.BLOB, "test");
|
||||
lobHandlerControl.verify();
|
||||
lobCreatorControl.verify();
|
||||
}
|
||||
|
||||
public void test2() throws SQLException {
|
||||
String testString = "Bla";
|
||||
|
||||
creator.setBlobAsBytes(ps, 1, testString.getBytes());
|
||||
// set a matcher to match the byte array!
|
||||
lobCreatorControl.setMatcher(new ArgumentsMatcher() {
|
||||
public boolean matches(Object[] arg0, Object[] arg1) {
|
||||
byte[] one = (byte[]) arg0[2];
|
||||
byte[] two = (byte[]) arg1[2];
|
||||
return Arrays.equals(one, two);
|
||||
}
|
||||
public String toString(Object[] arg0) {
|
||||
return "bla";
|
||||
}
|
||||
});
|
||||
|
||||
replay();
|
||||
|
||||
SqlLobValue lob = new SqlLobValue(testString, handler);
|
||||
lob.setTypeValue(ps, 1, Types.BLOB, "test");
|
||||
lobHandlerControl.verify();
|
||||
lobCreatorControl.verify();
|
||||
|
||||
}
|
||||
|
||||
public void test3()
|
||||
throws SQLException {
|
||||
|
||||
Date testContent = new Date();
|
||||
|
||||
SqlLobValue lob =
|
||||
new SqlLobValue(new InputStreamReader(new ByteArrayInputStream("Bla".getBytes())), 12);
|
||||
try {
|
||||
lob.setTypeValue(ps, 1, Types.BLOB, "test");
|
||||
fail("IllegalArgumentException should have been thrown");
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void test4() throws SQLException {
|
||||
String testContent = "Bla";
|
||||
creator.setClobAsString(ps, 1, testContent);
|
||||
|
||||
replay();
|
||||
|
||||
SqlLobValue lob = new SqlLobValue(testContent, handler);
|
||||
lob.setTypeValue(ps, 1, Types.CLOB, "test");
|
||||
lobHandlerControl.verify();
|
||||
lobCreatorControl.verify();
|
||||
}
|
||||
|
||||
public void test5() throws SQLException {
|
||||
byte[] testContent = "Bla".getBytes();
|
||||
ByteArrayInputStream bais = new ByteArrayInputStream(testContent);
|
||||
creator.setClobAsAsciiStream(ps, 1, bais, 3);
|
||||
lobCreatorControl.setMatcher(new ArgumentsMatcher() {
|
||||
public boolean matches(Object[] arg0, Object[] arg1) {
|
||||
// for now, match always
|
||||
return true;
|
||||
}
|
||||
public String toString(Object[] arg0) {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
replay();
|
||||
|
||||
SqlLobValue lob = new SqlLobValue(new ByteArrayInputStream(testContent), 3, handler);
|
||||
lob.setTypeValue(ps, 1, Types.CLOB, "test");
|
||||
lobHandlerControl.verify();
|
||||
lobCreatorControl.verify();
|
||||
}
|
||||
|
||||
public void test6()throws SQLException {
|
||||
byte[] testContent = "Bla".getBytes();
|
||||
ByteArrayInputStream bais = new ByteArrayInputStream(testContent);
|
||||
InputStreamReader reader = new InputStreamReader(bais);
|
||||
creator.setClobAsCharacterStream(ps, 1, reader, 3);
|
||||
lobCreatorControl.setMatcher(new ArgumentsMatcher() {
|
||||
public boolean matches(Object[] arg0, Object[] arg1) {
|
||||
// for now, match always
|
||||
return true;
|
||||
}
|
||||
public String toString(Object[] arg0) {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
replay();
|
||||
|
||||
SqlLobValue lob = new SqlLobValue(reader, 3, handler);
|
||||
lob.setTypeValue(ps, 1, Types.CLOB, "test");
|
||||
lobHandlerControl.verify();
|
||||
lobCreatorControl.verify();
|
||||
|
||||
}
|
||||
|
||||
public void test7() throws SQLException {
|
||||
Date testContent = new Date();
|
||||
|
||||
SqlLobValue lob = new SqlLobValue("bla".getBytes());
|
||||
try {
|
||||
lob.setTypeValue(ps, 1, Types.CLOB, "test");
|
||||
fail("IllegalArgumentException should have been thrown");
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testOtherConstructors() throws SQLException {
|
||||
// a bit BS, but we need to test them, as long as they don't throw exceptions
|
||||
|
||||
SqlLobValue lob = new SqlLobValue("bla");
|
||||
lob.setTypeValue(ps, 1, Types.CLOB, "test");
|
||||
|
||||
try {
|
||||
lob = new SqlLobValue("bla".getBytes());
|
||||
lob.setTypeValue(ps, 1, Types.CLOB, "test");
|
||||
fail("IllegalArgumentException should have been thrown");
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
// expected
|
||||
}
|
||||
|
||||
lob = new SqlLobValue(new ByteArrayInputStream("bla".getBytes()), 3);
|
||||
lob.setTypeValue(ps, 1, Types.CLOB, "test");
|
||||
|
||||
lob = new SqlLobValue(new InputStreamReader(
|
||||
new ByteArrayInputStream("bla".getBytes())), 3);
|
||||
lob.setTypeValue(ps, 1, Types.CLOB, "test");
|
||||
|
||||
// same for BLOB
|
||||
lob = new SqlLobValue("bla");
|
||||
lob.setTypeValue(ps, 1, Types.BLOB, "test");
|
||||
|
||||
lob = new SqlLobValue("bla".getBytes());
|
||||
lob.setTypeValue(ps, 1, Types.BLOB, "test");
|
||||
|
||||
lob = new SqlLobValue(new ByteArrayInputStream("bla".getBytes()), 3);
|
||||
lob.setTypeValue(ps, 1, Types.BLOB, "test");
|
||||
|
||||
lob = new SqlLobValue(new InputStreamReader(
|
||||
new ByteArrayInputStream("bla".getBytes())), 3);
|
||||
|
||||
try {
|
||||
lob.setTypeValue(ps, 1, Types.BLOB, "test");
|
||||
fail("IllegalArgumentException should have been thrown");
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testCorrectCleanup() throws SQLException {
|
||||
creator.setClobAsString(ps, 1, "Bla");
|
||||
creator.close();
|
||||
|
||||
replay();
|
||||
SqlLobValue lob = new SqlLobValue("Bla", handler);
|
||||
lob.setTypeValue(ps, 1, Types.CLOB, "test");
|
||||
lob.cleanup();
|
||||
|
||||
lobCreatorControl.verify();
|
||||
}
|
||||
|
||||
public void testOtherSqlType() throws SQLException {
|
||||
replay();
|
||||
SqlLobValue lob = new SqlLobValue("Bla", handler);
|
||||
try {
|
||||
lob.setTypeValue(ps, 1, Types.SMALLINT, "test");
|
||||
fail("IllegalArgumentException should have been thrown");
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* 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.test;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author Thomas Risberg
|
||||
*/
|
||||
public abstract class AbstractPerson {
|
||||
|
||||
private String name;
|
||||
|
||||
private long age;
|
||||
|
||||
private Date birth_date;
|
||||
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(long age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Date getBirth_date() {
|
||||
return birth_date;
|
||||
}
|
||||
|
||||
public void setBirth_date(Date birth_date) {
|
||||
this.birth_date = birth_date;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* 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.test;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* @author Thomas Risberg
|
||||
*/
|
||||
public class ConcretePerson extends AbstractPerson {
|
||||
|
||||
private BigDecimal balance;
|
||||
|
||||
|
||||
public BigDecimal getBalance() {
|
||||
return balance;
|
||||
}
|
||||
|
||||
public void setBalance(BigDecimal balance) {
|
||||
this.balance = balance;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* 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.test;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class ExtendedPerson extends ConcretePerson {
|
||||
|
||||
private Object someField;
|
||||
|
||||
|
||||
public Object getSomeField() {
|
||||
return someField;
|
||||
}
|
||||
|
||||
public void setSomeField(Object someField) {
|
||||
this.someField = someField;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* 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.test;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* @author Thomas Risberg
|
||||
*/
|
||||
public class Person {
|
||||
|
||||
private String name;
|
||||
|
||||
private long age;
|
||||
|
||||
private java.util.Date birth_date;
|
||||
|
||||
private BigDecimal balance;
|
||||
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(long age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public java.util.Date getBirth_date() {
|
||||
return birth_date;
|
||||
}
|
||||
|
||||
public void setBirth_date(java.util.Date birth_date) {
|
||||
this.birth_date = birth_date;
|
||||
}
|
||||
|
||||
public BigDecimal getBalance() {
|
||||
return balance;
|
||||
}
|
||||
|
||||
public void setBalance(BigDecimal balanace) {
|
||||
this.balance = balanace;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user