Moved tests from testsuite to beans
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* 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.factory.xml;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.beans.PropertyValue;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
/**
|
||||
* @author Rob Harrop
|
||||
*/
|
||||
public class MetadataAttachmentTests extends TestCase {
|
||||
|
||||
private XmlBeanFactory beanFactory;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
this.beanFactory = new XmlBeanFactory(new ClassPathResource("withMeta.xml", getClass()));
|
||||
}
|
||||
|
||||
public void testMetadataAttachment() throws Exception {
|
||||
BeanDefinition beanDefinition1 = this.beanFactory.getMergedBeanDefinition("testBean1");
|
||||
assertEquals("bar", beanDefinition1.getAttribute("foo"));
|
||||
}
|
||||
|
||||
public void testMetadataIsInherited() throws Exception {
|
||||
BeanDefinition beanDefinition = this.beanFactory.getMergedBeanDefinition("testBean2");
|
||||
assertEquals("Metadata not inherited", "bar", beanDefinition.getAttribute("foo"));
|
||||
assertEquals("Child metdata not attached", "123", beanDefinition.getAttribute("abc"));
|
||||
}
|
||||
|
||||
public void testPropertyMetadata() throws Exception {
|
||||
BeanDefinition beanDefinition = this.beanFactory.getMergedBeanDefinition("testBean3");
|
||||
PropertyValue pv = beanDefinition.getPropertyValues().getPropertyValue("name");
|
||||
assertEquals("Harrop", pv.getAttribute("surname"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* 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.factory.xml;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.xml.sax.SAXParseException;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
/**
|
||||
* @author Rob Harrop
|
||||
*/
|
||||
public class SchemaValidationTests extends TestCase {
|
||||
|
||||
public void testWithAutodetection() throws Exception {
|
||||
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(bf);
|
||||
try {
|
||||
reader.loadBeanDefinitions(new ClassPathResource("invalidPerSchema.xml", getClass()));
|
||||
fail("Should not be able to parse a file with errors");
|
||||
}
|
||||
catch (BeansException ex) {
|
||||
assertTrue(ex.getCause() instanceof SAXParseException);
|
||||
}
|
||||
}
|
||||
|
||||
public void testWithExplicitValidationMode() throws Exception {
|
||||
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(bf);
|
||||
reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);
|
||||
try {
|
||||
reader.loadBeanDefinitions(new ClassPathResource("invalidPerSchema.xml", getClass()));
|
||||
fail("Should not be able to parse a file with errors");
|
||||
}
|
||||
catch (BeansException ex) {
|
||||
assertTrue(ex.getCause() instanceof SAXParseException);
|
||||
}
|
||||
}
|
||||
|
||||
public void testLoadDefinitions() throws Exception {
|
||||
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(bf);
|
||||
reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);
|
||||
reader.loadBeanDefinitions(new ClassPathResource("schemaValidated.xml", getClass()));
|
||||
|
||||
TestBean foo = (TestBean) bf.getBean("fooBean");
|
||||
assertNotNull("Spouse is null", foo.getSpouse());
|
||||
assertEquals("Incorrect number of friends", 2, foo.getFriends().size());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* 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.factory.xml;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.beans.ITestBean;
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.beans.factory.BeanDefinitionStoreException;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
/**
|
||||
* @author Rob Harrop
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class ShortcutTests extends TestCase {
|
||||
|
||||
public void testSimpleBeanConfigured() throws Exception {
|
||||
XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("shortcutTests.xml", getClass()));
|
||||
ITestBean rob = (TestBean) beanFactory.getBean("rob");
|
||||
ITestBean sally = (TestBean) beanFactory.getBean("sally");
|
||||
assertEquals("Rob Harrop", rob.getName());
|
||||
assertEquals(24, rob.getAge());
|
||||
assertEquals(rob.getSpouse(), sally);
|
||||
}
|
||||
|
||||
public void testInnerBeanConfigured() throws Exception {
|
||||
XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("shortcutTests.xml", getClass()));
|
||||
TestBean sally = (TestBean) beanFactory.getBean("sally2");
|
||||
ITestBean rob = (TestBean) sally.getSpouse();
|
||||
assertEquals("Rob Harrop", rob.getName());
|
||||
assertEquals(24, rob.getAge());
|
||||
assertEquals(rob.getSpouse(), sally);
|
||||
}
|
||||
|
||||
public void testWithPropertyDefinedTwice() throws Exception {
|
||||
try {
|
||||
new XmlBeanFactory(new ClassPathResource("shortcutTestsWithErrors.xml", getClass()));
|
||||
fail("Should not be able to load a file with property specified twice.");
|
||||
}
|
||||
catch (BeanDefinitionStoreException e) {
|
||||
// success
|
||||
}
|
||||
}
|
||||
|
||||
public void testPropertyWithNameEndingInRef() throws Exception {
|
||||
XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("shortcutTests.xml", getClass()));
|
||||
ITestBean sally = (TestBean) beanFactory.getBean("derivedSally");
|
||||
assertEquals("r", sally.getSpouse().getName());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,310 @@
|
||||
/*
|
||||
* 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.factory.xml;
|
||||
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.beans.factory.parsing.CollectingReaderEventListener;
|
||||
import org.springframework.beans.factory.parsing.ComponentDefinition;
|
||||
import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
||||
import org.springframework.beans.factory.config.PropertiesFactoryBean;
|
||||
import org.springframework.beans.factory.config.FieldRetrievingFactoryBean;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
/**
|
||||
* @author Rob Harrop
|
||||
* @author Juergen Hoeller
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class UtilNamespaceHandlerTests extends TestCase {
|
||||
|
||||
private DefaultListableBeanFactory beanFactory;
|
||||
|
||||
private CollectingReaderEventListener listener = new CollectingReaderEventListener();
|
||||
|
||||
public void setUp() {
|
||||
this.beanFactory = new DefaultListableBeanFactory();
|
||||
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this.beanFactory);
|
||||
reader.setEventListener(this.listener);
|
||||
reader.loadBeanDefinitions(new ClassPathResource("testUtilNamespace.xml", getClass()));
|
||||
}
|
||||
|
||||
public void testConstant() throws Exception {
|
||||
Integer min = (Integer) this.beanFactory.getBean("min");
|
||||
assertEquals(Integer.MIN_VALUE, min.intValue());
|
||||
}
|
||||
|
||||
public void testConstantWithDefaultName() throws Exception {
|
||||
Integer max = (Integer) this.beanFactory.getBean("java.lang.Integer.MAX_VALUE");
|
||||
assertEquals(Integer.MAX_VALUE, max.intValue());
|
||||
}
|
||||
|
||||
public void testEvents() throws Exception {
|
||||
ComponentDefinition propertiesComponent = this.listener.getComponentDefinition("myProperties");
|
||||
assertNotNull("Event for 'myProperties' not sent", propertiesComponent);
|
||||
AbstractBeanDefinition propertiesBean = (AbstractBeanDefinition) propertiesComponent.getBeanDefinitions()[0];
|
||||
assertEquals("Incorrect BeanDefinition", PropertiesFactoryBean.class, propertiesBean.getBeanClass());
|
||||
|
||||
ComponentDefinition constantComponent = this.listener.getComponentDefinition("min");
|
||||
assertNotNull("Event for 'min' not sent", propertiesComponent);
|
||||
AbstractBeanDefinition constantBean = (AbstractBeanDefinition) constantComponent.getBeanDefinitions()[0];
|
||||
assertEquals("Incorrect BeanDefinition", FieldRetrievingFactoryBean.class, constantBean.getBeanClass());
|
||||
}
|
||||
|
||||
public void testNestedProperties() throws Exception {
|
||||
TestBean bean = (TestBean) this.beanFactory.getBean("testBean");
|
||||
Properties props = bean.getSomeProperties();
|
||||
assertEquals("Incorrect property value", "bar", props.get("foo"));
|
||||
}
|
||||
|
||||
public void testPropertyPath() throws Exception {
|
||||
String name = (String) this.beanFactory.getBean("name");
|
||||
assertEquals("Rob Harrop", name);
|
||||
}
|
||||
|
||||
public void testNestedPropertyPath() throws Exception {
|
||||
TestBean bean = (TestBean) this.beanFactory.getBean("testBean");
|
||||
assertEquals("Rob Harrop", bean.getName());
|
||||
}
|
||||
|
||||
public void testSimpleMap() throws Exception {
|
||||
Map map = (Map) this.beanFactory.getBean("simpleMap");
|
||||
assertEquals("bar", map.get("foo"));
|
||||
Map map2 = (Map) this.beanFactory.getBean("simpleMap");
|
||||
assertTrue(map == map2);
|
||||
}
|
||||
|
||||
public void testScopedMap() throws Exception {
|
||||
Map map = (Map) this.beanFactory.getBean("scopedMap");
|
||||
assertEquals("bar", map.get("foo"));
|
||||
Map map2 = (Map) this.beanFactory.getBean("scopedMap");
|
||||
assertEquals("bar", map2.get("foo"));
|
||||
assertTrue(map != map2);
|
||||
}
|
||||
|
||||
public void testSimpleList() throws Exception {
|
||||
List list = (List) this.beanFactory.getBean("simpleList");
|
||||
assertEquals("Rob Harrop", list.get(0));
|
||||
List list2 = (List) this.beanFactory.getBean("simpleList");
|
||||
assertTrue(list == list2);
|
||||
}
|
||||
|
||||
public void testScopedList() throws Exception {
|
||||
List list = (List) this.beanFactory.getBean("scopedList");
|
||||
assertEquals("Rob Harrop", list.get(0));
|
||||
List list2 = (List) this.beanFactory.getBean("scopedList");
|
||||
assertEquals("Rob Harrop", list2.get(0));
|
||||
assertTrue(list != list2);
|
||||
}
|
||||
|
||||
public void testSimpleSet() throws Exception {
|
||||
Set set = (Set) this.beanFactory.getBean("simpleSet");
|
||||
assertTrue(set.contains("Rob Harrop"));
|
||||
Set set2 = (Set) this.beanFactory.getBean("simpleSet");
|
||||
assertTrue(set == set2);
|
||||
}
|
||||
|
||||
public void testScopedSet() throws Exception {
|
||||
Set set = (Set) this.beanFactory.getBean("scopedSet");
|
||||
assertTrue(set.contains("Rob Harrop"));
|
||||
Set set2 = (Set) this.beanFactory.getBean("scopedSet");
|
||||
assertTrue(set2.contains("Rob Harrop"));
|
||||
assertTrue(set != set2);
|
||||
}
|
||||
|
||||
public void testMapWithRef() throws Exception {
|
||||
Map map = (Map) this.beanFactory.getBean("mapWithRef");
|
||||
assertTrue(map instanceof TreeMap);
|
||||
assertEquals(this.beanFactory.getBean("testBean"), map.get("bean"));
|
||||
}
|
||||
|
||||
public void testNestedCollections() throws Exception {
|
||||
TestBean bean = (TestBean) this.beanFactory.getBean("nestedCollectionsBean");
|
||||
|
||||
List list = bean.getSomeList();
|
||||
assertEquals(1, list.size());
|
||||
assertEquals("foo", list.get(0));
|
||||
|
||||
Set set = bean.getSomeSet();
|
||||
assertEquals(1, set.size());
|
||||
assertTrue(set.contains("bar"));
|
||||
|
||||
Map map = bean.getSomeMap();
|
||||
assertEquals(1, map.size());
|
||||
assertTrue(map.get("foo") instanceof Set);
|
||||
Set innerSet = (Set) map.get("foo");
|
||||
assertEquals(1, innerSet.size());
|
||||
assertTrue(innerSet.contains("bar"));
|
||||
}
|
||||
|
||||
public void testNestedInCollections() throws Exception {
|
||||
TestBean bean = (TestBean) this.beanFactory.getBean("nestedCustomTagBean");
|
||||
Integer min = new Integer(Integer.MIN_VALUE);
|
||||
|
||||
List list = bean.getSomeList();
|
||||
assertEquals(1, list.size());
|
||||
assertEquals(min, list.get(0));
|
||||
|
||||
Set set = bean.getSomeSet();
|
||||
assertEquals(1, set.size());
|
||||
assertTrue(set.contains(min));
|
||||
|
||||
Map map = bean.getSomeMap();
|
||||
assertEquals(1, map.size());
|
||||
assertEquals(min, map.get("min"));
|
||||
}
|
||||
|
||||
public void testCircularCollections() throws Exception {
|
||||
TestBean bean = (TestBean) this.beanFactory.getBean("circularCollectionsBean");
|
||||
|
||||
List list = bean.getSomeList();
|
||||
assertEquals(1, list.size());
|
||||
assertEquals(bean, list.get(0));
|
||||
|
||||
Set set = bean.getSomeSet();
|
||||
assertEquals(1, set.size());
|
||||
assertTrue(set.contains(bean));
|
||||
|
||||
Map map = bean.getSomeMap();
|
||||
assertEquals(1, map.size());
|
||||
assertEquals(bean, map.get("foo"));
|
||||
}
|
||||
|
||||
public void testCircularCollectionBeansStartingWithList() throws Exception {
|
||||
this.beanFactory.getBean("circularList");
|
||||
TestBean bean = (TestBean) this.beanFactory.getBean("circularCollectionBeansBean");
|
||||
|
||||
List list = bean.getSomeList();
|
||||
assertTrue(Proxy.isProxyClass(list.getClass()));
|
||||
assertEquals(1, list.size());
|
||||
assertEquals(bean, list.get(0));
|
||||
|
||||
Set set = bean.getSomeSet();
|
||||
assertFalse(Proxy.isProxyClass(set.getClass()));
|
||||
assertEquals(1, set.size());
|
||||
assertTrue(set.contains(bean));
|
||||
|
||||
Map map = bean.getSomeMap();
|
||||
assertFalse(Proxy.isProxyClass(map.getClass()));
|
||||
assertEquals(1, map.size());
|
||||
assertEquals(bean, map.get("foo"));
|
||||
}
|
||||
|
||||
public void testCircularCollectionBeansStartingWithSet() throws Exception {
|
||||
this.beanFactory.getBean("circularSet");
|
||||
TestBean bean = (TestBean) this.beanFactory.getBean("circularCollectionBeansBean");
|
||||
|
||||
List list = bean.getSomeList();
|
||||
assertFalse(Proxy.isProxyClass(list.getClass()));
|
||||
assertEquals(1, list.size());
|
||||
assertEquals(bean, list.get(0));
|
||||
|
||||
Set set = bean.getSomeSet();
|
||||
assertTrue(Proxy.isProxyClass(set.getClass()));
|
||||
assertEquals(1, set.size());
|
||||
assertTrue(set.contains(bean));
|
||||
|
||||
Map map = bean.getSomeMap();
|
||||
assertFalse(Proxy.isProxyClass(map.getClass()));
|
||||
assertEquals(1, map.size());
|
||||
assertEquals(bean, map.get("foo"));
|
||||
}
|
||||
|
||||
public void testCircularCollectionBeansStartingWithMap() throws Exception {
|
||||
this.beanFactory.getBean("circularMap");
|
||||
TestBean bean = (TestBean) this.beanFactory.getBean("circularCollectionBeansBean");
|
||||
|
||||
List list = bean.getSomeList();
|
||||
assertFalse(Proxy.isProxyClass(list.getClass()));
|
||||
assertEquals(1, list.size());
|
||||
assertEquals(bean, list.get(0));
|
||||
|
||||
Set set = bean.getSomeSet();
|
||||
assertFalse(Proxy.isProxyClass(set.getClass()));
|
||||
assertEquals(1, set.size());
|
||||
assertTrue(set.contains(bean));
|
||||
|
||||
Map map = bean.getSomeMap();
|
||||
assertTrue(Proxy.isProxyClass(map.getClass()));
|
||||
assertEquals(1, map.size());
|
||||
assertEquals(bean, map.get("foo"));
|
||||
}
|
||||
|
||||
public void testNestedInConstructor() throws Exception {
|
||||
TestBean bean = (TestBean) this.beanFactory.getBean("constructedTestBean");
|
||||
assertEquals("Rob Harrop", bean.getName());
|
||||
}
|
||||
|
||||
public void testLoadProperties() throws Exception {
|
||||
Properties props = (Properties) this.beanFactory.getBean("myProperties");
|
||||
assertEquals("Incorrect property value", "bar", props.get("foo"));
|
||||
assertEquals("Incorrect property value", null, props.get("foo2"));
|
||||
Properties props2 = (Properties) this.beanFactory.getBean("myProperties");
|
||||
assertTrue(props == props2);
|
||||
}
|
||||
|
||||
public void testScopedProperties() throws Exception {
|
||||
Properties props = (Properties) this.beanFactory.getBean("myScopedProperties");
|
||||
assertEquals("Incorrect property value", "bar", props.get("foo"));
|
||||
assertEquals("Incorrect property value", null, props.get("foo2"));
|
||||
Properties props2 = (Properties) this.beanFactory.getBean("myScopedProperties");
|
||||
assertEquals("Incorrect property value", "bar", props.get("foo"));
|
||||
assertEquals("Incorrect property value", null, props.get("foo2"));
|
||||
assertTrue(props != props2);
|
||||
}
|
||||
|
||||
public void testLocalProperties() throws Exception {
|
||||
Properties props = (Properties) this.beanFactory.getBean("myLocalProperties");
|
||||
assertEquals("Incorrect property value", null, props.get("foo"));
|
||||
assertEquals("Incorrect property value", "bar2", props.get("foo2"));
|
||||
}
|
||||
|
||||
public void testMergedProperties() throws Exception {
|
||||
Properties props = (Properties) this.beanFactory.getBean("myMergedProperties");
|
||||
assertEquals("Incorrect property value", "bar", props.get("foo"));
|
||||
assertEquals("Incorrect property value", "bar2", props.get("foo2"));
|
||||
}
|
||||
|
||||
public void testLocalOverrideDefault() {
|
||||
Properties props = (Properties) this.beanFactory.getBean("defaultLocalOverrideProperties");
|
||||
assertEquals("Incorrect property value", "bar", props.get("foo"));
|
||||
assertEquals("Incorrect property value", "local2", props.get("foo2"));
|
||||
}
|
||||
|
||||
public void testLocalOverrideFalse() {
|
||||
Properties props = (Properties) this.beanFactory.getBean("falseLocalOverrideProperties");
|
||||
assertEquals("Incorrect property value", "bar", props.get("foo"));
|
||||
assertEquals("Incorrect property value", "local2", props.get("foo2"));
|
||||
}
|
||||
|
||||
public void testLocalOverrideTrue() {
|
||||
Properties props = (Properties) this.beanFactory.getBean("trueLocalOverrideProperties");
|
||||
assertEquals("Incorrect property value", "local", props.get("foo"));
|
||||
assertEquals("Incorrect property value", "local2", props.get("foo2"));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user