moving .test.* unit tests from .testsuite -> .test
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,39 @@
|
||||
|
||||
/*
|
||||
* 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 class Employee extends TestBean {
|
||||
|
||||
private String co;
|
||||
|
||||
/**
|
||||
* Constructor for Employee.
|
||||
*/
|
||||
public Employee() {
|
||||
super();
|
||||
}
|
||||
|
||||
public String getCompany() {
|
||||
return co;
|
||||
}
|
||||
|
||||
public void setCompany(String co) {
|
||||
this.co = co;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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,54 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* @author Rob Harrop
|
||||
* @since 2.0
|
||||
*/
|
||||
public class Pet {
|
||||
|
||||
private String name;
|
||||
|
||||
public Pet(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return getName();
|
||||
}
|
||||
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
final Pet pet = (Pet) o;
|
||||
|
||||
if (name != null ? !name.equals(pet.name) : pet.name != null) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return (name != null ? name.hashCode() : 0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 ITestBean#exceptional(Throwable)
|
||||
*/
|
||||
public void exceptional(Throwable t) throws Throwable {
|
||||
if (t != null) {
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
|
||||
public void unreliableFileOperation() throws IOException {
|
||||
throw new IOException();
|
||||
}
|
||||
/**
|
||||
* @see ITestBean#returnsThis()
|
||||
*/
|
||||
public Object returnsThis() {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see 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,77 @@
|
||||
/*
|
||||
* Copyright 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.test;
|
||||
|
||||
import org.springframework.beans.Pet;
|
||||
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
/**
|
||||
* Abstract JUnit 3.8 based unit test which verifies new functionality requested
|
||||
* in <a href="http://opensource.atlassian.com/projects/spring/browse/SPR-3550"
|
||||
* target="_blank">SPR-3350</a>.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
public abstract class AbstractSpr3350SingleSpringContextTests extends AbstractDependencyInjectionSpringContextTests {
|
||||
|
||||
private Pet cat;
|
||||
|
||||
|
||||
public AbstractSpr3350SingleSpringContextTests() {
|
||||
super();
|
||||
}
|
||||
|
||||
public AbstractSpr3350SingleSpringContextTests(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public final void setCat(final Pet cat) {
|
||||
this.cat = cat;
|
||||
}
|
||||
|
||||
/**
|
||||
* Forcing concrete subclasses to provide a config path appropriate to the
|
||||
* configured
|
||||
* {@link #createBeanDefinitionReader(org.springframework.context.support.GenericApplicationContext) BeanDefinitionReader}.
|
||||
*
|
||||
* @see org.springframework.test.AbstractSingleSpringContextTests#getConfigPath()
|
||||
*/
|
||||
protected abstract String getConfigPath();
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Test which addresses the following issue raised in SPR-3350:
|
||||
* </p>
|
||||
* <p>
|
||||
* {@link AbstractSingleSpringContextTests} always uses an
|
||||
* {@link XmlBeanDefinitionReader} internally when creating the
|
||||
* {@link ApplicationContext} inside
|
||||
* {@link #createApplicationContext(String[])}. It would be nice to have
|
||||
* the bean definition reader creation in a separate method so that
|
||||
* subclasses can choose that individually without having to copy-n-paste
|
||||
* code from createApplicationContext() to do the context creation and
|
||||
* refresh. Consider JavaConfig where an Annotation based reader can be
|
||||
* plugged in.
|
||||
* </p>
|
||||
*/
|
||||
public final void testApplicationContextNotAutoCreated() {
|
||||
assertNotNull("The cat field should have been autowired.", this.cat);
|
||||
assertEquals("Garfield", this.cat.getName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
cat.(class)=org.springframework.beans.Pet
|
||||
cat.$0=Garfield
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright 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.test;
|
||||
|
||||
import org.springframework.beans.factory.support.BeanDefinitionReader;
|
||||
import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
|
||||
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
|
||||
/**
|
||||
* Concrete implementation of {@link AbstractSpr3350SingleSpringContextTests}
|
||||
* which configures a {@link PropertiesBeanDefinitionReader} instead of the
|
||||
* default {@link XmlBeanDefinitionReader}.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
public class PropertiesBasedSpr3350SingleSpringContextTests extends AbstractSpr3350SingleSpringContextTests {
|
||||
|
||||
public PropertiesBasedSpr3350SingleSpringContextTests() {
|
||||
super();
|
||||
}
|
||||
|
||||
public PropertiesBasedSpr3350SingleSpringContextTests(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link PropertiesBeanDefinitionReader}.
|
||||
*
|
||||
* @see org.springframework.test.AbstractSingleSpringContextTests#createBeanDefinitionReader(org.springframework.context.support.GenericApplicationContext)
|
||||
*/
|
||||
protected final BeanDefinitionReader createBeanDefinitionReader(GenericApplicationContext context) {
|
||||
return new PropertiesBeanDefinitionReader(context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns
|
||||
* "PropertiesBasedSpr3350SingleSpringContextTests-context.properties".
|
||||
*/
|
||||
protected final String getConfigPath() {
|
||||
return "PropertiesBasedSpr3350SingleSpringContextTests-context.properties";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright 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.test;
|
||||
|
||||
/**
|
||||
* JUnit 3.8 based unit test which verifies new functionality requested in <a
|
||||
* href="http://opensource.atlassian.com/projects/spring/browse/SPR-3264"
|
||||
* target="_blank">SPR-3264</a>.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
* @see Spr3264SingleSpringContextTests
|
||||
*/
|
||||
public class Spr3264DependencyInjectionSpringContextTests extends AbstractDependencyInjectionSpringContextTests {
|
||||
|
||||
public Spr3264DependencyInjectionSpringContextTests() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Spr3264DependencyInjectionSpringContextTests(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Test which addresses the following issue raised in SPR-3264:
|
||||
* </p>
|
||||
* <p>
|
||||
* AbstractDependencyInjectionSpringContextTests will try to apply
|
||||
* auto-injection; this can be disabled but it has to be done manually
|
||||
* inside the onSetUp...
|
||||
* </p>
|
||||
*/
|
||||
public void testInjectDependenciesThrowsIllegalStateException() {
|
||||
|
||||
// Re-assert issues covered by Spr3264SingleSpringContextTests as a
|
||||
// safety net.
|
||||
assertNull("The ApplicationContext should NOT be automatically created if no 'locations' are defined.",
|
||||
this.applicationContext);
|
||||
assertEquals("Verifying the ApplicationContext load count.", 0, super.getLoadCount());
|
||||
|
||||
// Assert changes to AbstractDependencyInjectionSpringContextTests:
|
||||
new AssertThrows(IllegalStateException.class) {
|
||||
|
||||
public void test() throws Exception {
|
||||
Spr3264DependencyInjectionSpringContextTests.super.injectDependencies();
|
||||
}
|
||||
}.runTest();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 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.test;
|
||||
|
||||
/**
|
||||
* JUnit 3.8 based unit test which verifies new functionality requested in <a
|
||||
* href="http://opensource.atlassian.com/projects/spring/browse/SPR-3264"
|
||||
* target="_blank">SPR-3264</a>.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
* @see Spr3264DependencyInjectionSpringContextTests
|
||||
*/
|
||||
public class Spr3264SingleSpringContextTests extends AbstractSingleSpringContextTests {
|
||||
|
||||
public Spr3264SingleSpringContextTests() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Spr3264SingleSpringContextTests(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Test which addresses the following issue raised in SPR-3264:
|
||||
* </p>
|
||||
* <p>
|
||||
* AbstractSingleSpringContextTests always expects an application context to
|
||||
* be created even if no files/locations are specified which can lead to NPE
|
||||
* problems or force an appCtx to be instantiated even if not needed.
|
||||
* </p>
|
||||
*/
|
||||
public void testApplicationContextNotAutoCreated() {
|
||||
assertNull("The ApplicationContext should NOT be automatically created if no 'locations' are defined.",
|
||||
super.applicationContext);
|
||||
assertEquals("Verifying the ApplicationContext load count.", 0, super.getLoadCount());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
|
||||
|
||||
<bean id="cat" class="org.springframework.beans.Pet">
|
||||
<constructor-arg value="Garfield" />
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 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.test;
|
||||
|
||||
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
||||
|
||||
/**
|
||||
* Concrete implementation of {@link AbstractSpr3350SingleSpringContextTests}
|
||||
* which is based on the default {@link XmlBeanDefinitionReader}.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
public class XmlBasedSpr3350SingleSpringContextTests extends AbstractSpr3350SingleSpringContextTests {
|
||||
|
||||
public XmlBasedSpr3350SingleSpringContextTests() {
|
||||
super();
|
||||
}
|
||||
|
||||
public XmlBasedSpr3350SingleSpringContextTests(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns "XmlBasedSpr3350SingleSpringContextTests-context.xml".
|
||||
*/
|
||||
protected final String getConfigPath() {
|
||||
return "XmlBasedSpr3350SingleSpringContextTests-context.xml";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:p="http://www.springframework.org/schema/p"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
|
||||
|
||||
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
|
||||
p:driverClassName="org.hsqldb.jdbcDriver" p:url="jdbc:hsqldb:mem:transactional_tests" p:username="sa" p:password="" />
|
||||
|
||||
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
|
||||
p:data-source-ref="dataSource" />
|
||||
|
||||
</beans>
|
||||
@@ -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.test.annotation;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestResult;
|
||||
|
||||
/**
|
||||
* Verifies proper handling of {@link IfProfileValue @IfProfileValue} and
|
||||
* {@link ProfileValueSourceConfiguration @ProfileValueSourceConfiguration} in
|
||||
* conjunction with {@link AbstractAnnotationAwareTransactionalTests}.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
public class ProfileValueAnnotationAwareTransactionalTests extends TestCase {
|
||||
|
||||
private static final String NAME = "ProfileValueAnnotationAwareTransactionalTests.profile_value.name";
|
||||
|
||||
private static final String VALUE = "enigma";
|
||||
|
||||
|
||||
public ProfileValueAnnotationAwareTransactionalTests() {
|
||||
System.setProperty(NAME, VALUE);
|
||||
}
|
||||
|
||||
private void runTestAndAssertCounters(Class<? extends DefaultProfileValueSourceTestCase> testCaseType,
|
||||
String testName, int expectedInvocationCount, int expectedErrorCount, int expectedFailureCount)
|
||||
throws Exception {
|
||||
|
||||
DefaultProfileValueSourceTestCase testCase = testCaseType.newInstance();
|
||||
testCase.setName(testName);
|
||||
TestResult testResult = testCase.run();
|
||||
assertEquals("Verifying number of invocations for test method [" + testName + "].", expectedInvocationCount,
|
||||
testCase.invocationCount);
|
||||
assertEquals("Verifying number of errors for test method [" + testName + "].", expectedErrorCount,
|
||||
testResult.errorCount());
|
||||
assertEquals("Verifying number of failures for test method [" + testName + "].", expectedFailureCount,
|
||||
testResult.failureCount());
|
||||
}
|
||||
|
||||
private void runTests(Class<? extends DefaultProfileValueSourceTestCase> testCaseType) throws Exception {
|
||||
runTestAndAssertCounters(testCaseType, "testIfProfileValueEmpty", 0, 0, 0);
|
||||
runTestAndAssertCounters(testCaseType, "testIfProfileValueDisabledViaWrongName", 0, 0, 0);
|
||||
runTestAndAssertCounters(testCaseType, "testIfProfileValueDisabledViaWrongValue", 0, 0, 0);
|
||||
runTestAndAssertCounters(testCaseType, "testIfProfileValueEnabledViaSingleValue", 1, 0, 0);
|
||||
runTestAndAssertCounters(testCaseType, "testIfProfileValueEnabledViaMultipleValues", 1, 0, 0);
|
||||
runTestAndAssertCounters(testCaseType, "testIfProfileValueNotConfigured", 1, 0, 0);
|
||||
}
|
||||
|
||||
public void testDefaultProfileValueSource() throws Exception {
|
||||
assertEquals("Verifying the type of the configured ProfileValueSource.", SystemProfileValueSource.class,
|
||||
new DefaultProfileValueSourceTestCase().getProfileValueSource().getClass());
|
||||
runTests(DefaultProfileValueSourceTestCase.class);
|
||||
}
|
||||
|
||||
public void testHardCodedProfileValueSource() throws Exception {
|
||||
assertEquals("Verifying the type of the configured ProfileValueSource.", HardCodedProfileValueSource.class,
|
||||
new HardCodedProfileValueSourceTestCase().getProfileValueSource().getClass());
|
||||
runTests(HardCodedProfileValueSourceTestCase.class);
|
||||
}
|
||||
|
||||
|
||||
protected static class DefaultProfileValueSourceTestCase extends AbstractAnnotationAwareTransactionalTests {
|
||||
|
||||
int invocationCount = 0;
|
||||
|
||||
public DefaultProfileValueSourceTestCase() {
|
||||
}
|
||||
|
||||
public ProfileValueSource getProfileValueSource() {
|
||||
return super.profileValueSource;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getConfigPath() {
|
||||
return "ProfileValueAnnotationAwareTransactionalTests-context.xml";
|
||||
}
|
||||
|
||||
@NotTransactional
|
||||
@IfProfileValue(name = NAME)
|
||||
public void testIfProfileValueEmpty() {
|
||||
this.invocationCount++;
|
||||
fail("The body of a disabled test should never be executed!");
|
||||
}
|
||||
|
||||
@NotTransactional
|
||||
@IfProfileValue(name = NAME + "X", value = VALUE)
|
||||
public void testIfProfileValueDisabledViaWrongName() {
|
||||
this.invocationCount++;
|
||||
fail("The body of a disabled test should never be executed!");
|
||||
}
|
||||
|
||||
@NotTransactional
|
||||
@IfProfileValue(name = NAME, value = VALUE + "X")
|
||||
public void testIfProfileValueDisabledViaWrongValue() {
|
||||
this.invocationCount++;
|
||||
fail("The body of a disabled test should never be executed!");
|
||||
}
|
||||
|
||||
@NotTransactional
|
||||
@IfProfileValue(name = NAME, value = VALUE)
|
||||
public void testIfProfileValueEnabledViaSingleValue() {
|
||||
this.invocationCount++;
|
||||
}
|
||||
|
||||
@NotTransactional
|
||||
@IfProfileValue(name = NAME, values = { "foo", VALUE, "bar" })
|
||||
public void testIfProfileValueEnabledViaMultipleValues() {
|
||||
this.invocationCount++;
|
||||
}
|
||||
|
||||
@NotTransactional
|
||||
public void testIfProfileValueNotConfigured() {
|
||||
this.invocationCount++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ProfileValueSourceConfiguration(HardCodedProfileValueSource.class)
|
||||
protected static class HardCodedProfileValueSourceTestCase extends DefaultProfileValueSourceTestCase {
|
||||
}
|
||||
|
||||
|
||||
public static class HardCodedProfileValueSource implements ProfileValueSource {
|
||||
|
||||
public String get(String key) {
|
||||
return (key.equals(NAME) ? VALUE : null);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
/*
|
||||
* 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.test.context;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
import static org.junit.Assert.assertSame;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* JUnit 4 based unit test which verifies correct
|
||||
* {@link ContextCache application context caching} in conjunction with the
|
||||
* {@link SpringJUnit4ClassRunner} and the {@link DirtiesContext} annotation.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @author Juergen Hoeller
|
||||
* @since 2.5
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = "/org/springframework/test/context/junit4/SpringJUnit4ClassRunnerAppCtxTests-context.xml")
|
||||
public class SpringRunnerContextCacheTests implements ApplicationContextAware {
|
||||
|
||||
private static ApplicationContext dirtiedApplicationContext;
|
||||
|
||||
protected ApplicationContext applicationContext;
|
||||
|
||||
|
||||
/**
|
||||
* Asserts the statistics of the supplied context cache.
|
||||
* @param usageScenario the scenario in which the statistics are used
|
||||
* @param expectedSize the expected number of contexts in the cache
|
||||
* @param expectedHitCount the expected hit count
|
||||
* @param expectedMissCount the expected miss count
|
||||
*/
|
||||
public static final void assertContextCacheStatistics(String usageScenario, int expectedSize,
|
||||
int expectedHitCount, int expectedMissCount) {
|
||||
|
||||
ContextCache contextCache = TestContextManager.contextCache;
|
||||
assertEquals("Verifying number of contexts in cache (" + usageScenario + ").", expectedSize,
|
||||
contextCache.size());
|
||||
assertEquals("Verifying number of cache hits (" + usageScenario + ").", expectedHitCount,
|
||||
contextCache.getHitCount());
|
||||
assertEquals("Verifying number of cache misses (" + usageScenario + ").", expectedMissCount,
|
||||
contextCache.getMissCount());
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void verifyInitialCacheState() {
|
||||
dirtiedApplicationContext = null;
|
||||
ContextCache contextCache = TestContextManager.contextCache;
|
||||
contextCache.clear();
|
||||
contextCache.clearStatistics();
|
||||
assertContextCacheStatistics("BeforeClass", 0, 0, 0);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void verifyFinalCacheState() {
|
||||
assertContextCacheStatistics("AfterClass", 1, 1, 2);
|
||||
}
|
||||
|
||||
|
||||
public final void setApplicationContext(final ApplicationContext applicationContext) {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void dirtyContext() {
|
||||
assertContextCacheStatistics("dirtyContext()", 1, 0, 1);
|
||||
assertNotNull("The application context should have been set due to ApplicationContextAware semantics.",
|
||||
this.applicationContext);
|
||||
SpringRunnerContextCacheTests.dirtiedApplicationContext = this.applicationContext;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void verifyContextWasDirtied() {
|
||||
assertContextCacheStatistics("verifyContextWasDirtied()", 1, 0, 2);
|
||||
assertNotNull("The application context should have been set due to ApplicationContextAware semantics.",
|
||||
this.applicationContext);
|
||||
assertNotSame("The application context should have been 'dirtied'.",
|
||||
SpringRunnerContextCacheTests.dirtiedApplicationContext, this.applicationContext);
|
||||
SpringRunnerContextCacheTests.dirtiedApplicationContext = this.applicationContext;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void verifyContextWasNotDirtied() {
|
||||
assertContextCacheStatistics("verifyContextWasNotDirtied()", 1, 1, 2);
|
||||
assertNotNull("The application context should have been set due to ApplicationContextAware semantics.",
|
||||
this.applicationContext);
|
||||
assertSame("The application context should NOT have been 'dirtied'.",
|
||||
SpringRunnerContextCacheTests.dirtiedApplicationContext, this.applicationContext);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
/*
|
||||
* 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.test.context;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.test.context.support.AbstractTestExecutionListener;
|
||||
import org.springframework.core.style.ToStringCreator;
|
||||
|
||||
/**
|
||||
* JUnit 4 based unit test for {@link TestContextManager}, which verifies
|
||||
* proper <em>execution order</em> of registered
|
||||
* {@link TestExecutionListener TestExecutionListeners}.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
public class TestContextManagerTests {
|
||||
|
||||
private static final String FIRST = "veni";
|
||||
private static final String SECOND = "vidi";
|
||||
private static final String THIRD = "vici";
|
||||
|
||||
private static final List<String> afterTestMethodCalls = new ArrayList<String>();
|
||||
private static final List<String> beforeTestMethodCalls = new ArrayList<String>();
|
||||
|
||||
protected static final Log logger = LogFactory.getLog(TestContextManagerTests.class);
|
||||
|
||||
private TestContextManager testContextManager = null;
|
||||
|
||||
|
||||
/**
|
||||
* Asserts the <em>execution order</em> of 'before' and 'after' test
|
||||
* method calls on {@link TestExecutionListener listeners} registered for
|
||||
* the configured {@link TestContextManager}.
|
||||
*
|
||||
* @see #beforeTestMethodCalls
|
||||
* @see #afterTestMethodCalls
|
||||
*/
|
||||
private static void assertExecutionOrder(List<String> expectedBeforeTestMethodCalls,
|
||||
List<String> expectedAfterTestMethodCalls, final String usageContext) {
|
||||
|
||||
if (expectedBeforeTestMethodCalls == null) {
|
||||
expectedBeforeTestMethodCalls = new ArrayList<String>();
|
||||
}
|
||||
if (expectedAfterTestMethodCalls == null) {
|
||||
expectedAfterTestMethodCalls = new ArrayList<String>();
|
||||
}
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
for (final String listenerName : beforeTestMethodCalls) {
|
||||
logger.debug("'before' listener [" + listenerName + "] (" + usageContext + ").");
|
||||
}
|
||||
for (final String listenerName : afterTestMethodCalls) {
|
||||
logger.debug("'after' listener [" + listenerName + "] (" + usageContext + ").");
|
||||
}
|
||||
}
|
||||
|
||||
assertTrue("Verifying execution order of 'before' listeners' (" + usageContext + ").",
|
||||
CollectionUtils.isEqualCollection(expectedBeforeTestMethodCalls, beforeTestMethodCalls));
|
||||
assertTrue("Verifying execution order of 'after' listeners' (" + usageContext + ").",
|
||||
CollectionUtils.isEqualCollection(expectedAfterTestMethodCalls, afterTestMethodCalls));
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpBeforeClass() throws Exception {
|
||||
beforeTestMethodCalls.clear();
|
||||
afterTestMethodCalls.clear();
|
||||
assertExecutionOrder(null, null, "BeforeClass");
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies the expected {@link TestExecutionListener}
|
||||
* <em>execution order</em> after all test methods have completed.
|
||||
*/
|
||||
@AfterClass
|
||||
public static void verifyListenerExecutionOrderAfterClass() throws Exception {
|
||||
assertExecutionOrder(Arrays.<String> asList(FIRST, SECOND, THIRD),
|
||||
Arrays.<String> asList(THIRD, SECOND, FIRST), "AfterClass");
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUpTestContextManager() throws Throwable {
|
||||
|
||||
final Method testMethod = ExampleTestCase.class.getDeclaredMethod("exampleTestMethod", (Class<?>[]) null);
|
||||
this.testContextManager = new TestContextManager(ExampleTestCase.class);
|
||||
this.testContextManager.registerTestExecutionListeners(new NamedTestExecutionListener(FIRST),
|
||||
new NamedTestExecutionListener(SECOND), new NamedTestExecutionListener(THIRD));
|
||||
|
||||
assertEquals("Verifying the number of registered TestExecutionListeners.", 6,
|
||||
this.testContextManager.getTestExecutionListeners().size());
|
||||
|
||||
this.testContextManager.beforeTestMethod(new ExampleTestCase(), testMethod);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies the expected {@link TestExecutionListener}
|
||||
* <em>execution order</em> within a test method.
|
||||
*
|
||||
* @see #verifyListenerExecutionOrderAfterClass()
|
||||
*/
|
||||
@Test
|
||||
public void verifyListenerExecutionOrderWithinTestMethod() {
|
||||
assertExecutionOrder(Arrays.<String> asList(FIRST, SECOND, THIRD), null, "Test");
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDownTestContextManager() throws Throwable {
|
||||
final Method testMethod = ExampleTestCase.class.getDeclaredMethod("exampleTestMethod", (Class<?>[]) null);
|
||||
this.testContextManager.afterTestMethod(new ExampleTestCase(), testMethod, null);
|
||||
this.testContextManager = null;
|
||||
}
|
||||
|
||||
|
||||
@ContextConfiguration
|
||||
private static class ExampleTestCase {
|
||||
|
||||
public void exampleTestMethod() {
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
private static class NamedTestExecutionListener extends AbstractTestExecutionListener {
|
||||
|
||||
private final String name;
|
||||
|
||||
|
||||
public NamedTestExecutionListener(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTestMethod(final TestContext testContext) {
|
||||
afterTestMethodCalls.add(this.name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeTestMethod(final TestContext testContext) {
|
||||
beforeTestMethodCalls.add(this.name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new ToStringCreator(this).append("name", this.name).toString();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
/*
|
||||
* 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.test.context;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.test.context.support.AbstractTestExecutionListener;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* JUnit 4 based unit test for the
|
||||
* {@link TestExecutionListeners @TestExecutionListeners} annotation, which
|
||||
* verifies:
|
||||
* </p>
|
||||
* <ul>
|
||||
* <li>Proper registering of {@link TestExecutionListener listeners} in
|
||||
* conjunction with a {@link TestContextManager}</li>
|
||||
* <li><em>Inherited</em> functionality proposed in <a
|
||||
* href="http://opensource.atlassian.com/projects/spring/browse/SPR-3896"
|
||||
* target="_blank">SPR-3896</a></li>
|
||||
* </ul>
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
public class TestExecutionListenersTests {
|
||||
|
||||
@Test
|
||||
public void verifyNumDefaultListenersRegistered() throws Exception {
|
||||
TestContextManager testContextManager = new TestContextManager(DefaultListenersExampleTestCase.class);
|
||||
assertEquals("Verifying the number of registered TestExecutionListeners for DefaultListenersExampleTest.", 3,
|
||||
testContextManager.getTestExecutionListeners().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void verifyNumNonInheritedDefaultListenersRegistered() throws Exception {
|
||||
TestContextManager testContextManager = new TestContextManager(NonInheritedDefaultListenersExampleTestCase.class);
|
||||
assertEquals(
|
||||
"Verifying the number of registered TestExecutionListeners for NonInheritedDefaultListenersExampleTest.",
|
||||
1, testContextManager.getTestExecutionListeners().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void verifyNumInheritedDefaultListenersRegistered() throws Exception {
|
||||
TestContextManager testContextManager = new TestContextManager(InheritedDefaultListenersExampleTestCase.class);
|
||||
assertEquals(
|
||||
"Verifying the number of registered TestExecutionListeners for InheritedDefaultListenersExampleTest.",
|
||||
1, testContextManager.getTestExecutionListeners().size());
|
||||
|
||||
testContextManager = new TestContextManager(SubInheritedDefaultListenersExampleTestCase.class);
|
||||
assertEquals(
|
||||
"Verifying the number of registered TestExecutionListeners for SubInheritedDefaultListenersExampleTest.",
|
||||
1, testContextManager.getTestExecutionListeners().size());
|
||||
|
||||
testContextManager = new TestContextManager(SubSubInheritedDefaultListenersExampleTestCase.class);
|
||||
assertEquals(
|
||||
"Verifying the number of registered TestExecutionListeners for SubSubInheritedDefaultListenersExampleTest.",
|
||||
2, testContextManager.getTestExecutionListeners().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void verifyNumListenersRegistered() throws Exception {
|
||||
TestContextManager testContextManager = new TestContextManager(ExampleTestCase.class);
|
||||
assertEquals("Verifying the number of registered TestExecutionListeners for ExampleTest.", 3,
|
||||
testContextManager.getTestExecutionListeners().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void verifyNumNonInheritedListenersRegistered() throws Exception {
|
||||
TestContextManager testContextManager = new TestContextManager(NonInheritedListenersExampleTestCase.class);
|
||||
assertEquals("Verifying the number of registered TestExecutionListeners for NonInheritedListenersExampleTest.",
|
||||
1, testContextManager.getTestExecutionListeners().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void verifyNumInheritedListenersRegistered() throws Exception {
|
||||
TestContextManager testContextManager = new TestContextManager(InheritedListenersExampleTestCase.class);
|
||||
assertEquals("Verifying the number of registered TestExecutionListeners for InheritedListenersExampleTest.", 4,
|
||||
testContextManager.getTestExecutionListeners().size());
|
||||
}
|
||||
|
||||
|
||||
static class DefaultListenersExampleTestCase {
|
||||
}
|
||||
|
||||
@TestExecutionListeners( { QuuxTestExecutionListener.class })
|
||||
static class InheritedDefaultListenersExampleTestCase extends DefaultListenersExampleTestCase {
|
||||
public void testDoSomething() {
|
||||
fail("whaa?");
|
||||
}
|
||||
}
|
||||
|
||||
static class SubInheritedDefaultListenersExampleTestCase extends InheritedDefaultListenersExampleTestCase {
|
||||
}
|
||||
|
||||
@TestExecutionListeners( { EnigmaTestExecutionListener.class })
|
||||
static class SubSubInheritedDefaultListenersExampleTestCase extends SubInheritedDefaultListenersExampleTestCase {
|
||||
}
|
||||
|
||||
@TestExecutionListeners(value = { QuuxTestExecutionListener.class }, inheritListeners = false)
|
||||
static class NonInheritedDefaultListenersExampleTestCase extends InheritedDefaultListenersExampleTestCase {
|
||||
}
|
||||
|
||||
@TestExecutionListeners( { FooTestExecutionListener.class, BarTestExecutionListener.class,
|
||||
BazTestExecutionListener.class })
|
||||
static class ExampleTestCase {
|
||||
}
|
||||
|
||||
@TestExecutionListeners( { QuuxTestExecutionListener.class })
|
||||
static class InheritedListenersExampleTestCase extends ExampleTestCase {
|
||||
}
|
||||
|
||||
@TestExecutionListeners(value = { QuuxTestExecutionListener.class }, inheritListeners = false)
|
||||
static class NonInheritedListenersExampleTestCase extends InheritedListenersExampleTestCase {
|
||||
}
|
||||
|
||||
static class FooTestExecutionListener extends AbstractTestExecutionListener {
|
||||
}
|
||||
|
||||
static class BarTestExecutionListener extends AbstractTestExecutionListener {
|
||||
}
|
||||
|
||||
static class BazTestExecutionListener extends AbstractTestExecutionListener {
|
||||
}
|
||||
|
||||
static class QuuxTestExecutionListener extends AbstractTestExecutionListener {
|
||||
}
|
||||
|
||||
static class EnigmaTestExecutionListener extends AbstractTestExecutionListener {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:p="http://www.springframework.org/schema/p"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
|
||||
|
||||
<bean id="employee" class="org.springframework.beans.Employee">
|
||||
<property name="name" value="John Smith" />
|
||||
<property name="age" value="42" />
|
||||
<property name="company" value="Acme Widgets, Inc." />
|
||||
</bean>
|
||||
|
||||
<bean id="pet" class="org.springframework.beans.Pet">
|
||||
<constructor-arg value="Fido" />
|
||||
</bean>
|
||||
|
||||
<bean id="foo" class="java.lang.String">
|
||||
<constructor-arg value="Foo" />
|
||||
</bean>
|
||||
|
||||
<bean id="bar" class="java.lang.String">
|
||||
<constructor-arg value="Bar" />
|
||||
</bean>
|
||||
|
||||
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
|
||||
p:driverClassName="org.hsqldb.jdbcDriver" p:url="jdbc:hsqldb:mem:transactional_tests" p:username="sa" p:password="" />
|
||||
|
||||
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
|
||||
p:data-source-ref="dataSource" />
|
||||
|
||||
<bean id="databaseSetup"
|
||||
class="org.springframework.test.context.junit4.ConcreteTransactionalJUnit4SpringContextTests$DatabaseSetup" />
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,229 @@
|
||||
/*
|
||||
* 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.test.context.junit38;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.internal.runners.JUnit38ClassRunner;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.Employee;
|
||||
import org.springframework.beans.Pet;
|
||||
import org.springframework.beans.factory.BeanNameAware;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.BadSqlGrammarException;
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
|
||||
import org.springframework.test.annotation.ExpectedException;
|
||||
import org.springframework.test.annotation.NotTransactional;
|
||||
import org.springframework.test.annotation.Timed;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.transaction.AfterTransaction;
|
||||
import org.springframework.test.context.transaction.BeforeTransaction;
|
||||
import org.springframework.test.jdbc.SimpleJdbcTestUtils;
|
||||
|
||||
/**
|
||||
* Combined unit test for {@link AbstractJUnit38SpringContextTests} and
|
||||
* {@link AbstractTransactionalJUnit38SpringContextTests}.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
@RunWith(JUnit38ClassRunner.class)
|
||||
@ContextConfiguration
|
||||
public class ConcreteTransactionalJUnit38SpringContextTests extends AbstractTransactionalJUnit38SpringContextTests
|
||||
implements BeanNameAware, InitializingBean {
|
||||
|
||||
protected static final String BOB = "bob";
|
||||
protected static final String JANE = "jane";
|
||||
protected static final String SUE = "sue";
|
||||
protected static final String YODA = "yoda";
|
||||
|
||||
private boolean beanInitialized = false;
|
||||
|
||||
private String beanName = "replace me with [" + getClass().getName() + "]";
|
||||
|
||||
private Employee employee;
|
||||
|
||||
@Autowired
|
||||
private Pet pet;
|
||||
|
||||
@Autowired(required = false)
|
||||
protected Long nonrequiredLong;
|
||||
|
||||
@Resource()
|
||||
protected String foo;
|
||||
|
||||
protected String bar;
|
||||
|
||||
private boolean inTransaction = false;
|
||||
|
||||
|
||||
public ConcreteTransactionalJUnit38SpringContextTests() throws Exception {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public ConcreteTransactionalJUnit38SpringContextTests(final String name) throws Exception {
|
||||
super(name);
|
||||
}
|
||||
|
||||
protected static int clearPersonTable(final SimpleJdbcTemplate simpleJdbcTemplate) {
|
||||
return SimpleJdbcTestUtils.deleteFromTables(simpleJdbcTemplate, "person");
|
||||
}
|
||||
|
||||
protected static void createPersonTable(final SimpleJdbcTemplate simpleJdbcTemplate) {
|
||||
try {
|
||||
simpleJdbcTemplate.update("CREATE TABLE person (name VARCHAR(20) NOT NULL, PRIMARY KEY(name))");
|
||||
}
|
||||
catch (final BadSqlGrammarException bsge) {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
|
||||
protected static int countRowsInPersonTable(final SimpleJdbcTemplate simpleJdbcTemplate) {
|
||||
return SimpleJdbcTestUtils.countRowsInTable(simpleJdbcTemplate, "person");
|
||||
}
|
||||
|
||||
protected static int addPerson(final SimpleJdbcTemplate simpleJdbcTemplate, final String name) {
|
||||
return simpleJdbcTemplate.update("INSERT INTO person VALUES(?)", name);
|
||||
}
|
||||
|
||||
protected static int deletePerson(final SimpleJdbcTemplate simpleJdbcTemplate, final String name) {
|
||||
return simpleJdbcTemplate.update("DELETE FROM person WHERE name=?", name);
|
||||
}
|
||||
|
||||
public final void afterPropertiesSet() throws Exception {
|
||||
this.beanInitialized = true;
|
||||
}
|
||||
|
||||
public final void setBeanName(final String beanName) {
|
||||
this.beanName = beanName;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
protected final void setEmployee(final Employee employee) {
|
||||
this.employee = employee;
|
||||
}
|
||||
|
||||
@Resource
|
||||
protected final void setBar(final String bar) {
|
||||
this.bar = bar;
|
||||
}
|
||||
|
||||
@NotTransactional
|
||||
@Timed(millis = 10000)
|
||||
public void testNoOpShouldNotTimeOut() throws Exception {
|
||||
/* no-op */
|
||||
}
|
||||
|
||||
@NotTransactional
|
||||
@ExpectedException(IndexOutOfBoundsException.class)
|
||||
public void testExpectedExceptionAnnotation() {
|
||||
new ArrayList<Object>().get(1);
|
||||
}
|
||||
|
||||
@NotTransactional
|
||||
public void testApplicationContextSet() {
|
||||
assertNotNull("The application context should have been set due to ApplicationContextAware semantics.",
|
||||
super.applicationContext);
|
||||
}
|
||||
|
||||
@NotTransactional
|
||||
public void testBeanInitialized() {
|
||||
assertTrue("This test bean should have been initialized due to InitializingBean semantics.",
|
||||
this.beanInitialized);
|
||||
}
|
||||
|
||||
@NotTransactional
|
||||
public void testBeanNameSet() {
|
||||
assertEquals("The bean name of this test instance should have been set to the fully qualified class name "
|
||||
+ "due to BeanNameAware semantics.", getClass().getName(), this.beanName);
|
||||
}
|
||||
|
||||
@NotTransactional
|
||||
public void testAnnotationAutowiredFields() {
|
||||
assertNull("The nonrequiredLong property should NOT have been autowired.", this.nonrequiredLong);
|
||||
assertNotNull("The pet field should have been autowired.", this.pet);
|
||||
assertEquals("Fido", this.pet.getName());
|
||||
}
|
||||
|
||||
@NotTransactional
|
||||
public void testAnnotationAutowiredMethods() {
|
||||
assertNotNull("The employee setter method should have been autowired.", this.employee);
|
||||
assertEquals("John Smith", this.employee.getName());
|
||||
}
|
||||
|
||||
@NotTransactional
|
||||
public void testResourceAnnotationWiredFields() {
|
||||
assertEquals("The foo field should have been wired via @Resource.", "Foo", this.foo);
|
||||
}
|
||||
|
||||
@NotTransactional
|
||||
public void testResourceAnnotationWiredMethods() {
|
||||
assertEquals("The bar method should have been wired via @Resource.", "Bar", this.bar);
|
||||
}
|
||||
|
||||
@BeforeTransaction
|
||||
public void beforeTransaction() {
|
||||
this.inTransaction = true;
|
||||
assertEquals("Verifying the number of rows in the person table before a transactional test method.", 1,
|
||||
countRowsInPersonTable(super.simpleJdbcTemplate));
|
||||
assertEquals("Adding yoda", 1, addPerson(super.simpleJdbcTemplate, YODA));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
assertEquals("Verifying the number of rows in the person table before a test method.", (this.inTransaction ? 2
|
||||
: 1), countRowsInPersonTable(super.simpleJdbcTemplate));
|
||||
}
|
||||
|
||||
public void testModifyTestDataWithinTransaction() {
|
||||
assertEquals("Adding jane", 1, addPerson(super.simpleJdbcTemplate, JANE));
|
||||
assertEquals("Adding sue", 1, addPerson(super.simpleJdbcTemplate, SUE));
|
||||
assertEquals("Verifying the number of rows in the person table within transactionalMethod2().", 4,
|
||||
countRowsInPersonTable(super.simpleJdbcTemplate));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tearDown() throws Exception {
|
||||
assertEquals("Verifying the number of rows in the person table after a test method.", (this.inTransaction ? 4
|
||||
: 1), countRowsInPersonTable(super.simpleJdbcTemplate));
|
||||
}
|
||||
|
||||
@AfterTransaction
|
||||
public void afterTransaction() {
|
||||
assertEquals("Deleting yoda", 1, deletePerson(super.simpleJdbcTemplate, YODA));
|
||||
assertEquals("Verifying the number of rows in the person table after a transactional test method.", 1,
|
||||
countRowsInPersonTable(super.simpleJdbcTemplate));
|
||||
}
|
||||
|
||||
|
||||
public static class DatabaseSetup {
|
||||
|
||||
@Autowired
|
||||
void setDataSource(final DataSource dataSource) {
|
||||
final SimpleJdbcTemplate simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
|
||||
createPersonTable(simpleJdbcTemplate);
|
||||
clearPersonTable(simpleJdbcTemplate);
|
||||
addPerson(simpleJdbcTemplate, BOB);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:p="http://www.springframework.org/schema/p"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
|
||||
|
||||
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
|
||||
p:driverClassName="org.hsqldb.jdbcDriver" p:url="jdbc:hsqldb:mem:transactional_tests" p:username="sa" p:password="" />
|
||||
|
||||
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
|
||||
p:data-source-ref="dataSource" />
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,150 @@
|
||||
/*
|
||||
* 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.test.context.junit38;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestResult;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestContext;
|
||||
import org.springframework.test.context.TestExecutionListener;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
import org.springframework.test.context.support.AbstractTestExecutionListener;
|
||||
import org.springframework.test.context.transaction.AfterTransaction;
|
||||
import org.springframework.test.context.transaction.BeforeTransaction;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* JUnit 4 based unit test for verifying that '<em>before</em>' and '<em>after</em>'
|
||||
* methods of {@link TestExecutionListener TestExecutionListeners} as well as
|
||||
* {@link BeforeTransaction @BeforeTransaction} and
|
||||
* {@link AfterTransaction @AfterTransaction} methods can fail a test in a JUnit
|
||||
* 3.8 environment, as requested in <a
|
||||
* href="http://opensource.atlassian.com/projects/spring/browse/SPR-3960"
|
||||
* target="_blank">SPR-3960</a>.
|
||||
* </p>
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class FailingBeforeAndAfterMethodsTests {
|
||||
|
||||
protected final Class<?> clazz;
|
||||
|
||||
|
||||
public FailingBeforeAndAfterMethodsTests(final Class<?> clazz) {
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
@Parameters
|
||||
public static Collection<Object[]> testData() {
|
||||
return Arrays.asList(new Object[][] {
|
||||
|
||||
{ AlwaysFailingBeforeTestMethodTestCase.class },
|
||||
|
||||
{ AlwaysFailingAfterTestMethodTestCase.class },
|
||||
|
||||
{ FailingBeforeTransactionalTestCase.class },
|
||||
|
||||
{ FailingAfterTransactionalTestCase.class }
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void runTestAndAssertCounters() throws Exception {
|
||||
final String testName = "testNothing";
|
||||
final TestCase testCase = (TestCase) this.clazz.newInstance();
|
||||
testCase.setName(testName);
|
||||
TestResult testResult = testCase.run();
|
||||
assertEquals("Verifying number of errors for test method [" + testName + "] and class [" + this.clazz + "].",
|
||||
0, testResult.errorCount());
|
||||
assertEquals("Verifying number of failures for test method [" + testName + "] and class [" + this.clazz + "].",
|
||||
1, testResult.failureCount());
|
||||
}
|
||||
|
||||
|
||||
static class AlwaysFailingBeforeTestMethodTestExecutionListener extends AbstractTestExecutionListener {
|
||||
|
||||
@Override
|
||||
public void beforeTestMethod(TestContext testContext) {
|
||||
junit.framework.Assert.fail("always failing beforeTestMethod()");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static class AlwaysFailingAfterTestMethodTestExecutionListener extends AbstractTestExecutionListener {
|
||||
|
||||
@Override
|
||||
public void afterTestMethod(TestContext testContext) {
|
||||
junit.framework.Assert.fail("always failing afterTestMethod()");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@TestExecutionListeners(value = { AlwaysFailingBeforeTestMethodTestExecutionListener.class }, inheritListeners = false)
|
||||
public static class AlwaysFailingBeforeTestMethodTestCase extends AbstractJUnit38SpringContextTests {
|
||||
|
||||
public void testNothing() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@TestExecutionListeners(value = { AlwaysFailingAfterTestMethodTestExecutionListener.class }, inheritListeners = false)
|
||||
public static class AlwaysFailingAfterTestMethodTestCase extends AbstractJUnit38SpringContextTests {
|
||||
|
||||
public void testNothing() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ContextConfiguration(locations = { "FailingBeforeAndAfterMethodsTests-context.xml" })
|
||||
public static class FailingBeforeTransactionalTestCase extends AbstractTransactionalJUnit38SpringContextTests {
|
||||
|
||||
public void testNothing() {
|
||||
}
|
||||
|
||||
@BeforeTransaction
|
||||
public void beforeTransaction() {
|
||||
fail("always failing beforeTransaction()");
|
||||
}
|
||||
}
|
||||
|
||||
@ContextConfiguration(locations = { "FailingBeforeAndAfterMethodsTests-context.xml" })
|
||||
public static class FailingAfterTransactionalTestCase extends AbstractTransactionalJUnit38SpringContextTests {
|
||||
|
||||
public void testNothing() {
|
||||
}
|
||||
|
||||
@AfterTransaction
|
||||
public void afterTransaction() {
|
||||
fail("always failing afterTransaction()");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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.test.context.junit38;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestResult;
|
||||
|
||||
import org.springframework.test.annotation.IfProfileValue;
|
||||
import org.springframework.test.annotation.ProfileValueSource;
|
||||
import org.springframework.test.annotation.ProfileValueSourceConfiguration;
|
||||
import org.springframework.test.annotation.SystemProfileValueSource;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
|
||||
/**
|
||||
* Verifies proper handling of {@link IfProfileValue @IfProfileValue} and
|
||||
* {@link ProfileValueSourceConfiguration @ProfileValueSourceConfiguration} in
|
||||
* conjunction with {@link AbstractJUnit38SpringContextTests}.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
public class ProfileValueJUnit38SpringContextTests extends TestCase {
|
||||
|
||||
private static final String NAME = "ProfileValueAnnotationAwareTransactionalTests.profile_value.name";
|
||||
|
||||
private static final String VALUE = "enigma";
|
||||
|
||||
|
||||
public ProfileValueJUnit38SpringContextTests() {
|
||||
System.setProperty(NAME, VALUE);
|
||||
}
|
||||
|
||||
|
||||
private void runTestAndAssertCounters(Class<? extends DefaultProfileValueSourceTestCase> testCaseType,
|
||||
String testName, int expectedInvocationCount, int expectedErrorCount,
|
||||
int expectedFailureCount) throws Exception {
|
||||
|
||||
DefaultProfileValueSourceTestCase testCase = testCaseType.newInstance();
|
||||
testCase.setName(testName);
|
||||
TestResult testResult = testCase.run();
|
||||
assertEquals("Verifying number of invocations for test method [" + testName + "].", expectedInvocationCount,
|
||||
testCase.invocationCount);
|
||||
assertEquals("Verifying number of errors for test method [" + testName + "].", expectedErrorCount,
|
||||
testResult.errorCount());
|
||||
assertEquals("Verifying number of failures for test method [" + testName + "].", expectedFailureCount,
|
||||
testResult.failureCount());
|
||||
}
|
||||
|
||||
private void runTests(final Class<? extends DefaultProfileValueSourceTestCase> testCaseType) throws Exception {
|
||||
runTestAndAssertCounters(testCaseType, "testIfProfileValueEmpty", 0, 0, 0);
|
||||
runTestAndAssertCounters(testCaseType, "testIfProfileValueDisabledViaWrongName", 0, 0, 0);
|
||||
runTestAndAssertCounters(testCaseType, "testIfProfileValueDisabledViaWrongValue", 0, 0, 0);
|
||||
runTestAndAssertCounters(testCaseType, "testIfProfileValueEnabledViaSingleValue", 1, 0, 0);
|
||||
runTestAndAssertCounters(testCaseType, "testIfProfileValueEnabledViaMultipleValues", 1, 0, 0);
|
||||
runTestAndAssertCounters(testCaseType, "testIfProfileValueNotConfigured", 1, 0, 0);
|
||||
}
|
||||
|
||||
public void testDefaultProfileValueSource() throws Exception {
|
||||
assertEquals("Verifying the type of the configured ProfileValueSource.", SystemProfileValueSource.class,
|
||||
new DefaultProfileValueSourceTestCase().getProfileValueSource().getClass());
|
||||
runTests(DefaultProfileValueSourceTestCase.class);
|
||||
}
|
||||
|
||||
public void testHardCodedProfileValueSource() throws Exception {
|
||||
assertEquals("Verifying the type of the configured ProfileValueSource.", HardCodedProfileValueSource.class,
|
||||
new HardCodedProfileValueSourceTestCase().getProfileValueSource().getClass());
|
||||
runTests(HardCodedProfileValueSourceTestCase.class);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Note that {@link TestExecutionListeners @TestExecutionListeners} is
|
||||
* explicitly configured with an empty list, thus disabling all default
|
||||
* listeners.
|
||||
*/
|
||||
@TestExecutionListeners(value = {}, inheritListeners = false)
|
||||
public static class DefaultProfileValueSourceTestCase extends AbstractJUnit38SpringContextTests {
|
||||
|
||||
int invocationCount = 0;
|
||||
|
||||
public ProfileValueSource getProfileValueSource() {
|
||||
return super.profileValueSource;
|
||||
}
|
||||
|
||||
@IfProfileValue(name = NAME, value = "")
|
||||
public void testIfProfileValueEmpty() {
|
||||
this.invocationCount++;
|
||||
fail("An empty profile value should throw an IllegalArgumentException.");
|
||||
}
|
||||
|
||||
@IfProfileValue(name = NAME + "X", value = VALUE)
|
||||
public void testIfProfileValueDisabledViaWrongName() {
|
||||
this.invocationCount++;
|
||||
fail("The body of a disabled test should never be executed!");
|
||||
}
|
||||
|
||||
@IfProfileValue(name = NAME, value = VALUE + "X")
|
||||
public void testIfProfileValueDisabledViaWrongValue() {
|
||||
this.invocationCount++;
|
||||
fail("The body of a disabled test should never be executed!");
|
||||
}
|
||||
|
||||
@IfProfileValue(name = NAME, value = VALUE)
|
||||
public void testIfProfileValueEnabledViaSingleValue() {
|
||||
this.invocationCount++;
|
||||
}
|
||||
|
||||
@IfProfileValue(name = NAME, values = { "foo", VALUE, "bar" })
|
||||
public void testIfProfileValueEnabledViaMultipleValues() {
|
||||
this.invocationCount++;
|
||||
}
|
||||
|
||||
public void testIfProfileValueNotConfigured() {
|
||||
this.invocationCount++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ProfileValueSourceConfiguration(HardCodedProfileValueSource.class)
|
||||
public static class HardCodedProfileValueSourceTestCase extends DefaultProfileValueSourceTestCase {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static class HardCodedProfileValueSource implements ProfileValueSource {
|
||||
|
||||
public String get(final String key) {
|
||||
return (key.equals(NAME) ? VALUE : null);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* 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.test.context.junit38;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.test.annotation.Repeat;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
|
||||
/**
|
||||
* Unit test for {@link AbstractJUnit38SpringContextTests} which focuses on
|
||||
* proper support of the {@link Repeat @Repeat} annotation.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
public class RepeatedJUnit38SpringContextTests extends TestCase {
|
||||
|
||||
public RepeatedJUnit38SpringContextTests() throws Exception {
|
||||
super();
|
||||
}
|
||||
|
||||
public RepeatedJUnit38SpringContextTests(final String name) throws Exception {
|
||||
super(name);
|
||||
}
|
||||
|
||||
private void assertRepetitions(final String testName, final int expectedNumInvocations) throws Exception {
|
||||
final RepeatedTestCase repeatedTestCase = new RepeatedTestCase(testName);
|
||||
repeatedTestCase.run();
|
||||
assertEquals("Verifying number of invocations for test method [" + testName + "].", expectedNumInvocations,
|
||||
repeatedTestCase.invocationCount);
|
||||
}
|
||||
|
||||
public void testRepeatAnnotationSupport() throws Exception {
|
||||
assertRepetitions("testNonAnnotated", 1);
|
||||
assertRepetitions("testNegativeRepeatValue", 1);
|
||||
assertRepetitions("testDefaultRepeatValue", 1);
|
||||
assertRepetitions("testRepeatedFiveTimes", 5);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Note that {@link TestExecutionListeners @TestExecutionListeners} is
|
||||
* explicitly configured with an empty list, thus disabling all default
|
||||
* listeners.
|
||||
*/
|
||||
@TestExecutionListeners(value = {}, inheritListeners = false)
|
||||
protected static class RepeatedTestCase extends AbstractJUnit38SpringContextTests {
|
||||
|
||||
int invocationCount = 0;
|
||||
|
||||
|
||||
public RepeatedTestCase(final String name) throws Exception {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
this.invocationCount++;
|
||||
}
|
||||
|
||||
public void testNonAnnotated() {
|
||||
/* no-op */
|
||||
}
|
||||
|
||||
@Repeat(-5)
|
||||
public void testNegativeRepeatValue() {
|
||||
/* no-op */
|
||||
}
|
||||
|
||||
@Repeat
|
||||
public void testDefaultRepeatValue() {
|
||||
/* no-op */
|
||||
}
|
||||
|
||||
@Repeat(5)
|
||||
public void testRepeatedFiveTimes() {
|
||||
/* no-op */
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* 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.test.context.junit4;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
/**
|
||||
* Extension of {@link SpringJUnit4ClassRunnerAppCtxTests}, which verifies that
|
||||
* we can specify an explicit, <em>absolute path</em> location for our
|
||||
* application context.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
* @see SpringJUnit4ClassRunnerAppCtxTests
|
||||
* @see ClassPathResourceSpringJUnit4ClassRunnerAppCtxTests
|
||||
* @see RelativePathSpringJUnit4ClassRunnerAppCtxTests
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { SpringJUnit4ClassRunnerAppCtxTests.DEFAULT_CONTEXT_RESOURCE_PATH })
|
||||
public class AbsolutePathSpringJUnit4ClassRunnerAppCtxTests extends SpringJUnit4ClassRunnerAppCtxTests {
|
||||
/* all tests are in the parent class. */
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* 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.test.context.junit4;
|
||||
|
||||
import org.springframework.jdbc.BadSqlGrammarException;
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
|
||||
import org.springframework.test.annotation.NotTransactional;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* Abstract base class for verifying support of Spring's
|
||||
* {@link Transactional @Transactional} and
|
||||
* {@link NotTransactional @NotTransactional} annotations.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
* @see ClassLevelTransactionalSpringRunnerTests
|
||||
* @see MethodLevelTransactionalSpringRunnerTests
|
||||
* @see Transactional
|
||||
* @see NotTransactional
|
||||
*/
|
||||
@ContextConfiguration(locations = {"transactionalTests-context.xml"})
|
||||
public abstract class AbstractTransactionalSpringRunnerTests {
|
||||
|
||||
protected static final String BOB = "bob";
|
||||
protected static final String JANE = "jane";
|
||||
protected static final String SUE = "sue";
|
||||
protected static final String LUKE = "luke";
|
||||
protected static final String LEIA = "leia";
|
||||
protected static final String YODA = "yoda";
|
||||
|
||||
|
||||
protected static int clearPersonTable(SimpleJdbcTemplate simpleJdbcTemplate) {
|
||||
return simpleJdbcTemplate.update("DELETE FROM person");
|
||||
}
|
||||
|
||||
protected static void createPersonTable(SimpleJdbcTemplate simpleJdbcTemplate) {
|
||||
try {
|
||||
simpleJdbcTemplate.update("CREATE TABLE person (name VARCHAR(20) NOT NULL, PRIMARY KEY(name))");
|
||||
}
|
||||
catch (BadSqlGrammarException bsge) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
protected static int countRowsInPersonTable(SimpleJdbcTemplate simpleJdbcTemplate) {
|
||||
return simpleJdbcTemplate.queryForInt("SELECT COUNT(0) FROM person");
|
||||
}
|
||||
|
||||
protected static int addPerson(SimpleJdbcTemplate simpleJdbcTemplate, String name) {
|
||||
return simpleJdbcTemplate.update("INSERT INTO person VALUES(?)", name);
|
||||
}
|
||||
|
||||
protected static int deletePerson(SimpleJdbcTemplate simpleJdbcTemplate, String name) {
|
||||
return simpleJdbcTemplate.update("DELETE FROM person WHERE name=?", name);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
|
||||
|
||||
<bean id="databaseSetup"
|
||||
class="org.springframework.test.context.junit4.BeforeAndAfterTransactionAnnotationTests$DatabaseSetup" />
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
* 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.test.context.junit4;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.springframework.test.transaction.TransactionTestUtils.assertInTransaction;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
import org.springframework.test.context.transaction.AfterTransaction;
|
||||
import org.springframework.test.context.transaction.BeforeTransaction;
|
||||
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* JUnit 4 based unit test which verifies
|
||||
* {@link BeforeTransaction @BeforeTransaction} and
|
||||
* {@link AfterTransaction @AfterTransaction} behavior.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration
|
||||
@TestExecutionListeners({TransactionalTestExecutionListener.class})
|
||||
public class BeforeAndAfterTransactionAnnotationTests extends AbstractTransactionalSpringRunnerTests {
|
||||
|
||||
protected static SimpleJdbcTemplate simpleJdbcTemplate;
|
||||
|
||||
protected static int numBeforeTransactionCalls = 0;
|
||||
protected static int numAfterTransactionCalls = 0;
|
||||
|
||||
protected boolean inTransaction = false;
|
||||
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeClass() {
|
||||
BeforeAndAfterTransactionAnnotationTests.numBeforeTransactionCalls = 0;
|
||||
BeforeAndAfterTransactionAnnotationTests.numAfterTransactionCalls = 0;
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterClass() {
|
||||
assertEquals("Verifying the final number of rows in the person table after all tests.", 3,
|
||||
countRowsInPersonTable(simpleJdbcTemplate));
|
||||
assertEquals("Verifying the total number of calls to beforeTransaction().", 2,
|
||||
BeforeAndAfterTransactionAnnotationTests.numBeforeTransactionCalls);
|
||||
assertEquals("Verifying the total number of calls to afterTransaction().", 2,
|
||||
BeforeAndAfterTransactionAnnotationTests.numAfterTransactionCalls);
|
||||
}
|
||||
|
||||
|
||||
@BeforeTransaction
|
||||
public void beforeTransaction() {
|
||||
assertInTransaction(false);
|
||||
this.inTransaction = true;
|
||||
BeforeAndAfterTransactionAnnotationTests.numBeforeTransactionCalls++;
|
||||
clearPersonTable(simpleJdbcTemplate);
|
||||
assertEquals("Adding yoda", 1, addPerson(simpleJdbcTemplate, YODA));
|
||||
}
|
||||
|
||||
@AfterTransaction
|
||||
public void afterTransaction() {
|
||||
assertInTransaction(false);
|
||||
this.inTransaction = false;
|
||||
BeforeAndAfterTransactionAnnotationTests.numAfterTransactionCalls++;
|
||||
assertEquals("Deleting yoda", 1, deletePerson(simpleJdbcTemplate, YODA));
|
||||
assertEquals("Verifying the number of rows in the person table after a transactional test method.", 0,
|
||||
countRowsInPersonTable(simpleJdbcTemplate));
|
||||
}
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
assertEquals("Verifying the number of rows in the person table before a test method.", (this.inTransaction ? 1 : 0), countRowsInPersonTable(simpleJdbcTemplate));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void transactionalMethod1() {
|
||||
assertInTransaction(true);
|
||||
assertEquals("Adding jane", 1, addPerson(simpleJdbcTemplate, JANE));
|
||||
assertEquals("Verifying the number of rows in the person table within transactionalMethod1().", 2,
|
||||
countRowsInPersonTable(simpleJdbcTemplate));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void transactionalMethod2() {
|
||||
assertInTransaction(true);
|
||||
assertEquals("Adding jane", 1, addPerson(simpleJdbcTemplate, JANE));
|
||||
assertEquals("Adding sue", 1, addPerson(simpleJdbcTemplate, SUE));
|
||||
assertEquals("Verifying the number of rows in the person table within transactionalMethod2().", 3,
|
||||
countRowsInPersonTable(simpleJdbcTemplate));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nonTransactionalMethod() {
|
||||
assertInTransaction(false);
|
||||
assertEquals("Adding luke", 1, addPerson(simpleJdbcTemplate, LUKE));
|
||||
assertEquals("Adding leia", 1, addPerson(simpleJdbcTemplate, LEIA));
|
||||
assertEquals("Adding yoda", 1, addPerson(simpleJdbcTemplate, YODA));
|
||||
assertEquals("Verifying the number of rows in the person table without a transaction.", 3,
|
||||
countRowsInPersonTable(simpleJdbcTemplate));
|
||||
}
|
||||
|
||||
|
||||
public static class DatabaseSetup {
|
||||
|
||||
@Resource
|
||||
void setDataSource(DataSource dataSource) {
|
||||
simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
|
||||
createPersonTable(simpleJdbcTemplate);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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.test.context.junit4;
|
||||
|
||||
import static org.junit.Assert.fail;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.test.annotation.IfProfileValue;
|
||||
import org.springframework.test.context.TestContext;
|
||||
import org.springframework.test.context.TestExecutionListener;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@TestExecutionListeners(ClassLevelDisabledSpringRunnerTests.CustomTestExecutionListener.class)
|
||||
@IfProfileValue(name = "ClassLevelDisabledSpringRunnerTests.profile_value.name", value = "enigmaX")
|
||||
public class ClassLevelDisabledSpringRunnerTests {
|
||||
|
||||
@Test
|
||||
public void testIfProfileValueDisabled() {
|
||||
fail("The body of a disabled test should never be executed!");
|
||||
}
|
||||
|
||||
|
||||
public static class CustomTestExecutionListener implements TestExecutionListener {
|
||||
|
||||
public void prepareTestInstance(TestContext testContext) throws Exception {
|
||||
fail("A listener method for a disabled test should never be executed!");
|
||||
}
|
||||
|
||||
public void beforeTestMethod(TestContext testContext) throws Exception {
|
||||
fail("A listener method for a disabled test should never be executed!");
|
||||
}
|
||||
|
||||
public void afterTestMethod(TestContext testContext) throws Exception {
|
||||
fail("A listener method for a disabled test should never be executed!");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
|
||||
|
||||
<bean id="databaseSetup"
|
||||
class="org.springframework.test.context.junit4.ClassLevelTransactionalSpringRunnerTests$DatabaseSetup" />
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,118 @@
|
||||
/*
|
||||
* 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.test.context.junit4;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.springframework.test.transaction.TransactionTestUtils.assertInTransaction;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
|
||||
import org.springframework.test.annotation.NotTransactional;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestExecutionListener;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
||||
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
|
||||
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* JUnit 4 based unit test which verifies support of Spring's
|
||||
* {@link Transactional @Transactional},
|
||||
* {@link NotTransactional @NotTransactional},
|
||||
* {@link TestExecutionListeners @TestExecutionListeners}, and
|
||||
* {@link ContextConfiguration @ContextConfiguration} annotations in conjunction
|
||||
* with the {@link SpringJUnit4ClassRunner} and the following
|
||||
* {@link TestExecutionListener TestExecutionListeners}:
|
||||
* </p>
|
||||
* <ul>
|
||||
* <li>{@link DependencyInjectionTestExecutionListener}</li>
|
||||
* <li>{@link DirtiesContextTestExecutionListener}</li>
|
||||
* <li>{@link TransactionalTestExecutionListener}</li>
|
||||
* </ul>
|
||||
* <p>
|
||||
* This class specifically tests usage of <code>@Transactional</code>
|
||||
* defined at the <strong>class level</strong>.
|
||||
* </p>
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
* @see MethodLevelTransactionalSpringRunnerTests
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration
|
||||
@Transactional
|
||||
public class ClassLevelTransactionalSpringRunnerTests extends AbstractTransactionalSpringRunnerTests {
|
||||
|
||||
protected static SimpleJdbcTemplate simpleJdbcTemplate;
|
||||
|
||||
|
||||
@AfterClass
|
||||
public static void verifyFinalTestData() {
|
||||
assertEquals("Verifying the final number of rows in the person table after all tests.", 4,
|
||||
countRowsInPersonTable(simpleJdbcTemplate));
|
||||
}
|
||||
|
||||
@Before
|
||||
public void verifyInitialTestData() {
|
||||
clearPersonTable(simpleJdbcTemplate);
|
||||
assertEquals("Adding bob", 1, addPerson(simpleJdbcTemplate, BOB));
|
||||
assertEquals("Verifying the initial number of rows in the person table.", 1,
|
||||
countRowsInPersonTable(simpleJdbcTemplate));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void modifyTestDataWithinTransaction() {
|
||||
assertInTransaction(true);
|
||||
assertEquals("Deleting bob", 1, deletePerson(simpleJdbcTemplate, BOB));
|
||||
assertEquals("Adding jane", 1, addPerson(simpleJdbcTemplate, JANE));
|
||||
assertEquals("Adding sue", 1, addPerson(simpleJdbcTemplate, SUE));
|
||||
assertEquals("Verifying the number of rows in the person table within a transaction.", 2,
|
||||
countRowsInPersonTable(simpleJdbcTemplate));
|
||||
}
|
||||
|
||||
@Test
|
||||
@NotTransactional
|
||||
public void modifyTestDataWithoutTransaction() {
|
||||
assertInTransaction(false);
|
||||
assertEquals("Adding luke", 1, addPerson(simpleJdbcTemplate, LUKE));
|
||||
assertEquals("Adding leia", 1, addPerson(simpleJdbcTemplate, LEIA));
|
||||
assertEquals("Adding yoda", 1, addPerson(simpleJdbcTemplate, YODA));
|
||||
assertEquals("Verifying the number of rows in the person table without a transaction.", 4,
|
||||
countRowsInPersonTable(simpleJdbcTemplate));
|
||||
}
|
||||
|
||||
|
||||
public static class DatabaseSetup {
|
||||
|
||||
@Resource
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
|
||||
createPersonTable(simpleJdbcTemplate);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* 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.test.context.junit4;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.util.ResourceUtils;
|
||||
|
||||
/**
|
||||
* Extension of {@link SpringJUnit4ClassRunnerAppCtxTests}, which verifies that
|
||||
* we can specify an explicit, <em>classpath</em> location for our application
|
||||
* context.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
* @see SpringJUnit4ClassRunnerAppCtxTests
|
||||
* @see #CLASSPATH_CONTEXT_RESOURCE_PATH
|
||||
* @see AbsolutePathSpringJUnit4ClassRunnerAppCtxTests
|
||||
* @see RelativePathSpringJUnit4ClassRunnerAppCtxTests
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { ClassPathResourceSpringJUnit4ClassRunnerAppCtxTests.CLASSPATH_CONTEXT_RESOURCE_PATH })
|
||||
public class ClassPathResourceSpringJUnit4ClassRunnerAppCtxTests extends SpringJUnit4ClassRunnerAppCtxTests {
|
||||
|
||||
/**
|
||||
* Classpath-based resource path for the application context configuration
|
||||
* for {@link SpringJUnit4ClassRunnerAppCtxTests}:
|
||||
* <code>"classpath:/org/springframework/test/context/junit4/SpringJUnit4ClassRunnerAppCtxTests-context.xml"</code>
|
||||
*
|
||||
* @see SpringJUnit4ClassRunnerAppCtxTests#DEFAULT_CONTEXT_RESOURCE_PATH
|
||||
* @see ResourceUtils#CLASSPATH_URL_PREFIX
|
||||
*/
|
||||
public static final String CLASSPATH_CONTEXT_RESOURCE_PATH = ResourceUtils.CLASSPATH_URL_PREFIX
|
||||
+ SpringJUnit4ClassRunnerAppCtxTests.DEFAULT_CONTEXT_RESOURCE_PATH;
|
||||
|
||||
/* all tests are in the parent class. */
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:p="http://www.springframework.org/schema/p"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
|
||||
|
||||
<import resource="transactionalTests-context.xml" />
|
||||
|
||||
<bean id="databaseSetup"
|
||||
class="org.springframework.test.context.junit4.ConcreteTransactionalJUnit4SpringContextTests$DatabaseSetup" />
|
||||
|
||||
<bean id="employee" class="org.springframework.beans.Employee">
|
||||
<property name="name" value="John Smith" />
|
||||
<property name="age" value="42" />
|
||||
<property name="company" value="Acme Widgets, Inc." />
|
||||
</bean>
|
||||
|
||||
<bean id="pet" class="org.springframework.beans.Pet">
|
||||
<constructor-arg value="Fido" />
|
||||
</bean>
|
||||
|
||||
<bean id="foo" class="java.lang.String">
|
||||
<constructor-arg value="Foo" />
|
||||
</bean>
|
||||
|
||||
<bean id="bar" class="java.lang.String">
|
||||
<constructor-arg value="Bar" />
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,236 @@
|
||||
/*
|
||||
* 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.test.context.junit4;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.springframework.test.transaction.TransactionTestUtils.assertInTransaction;
|
||||
import static org.springframework.test.transaction.TransactionTestUtils.inTransaction;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.Employee;
|
||||
import org.springframework.beans.Pet;
|
||||
import org.springframework.beans.factory.BeanNameAware;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.BadSqlGrammarException;
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
|
||||
import org.springframework.test.annotation.NotTransactional;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.transaction.AfterTransaction;
|
||||
import org.springframework.test.context.transaction.BeforeTransaction;
|
||||
import org.springframework.test.jdbc.SimpleJdbcTestUtils;
|
||||
|
||||
/**
|
||||
* Combined unit test for {@link AbstractJUnit4SpringContextTests} and
|
||||
* {@link AbstractTransactionalJUnit4SpringContextTests}.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
@ContextConfiguration
|
||||
public class ConcreteTransactionalJUnit4SpringContextTests extends AbstractTransactionalJUnit4SpringContextTests
|
||||
implements BeanNameAware, InitializingBean {
|
||||
|
||||
protected static final String BOB = "bob";
|
||||
protected static final String JANE = "jane";
|
||||
protected static final String SUE = "sue";
|
||||
protected static final String LUKE = "luke";
|
||||
protected static final String LEIA = "leia";
|
||||
protected static final String YODA = "yoda";
|
||||
|
||||
|
||||
private boolean beanInitialized = false;
|
||||
|
||||
private String beanName = "replace me with [" + getClass().getName() + "]";
|
||||
|
||||
private Employee employee;
|
||||
|
||||
@Autowired
|
||||
private Pet pet;
|
||||
|
||||
@Autowired(required = false)
|
||||
protected Long nonrequiredLong;
|
||||
|
||||
@Resource
|
||||
protected String foo;
|
||||
|
||||
protected String bar;
|
||||
|
||||
|
||||
protected static int clearPersonTable(final SimpleJdbcTemplate simpleJdbcTemplate) {
|
||||
return SimpleJdbcTestUtils.deleteFromTables(simpleJdbcTemplate, "person");
|
||||
}
|
||||
|
||||
protected static void createPersonTable(final SimpleJdbcTemplate simpleJdbcTemplate) {
|
||||
try {
|
||||
simpleJdbcTemplate.update("CREATE TABLE person (name VARCHAR(20) NOT NULL, PRIMARY KEY(name))");
|
||||
}
|
||||
catch (final BadSqlGrammarException bsge) {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
|
||||
protected static int countRowsInPersonTable(final SimpleJdbcTemplate simpleJdbcTemplate) {
|
||||
return SimpleJdbcTestUtils.countRowsInTable(simpleJdbcTemplate, "person");
|
||||
}
|
||||
|
||||
protected static int addPerson(final SimpleJdbcTemplate simpleJdbcTemplate, final String name) {
|
||||
return simpleJdbcTemplate.update("INSERT INTO person VALUES(?)", name);
|
||||
}
|
||||
|
||||
protected static int deletePerson(final SimpleJdbcTemplate simpleJdbcTemplate, final String name) {
|
||||
return simpleJdbcTemplate.update("DELETE FROM person WHERE name=?", name);
|
||||
}
|
||||
|
||||
|
||||
@Resource
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
super.setDataSource(dataSource);
|
||||
}
|
||||
|
||||
@Autowired
|
||||
protected final void setEmployee(final Employee employee) {
|
||||
this.employee = employee;
|
||||
}
|
||||
|
||||
@Resource
|
||||
protected final void setBar(final String bar) {
|
||||
this.bar = bar;
|
||||
}
|
||||
|
||||
public final void setBeanName(final String beanName) {
|
||||
this.beanName = beanName;
|
||||
}
|
||||
|
||||
public final void afterPropertiesSet() throws Exception {
|
||||
this.beanInitialized = true;
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@NotTransactional
|
||||
public final void verifyApplicationContext() {
|
||||
assertInTransaction(false);
|
||||
assertNotNull("The application context should have been set due to ApplicationContextAware semantics.",
|
||||
super.applicationContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
@NotTransactional
|
||||
public final void verifyBeanInitialized() {
|
||||
assertInTransaction(false);
|
||||
assertTrue("This test bean should have been initialized due to InitializingBean semantics.",
|
||||
this.beanInitialized);
|
||||
}
|
||||
|
||||
@Test
|
||||
@NotTransactional
|
||||
public final void verifyBeanNameSet() {
|
||||
assertInTransaction(false);
|
||||
assertEquals("The bean name of this test instance should have been set to the fully qualified class name "
|
||||
+ "due to BeanNameAware semantics.", getClass().getName(), this.beanName);
|
||||
}
|
||||
|
||||
@Test
|
||||
@NotTransactional
|
||||
public final void verifyAnnotationAutowiredFields() {
|
||||
assertInTransaction(false);
|
||||
assertNull("The nonrequiredLong property should NOT have been autowired.", this.nonrequiredLong);
|
||||
assertNotNull("The pet field should have been autowired.", this.pet);
|
||||
assertEquals("Fido", this.pet.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
@NotTransactional
|
||||
public final void verifyAnnotationAutowiredMethods() {
|
||||
assertInTransaction(false);
|
||||
assertNotNull("The employee setter method should have been autowired.", this.employee);
|
||||
assertEquals("John Smith", this.employee.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
@NotTransactional
|
||||
public final void verifyResourceAnnotationWiredFields() {
|
||||
assertInTransaction(false);
|
||||
assertEquals("The foo field should have been wired via @Resource.", "Foo", this.foo);
|
||||
}
|
||||
|
||||
@Test
|
||||
@NotTransactional
|
||||
public final void verifyResourceAnnotationWiredMethods() {
|
||||
assertInTransaction(false);
|
||||
assertEquals("The bar method should have been wired via @Resource.", "Bar", this.bar);
|
||||
}
|
||||
|
||||
|
||||
@BeforeTransaction
|
||||
public void beforeTransaction() {
|
||||
assertEquals("Verifying the number of rows in the person table before a transactional test method.", 1,
|
||||
countRowsInPersonTable(super.simpleJdbcTemplate));
|
||||
assertEquals("Adding yoda", 1, addPerson(super.simpleJdbcTemplate, YODA));
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
assertEquals("Verifying the number of rows in the person table before a test method.",
|
||||
(inTransaction() ? 2 : 1), countRowsInPersonTable(super.simpleJdbcTemplate));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void modifyTestDataWithinTransaction() {
|
||||
assertInTransaction(true);
|
||||
assertEquals("Adding jane", 1, addPerson(super.simpleJdbcTemplate, JANE));
|
||||
assertEquals("Adding sue", 1, addPerson(super.simpleJdbcTemplate, SUE));
|
||||
assertEquals("Verifying the number of rows in the person table in modifyTestDataWithinTransaction().", 4,
|
||||
countRowsInPersonTable(super.simpleJdbcTemplate));
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
assertEquals("Verifying the number of rows in the person table after a test method.",
|
||||
(inTransaction() ? 4 : 1), countRowsInPersonTable(super.simpleJdbcTemplate));
|
||||
}
|
||||
|
||||
@AfterTransaction
|
||||
public void afterTransaction() {
|
||||
assertEquals("Deleting yoda", 1, deletePerson(super.simpleJdbcTemplate, YODA));
|
||||
assertEquals("Verifying the number of rows in the person table after a transactional test method.", 1,
|
||||
countRowsInPersonTable(super.simpleJdbcTemplate));
|
||||
}
|
||||
|
||||
|
||||
public static class DatabaseSetup {
|
||||
|
||||
@Resource
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
SimpleJdbcTemplate simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
|
||||
createPersonTable(simpleJdbcTemplate);
|
||||
clearPersonTable(simpleJdbcTemplate);
|
||||
addPerson(simpleJdbcTemplate, BOB);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:p="http://www.springframework.org/schema/p"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
|
||||
|
||||
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
|
||||
p:driverClassName="org.hsqldb.jdbcDriver" p:url="jdbc:hsqldb:mem:transactional_tests" p:username="sa" p:password="" />
|
||||
|
||||
<bean id="txMgr" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
|
||||
p:data-source-ref="dataSource" />
|
||||
|
||||
<bean id="databaseSetup"
|
||||
class="org.springframework.test.context.junit4.DefaultRollbackFalseTransactionalSpringRunnerTests$DatabaseSetup" />
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* 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.test.context.junit4;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.springframework.test.transaction.TransactionTestUtils.assertInTransaction;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.transaction.TransactionConfiguration;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* JUnit 4 based unit test which verifies proper transactional behavior when the
|
||||
* {@link TransactionConfiguration#defaultRollback() defaultRollback} attribute
|
||||
* of the {@link TransactionConfiguration} annotation is set to <strong><code>false</code></strong>.
|
||||
* Also tests configuration of the
|
||||
* {@link TransactionConfiguration#transactionManager() transaction manager name}.
|
||||
* </p>
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
* @see TransactionConfiguration
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration
|
||||
@TransactionConfiguration(transactionManager = "txMgr", defaultRollback = false)
|
||||
@Transactional
|
||||
public class DefaultRollbackFalseTransactionalSpringRunnerTests extends AbstractTransactionalSpringRunnerTests {
|
||||
|
||||
protected static SimpleJdbcTemplate simpleJdbcTemplate;
|
||||
|
||||
|
||||
@AfterClass
|
||||
public static void verifyFinalTestData() {
|
||||
assertEquals("Verifying the final number of rows in the person table after all tests.", 2,
|
||||
countRowsInPersonTable(simpleJdbcTemplate));
|
||||
}
|
||||
|
||||
@Before
|
||||
public void verifyInitialTestData() {
|
||||
clearPersonTable(simpleJdbcTemplate);
|
||||
assertEquals("Adding bob", 1, addPerson(simpleJdbcTemplate, BOB));
|
||||
assertEquals("Verifying the initial number of rows in the person table.", 1,
|
||||
countRowsInPersonTable(simpleJdbcTemplate));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void modifyTestDataWithinTransaction() {
|
||||
assertInTransaction(true);
|
||||
assertEquals("Deleting bob", 1, deletePerson(simpleJdbcTemplate, BOB));
|
||||
assertEquals("Adding jane", 1, addPerson(simpleJdbcTemplate, JANE));
|
||||
assertEquals("Adding sue", 1, addPerson(simpleJdbcTemplate, SUE));
|
||||
assertEquals("Verifying the number of rows in the person table within a transaction.", 2,
|
||||
countRowsInPersonTable(simpleJdbcTemplate));
|
||||
}
|
||||
|
||||
|
||||
public static class DatabaseSetup {
|
||||
|
||||
@Resource
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
|
||||
createPersonTable(simpleJdbcTemplate);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
|
||||
|
||||
<bean id="databaseSetup"
|
||||
class="org.springframework.test.context.junit4.DefaultRollbackTrueTransactionalSpringRunnerTests$DatabaseSetup" />
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* 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.test.context.junit4;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.springframework.test.transaction.TransactionTestUtils.assertInTransaction;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.transaction.TransactionConfiguration;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* JUnit 4 based unit test which verifies proper transactional behavior when the
|
||||
* {@link TransactionConfiguration#defaultRollback() defaultRollback} attribute
|
||||
* of the {@link TransactionConfiguration} annotation is set to <strong><code>true</code></strong>.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
* @see TransactionConfiguration
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration
|
||||
@TransactionConfiguration(defaultRollback = true)
|
||||
public class DefaultRollbackTrueTransactionalSpringRunnerTests extends AbstractTransactionalSpringRunnerTests {
|
||||
|
||||
protected static int originalNumRows;
|
||||
|
||||
protected static SimpleJdbcTemplate simpleJdbcTemplate;
|
||||
|
||||
|
||||
@AfterClass
|
||||
public static void verifyFinalTestData() {
|
||||
assertEquals("Verifying the final number of rows in the person table after all tests.", originalNumRows,
|
||||
countRowsInPersonTable(simpleJdbcTemplate));
|
||||
}
|
||||
|
||||
@Before
|
||||
public void verifyInitialTestData() {
|
||||
originalNumRows = clearPersonTable(simpleJdbcTemplate);
|
||||
assertEquals("Adding bob", 1, addPerson(simpleJdbcTemplate, BOB));
|
||||
assertEquals("Verifying the initial number of rows in the person table.", 1,
|
||||
countRowsInPersonTable(simpleJdbcTemplate));
|
||||
}
|
||||
|
||||
@Test(timeout = 1000)
|
||||
@Transactional
|
||||
public void modifyTestDataWithinTransaction() {
|
||||
assertInTransaction(true);
|
||||
assertEquals("Adding jane", 1, addPerson(simpleJdbcTemplate, JANE));
|
||||
assertEquals("Adding sue", 1, addPerson(simpleJdbcTemplate, SUE));
|
||||
assertEquals("Verifying the number of rows in the person table within a transaction.", 3,
|
||||
countRowsInPersonTable(simpleJdbcTemplate));
|
||||
}
|
||||
|
||||
|
||||
public static class DatabaseSetup {
|
||||
|
||||
@Resource
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
|
||||
createPersonTable(simpleJdbcTemplate);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* 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.test.context.junit4;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.test.annotation.IfProfileValue;
|
||||
import org.springframework.test.annotation.ProfileValueSource;
|
||||
import org.springframework.test.annotation.ProfileValueSourceConfiguration;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
|
||||
/**
|
||||
* Verifies proper handling of JUnit's {@link org.junit.Ignore @Ignore} and
|
||||
* Spring's
|
||||
* {@link org.springframework.test.annotation.IfProfileValue @IfProfileValue}
|
||||
* and {@link ProfileValueSourceConfiguration @ProfileValueSourceConfiguration}
|
||||
* (with the <em>implicit, default {@link ProfileValueSource}</em>)
|
||||
* annotations in conjunction with the {@link SpringJUnit4ClassRunner}.
|
||||
*
|
||||
* <p>Note that {@link TestExecutionListeners @TestExecutionListeners} is
|
||||
* explicitly configured with an empty list, thus disabling all default
|
||||
* listeners.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
* @see HardCodedProfileValueSourceSpringRunnerTests
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@TestExecutionListeners({})
|
||||
public class EnabledAndIgnoredSpringRunnerTests {
|
||||
|
||||
protected static final String NAME = "EnabledAndIgnoredSpringRunnerTests.profile_value.name";
|
||||
|
||||
protected static final String VALUE = "enigma";
|
||||
|
||||
protected static int numTestsExecuted = 0;
|
||||
|
||||
|
||||
@BeforeClass
|
||||
public static void setProfileValue() {
|
||||
numTestsExecuted = 0;
|
||||
System.setProperty(NAME, VALUE);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void verifyNumTestsExecuted() {
|
||||
assertEquals("Verifying the number of tests executed.", 3, numTestsExecuted);
|
||||
}
|
||||
|
||||
@Test
|
||||
@IfProfileValue(name = NAME, value = VALUE + "X")
|
||||
public void testIfProfileValueDisabled() {
|
||||
numTestsExecuted++;
|
||||
fail("The body of a disabled test should never be executed!");
|
||||
}
|
||||
|
||||
@Test
|
||||
@IfProfileValue(name = NAME, value = VALUE)
|
||||
public void testIfProfileValueEnabledViaSingleValue() {
|
||||
numTestsExecuted++;
|
||||
}
|
||||
|
||||
@Test
|
||||
@IfProfileValue(name = NAME, values = { "foo", VALUE, "bar" })
|
||||
public void testIfProfileValueEnabledViaMultipleValues() {
|
||||
numTestsExecuted++;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIfProfileValueNotConfigured() {
|
||||
numTestsExecuted++;
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void testJUnitIgnoreAnnotation() {
|
||||
numTestsExecuted++;
|
||||
fail("The body of an ignored test should never be executed!");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:p="http://www.springframework.org/schema/p"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
|
||||
|
||||
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
|
||||
p:driverClassName="org.hsqldb.jdbcDriver" p:url="jdbc:hsqldb:mem:transactional_tests" p:username="sa" p:password="" />
|
||||
|
||||
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
|
||||
p:data-source-ref="dataSource" />
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,165 @@
|
||||
/*
|
||||
* 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.test.context.junit4;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runner.notification.Failure;
|
||||
import org.junit.runner.notification.RunListener;
|
||||
import org.junit.runner.notification.RunNotifier;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestContext;
|
||||
import org.springframework.test.context.TestExecutionListener;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
import org.springframework.test.context.support.AbstractTestExecutionListener;
|
||||
import org.springframework.test.context.transaction.AfterTransaction;
|
||||
import org.springframework.test.context.transaction.BeforeTransaction;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* JUnit 4 based unit test for verifying that '<em>before</em>' and '<em>after</em>'
|
||||
* methods of {@link TestExecutionListener TestExecutionListeners} as well as
|
||||
* {@link BeforeTransaction @BeforeTransaction} and
|
||||
* {@link AfterTransaction @AfterTransaction} methods can fail a test in a JUnit
|
||||
* 4.4 environment, as requested in <a
|
||||
* href="http://opensource.atlassian.com/projects/spring/browse/SPR-3960"
|
||||
* target="_blank">SPR-3960</a>.
|
||||
* </p>
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class FailingBeforeAndAfterMethodsTests {
|
||||
|
||||
protected final Class<?> clazz;
|
||||
|
||||
|
||||
public FailingBeforeAndAfterMethodsTests(final Class<?> clazz) {
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
@Parameters
|
||||
public static Collection<Object[]> testData() {
|
||||
return Arrays.asList(new Object[][] {
|
||||
|
||||
{ AlwaysFailingBeforeTestMethodTestCase.class },
|
||||
|
||||
{ AlwaysFailingAfterTestMethodTestCase.class },
|
||||
|
||||
{ FailingBeforeTransactionalTestCase.class },
|
||||
|
||||
{ FailingAfterTransactionalTestCase.class }
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void runTestAndAssertCounters() throws Exception {
|
||||
final FailureTrackingRunListener failureTrackingRunListener = new FailureTrackingRunListener();
|
||||
final RunNotifier notifier = new RunNotifier();
|
||||
notifier.addListener(failureTrackingRunListener);
|
||||
|
||||
new SpringJUnit4ClassRunner(this.clazz).run(notifier);
|
||||
assertEquals("Verifying number of failures for test class [" + this.clazz + "].", 1,
|
||||
failureTrackingRunListener.failureCount);
|
||||
}
|
||||
|
||||
|
||||
static class FailureTrackingRunListener extends RunListener {
|
||||
|
||||
int failureCount = 0;
|
||||
|
||||
|
||||
public void testFailure(Failure failure) throws Exception {
|
||||
this.failureCount++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static class AlwaysFailingBeforeTestMethodTestExecutionListener extends AbstractTestExecutionListener {
|
||||
|
||||
@Override
|
||||
public void beforeTestMethod(TestContext testContext) {
|
||||
org.junit.Assert.fail("always failing beforeTestMethod()");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static class AlwaysFailingAfterTestMethodTestExecutionListener extends AbstractTestExecutionListener {
|
||||
|
||||
@Override
|
||||
public void afterTestMethod(TestContext testContext) {
|
||||
org.junit.Assert.fail("always failing afterTestMethod()");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@TestExecutionListeners(value = { AlwaysFailingBeforeTestMethodTestExecutionListener.class }, inheritListeners = false)
|
||||
public static class AlwaysFailingBeforeTestMethodTestCase extends AbstractJUnit4SpringContextTests {
|
||||
|
||||
@Test
|
||||
public void testNothing() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@TestExecutionListeners(value = { AlwaysFailingAfterTestMethodTestExecutionListener.class }, inheritListeners = false)
|
||||
public static class AlwaysFailingAfterTestMethodTestCase extends AbstractJUnit4SpringContextTests {
|
||||
|
||||
@Test
|
||||
public void testNothing() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ContextConfiguration(locations = { "FailingBeforeAndAfterMethodsTests-context.xml" })
|
||||
public static class FailingBeforeTransactionalTestCase extends AbstractTransactionalJUnit4SpringContextTests {
|
||||
|
||||
@Test
|
||||
public void testNothing() {
|
||||
}
|
||||
|
||||
@BeforeTransaction
|
||||
public void beforeTransaction() {
|
||||
org.junit.Assert.fail("always failing beforeTransaction()");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ContextConfiguration(locations = { "FailingBeforeAndAfterMethodsTests-context.xml" })
|
||||
public static class FailingAfterTransactionalTestCase extends AbstractTransactionalJUnit4SpringContextTests {
|
||||
|
||||
@Test
|
||||
public void testNothing() {
|
||||
}
|
||||
|
||||
@AfterTransaction
|
||||
public void afterTransaction() {
|
||||
org.junit.Assert.fail("always failing afterTransaction()");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* 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.test.context.junit4;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
|
||||
import org.springframework.test.annotation.ProfileValueSource;
|
||||
import org.springframework.test.annotation.ProfileValueSourceConfiguration;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Verifies proper handling of JUnit's {@link org.junit.Ignore @Ignore} and
|
||||
* Spring's
|
||||
* {@link org.springframework.test.annotation.IfProfileValue @IfProfileValue}
|
||||
* and {@link ProfileValueSourceConfiguration @ProfileValueSourceConfiguration}
|
||||
* (with an <em>explicit, custom defined {@link ProfileValueSource}</em>)
|
||||
* annotations in conjunction with the {@link SpringJUnit4ClassRunner}.
|
||||
* </p>
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
* @see EnabledAndIgnoredSpringRunnerTests
|
||||
*/
|
||||
@ProfileValueSourceConfiguration(HardCodedProfileValueSourceSpringRunnerTests.HardCodedProfileValueSource.class)
|
||||
public class HardCodedProfileValueSourceSpringRunnerTests extends EnabledAndIgnoredSpringRunnerTests {
|
||||
|
||||
@BeforeClass
|
||||
public static void setProfileValue() {
|
||||
numTestsExecuted = 0;
|
||||
// Set the system property to something other than VALUE as a sanity
|
||||
// check.
|
||||
System.setProperty(NAME, "999999999999");
|
||||
}
|
||||
|
||||
|
||||
public static class HardCodedProfileValueSource implements ProfileValueSource {
|
||||
|
||||
public String get(final String key) {
|
||||
return (key.equals(NAME) ? VALUE : null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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.test.context.junit4;
|
||||
|
||||
import java.lang.annotation.Inherited;
|
||||
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
/**
|
||||
* Extension of {@link SpringJUnit4ClassRunnerAppCtxTests} which verifies that
|
||||
* the configuration of an application context and dependency injection of a
|
||||
* test instance function as expected within a class hierarchy, since
|
||||
* {@link ContextConfiguration configuration} is {@link Inherited inherited}.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
* @see SpringJUnit4ClassRunnerAppCtxTests
|
||||
*/
|
||||
public class InheritedConfigSpringJUnit4ClassRunnerAppCtxTests extends SpringJUnit4ClassRunnerAppCtxTests {
|
||||
/* all tests are in the parent class. */
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
|
||||
|
||||
<bean id="databaseSetup"
|
||||
class="org.springframework.test.context.junit4.MethodLevelTransactionalSpringRunnerTests$DatabaseSetup" />
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,118 @@
|
||||
/*
|
||||
* 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.test.context.junit4;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.springframework.test.transaction.TransactionTestUtils.assertInTransaction;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestExecutionListener;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
||||
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
|
||||
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* JUnit 4 based unit test which verifies support of Spring's
|
||||
* {@link Transactional @Transactional},
|
||||
* {@link TestExecutionListeners @TestExecutionListeners}, and
|
||||
* {@link ContextConfiguration @ContextConfiguration} annotations in conjunction
|
||||
* with the {@link SpringJUnit4ClassRunner} and the following
|
||||
* {@link TestExecutionListener TestExecutionListeners}:
|
||||
* </p>
|
||||
* <ul>
|
||||
* <li>{@link DependencyInjectionTestExecutionListener}</li>
|
||||
* <li>{@link DirtiesContextTestExecutionListener}</li>
|
||||
* <li>{@link TransactionalTestExecutionListener}</li>
|
||||
* </ul>
|
||||
* <p>
|
||||
* This class specifically tests usage of <code>@Transactional</code>
|
||||
* defined at the <strong>method level</strong>. In contrast to
|
||||
* {@link ClassLevelTransactionalSpringRunnerTests}, this class omits usage of
|
||||
* <code>@NotTransactional</code>.
|
||||
* </p>
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
* @see ClassLevelTransactionalSpringRunnerTests
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration
|
||||
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class,
|
||||
TransactionalTestExecutionListener.class})
|
||||
public class MethodLevelTransactionalSpringRunnerTests extends AbstractTransactionalSpringRunnerTests {
|
||||
|
||||
protected static SimpleJdbcTemplate simpleJdbcTemplate;
|
||||
|
||||
|
||||
@AfterClass
|
||||
public static void verifyFinalTestData() {
|
||||
assertEquals("Verifying the final number of rows in the person table after all tests.", 4,
|
||||
countRowsInPersonTable(simpleJdbcTemplate));
|
||||
}
|
||||
|
||||
@Before
|
||||
public void verifyInitialTestData() {
|
||||
clearPersonTable(simpleJdbcTemplate);
|
||||
assertEquals("Adding bob", 1, addPerson(simpleJdbcTemplate, BOB));
|
||||
assertEquals("Verifying the initial number of rows in the person table.", 1,
|
||||
countRowsInPersonTable(simpleJdbcTemplate));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void modifyTestDataWithinTransaction() {
|
||||
assertInTransaction(true);
|
||||
assertEquals("Deleting bob", 1, deletePerson(simpleJdbcTemplate, BOB));
|
||||
assertEquals("Adding jane", 1, addPerson(simpleJdbcTemplate, JANE));
|
||||
assertEquals("Adding sue", 1, addPerson(simpleJdbcTemplate, SUE));
|
||||
assertEquals("Verifying the number of rows in the person table within a transaction.", 2,
|
||||
countRowsInPersonTable(simpleJdbcTemplate));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void modifyTestDataWithoutTransaction() {
|
||||
assertInTransaction(false);
|
||||
assertEquals("Adding luke", 1, addPerson(simpleJdbcTemplate, LUKE));
|
||||
assertEquals("Adding leia", 1, addPerson(simpleJdbcTemplate, LEIA));
|
||||
assertEquals("Adding yoda", 1, addPerson(simpleJdbcTemplate, YODA));
|
||||
assertEquals("Verifying the number of rows in the person table without a transaction.", 4,
|
||||
countRowsInPersonTable(simpleJdbcTemplate));
|
||||
}
|
||||
|
||||
|
||||
public static class DatabaseSetup {
|
||||
|
||||
@Resource
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
|
||||
createPersonTable(simpleJdbcTemplate);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
|
||||
|
||||
<bean id="employee" class="org.springframework.beans.Employee">
|
||||
<property name="name" value="John Smith" />
|
||||
<property name="age" value="42" />
|
||||
<property name="company" value="Acme Widgets, Inc." />
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
|
||||
|
||||
<bean id="pet" class="org.springframework.beans.Pet">
|
||||
<constructor-arg value="Fido" />
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
|
||||
|
||||
<bean id="foo" class="java.lang.String">
|
||||
<constructor-arg value="Foo" />
|
||||
</bean>
|
||||
|
||||
<bean id="bar" class="java.lang.String">
|
||||
<constructor-arg value="Bar" />
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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.test.context.junit4;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.util.ResourceUtils;
|
||||
|
||||
/**
|
||||
* Extension of {@link SpringJUnit4ClassRunnerAppCtxTests}, which verifies that
|
||||
* we can specify multiple resource locations for our application context, each
|
||||
* configured differently.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
* @see SpringJUnit4ClassRunnerAppCtxTests
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { MultipleResourcesSpringJUnit4ClassRunnerAppCtxTests.CLASSPATH_RESOURCE_PATH,
|
||||
MultipleResourcesSpringJUnit4ClassRunnerAppCtxTests.LOCAL_RESOURCE_PATH,
|
||||
MultipleResourcesSpringJUnit4ClassRunnerAppCtxTests.ABSOLUTE_RESOURCE_PATH })
|
||||
public class MultipleResourcesSpringJUnit4ClassRunnerAppCtxTests extends SpringJUnit4ClassRunnerAppCtxTests {
|
||||
|
||||
public static final String CLASSPATH_RESOURCE_PATH = ResourceUtils.CLASSPATH_URL_PREFIX
|
||||
+ "/org/springframework/test/context/junit4/MultipleResourcesSpringJUnit4ClassRunnerAppCtxTests-context1.xml";
|
||||
public static final String LOCAL_RESOURCE_PATH = "MultipleResourcesSpringJUnit4ClassRunnerAppCtxTests-context2.xml";
|
||||
public static final String ABSOLUTE_RESOURCE_PATH = "/org/springframework/test/context/junit4/MultipleResourcesSpringJUnit4ClassRunnerAppCtxTests-context3.xml";
|
||||
|
||||
/* all tests are in the parent class. */
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
|
||||
|
||||
<bean id="employee1" class="org.springframework.beans.Employee">
|
||||
<property name="name" value="John Smith" />
|
||||
<property name="age" value="42" />
|
||||
<property name="company" value="Acme Widgets, Inc." />
|
||||
</bean>
|
||||
|
||||
<bean id="employee2" class="org.springframework.beans.Employee">
|
||||
<property name="name" value="Jane Smith" />
|
||||
<property name="age" value="38" />
|
||||
<property name="company" value="Acme Widgets, Inc." />
|
||||
</bean>
|
||||
|
||||
<bean id="pet" class="org.springframework.beans.Pet">
|
||||
<constructor-arg value="Fido" />
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
* 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.test.context.junit4;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
|
||||
import org.springframework.beans.Employee;
|
||||
import org.springframework.beans.Pet;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestContextManager;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
||||
|
||||
/**
|
||||
* Simple JUnit 4 based unit test which demonstrates how to use JUnit's
|
||||
* {@link Parameterized} Runner in conjunction with
|
||||
* {@link ContextConfiguration @ContextConfiguration}, the
|
||||
* {@link DependencyInjectionTestExecutionListener}, and a
|
||||
* {@link TestContextManager} to provide dependency injection to a
|
||||
* <em>parameterized test instance</em>.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
@ContextConfiguration
|
||||
@TestExecutionListeners( { DependencyInjectionTestExecutionListener.class })
|
||||
public class ParameterizedDependencyInjectionTests {
|
||||
|
||||
private static final List<Employee> employees = new ArrayList<Employee>();
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@Autowired
|
||||
private Pet pet;
|
||||
|
||||
private final String employeeBeanName;
|
||||
private final String employeeName;
|
||||
|
||||
private final TestContextManager testContextManager;
|
||||
|
||||
|
||||
public ParameterizedDependencyInjectionTests(final String employeeBeanName, final String employeeName)
|
||||
throws Exception {
|
||||
this.testContextManager = new TestContextManager(getClass());
|
||||
this.employeeBeanName = employeeBeanName;
|
||||
this.employeeName = employeeName;
|
||||
}
|
||||
|
||||
@Parameters
|
||||
public static Collection<String[]> employeeData() {
|
||||
return Arrays.asList(new String[][] { { "employee1", "John Smith" }, { "employee2", "Jane Smith" } });
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void clearEmployees() {
|
||||
employees.clear();
|
||||
}
|
||||
|
||||
@Before
|
||||
public void injectDependencies() throws Throwable {
|
||||
this.testContextManager.prepareTestInstance(this);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void verifyPetAndEmployee() {
|
||||
|
||||
// Verifying dependency injection:
|
||||
assertNotNull("The pet field should have been autowired.", this.pet);
|
||||
|
||||
// Verifying 'parameterized' support:
|
||||
final Employee employee = (Employee) this.applicationContext.getBean(this.employeeBeanName);
|
||||
employees.add(employee);
|
||||
assertEquals("Verifying the name of the employee configured as bean [" + this.employeeBeanName + "].",
|
||||
this.employeeName, employee.getName());
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void verifyNumParameterizedRuns() {
|
||||
assertEquals("Verifying the number of times the parameterized test method was executed.",
|
||||
employeeData().size(), employees.size());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
cat.(class)=org.springframework.beans.Pet
|
||||
cat.$0=Garfield
|
||||
|
||||
testString.(class)=java.lang.String
|
||||
testString.$0=Test String
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* 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.test.context.junit4;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.Pet;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.support.GenericPropertiesContextLoader;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* JUnit 4 based test class, which verifies the expected functionality of
|
||||
* {@link SpringJUnit4ClassRunner} in conjunction with support for application
|
||||
* contexts loaded from Java {@link Properties} files. Specifically, the
|
||||
* {@link ContextConfiguration#loader() loaderClass} and
|
||||
* {@link ContextConfiguration#resourceSuffix() resourceSuffix} attributes of
|
||||
* @ContextConfiguration are tested.
|
||||
* </p>
|
||||
* <p>
|
||||
* Since no {@link ContextConfiguration#locations() locations} are explicitly
|
||||
* defined, the {@link ContextConfiguration#resourceSuffix() resourceSuffix} is
|
||||
* set to "-context.properties", and
|
||||
* {@link ContextConfiguration#generateDefaultLocations() generateDefaultLocations}
|
||||
* is left set to its default value of <code>true</code>, this test class's
|
||||
* dependencies will be injected via
|
||||
* {@link Autowired annotation-based autowiring} from beans defined in the
|
||||
* {@link ApplicationContext} loaded from the default classpath resource: "<code>/org/springframework/test/junit4/PropertiesBasedSpringJUnit4ClassRunnerAppCtxTests-context.properties</code>".
|
||||
* </p>
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
* @see GenericPropertiesContextLoader
|
||||
* @see SpringJUnit4ClassRunnerAppCtxTests
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(loader = GenericPropertiesContextLoader.class)
|
||||
public class PropertiesBasedSpringJUnit4ClassRunnerAppCtxTests {
|
||||
|
||||
@Autowired
|
||||
private Pet cat;
|
||||
|
||||
@Autowired
|
||||
private String testString;
|
||||
|
||||
|
||||
@Test
|
||||
public void verifyAnnotationAutowiredFields() {
|
||||
assertNotNull("The cat field should have been autowired.", this.cat);
|
||||
assertEquals("Garfield", this.cat.getName());
|
||||
|
||||
assertNotNull("The testString field should have been autowired.", this.testString);
|
||||
assertEquals("Test String", this.testString);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* 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.test.context.junit4;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
/**
|
||||
* Extension of {@link SpringJUnit4ClassRunnerAppCtxTests}, which verifies that
|
||||
* we can specify an explicit, <em>relative path</em> location for our
|
||||
* application context.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
* @see SpringJUnit4ClassRunnerAppCtxTests
|
||||
* @see AbsolutePathSpringJUnit4ClassRunnerAppCtxTests
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { "SpringJUnit4ClassRunnerAppCtxTests-context.xml" })
|
||||
public class RelativePathSpringJUnit4ClassRunnerAppCtxTests extends SpringJUnit4ClassRunnerAppCtxTests {
|
||||
/* all tests are in the parent class. */
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:p="http://www.springframework.org/schema/p"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
|
||||
|
||||
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
|
||||
p:driverClassName="org.hsqldb.jdbcDriver" p:url="jdbc:hsqldb:mem:transactional_tests" p:username="sa" p:password="" />
|
||||
|
||||
<bean id="txMgr" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
|
||||
p:data-source-ref="dataSource" />
|
||||
|
||||
<bean id="databaseSetup"
|
||||
class="org.springframework.test.context.junit4.RollbackOverrideDefaultRollbackFalseTransactionalSpringRunnerTests$DatabaseSetup" />
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* 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.test.context.junit4;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.springframework.test.transaction.TransactionTestUtils.assertInTransaction;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
|
||||
import org.springframework.test.annotation.Rollback;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
/**
|
||||
* Extension of {@link DefaultRollbackFalseTransactionalSpringRunnerTests} which
|
||||
* tests method-level <em>rollback override</em> behavior via the
|
||||
* {@link Rollback @Rollback} annotation.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
* @see Rollback
|
||||
*/
|
||||
@ContextConfiguration
|
||||
public class RollbackOverrideDefaultRollbackFalseTransactionalSpringRunnerTests extends
|
||||
DefaultRollbackFalseTransactionalSpringRunnerTests {
|
||||
|
||||
protected static int originalNumRows;
|
||||
|
||||
protected static SimpleJdbcTemplate simpleJdbcTemplate;
|
||||
|
||||
|
||||
@AfterClass
|
||||
public static void verifyFinalTestData() {
|
||||
assertEquals("Verifying the final number of rows in the person table after all tests.", originalNumRows,
|
||||
countRowsInPersonTable(simpleJdbcTemplate));
|
||||
}
|
||||
|
||||
@Before
|
||||
@Override
|
||||
public void verifyInitialTestData() {
|
||||
originalNumRows = clearPersonTable(simpleJdbcTemplate);
|
||||
assertEquals("Adding bob", 1, addPerson(simpleJdbcTemplate, BOB));
|
||||
assertEquals("Verifying the initial number of rows in the person table.", 1,
|
||||
countRowsInPersonTable(simpleJdbcTemplate));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Rollback(true)
|
||||
@Override
|
||||
public void modifyTestDataWithinTransaction() {
|
||||
assertInTransaction(true);
|
||||
assertEquals("Deleting bob", 1, deletePerson(simpleJdbcTemplate, BOB));
|
||||
assertEquals("Adding jane", 1, addPerson(simpleJdbcTemplate, JANE));
|
||||
assertEquals("Adding sue", 1, addPerson(simpleJdbcTemplate, SUE));
|
||||
assertEquals("Verifying the number of rows in the person table within a transaction.", 2,
|
||||
countRowsInPersonTable(simpleJdbcTemplate));
|
||||
}
|
||||
|
||||
|
||||
public static class DatabaseSetup {
|
||||
|
||||
@Resource
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
|
||||
createPersonTable(simpleJdbcTemplate);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
|
||||
|
||||
<bean id="databaseSetup"
|
||||
class="org.springframework.test.context.junit4.RollbackOverrideDefaultRollbackTrueTransactionalSpringRunnerTests$DatabaseSetup" />
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* 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.test.context.junit4;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.springframework.test.transaction.TransactionTestUtils.assertInTransaction;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
|
||||
import org.springframework.test.annotation.Rollback;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* Extension of {@link DefaultRollbackTrueTransactionalSpringRunnerTests} which
|
||||
* tests method-level <em>rollback override</em> behavior via the
|
||||
* {@link Rollback @Rollback} annotation.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
* @see Rollback
|
||||
*/
|
||||
@ContextConfiguration
|
||||
public class RollbackOverrideDefaultRollbackTrueTransactionalSpringRunnerTests extends
|
||||
DefaultRollbackTrueTransactionalSpringRunnerTests {
|
||||
|
||||
protected static SimpleJdbcTemplate simpleJdbcTemplate;
|
||||
|
||||
|
||||
@AfterClass
|
||||
public static void verifyFinalTestData() {
|
||||
assertEquals("Verifying the final number of rows in the person table after all tests.", 3,
|
||||
countRowsInPersonTable(simpleJdbcTemplate));
|
||||
}
|
||||
|
||||
@Before
|
||||
public void verifyInitialTestData() {
|
||||
clearPersonTable(simpleJdbcTemplate);
|
||||
assertEquals("Adding bob", 1, addPerson(simpleJdbcTemplate, BOB));
|
||||
assertEquals("Verifying the initial number of rows in the person table.", 1,
|
||||
countRowsInPersonTable(simpleJdbcTemplate));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
@Rollback(false)
|
||||
public void modifyTestDataWithinTransaction() {
|
||||
assertInTransaction(true);
|
||||
assertEquals("Adding jane", 1, addPerson(simpleJdbcTemplate, JANE));
|
||||
assertEquals("Adding sue", 1, addPerson(simpleJdbcTemplate, SUE));
|
||||
assertEquals("Verifying the number of rows in the person table within a transaction.", 3,
|
||||
countRowsInPersonTable(simpleJdbcTemplate));
|
||||
}
|
||||
|
||||
|
||||
public static class DatabaseSetup {
|
||||
|
||||
@Resource
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
|
||||
createPersonTable(simpleJdbcTemplate);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
|
||||
|
||||
<bean id="employee" class="org.springframework.beans.Employee">
|
||||
<property name="name" value="John Smith" />
|
||||
<property name="age" value="42" />
|
||||
<property name="company" value="Acme Widgets, Inc." />
|
||||
</bean>
|
||||
|
||||
<bean id="pet" class="org.springframework.beans.Pet">
|
||||
<constructor-arg value="Fido" />
|
||||
</bean>
|
||||
|
||||
<bean id="foo" class="java.lang.String">
|
||||
<constructor-arg value="Foo" />
|
||||
</bean>
|
||||
|
||||
<bean id="bar" class="java.lang.String">
|
||||
<constructor-arg value="Bar" />
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,171 @@
|
||||
/*
|
||||
* 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.test.context.junit4;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.Employee;
|
||||
import org.springframework.beans.Pet;
|
||||
import org.springframework.beans.factory.BeanNameAware;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
||||
import org.springframework.test.context.support.GenericXmlContextLoader;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* SpringJUnit4ClassRunnerAppCtxTests serves as a <em>proof of concept</em>
|
||||
* JUnit 4 based test class, which verifies the expected functionality of
|
||||
* {@link SpringJUnit4ClassRunner} in conjunction with the following:
|
||||
* </p>
|
||||
* <ul>
|
||||
* <li>{@link ContextConfiguration @ContextConfiguration}</li>
|
||||
* <li>{@link Autowired @Autowired}</li>
|
||||
* <li>{@link Resource @Resource}</li>
|
||||
* <li>{@link ApplicationContextAware}</li>
|
||||
* <li>{@link BeanNameAware}</li>
|
||||
* <li>{@link InitializingBean}</li>
|
||||
* </ul>
|
||||
* <p>
|
||||
* Since no application context resource
|
||||
* {@link ContextConfiguration#locations() locations} are explicitly declared
|
||||
* and since the {@link ContextConfiguration#loader() ContextLoader} is left set
|
||||
* to the default value of {@link GenericXmlContextLoader}, this test class's
|
||||
* dependencies will be injected via {@link Autowired @Autowired} and
|
||||
* {@link Resource @Resource} from beans defined in the
|
||||
* {@link ApplicationContext} loaded from the default classpath resource: "<code>/org/springframework/test/context/junit/SpringJUnit4ClassRunnerAppCtxTests-context.xml</code>".
|
||||
* </p>
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
* @see AbsolutePathSpringJUnit4ClassRunnerAppCtxTests
|
||||
* @see RelativePathSpringJUnit4ClassRunnerAppCtxTests
|
||||
* @see InheritedConfigSpringJUnit4ClassRunnerAppCtxTests
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration
|
||||
@TestExecutionListeners( { DependencyInjectionTestExecutionListener.class })
|
||||
public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAware, BeanNameAware, InitializingBean {
|
||||
|
||||
/**
|
||||
* Default resource path for the application context configuration for
|
||||
* {@link SpringJUnit4ClassRunnerAppCtxTests}:
|
||||
* <code>"/org/springframework/test/context/junit4/SpringJUnit4ClassRunnerAppCtxTests-context.xml"</code>
|
||||
*/
|
||||
public static final String DEFAULT_CONTEXT_RESOURCE_PATH = "/org/springframework/test/context/junit4/SpringJUnit4ClassRunnerAppCtxTests-context.xml";
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
private boolean beanInitialized = false;
|
||||
|
||||
private String beanName = "replace me with [" + getClass().getName() + "]";
|
||||
|
||||
private Employee employee;
|
||||
|
||||
@Autowired
|
||||
private Pet pet;
|
||||
|
||||
@Autowired(required = false)
|
||||
protected Long nonrequiredLong;
|
||||
|
||||
@Resource()
|
||||
protected String foo;
|
||||
|
||||
protected String bar;
|
||||
|
||||
|
||||
// ------------------------------------------------------------------------|
|
||||
|
||||
public final void afterPropertiesSet() throws Exception {
|
||||
this.beanInitialized = true;
|
||||
}
|
||||
|
||||
public final void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
public final void setBeanName(final String beanName) {
|
||||
this.beanName = beanName;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
protected final void setEmployee(final Employee employee) {
|
||||
this.employee = employee;
|
||||
}
|
||||
|
||||
@Resource
|
||||
protected final void setBar(final String bar) {
|
||||
this.bar = bar;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------|
|
||||
|
||||
@Test
|
||||
public final void verifyApplicationContextSet() {
|
||||
assertNotNull("The application context should have been set due to ApplicationContextAware semantics.",
|
||||
this.applicationContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void verifyBeanInitialized() {
|
||||
assertTrue("This test bean should have been initialized due to InitializingBean semantics.",
|
||||
this.beanInitialized);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void verifyBeanNameSet() {
|
||||
assertEquals("The bean name of this test instance should have been set due to BeanNameAware semantics.",
|
||||
getClass().getName(), this.beanName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void verifyAnnotationAutowiredFields() {
|
||||
assertNull("The nonrequiredLong field should NOT have been autowired.", this.nonrequiredLong);
|
||||
assertNotNull("The pet field should have been autowired.", this.pet);
|
||||
assertEquals("Fido", this.pet.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void verifyAnnotationAutowiredMethods() {
|
||||
assertNotNull("The employee setter method should have been autowired.", this.employee);
|
||||
assertEquals("John Smith", this.employee.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void verifyResourceAnnotationWiredFields() {
|
||||
assertEquals("The foo field should have been wired via @Resource.", "Foo", this.foo);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void verifyResourceAnnotationWiredMethods() {
|
||||
assertEquals("The bar method should have been wired via @Resource.", "Bar", this.bar);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* 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.test.context.junit4;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.test.context.TestContextManager;
|
||||
|
||||
/**
|
||||
* @author Rick Evans
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
public class SpringJUnit4ClassRunnerTests {
|
||||
|
||||
@Test(expected = Exception.class)
|
||||
public void checkThatExceptionsAreNotSilentlySwallowed() throws Exception {
|
||||
SpringJUnit4ClassRunner runner = new SpringJUnit4ClassRunner(getClass()) {
|
||||
|
||||
@Override
|
||||
protected TestContextManager createTestContextManager(Class<?> clazz) {
|
||||
return new TestContextManager(clazz) {
|
||||
|
||||
@Override
|
||||
public void prepareTestInstance(Object testInstance) {
|
||||
throw new RuntimeException("This RuntimeException should be caught and wrapped in an Exception.");
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
runner.createTest();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* 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.test.context.junit4;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Suite;
|
||||
import org.junit.runners.Suite.SuiteClasses;
|
||||
|
||||
import org.springframework.test.context.SpringRunnerContextCacheTests;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* JUnit 4 based test suite for tests involving {@link SpringJUnit4ClassRunner},
|
||||
* {@link SpringMethodRoadie}, and Spring's annotation-based test support.
|
||||
* </p>
|
||||
* <p>
|
||||
* This test suite serves a dual purpose of verifying that tests run with
|
||||
* {@link SpringJUnit4ClassRunner} can be used in conjunction with JUnit 4's
|
||||
* {@link Suite} runner.
|
||||
* </p>
|
||||
* <p>
|
||||
* Note that tests included in this suite will be executed at least twice if run
|
||||
* from an automated build process, test runner, etc. that is configured to run
|
||||
* tests based on a "*Tests.class" pattern match.
|
||||
* </p>
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
@RunWith(Suite.class)
|
||||
// Note: the following 'multi-line' layout is for enhanced code readability.
|
||||
@SuiteClasses( {
|
||||
|
||||
StandardJUnit4FeaturesTests.class,
|
||||
|
||||
StandardJUnit4FeaturesSpringRunnerTests.class,
|
||||
|
||||
EnabledAndIgnoredSpringRunnerTests.class,
|
||||
|
||||
HardCodedProfileValueSourceSpringRunnerTests.class,
|
||||
|
||||
SpringJUnit4ClassRunnerAppCtxTests.class,
|
||||
|
||||
ClassPathResourceSpringJUnit4ClassRunnerAppCtxTests.class,
|
||||
|
||||
AbsolutePathSpringJUnit4ClassRunnerAppCtxTests.class,
|
||||
|
||||
RelativePathSpringJUnit4ClassRunnerAppCtxTests.class,
|
||||
|
||||
MultipleResourcesSpringJUnit4ClassRunnerAppCtxTests.class,
|
||||
|
||||
InheritedConfigSpringJUnit4ClassRunnerAppCtxTests.class,
|
||||
|
||||
PropertiesBasedSpringJUnit4ClassRunnerAppCtxTests.class,
|
||||
|
||||
SpringRunnerContextCacheTests.class,
|
||||
|
||||
ParameterizedDependencyInjectionTests.class,
|
||||
|
||||
ClassLevelTransactionalSpringRunnerTests.class,
|
||||
|
||||
MethodLevelTransactionalSpringRunnerTests.class,
|
||||
|
||||
DefaultRollbackTrueTransactionalSpringRunnerTests.class,
|
||||
|
||||
DefaultRollbackFalseTransactionalSpringRunnerTests.class,
|
||||
|
||||
RollbackOverrideDefaultRollbackTrueTransactionalSpringRunnerTests.class,
|
||||
|
||||
RollbackOverrideDefaultRollbackFalseTransactionalSpringRunnerTests.class,
|
||||
|
||||
BeforeAndAfterTransactionAnnotationTests.class,
|
||||
|
||||
TimedTransactionalSpringRunnerTests.class
|
||||
|
||||
})
|
||||
public class SpringJUnit4SuiteTests {
|
||||
/* this test case is comprised completely of tests loaded as a suite. */
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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.test.context.junit4;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Simple unit test to verify that {@link SpringJUnit4ClassRunner} does not
|
||||
* hinder correct functionality of standard JUnit 4.4+ testing features.
|
||||
* </p>
|
||||
* <p>
|
||||
* Note that {@link TestExecutionListeners @TestExecutionListeners} is
|
||||
* explicitly configured with an empty list, thus disabling all default
|
||||
* listeners.
|
||||
* </p>
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
* @see StandardJUnit4FeaturesTests
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@TestExecutionListeners({})
|
||||
public class StandardJUnit4FeaturesSpringRunnerTests extends StandardJUnit4FeaturesTests {
|
||||
|
||||
/* All tests are in the parent class... */
|
||||
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* 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.test.context.junit4;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.junit.Assume.assumeTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Simple unit test to verify the expected functionality of standard JUnit 4.4+
|
||||
* testing features.
|
||||
* <p>
|
||||
* Currently testing: {@link Test @Test} (including expected exceptions and
|
||||
* timeouts), {@link BeforeClass @BeforeClass}, {@link Before @Before}, and
|
||||
* <em>assumptions</em>.
|
||||
* </p>
|
||||
* <p>
|
||||
* Due to the fact that JUnit does not guarantee a particular ordering of test
|
||||
* method execution, the following are currently not tested:
|
||||
* {@link org.junit.AfterClass @AfterClass} and {@link org.junit.After @After}.
|
||||
* </p>
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
* @see StandardJUnit4FeaturesSpringRunnerTests
|
||||
*/
|
||||
public class StandardJUnit4FeaturesTests {
|
||||
|
||||
private static int staticBeforeCounter = 0;
|
||||
|
||||
|
||||
@BeforeClass
|
||||
public static void incrementStaticBeforeCounter() {
|
||||
StandardJUnit4FeaturesTests.staticBeforeCounter++;
|
||||
}
|
||||
|
||||
|
||||
private int beforeCounter = 0;
|
||||
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void alwaysFailsButShouldBeIgnored() {
|
||||
fail("The body of an ignored test should never be executed!");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void alwaysSucceeds() {
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
@Test(expected = IndexOutOfBoundsException.class)
|
||||
public void expectingAnIndexOutOfBoundsException() {
|
||||
new ArrayList<Object>().get(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void failedAssumptionShouldPrecludeImminentFailure() {
|
||||
assumeTrue(false);
|
||||
fail("A failed assumption should preclude imminent failure!");
|
||||
}
|
||||
|
||||
@Before
|
||||
public void incrementBeforeCounter() {
|
||||
this.beforeCounter++;
|
||||
}
|
||||
|
||||
@Test(timeout = 10000)
|
||||
public void noOpShouldNotTimeOut() {
|
||||
/* no-op */
|
||||
}
|
||||
|
||||
@Test
|
||||
public void verifyBeforeAnnotation() {
|
||||
assertEquals(1, this.beforeCounter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void verifyBeforeClassAnnotation() {
|
||||
// Instead of testing for equality to 1, we just assert that the value
|
||||
// was incremented at least once, since this test class may serve as a
|
||||
// parent class to other tests in a suite, etc.
|
||||
assertTrue(StandardJUnit4FeaturesTests.staticBeforeCounter > 0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* 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.test.context.junit4;
|
||||
|
||||
import static org.springframework.test.transaction.TransactionTestUtils.assertInTransaction;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.test.annotation.NotTransactional;
|
||||
import org.springframework.test.annotation.Repeat;
|
||||
import org.springframework.test.annotation.Timed;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* JUnit 4 based unit test which verifies support of Spring's
|
||||
* {@link Transactional @Transactional} and
|
||||
* {@link NotTransactional @NotTransactional} annotations in conjunction with
|
||||
* {@link Timed @Timed} and JUnit 4's {@link Test#timeout() timeout} attribute.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = {"transactionalTests-context.xml"})
|
||||
@Transactional
|
||||
public class TimedTransactionalSpringRunnerTests {
|
||||
|
||||
@Test
|
||||
@Timed(millis = 10000)
|
||||
@Repeat(5)
|
||||
public void transactionalWithSpringTimeout() {
|
||||
assertInTransaction(true);
|
||||
}
|
||||
|
||||
@Test(timeout = 10000)
|
||||
@Repeat(5)
|
||||
public void transactionalWithJUnitTimeout() {
|
||||
assertInTransaction(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
@NotTransactional
|
||||
@Timed(millis = 10000)
|
||||
@Repeat(5)
|
||||
public void notTransactionalWithSpringTimeout() {
|
||||
assertInTransaction(false);
|
||||
}
|
||||
|
||||
@Test(timeout = 10000)
|
||||
@NotTransactional
|
||||
@Repeat(5)
|
||||
public void notTransactionalWithJUnitTimeout() {
|
||||
assertInTransaction(false);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
|
||||
|
||||
<bean id="employee" class="org.springframework.beans.Employee">
|
||||
<property name="name" value="Yoda" />
|
||||
<property name="age" value="900" />
|
||||
<property name="company" value="The Force" />
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 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.test.context.junit4.spr3896;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* JUnit 4 based unit test for verifying support for the
|
||||
* {@link ContextConfiguration#inheritLocations() inheritLocations} flag of
|
||||
* {@link ContextConfiguration @ContextConfiguration} indirectly proposed in <a
|
||||
* href="http://opensource.atlassian.com/projects/spring/browse/SPR-3896"
|
||||
* target="_blank">SPR-3896</a>.
|
||||
* </p>
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
@ContextConfiguration
|
||||
public class BeanOverridingDefaultLocationsInheritedTests extends DefaultLocationsBaseTests {
|
||||
|
||||
@Test
|
||||
@Override
|
||||
public void verifyEmployeeSetFromBaseContextConfig() {
|
||||
assertNotNull("The employee should have been autowired.", this.employee);
|
||||
assertEquals("The employee bean should have been overridden.", "Yoda", this.employee.getName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 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.test.context.junit4.spr3896;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* JUnit 4 based unit test for verifying support for the
|
||||
* {@link ContextConfiguration#inheritLocations() inheritLocations} flag of
|
||||
* {@link ContextConfiguration @ContextConfiguration} indirectly proposed in <a
|
||||
* href="http://opensource.atlassian.com/projects/spring/browse/SPR-3896"
|
||||
* target="_blank">SPR-3896</a>.
|
||||
* </p>
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
@ContextConfiguration(locations = { "BeanOverridingDefaultLocationsInheritedTests-context.xml" })
|
||||
public class BeanOverridingExplicitLocationsInheritedTests extends ExplicitLocationsBaseTests {
|
||||
|
||||
@Test
|
||||
@Override
|
||||
public void verifyEmployeeSetFromBaseContextConfig() {
|
||||
assertNotNull("The employee should have been autowired.", this.employee);
|
||||
assertEquals("The employee bean should have been overridden.", "Yoda", this.employee.getName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
|
||||
|
||||
<bean id="employee" class="org.springframework.beans.Employee">
|
||||
<property name="name" value="John Smith" />
|
||||
<property name="age" value="42" />
|
||||
<property name="company" value="Acme Widgets, Inc." />
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright 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.test.context.junit4.spr3896;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.Employee;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* JUnit 4 based unit test for verifying support for the
|
||||
* {@link ContextConfiguration#inheritLocations() inheritLocations} flag of
|
||||
* {@link ContextConfiguration @ContextConfiguration} indirectly proposed in <a
|
||||
* href="http://opensource.atlassian.com/projects/spring/browse/SPR-3896"
|
||||
* target="_blank">SPR-3896</a>.
|
||||
* </p>
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration
|
||||
@TestExecutionListeners( { DependencyInjectionTestExecutionListener.class })
|
||||
public class DefaultLocationsBaseTests {
|
||||
|
||||
@Autowired
|
||||
protected Employee employee;
|
||||
|
||||
|
||||
@Test
|
||||
public void verifyEmployeeSetFromBaseContextConfig() {
|
||||
assertNotNull("The employee should have been autowired.", this.employee);
|
||||
assertEquals("John Smith", this.employee.getName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
|
||||
|
||||
<bean id="pet" class="org.springframework.beans.Pet">
|
||||
<constructor-arg value="Fido" />
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright 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.test.context.junit4.spr3896;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.Pet;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* JUnit 4 based unit test for verifying support for the
|
||||
* {@link ContextConfiguration#inheritLocations() inheritLocations} flag of
|
||||
* {@link ContextConfiguration @ContextConfiguration} indirectly proposed in <a
|
||||
* href="http://opensource.atlassian.com/projects/spring/browse/SPR-3896"
|
||||
* target="_blank">SPR-3896</a>.
|
||||
* </p>
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
@ContextConfiguration
|
||||
public class DefaultLocationsInheritedTests extends DefaultLocationsBaseTests {
|
||||
|
||||
@Autowired
|
||||
private Pet pet;
|
||||
|
||||
|
||||
@Test
|
||||
public void verifyPetSetFromExtendedContextConfig() {
|
||||
assertNotNull("The pet should have been autowired.", this.pet);
|
||||
assertEquals("Fido", this.pet.getName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright 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.test.context.junit4.spr3896;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.Employee;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* JUnit 4 based unit test for verifying support for the
|
||||
* {@link ContextConfiguration#inheritLocations() inheritLocations} flag of
|
||||
* {@link ContextConfiguration @ContextConfiguration} indirectly proposed in <a
|
||||
* href="http://opensource.atlassian.com/projects/spring/browse/SPR-3896"
|
||||
* target="_blank">SPR-3896</a>.
|
||||
* </p>
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { "DefaultLocationsBaseTests-context.xml" })
|
||||
@TestExecutionListeners( { DependencyInjectionTestExecutionListener.class })
|
||||
public class ExplicitLocationsBaseTests {
|
||||
|
||||
@Autowired
|
||||
protected Employee employee;
|
||||
|
||||
|
||||
@Test
|
||||
public void verifyEmployeeSetFromBaseContextConfig() {
|
||||
assertNotNull("The employee should have been autowired.", this.employee);
|
||||
assertEquals("John Smith", this.employee.getName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright 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.test.context.junit4.spr3896;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.Pet;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* JUnit 4 based unit test for verifying support for the
|
||||
* {@link ContextConfiguration#inheritLocations() inheritLocations} flag of
|
||||
* {@link ContextConfiguration @ContextConfiguration} indirectly proposed in <a
|
||||
* href="http://opensource.atlassian.com/projects/spring/browse/SPR-3896"
|
||||
* target="_blank">SPR-3896</a>.
|
||||
* </p>
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
@ContextConfiguration(locations = { "DefaultLocationsInheritedTests-context.xml" })
|
||||
public class ExplicitLocationsInheritedTests extends ExplicitLocationsBaseTests {
|
||||
|
||||
@Autowired
|
||||
private Pet pet;
|
||||
|
||||
|
||||
@Test
|
||||
public void verifyPetSetFromExtendedContextConfig() {
|
||||
assertNotNull("The pet should have been autowired.", this.pet);
|
||||
assertEquals("Fido", this.pet.getName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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.test.context.junit4.spr3896;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Suite;
|
||||
import org.junit.runners.Suite.SuiteClasses;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* JUnit 4 based test suite for functionality proposed in <a
|
||||
* href="http://opensource.atlassian.com/projects/spring/browse/SPR-3896"
|
||||
* target="_blank">SPR-3896</a>.
|
||||
* </p>
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
@RunWith(Suite.class)
|
||||
// Note: the following 'multi-line' layout is for enhanced code readability.
|
||||
@SuiteClasses( {
|
||||
|
||||
DefaultLocationsBaseTests.class,
|
||||
|
||||
DefaultLocationsInheritedTests.class,
|
||||
|
||||
ExplicitLocationsBaseTests.class,
|
||||
|
||||
ExplicitLocationsInheritedTests.class,
|
||||
|
||||
BeanOverridingDefaultLocationsInheritedTests.class,
|
||||
|
||||
BeanOverridingExplicitLocationsInheritedTests.class
|
||||
|
||||
})
|
||||
public class Spr3896SuiteTests {
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:p="http://www.springframework.org/schema/p"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
|
||||
|
||||
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
|
||||
p:driverClassName="org.hsqldb.jdbcDriver" p:url="jdbc:hsqldb:mem:transactional_tests" p:username="sa" p:password="" />
|
||||
|
||||
<bean id="dataSource2" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
|
||||
p:driverClassName="org.hsqldb.jdbcDriver" p:url="jdbc:hsqldb:mem:transactional_tests" p:username="sa" p:password="" />
|
||||
|
||||
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
|
||||
p:data-source-ref="dataSource" />
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
|
||||
|
||||
<!-- intentionally empty: only needed so that the ContextLoader can load a file -->
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* 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.test.context.support;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
|
||||
/**
|
||||
* Unit test which verifies that extensions of
|
||||
* {@link AbstractGenericContextLoader} are able to <em>customize</em> the
|
||||
* newly created <code>ApplicationContext</code>. Specifically, this test
|
||||
* addresses the issues raised in <a
|
||||
* href="http://opensource.atlassian.com/projects/spring/browse/SPR-4008"
|
||||
* target="_blank">SPR-4008</a>: <em>Supply an opportunity to customize context
|
||||
* before calling refresh in ContextLoaders</em>.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
public class CustomizedGenericXmlContextLoaderTests {
|
||||
|
||||
@Test
|
||||
public void customizeContext() throws Exception {
|
||||
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
final String expectedContents = "customizeContext() was called";
|
||||
|
||||
new GenericXmlContextLoader() {
|
||||
|
||||
@Override
|
||||
protected void customizeContext(GenericApplicationContext context) {
|
||||
assertFalse("The context should not yet have been refreshed.", context.isActive());
|
||||
builder.append(expectedContents);
|
||||
}
|
||||
}.loadContext("classpath:/org/springframework/test/context/support/CustomizedGenericXmlContextLoaderTests-context.xml");
|
||||
|
||||
assertEquals("customizeContext() should have been called.", expectedContents, builder.toString());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
* 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.test.context.support;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.ContextLoader;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* JUnit 4 based unit test which verifies proper
|
||||
* {@link ContextLoader#processLocations(Class,String...) processing} of
|
||||
* <code>resource locations</code> by a {@link GenericXmlContextLoader}
|
||||
* configured via {@link ContextConfiguration @ContextConfiguration}.
|
||||
* Specifically, this test addresses the issues raised in <a
|
||||
* href="http://opensource.atlassian.com/projects/spring/browse/SPR-3949"
|
||||
* target="_blank">SPR-3949</a>:
|
||||
* <em>ContextConfiguration annotation should accept not only classpath resources</em>.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class GenericXmlContextLoaderResourceLocationsTests {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(GenericXmlContextLoaderResourceLocationsTests.class);
|
||||
|
||||
protected final Class<?> testClass;
|
||||
protected final String[] expectedLocations;
|
||||
|
||||
|
||||
public GenericXmlContextLoaderResourceLocationsTests(final Class<?> testClass, final String[] expectedLocations) {
|
||||
this.testClass = testClass;
|
||||
this.expectedLocations = expectedLocations;
|
||||
}
|
||||
|
||||
@Parameters
|
||||
public static Collection<Object[]> contextConfigurationLocationsData() {
|
||||
@ContextConfiguration
|
||||
class ClasspathDefaultLocationsTestCase {
|
||||
}
|
||||
|
||||
@ContextConfiguration(locations = { "context1.xml", "context2.xml" })
|
||||
class ImplicitClasspathLocationsTestCase {
|
||||
}
|
||||
|
||||
@ContextConfiguration(locations = { "classpath:context.xml" })
|
||||
class ExplicitClasspathLocationsTestCase {
|
||||
}
|
||||
|
||||
@ContextConfiguration(locations = { "file:/testing/directory/context.xml" })
|
||||
class ExplicitFileLocationsTestCase {
|
||||
}
|
||||
|
||||
@ContextConfiguration(locations = { "http://example.com/context.xml" })
|
||||
class ExplicitUrlLocationsTestCase {
|
||||
}
|
||||
|
||||
@ContextConfiguration(locations = { "context1.xml", "classpath:context2.xml", "/context3.xml",
|
||||
"file:/testing/directory/context.xml", "http://example.com/context.xml" })
|
||||
class ExplicitMixedPathTypesLocationsTestCase {
|
||||
}
|
||||
|
||||
return Arrays.asList(new Object[][] {
|
||||
|
||||
{
|
||||
ClasspathDefaultLocationsTestCase.class,
|
||||
new String[] { "classpath:/org/springframework/test/context/support/GenericXmlContextLoaderResourceLocationsTests$1ClasspathDefaultLocationsTestCase-context.xml" } },
|
||||
|
||||
{
|
||||
ImplicitClasspathLocationsTestCase.class,
|
||||
new String[] { "classpath:/org/springframework/test/context/support/context1.xml",
|
||||
"classpath:/org/springframework/test/context/support/context2.xml" } },
|
||||
|
||||
{ ExplicitClasspathLocationsTestCase.class, new String[] { "classpath:context.xml" } },
|
||||
|
||||
{ ExplicitFileLocationsTestCase.class, new String[] { "file:/testing/directory/context.xml" } },
|
||||
|
||||
{ ExplicitUrlLocationsTestCase.class, new String[] { "http://example.com/context.xml" } },
|
||||
|
||||
{
|
||||
ExplicitMixedPathTypesLocationsTestCase.class,
|
||||
new String[] { "classpath:/org/springframework/test/context/support/context1.xml",
|
||||
"classpath:context2.xml", "classpath:/context3.xml", "file:/testing/directory/context.xml",
|
||||
"http://example.com/context.xml" } }
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assertContextConfigurationLocations() throws Exception {
|
||||
|
||||
final ContextConfiguration contextConfig = this.testClass.getAnnotation(ContextConfiguration.class);
|
||||
final ContextLoader contextLoader = new GenericXmlContextLoader();
|
||||
final String[] configuredLocations = (String[]) AnnotationUtils.getValue(contextConfig, "locations");
|
||||
final String[] processedLocations = contextLoader.processLocations(this.testClass, configuredLocations);
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("----------------------------------------------------------------------");
|
||||
logger.debug("Configured locations: " + ObjectUtils.nullSafeToString(configuredLocations));
|
||||
logger.debug("Expected locations: " + ObjectUtils.nullSafeToString(this.expectedLocations));
|
||||
logger.debug("Processed locations: " + ObjectUtils.nullSafeToString(processedLocations));
|
||||
}
|
||||
|
||||
assertArrayEquals("Verifying locations for test [" + this.testClass + "].", this.expectedLocations,
|
||||
processedLocations);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:p="http://www.springframework.org/schema/p"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
|
||||
|
||||
<bean id="employee" class="org.springframework.beans.Employee">
|
||||
<property name="name" value="John Smith" />
|
||||
<property name="age" value="42" />
|
||||
<property name="company" value="Acme Widgets, Inc." />
|
||||
</bean>
|
||||
|
||||
<bean id="pet" class="org.springframework.beans.Pet">
|
||||
<constructor-arg value="Fido" />
|
||||
</bean>
|
||||
|
||||
<bean id="foo" class="java.lang.String">
|
||||
<constructor-arg value="Foo" />
|
||||
</bean>
|
||||
|
||||
<bean id="bar" class="java.lang.String">
|
||||
<constructor-arg value="Bar" />
|
||||
</bean>
|
||||
|
||||
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
|
||||
p:driverClassName="org.hsqldb.jdbcDriver" p:url="jdbc:hsqldb:mem:transactional_tests" p:username="sa" p:password="" />
|
||||
|
||||
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
|
||||
p:data-source-ref="dataSource" />
|
||||
|
||||
<bean id="databaseSetup"
|
||||
class="org.springframework.test.context.testng.ConcreteTransactionalTestNGSpringContextTests$DatabaseSetup" />
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,293 @@
|
||||
/*
|
||||
* 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.test.context.testng;
|
||||
|
||||
import static org.springframework.test.transaction.TransactionTestUtils.assertInTransaction;
|
||||
import static org.springframework.test.transaction.TransactionTestUtils.inTransaction;
|
||||
import static org.testng.Assert.assertEquals;
|
||||
import static org.testng.Assert.assertNotNull;
|
||||
import static org.testng.Assert.assertNull;
|
||||
import static org.testng.Assert.assertTrue;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.springframework.beans.Employee;
|
||||
import org.springframework.beans.Pet;
|
||||
import org.springframework.beans.factory.BeanNameAware;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.BadSqlGrammarException;
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
|
||||
import org.springframework.test.annotation.NotTransactional;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.transaction.AfterTransaction;
|
||||
import org.springframework.test.context.transaction.BeforeTransaction;
|
||||
import org.springframework.test.jdbc.SimpleJdbcTestUtils;
|
||||
import org.testng.annotations.AfterClass;
|
||||
import org.testng.annotations.AfterMethod;
|
||||
import org.testng.annotations.BeforeClass;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
* Combined unit test for {@link AbstractTestNGSpringContextTests} and
|
||||
* {@link AbstractTransactionalTestNGSpringContextTests}.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
@Ignore // renamed to Tests_ to avoid being picked up by junit. Spring Build support for TestNG is pending.
|
||||
@ContextConfiguration
|
||||
public class ConcreteTransactionalTestNGSpringContextTests_ extends AbstractTransactionalTestNGSpringContextTests
|
||||
implements BeanNameAware, InitializingBean {
|
||||
|
||||
// ------------------------------------------------------------------------|
|
||||
// --- CONSTANTS ----------------------------------------------------------|
|
||||
// ------------------------------------------------------------------------|
|
||||
|
||||
private static final String BOB = "bob";
|
||||
private static final String JANE = "jane";
|
||||
private static final String SUE = "sue";
|
||||
private static final String YODA = "yoda";
|
||||
|
||||
// ------------------------------------------------------------------------|
|
||||
// --- STATIC VARIABLES ---------------------------------------------------|
|
||||
// ------------------------------------------------------------------------|
|
||||
|
||||
private static int numSetUpCalls = 0;
|
||||
private static int numSetUpCallsInTransaction = 0;
|
||||
private static int numTearDownCalls = 0;
|
||||
private static int numTearDownCallsInTransaction = 0;
|
||||
|
||||
// ------------------------------------------------------------------------|
|
||||
// --- INSTANCE VARIABLES -------------------------------------------------|
|
||||
// ------------------------------------------------------------------------|
|
||||
|
||||
private boolean beanInitialized = false;
|
||||
|
||||
private String beanName = "replace me with [" + getClass().getName() + "]";
|
||||
|
||||
private Employee employee;
|
||||
|
||||
@Autowired
|
||||
private Pet pet;
|
||||
|
||||
@Autowired(required = false)
|
||||
protected Long nonrequiredLong;
|
||||
|
||||
@Resource()
|
||||
protected String foo;
|
||||
|
||||
protected String bar;
|
||||
|
||||
|
||||
// ------------------------------------------------------------------------|
|
||||
// --- STATIC METHODS -----------------------------------------------------|
|
||||
// ------------------------------------------------------------------------|
|
||||
|
||||
private static int clearPersonTable(SimpleJdbcTemplate simpleJdbcTemplate) {
|
||||
return SimpleJdbcTestUtils.deleteFromTables(simpleJdbcTemplate, "person");
|
||||
}
|
||||
|
||||
private static void createPersonTable(SimpleJdbcTemplate simpleJdbcTemplate) {
|
||||
try {
|
||||
simpleJdbcTemplate.update("CREATE TABLE person (name VARCHAR(20) NOT NULL, PRIMARY KEY(name))");
|
||||
}
|
||||
catch (BadSqlGrammarException bsge) {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
|
||||
private static int countRowsInPersonTable(SimpleJdbcTemplate simpleJdbcTemplate) {
|
||||
return SimpleJdbcTestUtils.countRowsInTable(simpleJdbcTemplate, "person");
|
||||
}
|
||||
|
||||
private static int addPerson(SimpleJdbcTemplate simpleJdbcTemplate, String name) {
|
||||
return simpleJdbcTemplate.update("INSERT INTO person VALUES(?)", name);
|
||||
}
|
||||
|
||||
private static int deletePerson(SimpleJdbcTemplate simpleJdbcTemplate, String name) {
|
||||
return simpleJdbcTemplate.update("DELETE FROM person WHERE name=?", name);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------|
|
||||
// --- INSTANCE METHODS ---------------------------------------------------|
|
||||
// ------------------------------------------------------------------------|
|
||||
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
this.beanInitialized = true;
|
||||
}
|
||||
|
||||
public void setBeanName(String beanName) {
|
||||
this.beanName = beanName;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
protected void setEmployee(Employee employee) {
|
||||
this.employee = employee;
|
||||
}
|
||||
|
||||
@Resource
|
||||
protected void setBar(String bar) {
|
||||
this.bar = bar;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------|
|
||||
|
||||
private void assertNumRowsInPersonTable(int expectedNumRows, String testState) {
|
||||
assertEquals(countRowsInPersonTable(this.simpleJdbcTemplate), expectedNumRows,
|
||||
"Verifying the number of rows in the person table (" + testState + ").");
|
||||
}
|
||||
|
||||
private void assertAddPerson(final String name) {
|
||||
assertEquals(addPerson(this.simpleJdbcTemplate, name), 1, "Adding '" + name + "'");
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------|
|
||||
|
||||
@BeforeClass
|
||||
public void beforeClass() {
|
||||
numSetUpCalls = 0;
|
||||
numSetUpCallsInTransaction = 0;
|
||||
numTearDownCalls = 0;
|
||||
numTearDownCallsInTransaction = 0;
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public void afterClass() {
|
||||
assertEquals(numSetUpCalls, 8, "Verifying number of calls to setUp().");
|
||||
assertEquals(numSetUpCallsInTransaction, 1, "Verifying number of calls to setUp() within a transaction.");
|
||||
assertEquals(numTearDownCalls, 8, "Verifying number of calls to tearDown().");
|
||||
assertEquals(numTearDownCallsInTransaction, 1, "Verifying number of calls to tearDown() within a transaction.");
|
||||
}
|
||||
|
||||
@Test
|
||||
@NotTransactional
|
||||
public void verifyApplicationContextSet() {
|
||||
assertInTransaction(false);
|
||||
assertNotNull(super.applicationContext,
|
||||
"The application context should have been set due to ApplicationContextAware semantics.");
|
||||
Employee employeeBean = (Employee) super.applicationContext.getBean("employee");
|
||||
assertEquals(employeeBean.getName(), "John Smith", "Verifying employee's name.");
|
||||
}
|
||||
|
||||
@Test
|
||||
@NotTransactional
|
||||
public void verifyBeanInitialized() {
|
||||
assertInTransaction(false);
|
||||
assertTrue(this.beanInitialized,
|
||||
"This test instance should have been initialized due to InitializingBean semantics.");
|
||||
}
|
||||
|
||||
@Test
|
||||
@NotTransactional
|
||||
public void verifyBeanNameSet() {
|
||||
assertInTransaction(false);
|
||||
assertEquals(this.beanName, getClass().getName(),
|
||||
"The bean name of this test instance should have been set due to BeanNameAware semantics.");
|
||||
}
|
||||
|
||||
@Test
|
||||
@NotTransactional
|
||||
public void verifyAnnotationAutowiredFields() {
|
||||
assertInTransaction(false);
|
||||
assertNull(this.nonrequiredLong, "The nonrequiredLong field should NOT have been autowired.");
|
||||
assertNotNull(this.pet, "The pet field should have been autowired.");
|
||||
assertEquals(this.pet.getName(), "Fido", "Verifying pet's name.");
|
||||
}
|
||||
|
||||
@Test
|
||||
@NotTransactional
|
||||
public void verifyAnnotationAutowiredMethods() {
|
||||
assertInTransaction(false);
|
||||
assertNotNull(this.employee, "The setEmployee() method should have been autowired.");
|
||||
assertEquals(this.employee.getName(), "John Smith", "Verifying employee's name.");
|
||||
}
|
||||
|
||||
@Test
|
||||
@NotTransactional
|
||||
public void verifyResourceAnnotationInjectedFields() {
|
||||
assertInTransaction(false);
|
||||
assertEquals(this.foo, "Foo", "The foo field should have been injected via @Resource.");
|
||||
}
|
||||
|
||||
@Test
|
||||
@NotTransactional
|
||||
public void verifyResourceAnnotationInjectedMethods() {
|
||||
assertInTransaction(false);
|
||||
assertEquals(this.bar, "Bar", "The setBar() method should have been injected via @Resource.");
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------|
|
||||
|
||||
@BeforeTransaction
|
||||
public void beforeTransaction() {
|
||||
assertNumRowsInPersonTable(1, "before a transactional test method");
|
||||
assertAddPerson(YODA);
|
||||
}
|
||||
|
||||
@BeforeMethod
|
||||
public void setUp() throws Exception {
|
||||
numSetUpCalls++;
|
||||
if (inTransaction()) {
|
||||
numSetUpCallsInTransaction++;
|
||||
}
|
||||
assertNumRowsInPersonTable((inTransaction() ? 2 : 1), "before a test method");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void modifyTestDataWithinTransaction() {
|
||||
assertInTransaction(true);
|
||||
assertAddPerson(JANE);
|
||||
assertAddPerson(SUE);
|
||||
assertNumRowsInPersonTable(4, "in modifyTestDataWithinTransaction()");
|
||||
}
|
||||
|
||||
@AfterMethod
|
||||
public void tearDown() throws Exception {
|
||||
numTearDownCalls++;
|
||||
if (inTransaction()) {
|
||||
numTearDownCallsInTransaction++;
|
||||
}
|
||||
assertNumRowsInPersonTable((inTransaction() ? 4 : 1), "after a test method");
|
||||
}
|
||||
|
||||
@AfterTransaction
|
||||
public void afterTransaction() {
|
||||
assertEquals(deletePerson(this.simpleJdbcTemplate, YODA), 1, "Deleting yoda");
|
||||
assertNumRowsInPersonTable(1, "after a transactional test method");
|
||||
}
|
||||
|
||||
|
||||
// ------------------------------------------------------------------------|
|
||||
// --- TYPES --------------------------------------------------------------|
|
||||
// ------------------------------------------------------------------------|
|
||||
|
||||
public static class DatabaseSetup {
|
||||
|
||||
@Autowired
|
||||
void setDataSource(DataSource dataSource) {
|
||||
SimpleJdbcTemplate simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
|
||||
createPersonTable(simpleJdbcTemplate);
|
||||
clearPersonTable(simpleJdbcTemplate);
|
||||
addPerson(simpleJdbcTemplate, BOB);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:p="http://www.springframework.org/schema/p"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
|
||||
|
||||
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
|
||||
p:driverClassName="org.hsqldb.jdbcDriver" p:url="jdbc:hsqldb:mem:transactional_tests" p:username="sa" p:password="" />
|
||||
|
||||
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
|
||||
p:data-source-ref="dataSource" />
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* 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.test.context.testng;
|
||||
|
||||
import static org.springframework.test.transaction.TransactionTestUtils.assertInTransaction;
|
||||
import static org.testng.Assert.assertNotNull;
|
||||
import static org.testng.Assert.assertNotSame;
|
||||
import static org.testng.Assert.assertSame;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestContextManager;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* TestNG based unit test to assess the claim in <a
|
||||
* href="http://opensource.atlassian.com/projects/spring/browse/SPR-3880"
|
||||
* target="_blank">SPR-3880</a> that a "context marked dirty using
|
||||
* {@link DirtiesContext @DirtiesContext} in [a] TestNG based test is not
|
||||
* reloaded in subsequent tests".
|
||||
* </p>
|
||||
* <p>
|
||||
* After careful analysis, it turns out that the {@link ApplicationContext} was
|
||||
* in fact reloaded; however, due to how the test instance was instrumented with
|
||||
* the {@link TestContextManager} in {@link AbstractTestNGSpringContextTests},
|
||||
* dependency injection was not being performed on the test instance between
|
||||
* individual tests. DirtiesContextTransactionalTestNGSpringContextTests
|
||||
* therefore verifies the expected behavior and correct semantics.
|
||||
* </p>
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
@Ignore // renamed to Tests_ to avoid being picked up by junit. Spring Build support for TestNG is pending.
|
||||
@ContextConfiguration
|
||||
public class DirtiesContextTransactionalTestNGSpringContextTests_ extends AbstractTransactionalTestNGSpringContextTests {
|
||||
|
||||
private ApplicationContext dirtiedApplicationContext;
|
||||
|
||||
|
||||
private void performCommonAssertions() {
|
||||
assertInTransaction(true);
|
||||
assertNotNull(super.applicationContext,
|
||||
"The application context should have been set due to ApplicationContextAware semantics.");
|
||||
assertNotNull(super.simpleJdbcTemplate,
|
||||
"The SimpleJdbcTemplate should have been created in setDataSource() via DI for the DataSource.");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void dirtyContext() {
|
||||
performCommonAssertions();
|
||||
this.dirtiedApplicationContext = super.applicationContext;
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = { "dirtyContext" })
|
||||
public void verifyContextWasDirtied() {
|
||||
performCommonAssertions();
|
||||
assertNotSame(super.applicationContext, this.dirtiedApplicationContext,
|
||||
"The application context should have been 'dirtied'.");
|
||||
this.dirtiedApplicationContext = super.applicationContext;
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = { "verifyContextWasDirtied" })
|
||||
public void verifyContextWasNotDirtied() {
|
||||
assertSame(this.applicationContext, this.dirtiedApplicationContext,
|
||||
"The application context should NOT have been 'dirtied'.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:p="http://www.springframework.org/schema/p"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
|
||||
|
||||
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
|
||||
p:driverClassName="org.hsqldb.jdbcDriver" p:url="jdbc:hsqldb:mem:transactional_tests" p:username="sa" p:password="" />
|
||||
|
||||
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
|
||||
p:data-source-ref="dataSource" />
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,207 @@
|
||||
/*
|
||||
* 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.test.context.testng;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
import org.testng.ITestContext;
|
||||
import org.testng.ITestListener;
|
||||
import org.testng.ITestResult;
|
||||
import org.testng.TestNG;
|
||||
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestContext;
|
||||
import org.springframework.test.context.TestExecutionListener;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
import org.springframework.test.context.support.AbstractTestExecutionListener;
|
||||
import org.springframework.test.context.transaction.AfterTransaction;
|
||||
import org.springframework.test.context.transaction.BeforeTransaction;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* JUnit 4 based unit test for verifying that '<em>before</em>' and '<em>after</em>'
|
||||
* methods of {@link TestExecutionListener TestExecutionListeners} as well as
|
||||
* {@link BeforeTransaction @BeforeTransaction} and
|
||||
* {@link AfterTransaction @AfterTransaction} methods can fail a test in a
|
||||
* TestNG environment, as requested in <a
|
||||
* href="http://opensource.atlassian.com/projects/spring/browse/SPR-3960"
|
||||
* target="_blank">SPR-3960</a>.
|
||||
* </p>
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class FailingBeforeAndAfterMethodsTests {
|
||||
|
||||
protected final Class<?> clazz;
|
||||
protected final int expectedTestStartCount;
|
||||
protected final int expectedTestSuccessCount;
|
||||
protected final int expectedFailureCount;
|
||||
protected final int expectedFailedConfigurationsCount;
|
||||
|
||||
|
||||
public FailingBeforeAndAfterMethodsTests(final Class<?> clazz, final int expectedTestStartCount,
|
||||
final int expectedTestSuccessCount, final int expectedFailureCount,
|
||||
final int expectedFailedConfigurationsCount) {
|
||||
this.clazz = clazz;
|
||||
this.expectedTestStartCount = expectedTestStartCount;
|
||||
this.expectedTestSuccessCount = expectedTestSuccessCount;
|
||||
this.expectedFailureCount = expectedFailureCount;
|
||||
this.expectedFailedConfigurationsCount = expectedFailedConfigurationsCount;
|
||||
}
|
||||
|
||||
@Parameters
|
||||
public static Collection<Object[]> testData() {
|
||||
return Arrays.asList(new Object[][] {
|
||||
|
||||
{ AlwaysFailingBeforeTestMethodTestCase.class, 1, 0, 0, 1 },
|
||||
|
||||
{ AlwaysFailingAfterTestMethodTestCase.class, 1, 1, 0, 1 },
|
||||
|
||||
{ FailingBeforeTransactionalTestCase.class, 1, 0, 0, 1 },
|
||||
|
||||
{ FailingAfterTransactionalTestCase.class, 1, 1, 0, 1 }
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void runTestAndAssertCounters() throws Exception {
|
||||
final FailureTrackingTestListener listener = new FailureTrackingTestListener();
|
||||
final TestNG testNG = new TestNG();
|
||||
testNG.addListener(listener);
|
||||
testNG.setTestClasses(new Class<?>[] { this.clazz });
|
||||
testNG.setVerbose(0);
|
||||
testNG.run();
|
||||
|
||||
assertEquals("Verifying number of test starts for test class [" + this.clazz + "].",
|
||||
this.expectedTestStartCount, listener.testStartCount);
|
||||
assertEquals("Verifying number of successful tests for test class [" + this.clazz + "].",
|
||||
this.expectedTestSuccessCount, listener.testSuccessCount);
|
||||
assertEquals("Verifying number of failures for test class [" + this.clazz + "].", this.expectedFailureCount,
|
||||
listener.testFailureCount);
|
||||
assertEquals("Verifying number of failed configurations for test class [" + this.clazz + "].",
|
||||
this.expectedFailedConfigurationsCount, listener.failedConfigurationsCount);
|
||||
}
|
||||
|
||||
|
||||
static class FailureTrackingTestListener implements ITestListener {
|
||||
|
||||
int testStartCount = 0;
|
||||
int testSuccessCount = 0;
|
||||
int testFailureCount = 0;
|
||||
int failedConfigurationsCount = 0;
|
||||
|
||||
|
||||
public void onFinish(ITestContext testContext) {
|
||||
this.failedConfigurationsCount += testContext.getFailedConfigurations().size();
|
||||
}
|
||||
|
||||
public void onStart(ITestContext testContext) {
|
||||
}
|
||||
|
||||
public void onTestFailedButWithinSuccessPercentage(ITestResult testResult) {
|
||||
}
|
||||
|
||||
public void onTestFailure(ITestResult testResult) {
|
||||
this.testFailureCount++;
|
||||
}
|
||||
|
||||
public void onTestSkipped(ITestResult testResult) {
|
||||
}
|
||||
|
||||
public void onTestStart(ITestResult testResult) {
|
||||
this.testStartCount++;
|
||||
}
|
||||
|
||||
public void onTestSuccess(ITestResult testResult) {
|
||||
this.testSuccessCount++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static class AlwaysFailingBeforeTestMethodTestExecutionListener extends AbstractTestExecutionListener {
|
||||
|
||||
@Override
|
||||
public void beforeTestMethod(TestContext testContext) {
|
||||
org.testng.Assert.fail("always failing beforeTestMethod()");
|
||||
}
|
||||
}
|
||||
|
||||
static class AlwaysFailingAfterTestMethodTestExecutionListener extends AbstractTestExecutionListener {
|
||||
|
||||
@Override
|
||||
public void afterTestMethod(TestContext testContext) {
|
||||
org.testng.Assert.fail("always failing afterTestMethod()");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@TestExecutionListeners(value = { AlwaysFailingBeforeTestMethodTestExecutionListener.class }, inheritListeners = false)
|
||||
public static class AlwaysFailingBeforeTestMethodTestCase extends AbstractTestNGSpringContextTests {
|
||||
|
||||
@org.testng.annotations.Test
|
||||
public void testNothing() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@TestExecutionListeners(value = { AlwaysFailingAfterTestMethodTestExecutionListener.class }, inheritListeners = false)
|
||||
public static class AlwaysFailingAfterTestMethodTestCase extends AbstractTestNGSpringContextTests {
|
||||
|
||||
@org.testng.annotations.Test
|
||||
public void testNothing() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ContextConfiguration(locations = { "FailingBeforeAndAfterMethodsTests-context.xml" })
|
||||
public static class FailingBeforeTransactionalTestCase extends AbstractTransactionalTestNGSpringContextTests {
|
||||
|
||||
@org.testng.annotations.Test
|
||||
public void testNothing() {
|
||||
}
|
||||
|
||||
@BeforeTransaction
|
||||
public void beforeTransaction() {
|
||||
org.testng.Assert.fail("always failing beforeTransaction()");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ContextConfiguration(locations = { "FailingBeforeAndAfterMethodsTests-context.xml" })
|
||||
public static class FailingAfterTransactionalTestCase extends AbstractTransactionalTestNGSpringContextTests {
|
||||
|
||||
@org.testng.annotations.Test
|
||||
public void testNothing() {
|
||||
}
|
||||
|
||||
@AfterTransaction
|
||||
public void afterTransaction() {
|
||||
org.testng.Assert.fail("always failing afterTransaction()");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* 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.test.jdbc;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link JdbcTestUtils}.
|
||||
*
|
||||
* @author Thomas Risberg
|
||||
* @since 2.5.4
|
||||
*/
|
||||
public class JdbcTestUtilsTests extends TestCase {
|
||||
|
||||
public void testContainsDelimiters() {
|
||||
assertTrue("test with ';' is wrong", !JdbcTestUtils.containsSqlScriptDelimiters("select 1\n select ';'", ';'));
|
||||
assertTrue("test with delimiter ; is wrong", JdbcTestUtils.containsSqlScriptDelimiters("select 1; select 2", ';'));
|
||||
assertTrue("test with '\\n' is wrong", !JdbcTestUtils.containsSqlScriptDelimiters("select 1; select '\\n\n';", '\n'));
|
||||
assertTrue("test with delimiter \\n is wrong", JdbcTestUtils.containsSqlScriptDelimiters("select 1\n select 2", '\n'));
|
||||
}
|
||||
|
||||
public void testSplitSqlScriptDelimitedWithSemicolon() {
|
||||
String statement1 = "insert into customer (id, name) \n" +
|
||||
"values (1, 'Rod ; Johnson'), (2, 'Adrian \n Collier')";
|
||||
String statement2 = "insert into orders(id, order_date, customer_id) \n" +
|
||||
"values (1, '2008-01-02', 2)";
|
||||
String statement3 = "insert into orders(id, order_date, customer_id) " +
|
||||
"values (1, '2008-01-02', 2)";
|
||||
char delim = ';';
|
||||
String script = statement1 + delim + statement2 + delim + statement3;
|
||||
List statements = new ArrayList();
|
||||
JdbcTestUtils.splitSqlScript(script, delim, statements);
|
||||
assertEquals("wrong number of statements", 3, statements.size());
|
||||
assertEquals("statement 1 not split correctly", statement1, statements.get(0));
|
||||
assertEquals("statement 2 not split correctly", statement2, statements.get(1));
|
||||
assertEquals("statement 3 not split correctly", statement3, statements.get(2));
|
||||
|
||||
}
|
||||
|
||||
public void testSplitSqlScriptDelimitedWithNewLine() {
|
||||
String statement1 = "insert into customer (id, name) " +
|
||||
"values (1, 'Rod ; Johnson'), (2, 'Adrian ; Collier')";
|
||||
String statement2 = "insert into orders(id, order_date, customer_id) " +
|
||||
"values (1, '2008-01-02', 2)";
|
||||
String statement3 = "insert into orders(id, order_date, customer_id) " +
|
||||
"values (1, '2008-01-02', 2)";
|
||||
char delim = '\n';
|
||||
String script = statement1 + delim + statement2 + delim + statement3;
|
||||
List statements = new ArrayList();
|
||||
JdbcTestUtils.splitSqlScript(script, delim, statements);
|
||||
assertEquals("wrong number of statements", 3, statements.size());
|
||||
assertEquals("statement 1 not split correctly", statement1, statements.get(0));
|
||||
assertEquals("statement 2 not split correctly", statement2, statements.get(1));
|
||||
assertEquals("statement 3 not split correctly", statement3, statements.get(2));
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* 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.test.transaction;
|
||||
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
|
||||
/**
|
||||
* Collection of JDK 1.4+ utilities for tests involving transactions. Intended
|
||||
* for internal use within the Spring testing suite.
|
||||
*
|
||||
* <p>All <code>assert*()</code> methods throw {@link AssertionError}s.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
public abstract class TransactionTestUtils {
|
||||
|
||||
/**
|
||||
* Convenience method for determining if a transaction is active for the
|
||||
* current {@link Thread}.
|
||||
* @return <code>true</code> if a transaction is currently active
|
||||
*/
|
||||
public static boolean inTransaction() {
|
||||
return TransactionSynchronizationManager.isActualTransactionActive();
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts whether or not a transaction is active for the current
|
||||
* {@link Thread}.
|
||||
* @param transactionExpected whether or not a transaction is expected
|
||||
* @throws AssertionError if the supplied assertion fails
|
||||
* @see #inTransaction()
|
||||
*/
|
||||
public static void assertInTransaction(boolean transactionExpected) {
|
||||
if (transactionExpected) {
|
||||
assertCondition(inTransaction(), "The current thread should be associated with a transaction.");
|
||||
}
|
||||
else {
|
||||
assertCondition(!inTransaction(), "The current thread should not be associated with a transaction");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fails by throwing an <code>AssertionError</code> with the supplied
|
||||
* <code>message</code>.
|
||||
* @param message the exception message to use
|
||||
* @see #assertCondition(boolean,String)
|
||||
*/
|
||||
private static void fail(String message) throws AssertionError {
|
||||
throw new AssertionError(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert the provided boolean <code>condition</code>, throwing
|
||||
* <code>AssertionError</code> with the supplied <code>message</code> if
|
||||
* the test result is <code>false</code>.
|
||||
* @param condition a boolean expression
|
||||
* @param message the exception message to use if the assertion fails
|
||||
* @throws AssertionError if condition is <code>false</code>
|
||||
* @see #fail(String)
|
||||
*/
|
||||
private static void assertCondition(boolean condition, String message) throws AssertionError {
|
||||
if (!condition) {
|
||||
fail(message);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
/*
|
||||
* 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.test.util;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.test.AssertThrows;
|
||||
import org.springframework.test.util.subpackage.Person;
|
||||
|
||||
/**
|
||||
* JUnit 3.8 based unit tests for {@link ReflectionTestUtils}.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class ReflectionTestUtilsTests extends TestCase {
|
||||
|
||||
protected static final Float PI = new Float((float) 22 / 7);
|
||||
|
||||
|
||||
public void testSetField() throws Exception {
|
||||
final Person person = new Person();
|
||||
|
||||
// Standard
|
||||
|
||||
ReflectionTestUtils.setField(person, "id", new Long(99), long.class);
|
||||
ReflectionTestUtils.setField(person, "name", "Tom");
|
||||
ReflectionTestUtils.setField(person, "age", new Integer(42));
|
||||
ReflectionTestUtils.setField(person, "eyeColor", "blue", String.class);
|
||||
ReflectionTestUtils.setField(person, "likesPets", Boolean.TRUE);
|
||||
ReflectionTestUtils.setField(person, "favoriteNumber", PI, Number.class);
|
||||
|
||||
assertEquals("Verifying that the person's ID (private field in a superclass) was set.", 99, person.getId());
|
||||
assertEquals("Verifying that the person's name (protected field) was set.", "Tom", person.getName());
|
||||
assertEquals("Verifying that the person's age (private field) was set.", 42, person.getAge());
|
||||
assertEquals("Verifying that the person's eye color (package private field) was set.", "blue",
|
||||
person.getEyeColor());
|
||||
assertEquals("Verifying that the person's 'likes pets' flag (package private boolean field) was set.", true,
|
||||
person.likesPets());
|
||||
assertEquals("Verifying that the person's 'favorite number' (package field) was set.", PI,
|
||||
person.getFavoriteNumber());
|
||||
|
||||
assertEquals(new Long(99), ReflectionTestUtils.getField(person, "id"));
|
||||
assertEquals("Tom", ReflectionTestUtils.getField(person, "name"));
|
||||
assertEquals(new Integer(42), ReflectionTestUtils.getField(person, "age"));
|
||||
assertEquals("blue", ReflectionTestUtils.getField(person, "eyeColor"));
|
||||
assertEquals(Boolean.TRUE, ReflectionTestUtils.getField(person, "likesPets"));
|
||||
assertEquals(PI, ReflectionTestUtils.getField(person, "favoriteNumber"));
|
||||
|
||||
// Null - non-primitives
|
||||
|
||||
ReflectionTestUtils.setField(person, "name", null, String.class);
|
||||
ReflectionTestUtils.setField(person, "eyeColor", null, String.class);
|
||||
ReflectionTestUtils.setField(person, "favoriteNumber", null, Number.class);
|
||||
|
||||
assertNull("Verifying that the person's name (protected field) was set.", person.getName());
|
||||
assertNull("Verifying that the person's eye color (package private field) was set.", person.getEyeColor());
|
||||
assertNull("Verifying that the person's 'favorite number' (package field) was set.", person.getFavoriteNumber());
|
||||
|
||||
// Null - primitives
|
||||
|
||||
new AssertThrows(IllegalArgumentException.class,
|
||||
"Calling setField() with NULL for a primitive type should throw an IllegalArgumentException.") {
|
||||
|
||||
public void test() throws Exception {
|
||||
ReflectionTestUtils.setField(person, "id", null, long.class);
|
||||
}
|
||||
}.runTest();
|
||||
|
||||
new AssertThrows(IllegalArgumentException.class,
|
||||
"Calling setField() with NULL for a primitive type should throw an IllegalArgumentException.") {
|
||||
|
||||
public void test() throws Exception {
|
||||
ReflectionTestUtils.setField(person, "age", null, int.class);
|
||||
}
|
||||
}.runTest();
|
||||
|
||||
new AssertThrows(IllegalArgumentException.class,
|
||||
"Calling setField() with NULL for a primitive type should throw an IllegalArgumentException.") {
|
||||
|
||||
public void test() throws Exception {
|
||||
ReflectionTestUtils.setField(person, "likesPets", null, boolean.class);
|
||||
}
|
||||
}.runTest();
|
||||
}
|
||||
|
||||
public void testInvokeSetterMethod() throws Exception {
|
||||
final Person person = new Person();
|
||||
|
||||
// Standard - properties
|
||||
|
||||
ReflectionTestUtils.invokeSetterMethod(person, "id", new Long(99), long.class);
|
||||
ReflectionTestUtils.invokeSetterMethod(person, "name", "Tom");
|
||||
ReflectionTestUtils.invokeSetterMethod(person, "age", new Integer(42));
|
||||
ReflectionTestUtils.invokeSetterMethod(person, "eyeColor", "blue", String.class);
|
||||
ReflectionTestUtils.invokeSetterMethod(person, "likesPets", Boolean.TRUE);
|
||||
ReflectionTestUtils.invokeSetterMethod(person, "favoriteNumber", PI, Number.class);
|
||||
|
||||
assertEquals("Verifying that the person's ID (protected method in a superclass) was set.", 99, person.getId());
|
||||
assertEquals("Verifying that the person's name (private method) was set.", "Tom", person.getName());
|
||||
assertEquals("Verifying that the person's age (protected method) was set.", 42, person.getAge());
|
||||
assertEquals("Verifying that the person's eye color (package private method) was set.", "blue",
|
||||
person.getEyeColor());
|
||||
assertEquals("Verifying that the person's 'likes pets' flag (protected method for a boolean) was set.", true,
|
||||
person.likesPets());
|
||||
assertEquals("Verifying that the person's 'favorite number' (protected method for a Number) was set.", PI,
|
||||
person.getFavoriteNumber());
|
||||
|
||||
assertEquals(new Long(99), ReflectionTestUtils.invokeGetterMethod(person, "id"));
|
||||
assertEquals("Tom", ReflectionTestUtils.invokeGetterMethod(person, "name"));
|
||||
assertEquals(new Integer(42), ReflectionTestUtils.invokeGetterMethod(person, "age"));
|
||||
assertEquals("blue", ReflectionTestUtils.invokeGetterMethod(person, "eyeColor"));
|
||||
assertEquals(Boolean.TRUE, ReflectionTestUtils.invokeGetterMethod(person, "likesPets"));
|
||||
assertEquals(PI, ReflectionTestUtils.invokeGetterMethod(person, "favoriteNumber"));
|
||||
|
||||
// Standard - setter methods
|
||||
|
||||
ReflectionTestUtils.invokeSetterMethod(person, "setId", new Long(1), long.class);
|
||||
ReflectionTestUtils.invokeSetterMethod(person, "setName", "Jerry", String.class);
|
||||
ReflectionTestUtils.invokeSetterMethod(person, "setAge", new Integer(33), int.class);
|
||||
ReflectionTestUtils.invokeSetterMethod(person, "setEyeColor", "green", String.class);
|
||||
ReflectionTestUtils.invokeSetterMethod(person, "setLikesPets", Boolean.FALSE, boolean.class);
|
||||
ReflectionTestUtils.invokeSetterMethod(person, "setFavoriteNumber", new Integer(42), Number.class);
|
||||
|
||||
assertEquals("Verifying that the person's ID (protected method in a superclass) was set.", 1, person.getId());
|
||||
assertEquals("Verifying that the person's name (private method) was set.", "Jerry", person.getName());
|
||||
assertEquals("Verifying that the person's age (protected method) was set.", 33, person.getAge());
|
||||
assertEquals("Verifying that the person's eye color (package private method) was set.", "green",
|
||||
person.getEyeColor());
|
||||
assertEquals("Verifying that the person's 'likes pets' flag (protected method for a boolean) was set.", false,
|
||||
person.likesPets());
|
||||
assertEquals("Verifying that the person's 'favorite number' (protected method for a Number) was set.",
|
||||
new Integer(42), person.getFavoriteNumber());
|
||||
|
||||
assertEquals(new Long(1), ReflectionTestUtils.invokeGetterMethod(person, "getId"));
|
||||
assertEquals("Jerry", ReflectionTestUtils.invokeGetterMethod(person, "getName"));
|
||||
assertEquals(new Integer(33), ReflectionTestUtils.invokeGetterMethod(person, "getAge"));
|
||||
assertEquals("green", ReflectionTestUtils.invokeGetterMethod(person, "getEyeColor"));
|
||||
assertEquals(Boolean.FALSE, ReflectionTestUtils.invokeGetterMethod(person, "likesPets"));
|
||||
assertEquals(new Integer(42), ReflectionTestUtils.invokeGetterMethod(person, "getFavoriteNumber"));
|
||||
|
||||
// Null - non-primitives
|
||||
|
||||
ReflectionTestUtils.invokeSetterMethod(person, "name", null, String.class);
|
||||
ReflectionTestUtils.invokeSetterMethod(person, "eyeColor", null, String.class);
|
||||
ReflectionTestUtils.invokeSetterMethod(person, "favoriteNumber", null, Number.class);
|
||||
|
||||
assertNull("Verifying that the person's name (private method) was set.", person.getName());
|
||||
assertNull("Verifying that the person's eye color (package private method) was set.", person.getEyeColor());
|
||||
assertNull("Verifying that the person's 'favorite number' (protected method for a Number) was set.",
|
||||
person.getFavoriteNumber());
|
||||
|
||||
// Null - primitives
|
||||
|
||||
new AssertThrows(RuntimeException.class,
|
||||
"Calling invokeSetterMethod() with NULL for a primitive type should throw an IllegalArgumentException.") {
|
||||
|
||||
public void test() throws Exception {
|
||||
ReflectionTestUtils.invokeSetterMethod(person, "id", null, long.class);
|
||||
}
|
||||
}.runTest();
|
||||
|
||||
new AssertThrows(RuntimeException.class,
|
||||
"Calling invokeSetterMethod() with NULL for a primitive type should throw an IllegalArgumentException.") {
|
||||
|
||||
public void test() throws Exception {
|
||||
ReflectionTestUtils.invokeSetterMethod(person, "age", null, int.class);
|
||||
}
|
||||
}.runTest();
|
||||
|
||||
new AssertThrows(RuntimeException.class,
|
||||
"Calling invokeSetterMethod() with NULL for a primitive type should throw an IllegalArgumentException.") {
|
||||
|
||||
public void test() throws Exception {
|
||||
ReflectionTestUtils.invokeSetterMethod(person, "likesPets", null, boolean.class);
|
||||
}
|
||||
}.runTest();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 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.test.util.subpackage;
|
||||
|
||||
/**
|
||||
* Abstract base class for <em>persistent entities</em>; intended for use in
|
||||
* unit tests.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
public abstract class PersistentEntity {
|
||||
|
||||
private long id;
|
||||
|
||||
|
||||
public final long getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
protected final void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright 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.test.util.subpackage;
|
||||
|
||||
import org.springframework.core.style.ToStringCreator;
|
||||
|
||||
/**
|
||||
* Concrete subclass of {@link PersistentEntity} representing a <em>person</em>
|
||||
* entity; intended for use in unit tests.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
public class Person extends PersistentEntity {
|
||||
|
||||
protected String name;
|
||||
|
||||
private int age;
|
||||
|
||||
String eyeColor;
|
||||
|
||||
boolean likesPets = false;
|
||||
|
||||
private Number favoriteNumber;
|
||||
|
||||
|
||||
public final String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
private final void setName(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public final int getAge() {
|
||||
return this.age;
|
||||
}
|
||||
|
||||
protected final void setAge(final int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public final String getEyeColor() {
|
||||
return this.eyeColor;
|
||||
}
|
||||
|
||||
final void setEyeColor(final String eyeColor) {
|
||||
this.eyeColor = eyeColor;
|
||||
}
|
||||
|
||||
public final boolean likesPets() {
|
||||
return this.likesPets;
|
||||
}
|
||||
|
||||
protected final void setLikesPets(final boolean likesPets) {
|
||||
this.likesPets = likesPets;
|
||||
}
|
||||
|
||||
public final Number getFavoriteNumber() {
|
||||
return this.favoriteNumber;
|
||||
}
|
||||
|
||||
protected final void setFavoriteNumber(Number favoriteNumber) {
|
||||
this.favoriteNumber = favoriteNumber;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return new ToStringCreator(this)
|
||||
|
||||
.append("id", this.getId())
|
||||
|
||||
.append("name", this.name)
|
||||
|
||||
.append("age", this.age)
|
||||
|
||||
.append("eyeColor", this.eyeColor)
|
||||
|
||||
.append("likesPets", this.likesPets)
|
||||
|
||||
.append("favoriteNumber", this.favoriteNumber)
|
||||
|
||||
.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user