Fix [deprecation] compiler warnings

Fix deprecation compiler warnings by refactoring code or applying
@SuppressWarnings("deprecation") annotations. JUnit tests of
internally deprecated classes are now themselves marked as
@Deprecated.

Numerous EasyMock deprecation warnings will remain until the
migration to mockito can be completed.
This commit is contained in:
Phillip Webb 2012-12-31 13:08:39 -08:00
parent 9364043a64
commit 6626a38730
153 changed files with 1665 additions and 1188 deletions

View File

@ -16,13 +16,15 @@
package org.springframework.aop.config;
import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static test.util.TestResourceUtils.qualifiedResource;
import org.junit.Test;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
/**
* @author Mark Fisher
@ -33,7 +35,9 @@ public final class AopNamespaceHandlerPointcutErrorTests {
@Test
public void testDuplicatePointcutConfig() {
try {
new XmlBeanFactory(qualifiedResource(getClass(), "pointcutDuplication.xml"));
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(
qualifiedResource(getClass(), "pointcutDuplication.xml"));
fail("parsing should have caused a BeanDefinitionStoreException");
}
catch (BeanDefinitionStoreException ex) {
@ -44,7 +48,9 @@ public final class AopNamespaceHandlerPointcutErrorTests {
@Test
public void testMissingPointcutConfig() {
try {
new XmlBeanFactory(qualifiedResource(getClass(), "pointcutMissing.xml"));
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(
qualifiedResource(getClass(), "pointcutMissing.xml"));
fail("parsing should have caused a BeanDefinitionStoreException");
}
catch (BeanDefinitionStoreException ex) {

View File

@ -22,7 +22,8 @@ import static test.util.TestResourceUtils.qualifiedResource;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.junit.Test;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.Resource;
/**
@ -37,12 +38,13 @@ public final class PrototypeTargetTests {
@Test
public void testPrototypeProxyWithPrototypeTarget() {
TestBeanImpl.constructionCount = 0;
XmlBeanFactory xbf = new XmlBeanFactory(CONTEXT);
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(CONTEXT);
for (int i = 0; i < 10; i++) {
TestBean tb = (TestBean) xbf.getBean("testBeanPrototype");
TestBean tb = (TestBean) bf.getBean("testBeanPrototype");
tb.doSomething();
}
TestInterceptor interceptor = (TestInterceptor) xbf.getBean("testInterceptor");
TestInterceptor interceptor = (TestInterceptor) bf.getBean("testInterceptor");
assertEquals(10, TestBeanImpl.constructionCount);
assertEquals(10, interceptor.invocationCount);
}
@ -50,12 +52,13 @@ public final class PrototypeTargetTests {
@Test
public void testSingletonProxyWithPrototypeTarget() {
TestBeanImpl.constructionCount = 0;
XmlBeanFactory xbf = new XmlBeanFactory(CONTEXT);
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(CONTEXT);
for (int i = 0; i < 10; i++) {
TestBean tb = (TestBean) xbf.getBean("testBeanSingleton");
TestBean tb = (TestBean) bf.getBean("testBeanSingleton");
tb.doSomething();
}
TestInterceptor interceptor = (TestInterceptor) xbf.getBean("testInterceptor");
TestInterceptor interceptor = (TestInterceptor) bf.getBean("testInterceptor");
assertEquals(1, TestBeanImpl.constructionCount);
assertEquals(10, interceptor.invocationCount);
}

View File

@ -16,12 +16,14 @@
package org.springframework.aop.interceptor;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static test.util.TestResourceUtils.qualifiedResource;
import org.aopalliance.intercept.MethodInvocation;
import org.junit.Test;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.Resource;
import test.beans.ITestBean;
@ -40,7 +42,8 @@ public final class ExposeInvocationInterceptorTests {
@Test
public void testXmlConfig() {
XmlBeanFactory bf = new XmlBeanFactory(CONTEXT);
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(CONTEXT);
ITestBean tb = (ITestBean) bf.getBean("proxy");
String name= "tony";
tb.setName(name);

View File

@ -20,7 +20,8 @@ import static org.junit.Assert.assertSame;
import static test.util.TestResourceUtils.qualifiedResource;
import org.junit.Test;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.Resource;
/**
@ -36,7 +37,8 @@ public final class ScopedProxyAutowireTests {
@Test
public void testScopedProxyInheritsAutowireCandidateFalse() {
XmlBeanFactory bf = new XmlBeanFactory(SCOPED_AUTOWIRE_FALSE_CONTEXT);
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(SCOPED_AUTOWIRE_FALSE_CONTEXT);
TestBean autowired = (TestBean) bf.getBean("autowired");
TestBean unscoped = (TestBean) bf.getBean("unscoped");
assertSame(unscoped, autowired.getChild());
@ -44,7 +46,8 @@ public final class ScopedProxyAutowireTests {
@Test
public void testScopedProxyReplacesAutowireCandidateTrue() {
XmlBeanFactory bf = new XmlBeanFactory(SCOPED_AUTOWIRE_TRUE_CONTEXT);
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(SCOPED_AUTOWIRE_TRUE_CONTEXT);
TestBean autowired = (TestBean) bf.getBean("autowired");
TestBean scoped = (TestBean) bf.getBean("scoped");
assertSame(scoped, autowired.getChild());

View File

@ -21,8 +21,8 @@ import static test.util.TestResourceUtils.qualifiedResource;
import org.junit.Test;
import org.springframework.aop.framework.Advised;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.Resource;
import test.aop.NopInterceptor;
@ -43,7 +43,8 @@ public final class RegexpMethodPointcutAdvisorIntegrationTests {
@Test
public void testSinglePattern() throws Throwable {
BeanFactory bf = new XmlBeanFactory(CONTEXT);
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(CONTEXT);
ITestBean advised = (ITestBean) bf.getBean("settersAdvised");
// Interceptor behind regexp advisor
NopInterceptor nop = (NopInterceptor) bf.getBean("nopInterceptor");
@ -61,7 +62,8 @@ public final class RegexpMethodPointcutAdvisorIntegrationTests {
@Test
public void testMultiplePatterns() throws Throwable {
BeanFactory bf = new XmlBeanFactory(CONTEXT);
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(CONTEXT);
// This is a CGLIB proxy, so we can proxy it to the target class
TestBean advised = (TestBean) bf.getBean("settersAndAbsquatulateAdvised");
// Interceptor behind regexp advisor
@ -84,7 +86,8 @@ public final class RegexpMethodPointcutAdvisorIntegrationTests {
@Test
public void testSerialization() throws Throwable {
BeanFactory bf = new XmlBeanFactory(CONTEXT);
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(CONTEXT);
// This is a CGLIB proxy, so we can proxy it to the target class
Person p = (Person) bf.getBean("serializableSettersAdvised");
// Interceptor behind regexp advisor

View File

@ -16,7 +16,9 @@
package org.springframework.aop.target;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static test.util.TestResourceUtils.qualifiedResource;
import org.junit.After;
@ -25,7 +27,8 @@ import org.junit.Test;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.Resource;
import test.aop.SerializableNopInterceptor;
@ -46,11 +49,12 @@ public final class HotSwappableTargetSourceTests {
/** Initial count value set in bean factory XML */
private static final int INITIAL_COUNT = 10;
private XmlBeanFactory beanFactory;
private DefaultListableBeanFactory beanFactory;
@Before
public void setUp() throws Exception {
this.beanFactory = new XmlBeanFactory(CONTEXT);
this.beanFactory = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(this.beanFactory).loadBeanDefinitions(CONTEXT);
}
/**

View File

@ -16,13 +16,16 @@
package org.springframework.aop.target;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static test.util.TestResourceUtils.qualifiedResource;
import java.util.Set;
import org.junit.Test;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.Resource;
import test.beans.ITestBean;
@ -43,7 +46,8 @@ public final class LazyInitTargetSourceTests {
@Test
public void testLazyInitSingletonTargetSource() {
XmlBeanFactory bf = new XmlBeanFactory(SINGLETON_CONTEXT);
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(SINGLETON_CONTEXT);
bf.preInstantiateSingletons();
ITestBean tb = (ITestBean) bf.getBean("proxy");
@ -54,7 +58,8 @@ public final class LazyInitTargetSourceTests {
@Test
public void testCustomLazyInitSingletonTargetSource() {
XmlBeanFactory bf = new XmlBeanFactory(CUSTOM_TARGET_CONTEXT);
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(CUSTOM_TARGET_CONTEXT);
bf.preInstantiateSingletons();
ITestBean tb = (ITestBean) bf.getBean("proxy");
@ -65,7 +70,8 @@ public final class LazyInitTargetSourceTests {
@Test
public void testLazyInitFactoryBeanTargetSource() {
XmlBeanFactory bf = new XmlBeanFactory(FACTORY_BEAN_CONTEXT);
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(FACTORY_BEAN_CONTEXT);
bf.preInstantiateSingletons();
Set<?> set1 = (Set<?>) bf.getBean("proxy1");

View File

@ -41,10 +41,12 @@ public final class PrototypeBasedTargetSourceTests {
public void testSerializability() throws Exception {
MutablePropertyValues tsPvs = new MutablePropertyValues();
tsPvs.add("targetBeanName", "person");
RootBeanDefinition tsBd = new RootBeanDefinition(TestTargetSource.class, tsPvs);
RootBeanDefinition tsBd = new RootBeanDefinition(TestTargetSource.class);
tsBd.setPropertyValues(tsPvs);
MutablePropertyValues pvs = new MutablePropertyValues();
RootBeanDefinition bd = new RootBeanDefinition(SerializablePerson.class, pvs);
RootBeanDefinition bd = new RootBeanDefinition(SerializablePerson.class);
bd.setPropertyValues(pvs);
bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();

View File

@ -22,7 +22,9 @@ import static test.util.TestResourceUtils.qualifiedResource;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.Resource;
import test.beans.SideEffectBean;
@ -43,7 +45,8 @@ public final class PrototypeTargetSourceTests {
@Before
public void setUp() throws Exception {
this.beanFactory = new XmlBeanFactory(CONTEXT);
this.beanFactory = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader((BeanDefinitionRegistry) this.beanFactory).loadBeanDefinitions(CONTEXT);
}
/**

View File

@ -16,12 +16,15 @@
package org.springframework.aop.target;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import static test.util.TestResourceUtils.qualifiedResource;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.Resource;
import test.beans.ITestBean;
@ -39,11 +42,12 @@ public class ThreadLocalTargetSourceTests {
/** Initial count value set in bean factory XML */
private static final int INITIAL_COUNT = 10;
private XmlBeanFactory beanFactory;
private DefaultListableBeanFactory beanFactory;
@Before
public void setUp() throws Exception {
this.beanFactory = new XmlBeanFactory(CONTEXT);
this.beanFactory = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(this.beanFactory).loadBeanDefinitions(CONTEXT);
}
/**

View File

@ -16,16 +16,18 @@
package org.springframework.mock.staticmock;
import javax.persistence.PersistenceException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.springframework.mock.staticmock.AnnotationDrivenStaticEntityMockingControl.expectReturn;
import static org.springframework.mock.staticmock.AnnotationDrivenStaticEntityMockingControl.expectThrow;
import static org.springframework.mock.staticmock.AnnotationDrivenStaticEntityMockingControl.playback;
import junit.framework.Assert;
import javax.persistence.PersistenceException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import static org.springframework.mock.staticmock.AnnotationDrivenStaticEntityMockingControl.*;
/**
* Test for static entity mocking framework.
@ -43,7 +45,7 @@ public class AnnotationDrivenStaticEntityMockingControlTest {
Person.countPeople();
expectReturn(expectedCount);
playback();
Assert.assertEquals(expectedCount, Person.countPeople());
assertEquals(expectedCount, Person.countPeople());
}
@Test(expected=PersistenceException.class)
@ -61,7 +63,7 @@ public class AnnotationDrivenStaticEntityMockingControlTest {
Person.findPerson(id);
expectReturn(found);
playback();
Assert.assertEquals(found, Person.findPerson(id));
assertEquals(found, Person.findPerson(id));
}
@ -81,10 +83,10 @@ public class AnnotationDrivenStaticEntityMockingControlTest {
expectReturn(0);
playback();
Assert.assertEquals(found1, Person.findPerson(id1));
Assert.assertEquals(found2, Person.findPerson(id2));
Assert.assertEquals(found1, Person.findPerson(id1));
Assert.assertEquals(0, Person.countPeople());
assertEquals(found1, Person.findPerson(id1));
assertEquals(found2, Person.findPerson(id2));
assertEquals(found1, Person.findPerson(id1));
assertEquals(0, Person.countPeople());
}
// Note delegation is used when tests are invalid and should fail, as otherwise
@ -94,7 +96,7 @@ public class AnnotationDrivenStaticEntityMockingControlTest {
public void testArgMethodNoMatchExpectReturn() {
try {
new Delegate().testArgMethodNoMatchExpectReturn();
Assert.fail();
fail();
} catch (IllegalArgumentException expected) {
}
}
@ -105,7 +107,7 @@ public class AnnotationDrivenStaticEntityMockingControlTest {
}
private void called(Person found, long id) {
Assert.assertEquals(found, Person.findPerson(id));
assertEquals(found, Person.findPerson(id));
}
@Test

View File

@ -16,12 +16,12 @@
package org.springframework.mock.staticmock;
import static org.junit.Assert.assertEquals;
import java.rmi.RemoteException;
import javax.persistence.PersistenceException;
import junit.framework.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.mock.staticmock.AnnotationDrivenStaticEntityMockingControl;
@ -40,7 +40,7 @@ public class Delegate {
Person.findPerson(id);
AnnotationDrivenStaticEntityMockingControl.expectReturn(found);
AnnotationDrivenStaticEntityMockingControl.playback();
Assert.assertEquals(found, Person.findPerson(id + 1));
assertEquals(found, Person.findPerson(id + 1));
}
@Test
@ -50,7 +50,7 @@ public class Delegate {
Person.findPerson(id);
AnnotationDrivenStaticEntityMockingControl.expectThrow(new PersistenceException());
AnnotationDrivenStaticEntityMockingControl.playback();
Assert.assertEquals(found, Person.findPerson(id + 1));
assertEquals(found, Person.findPerson(id + 1));
}
@Test
@ -62,7 +62,7 @@ public class Delegate {
Person.countPeople();
AnnotationDrivenStaticEntityMockingControl.expectReturn(25);
AnnotationDrivenStaticEntityMockingControl.playback();
Assert.assertEquals(found, Person.findPerson(id));
assertEquals(found, Person.findPerson(id));
}
@Test

View File

@ -234,6 +234,8 @@ public class CustomEditorConfigurer implements BeanFactoryPostProcessor, BeanCla
this.sharedEditor = sharedEditor;
}
@Override
@SuppressWarnings("deprecation")
public void registerCustomEditors(PropertyEditorRegistry registry) {
if (!(registry instanceof PropertyEditorRegistrySupport)) {
throw new IllegalArgumentException("Cannot registered shared editor " +

View File

@ -716,6 +716,7 @@ public class BeanDefinitionParserDelegate {
}
}
@SuppressWarnings("deprecation")
public int getAutowireMode(String attValue) {
String att = attValue;
if (DEFAULT_VALUE.equals(att)) {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2012 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.
@ -77,7 +77,7 @@ public class ResourceEntityResolver extends DelegatingEntityResolver {
try {
String decodedSystemId = URLDecoder.decode(systemId);
String givenUrl = new URL(decodedSystemId).toString();
String systemRootUrl = new File("").toURL().toString();
String systemRootUrl = new File("").toURI().toURL().toString();
// Try relative to resource base if currently in system root.
if (givenUrl.startsWith(systemRootUrl)) {
resourcePath = givenUrl.substring(systemRootUrl.length());

View File

@ -15,28 +15,30 @@
*/
package com.foo;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import java.util.List;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
/**
* @author Costin Leau
*/
public class ComponentBeanDefinitionParserTest {
private static XmlBeanFactory bf;
private static DefaultListableBeanFactory bf;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
bf = new XmlBeanFactory(new ClassPathResource(
"com/foo/component-config.xml"));
bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(
new ClassPathResource("com/foo/component-config.xml"));
}
@AfterClass
@ -71,4 +73,4 @@ public class ComponentBeanDefinitionParserTest {
assertThat("Karate-1", equalTo(components.get(0).getName()));
assertThat("Sport-1", equalTo(components.get(1).getName()));
}
}
}

View File

@ -16,16 +16,21 @@
package org.springframework.beans.factory;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static test.util.TestResourceUtils.qualifiedResource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.StaticListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.cglib.proxy.NoOp;
import org.springframework.core.io.Resource;
import org.springframework.util.ObjectUtils;
@ -35,9 +40,6 @@ import test.beans.ITestBean;
import test.beans.IndexedTestBean;
import test.beans.TestBean;
import static org.junit.Assert.*;
import static test.util.TestResourceUtils.qualifiedResource;
/**
* @author Rod Johnson
* @author Juergen Hoeller
@ -60,10 +62,17 @@ public final class BeanFactoryUtilsTests {
public void setUp() {
// Interesting hierarchical factory to test counts.
// Slow to read so we cache it.
XmlBeanFactory grandParent = new XmlBeanFactory(ROOT_CONTEXT);
XmlBeanFactory parent = new XmlBeanFactory(MIDDLE_CONTEXT, grandParent);
XmlBeanFactory child = new XmlBeanFactory(LEAF_CONTEXT, parent);
this.dependentBeansBF = new XmlBeanFactory(DEPENDENT_BEANS_CONTEXT);
DefaultListableBeanFactory grandParent = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(grandParent).loadBeanDefinitions(ROOT_CONTEXT);
DefaultListableBeanFactory parent = new DefaultListableBeanFactory(grandParent);
new XmlBeanDefinitionReader(parent).loadBeanDefinitions(MIDDLE_CONTEXT);
DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
new XmlBeanDefinitionReader(child).loadBeanDefinitions(LEAF_CONTEXT);
this.dependentBeansBF = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader((BeanDefinitionRegistry) this.dependentBeansBF).loadBeanDefinitions(DEPENDENT_BEANS_CONTEXT);
dependentBeansBF.preInstantiateSingletons();
this.listableBeanFactory = child;
}

View File

@ -16,7 +16,8 @@
package org.springframework.beans.factory;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static test.util.TestResourceUtils.qualifiedResource;
import java.text.DateFormat;
@ -34,7 +35,8 @@ import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.core.io.Resource;
@ -70,7 +72,8 @@ public final class ConcurrentBeanFactoryTests {
@Before
public void setUp() throws Exception {
XmlBeanFactory factory = new XmlBeanFactory(CONTEXT);
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(factory).loadBeanDefinitions(CONTEXT);
factory.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {

View File

@ -549,7 +549,9 @@ public class DefaultListableBeanFactoryTests {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("spouse", new RuntimeBeanReference("self"));
lbf.registerBeanDefinition("self", new RootBeanDefinition(TestBean.class, pvs));
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.setPropertyValues(pvs);
lbf.registerBeanDefinition("self", bd);
TestBean self = (TestBean) lbf.getBean("self");
assertEquals(self, self.getSpouse());
}
@ -560,7 +562,9 @@ public class DefaultListableBeanFactoryTests {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("ag", "foobar");
lbf.registerBeanDefinition("tb", new RootBeanDefinition(TestBean.class, pvs));
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.setPropertyValues(pvs);
lbf.registerBeanDefinition("tb", bd);
lbf.getBean("tb");
fail("Should throw exception on invalid property");
}
@ -846,7 +850,9 @@ public class DefaultListableBeanFactoryTests {
});
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("myFloat", "1,1");
lbf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class, pvs));
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.setPropertyValues(pvs);
lbf.registerBeanDefinition("testBean", bd);
TestBean testBean = (TestBean) lbf.getBean("testBean");
assertTrue(testBean.getMyFloat().floatValue() == 1.1f);
}
@ -870,7 +876,9 @@ public class DefaultListableBeanFactoryTests {
lbf.setConversionService(conversionService);
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("myFloat", "1,1");
lbf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class, pvs));
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.setPropertyValues(pvs);
lbf.registerBeanDefinition("testBean", bd);
TestBean testBean = (TestBean) lbf.getBean("testBean");
assertTrue(testBean.getMyFloat().floatValue() == 1.1f);
}
@ -887,7 +895,9 @@ public class DefaultListableBeanFactoryTests {
});
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("myFloat", new RuntimeBeanReference("myFloat"));
lbf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class, pvs));
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.setPropertyValues(pvs);
lbf.registerBeanDefinition("testBean", bd);
lbf.registerSingleton("myFloat", "1,1");
TestBean testBean = (TestBean) lbf.getBean("testBean");
assertTrue(testBean.getMyFloat().floatValue() == 1.1f);
@ -990,7 +1000,8 @@ public class DefaultListableBeanFactoryTests {
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("name", "Tony");
pvs.add("age", "48");
RootBeanDefinition bd = new RootBeanDefinition(DependenciesBean.class, pvs);
RootBeanDefinition bd = new RootBeanDefinition(DependenciesBean.class);
bd.setPropertyValues(pvs);
bd.setDependencyCheck(RootBeanDefinition.DEPENDENCY_CHECK_OBJECTS);
bd.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE);
lbf.registerBeanDefinition("test", bd);
@ -1039,7 +1050,8 @@ public class DefaultListableBeanFactoryTests {
bf.registerSingleton("resource1", new UrlResource("http://localhost:8080"));
bf.registerSingleton("resource2", new UrlResource("http://localhost:9090"));
RootBeanDefinition rbd = new RootBeanDefinition(ArrayBean.class, RootBeanDefinition.AUTOWIRE_BY_TYPE);
RootBeanDefinition rbd = new RootBeanDefinition(ArrayBean.class);
rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE);
bf.registerBeanDefinition("arrayBean", rbd);
ArrayBean ab = (ArrayBean) bf.getBean("arrayBean");
@ -1051,7 +1063,8 @@ public class DefaultListableBeanFactoryTests {
public void testArrayPropertyWithOptionalAutowiring() throws MalformedURLException {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
RootBeanDefinition rbd = new RootBeanDefinition(ArrayBean.class, RootBeanDefinition.AUTOWIRE_BY_TYPE);
RootBeanDefinition rbd = new RootBeanDefinition(ArrayBean.class);
rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE);
bf.registerBeanDefinition("arrayBean", rbd);
ArrayBean ab = (ArrayBean) bf.getBean("arrayBean");
@ -1064,7 +1077,8 @@ public class DefaultListableBeanFactoryTests {
bf.registerSingleton("integer1", new Integer(4));
bf.registerSingleton("integer2", new Integer(5));
RootBeanDefinition rbd = new RootBeanDefinition(ArrayBean.class, RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
RootBeanDefinition rbd = new RootBeanDefinition(ArrayBean.class);
rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
bf.registerBeanDefinition("arrayBean", rbd);
ArrayBean ab = (ArrayBean) bf.getBean("arrayBean");
@ -1076,7 +1090,8 @@ public class DefaultListableBeanFactoryTests {
public void testArrayConstructorWithOptionalAutowiring() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
RootBeanDefinition rbd = new RootBeanDefinition(ArrayBean.class, RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
RootBeanDefinition rbd = new RootBeanDefinition(ArrayBean.class);
rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
bf.registerBeanDefinition("arrayBean", rbd);
ArrayBean ab = (ArrayBean) bf.getBean("arrayBean");
@ -1091,7 +1106,8 @@ public class DefaultListableBeanFactoryTests {
bf.registerSingleton("resource1", new UrlResource("http://localhost:8080"));
bf.registerSingleton("resource2", new UrlResource("http://localhost:9090"));
RootBeanDefinition rbd = new RootBeanDefinition(ArrayBean.class, RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
RootBeanDefinition rbd = new RootBeanDefinition(ArrayBean.class);
rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
bf.registerBeanDefinition("arrayBean", rbd);
ArrayBean ab = (ArrayBean) bf.getBean("arrayBean");
@ -1107,7 +1123,8 @@ public class DefaultListableBeanFactoryTests {
bf.registerSingleton("resource1", new UrlResource("http://localhost:8080"));
bf.registerSingleton("resource2", new UrlResource("http://localhost:9090"));
RootBeanDefinition rbd = new RootBeanDefinition(ArrayBean.class, RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
RootBeanDefinition rbd = new RootBeanDefinition(ArrayBean.class);
rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
bf.registerBeanDefinition("arrayBean", rbd);
ArrayBean ab = (ArrayBean) bf.getBean("arrayBean");
@ -1121,7 +1138,7 @@ public class DefaultListableBeanFactoryTests {
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
lbf.registerBeanDefinition("rod", bd);
assertEquals(1, lbf.getBeanDefinitionCount());
Object registered = lbf.autowire(NoDependencies.class, AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT, false);
Object registered = lbf.autowire(NoDependencies.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
assertEquals(1, lbf.getBeanDefinitionCount());
assertTrue(registered instanceof NoDependencies);
}
@ -1131,11 +1148,12 @@ public class DefaultListableBeanFactoryTests {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("name", "Rod");
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, pvs);
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.setPropertyValues(pvs);
lbf.registerBeanDefinition("rod", bd);
assertEquals(1, lbf.getBeanDefinitionCount());
// Depends on age, name and spouse (TestBean)
Object registered = lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT, true);
Object registered = lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
assertEquals(1, lbf.getBeanDefinitionCount());
DependenciesBean kerry = (DependenciesBean) registered;
TestBean rod = (TestBean) lbf.getBean("rod");
@ -1147,10 +1165,11 @@ public class DefaultListableBeanFactoryTests {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("name", "Rod");
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, pvs);
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.setPropertyValues(pvs);
lbf.registerBeanDefinition("rod", bd);
assertEquals(1, lbf.getBeanDefinitionCount());
Object registered = lbf.autowire(ConstructorDependency.class, AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT, false);
Object registered = lbf.autowire(ConstructorDependency.class, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false);
assertEquals(1, lbf.getBeanDefinitionCount());
ConstructorDependency kerry = (ConstructorDependency) registered;
TestBean rod = (TestBean) lbf.getBean("rod");
@ -1165,7 +1184,7 @@ public class DefaultListableBeanFactoryTests {
RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
lbf.registerBeanDefinition("rod2", bd2);
try {
lbf.autowire(ConstructorDependency.class, AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT, false);
lbf.autowire(ConstructorDependency.class, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false);
fail("Should have thrown UnsatisfiedDependencyException");
}
catch (UnsatisfiedDependencyException ex) {
@ -1180,11 +1199,12 @@ public class DefaultListableBeanFactoryTests {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("name", "Rod"));
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, pvs);
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.setPropertyValues(pvs);
lbf.registerBeanDefinition("rod", bd);
assertEquals(1, lbf.getBeanDefinitionCount());
try {
lbf.autowire(UnsatisfiedConstructorDependency.class, AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT, true);
lbf.autowire(UnsatisfiedConstructorDependency.class, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, true);
fail("Should have unsatisfied constructor dependency on SideEffectBean");
}
catch (UnsatisfiedDependencyException ex) {
@ -1467,7 +1487,9 @@ public class DefaultListableBeanFactoryTests {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("age", "99");
lbf.registerBeanDefinition("test", new RootBeanDefinition(TestBean.class, pvs));
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.setPropertyValues(pvs);
lbf.registerBeanDefinition("test", bd);
TestBean tb = new TestBean();
assertEquals(0, tb.getAge());
lbf.applyBeanPropertyValues(tb, "test");
@ -1479,7 +1501,9 @@ public class DefaultListableBeanFactoryTests {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("age", "99");
lbf.registerBeanDefinition("test", new RootBeanDefinition(null, pvs));
RootBeanDefinition bd = new RootBeanDefinition();
bd.setPropertyValues(pvs);
lbf.registerBeanDefinition("test", bd);
TestBean tb = new TestBean();
assertEquals(0, tb.getAge());
lbf.applyBeanPropertyValues(tb, "test");
@ -1493,7 +1517,9 @@ public class DefaultListableBeanFactoryTests {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("age", "99");
lbf.registerBeanDefinition("test", new RootBeanDefinition(TestBean.class, pvs));
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.setPropertyValues(pvs);
lbf.registerBeanDefinition("test", bd);
TestBean tb = new TestBean();
assertEquals(0, tb.getAge());
lbf.configureBean(tb, "test");
@ -1509,7 +1535,9 @@ public class DefaultListableBeanFactoryTests {
lbf.registerBeanDefinition("spouse", bd);
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("age", "99");
lbf.registerBeanDefinition("test", new RootBeanDefinition(TestBean.class, RootBeanDefinition.AUTOWIRE_BY_NAME));
RootBeanDefinition tbd = new RootBeanDefinition(TestBean.class);
tbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_NAME);
lbf.registerBeanDefinition("test", tbd);
TestBean tb = new TestBean();
lbf.configureBean(tb, "test");
assertSame(lbf, tb.getBeanFactory());
@ -1523,7 +1551,8 @@ public class DefaultListableBeanFactoryTests {
for (int i = 0; i < 1000; i++) {
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("spouse", new RuntimeBeanReference("bean" + (i < 99 ? i + 1 : 0))));
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, pvs);
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.setPropertyValues(pvs);
lbf.registerBeanDefinition("bean" + i, bd);
}
lbf.preInstantiateSingletons();
@ -1537,7 +1566,9 @@ public class DefaultListableBeanFactoryTests {
@Test
public void testCircularReferenceThroughAutowiring() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
lbf.registerBeanDefinition("test", new RootBeanDefinition(ConstructorDependencyBean.class, RootBeanDefinition.AUTOWIRE_CONSTRUCTOR));
RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependencyBean.class);
bd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
lbf.registerBeanDefinition("test", bd);
try {
lbf.preInstantiateSingletons();
fail("Should have thrown UnsatisfiedDependencyException");
@ -1549,7 +1580,9 @@ public class DefaultListableBeanFactoryTests {
@Test
public void testCircularReferenceThroughFactoryBeanAutowiring() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
lbf.registerBeanDefinition("test", new RootBeanDefinition(ConstructorDependencyFactoryBean.class, RootBeanDefinition.AUTOWIRE_CONSTRUCTOR));
RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependencyFactoryBean.class);
bd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
lbf.registerBeanDefinition("test", bd);
try {
lbf.preInstantiateSingletons();
fail("Should have thrown UnsatisfiedDependencyException");
@ -1561,7 +1594,9 @@ public class DefaultListableBeanFactoryTests {
@Test
public void testCircularReferenceThroughFactoryBeanTypeCheck() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
lbf.registerBeanDefinition("test", new RootBeanDefinition(ConstructorDependencyFactoryBean.class, RootBeanDefinition.AUTOWIRE_CONSTRUCTOR));
RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependencyFactoryBean.class);
bd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
lbf.registerBeanDefinition("test", bd);
try {
lbf.getBeansOfType(String.class);
fail("Should have thrown UnsatisfiedDependencyException");
@ -1573,9 +1608,12 @@ public class DefaultListableBeanFactoryTests {
@Test
public void testAvoidCircularReferenceThroughAutowiring() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
lbf.registerBeanDefinition("test", new RootBeanDefinition(ConstructorDependencyFactoryBean.class, RootBeanDefinition.AUTOWIRE_CONSTRUCTOR));
lbf.registerBeanDefinition("string",
new RootBeanDefinition(String.class, RootBeanDefinition.AUTOWIRE_CONSTRUCTOR));
RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependencyFactoryBean.class);
bd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
lbf.registerBeanDefinition("test", bd);
RootBeanDefinition bd2 = new RootBeanDefinition(String.class);
bd2.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
lbf.registerBeanDefinition("string", bd2);
lbf.preInstantiateSingletons();
}

View File

@ -22,9 +22,10 @@ import static org.junit.Assert.assertThat;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.AbstractFactoryBean;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
/**
@ -37,7 +38,8 @@ public class FactoryBeanLookupTests {
@Before
public void setUp() {
beanFactory = new XmlBeanFactory(
beanFactory = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader((BeanDefinitionRegistry) beanFactory).loadBeanDefinitions(
new ClassPathResource("FactoryBeanLookupTests-context.xml", this.getClass()));
}

View File

@ -16,12 +16,15 @@
package org.springframework.beans.factory;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static test.util.TestResourceUtils.qualifiedResource;
import org.junit.Test;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
@ -38,14 +41,16 @@ public final class FactoryBeanTests {
@Test
public void testFactoryBeanReturnsNull() throws Exception {
XmlBeanFactory factory = new XmlBeanFactory(RETURNS_NULL_CONTEXT);
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(factory).loadBeanDefinitions(RETURNS_NULL_CONTEXT);
Object result = factory.getBean("factoryBean");
assertNull(result);
}
@Test
public void testFactoryBeansWithAutowiring() throws Exception {
XmlBeanFactory factory = new XmlBeanFactory(WITH_AUTOWIRING_CONTEXT);
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(factory).loadBeanDefinitions(WITH_AUTOWIRING_CONTEXT);
BeanFactoryPostProcessor ppc = (BeanFactoryPostProcessor) factory.getBean("propertyPlaceholderConfigurer");
ppc.postProcessBeanFactory(factory);
@ -62,7 +67,8 @@ public final class FactoryBeanTests {
@Test
public void testFactoryBeansWithIntermediateFactoryBeanAutowiringFailure() throws Exception {
XmlBeanFactory factory = new XmlBeanFactory(WITH_AUTOWIRING_CONTEXT);
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(factory).loadBeanDefinitions(WITH_AUTOWIRING_CONTEXT);
BeanFactoryPostProcessor ppc = (BeanFactoryPostProcessor) factory.getBean("propertyPlaceholderConfigurer");
ppc.postProcessBeanFactory(factory);

View File

@ -16,6 +16,14 @@
package org.springframework.beans.factory.annotation;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.Serializable;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
@ -25,15 +33,11 @@ import java.util.List;
import java.util.Map;
import org.junit.Test;
import test.beans.ITestBean;
import test.beans.IndexedTestBean;
import test.beans.NestedTestBean;
import test.beans.TestBean;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AutowireCandidateQualifier;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
@ -41,7 +45,10 @@ import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.util.SerializationTestUtils;
import static org.junit.Assert.*;
import test.beans.ITestBean;
import test.beans.IndexedTestBean;
import test.beans.NestedTestBean;
import test.beans.TestBean;
/**
* Unit tests for {@link AutowiredAnnotationBeanPostProcessor}.
@ -608,7 +615,9 @@ public final class AutowiredAnnotationBeanPostProcessorTests {
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
bpp.setBeanFactory(bf);
bf.addBeanPostProcessor(bpp);
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryInjectionBean.class, false));
RootBeanDefinition annotatedBeanDefinition = new RootBeanDefinition(ObjectFactoryInjectionBean.class);
annotatedBeanDefinition.setScope(BeanDefinition.SCOPE_PROTOTYPE);
bf.registerBeanDefinition("annotatedBean", annotatedBeanDefinition);
bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class));
ObjectFactoryInjectionBean bean = (ObjectFactoryInjectionBean) bf.getBean("annotatedBean");

View File

@ -32,6 +32,7 @@ import test.beans.TestBean;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AutowireCandidateQualifier;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
@ -406,7 +407,9 @@ public class InjectAnnotationBeanPostProcessorTests {
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
bpp.setBeanFactory(bf);
bf.addBeanPostProcessor(bpp);
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryQualifierFieldInjectionBean.class, false));
RootBeanDefinition annotatedBeanDefinition = new RootBeanDefinition(ObjectFactoryQualifierFieldInjectionBean.class);
annotatedBeanDefinition.setScope(BeanDefinition.SCOPE_PROTOTYPE);
bf.registerBeanDefinition("annotatedBean", annotatedBeanDefinition);
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "testBean"));
bf.registerBeanDefinition("testBean", bd);
@ -426,7 +429,9 @@ public class InjectAnnotationBeanPostProcessorTests {
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
bpp.setBeanFactory(bf);
bf.addBeanPostProcessor(bpp);
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryQualifierMethodInjectionBean.class, false));
RootBeanDefinition annotatedBeanDefinition = new RootBeanDefinition(ObjectFactoryQualifierMethodInjectionBean.class);
annotatedBeanDefinition.setScope(BeanDefinition.SCOPE_PROTOTYPE);
bf.registerBeanDefinition("annotatedBean", annotatedBeanDefinition);
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "testBean"));
bf.registerBeanDefinition("testBean", bd);

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2012 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.
@ -27,6 +27,7 @@ import org.junit.Test;
* @author Rick Evans
* @author Chris Beams
*/
@SuppressWarnings("deprecation")
public final class CommonsLogFactoryBeanTests {
@Test

View File

@ -62,10 +62,14 @@ public final class CustomEditorConfigurerTests {
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("date", "2.12.1975");
bf.registerBeanDefinition("tb1", new RootBeanDefinition(TestBean.class, pvs));
RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
bd1.setPropertyValues(pvs);
bf.registerBeanDefinition("tb1", bd1);
pvs = new MutablePropertyValues();
pvs.add("someMap[myKey]", new TypedStringValue("2.12.1975", Date.class));
bf.registerBeanDefinition("tb2", new RootBeanDefinition(TestBean.class, pvs));
RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
bd2.setPropertyValues(pvs);
bf.registerBeanDefinition("tb2", bd2);
TestBean tb1 = (TestBean) bf.getBean("tb1");
assertEquals(df.parse("2.12.1975"), tb1.getDate());
@ -85,10 +89,14 @@ public final class CustomEditorConfigurerTests {
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("date", "2.12.1975");
bf.registerBeanDefinition("tb1", new RootBeanDefinition(TestBean.class, pvs));
RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
bd1.setPropertyValues(pvs);
bf.registerBeanDefinition("tb1", bd1);
pvs = new MutablePropertyValues();
pvs.add("someMap[myKey]", new TypedStringValue("2.12.1975", Date.class));
bf.registerBeanDefinition("tb2", new RootBeanDefinition(TestBean.class, pvs));
RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
bd2.setPropertyValues(pvs);
bf.registerBeanDefinition("tb2", bd2);
TestBean tb1 = (TestBean) bf.getBean("tb1");
assertEquals(df.parse("2.12.1975"), tb1.getDate());
@ -107,7 +115,9 @@ public final class CustomEditorConfigurerTests {
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("date", "2.12.1975");
bf.registerBeanDefinition("tb", new RootBeanDefinition(TestBean.class, pvs));
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.setPropertyValues(pvs);
bf.registerBeanDefinition("tb", bd);
TestBean tb = (TestBean) bf.getBean("tb");
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMAN);
@ -125,7 +135,9 @@ public final class CustomEditorConfigurerTests {
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("date", "2.12.1975");
bf.registerBeanDefinition("tb", new RootBeanDefinition(TestBean.class, pvs));
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.setPropertyValues(pvs);
bf.registerBeanDefinition("tb", bd);
TestBean tb = (TestBean) bf.getBean("tb");
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMAN);
@ -143,7 +155,9 @@ public final class CustomEditorConfigurerTests {
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("stringArray", "xxx");
bf.registerBeanDefinition("tb", new RootBeanDefinition(TestBean.class, pvs));
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.setPropertyValues(pvs);
bf.registerBeanDefinition("tb", bd);
TestBean tb = (TestBean) bf.getBean("tb");
assertTrue(tb.getStringArray() != null && tb.getStringArray().length == 1);

View File

@ -38,6 +38,7 @@ public class DeprecatedBeanWarnerTests {
@Test
@SuppressWarnings("deprecation")
public void postProcess() {
beanFactory = new DefaultListableBeanFactory();
BeanDefinition def = new RootBeanDefinition(MyDeprecatedBean.class);

View File

@ -16,21 +16,25 @@
package org.springframework.beans.factory.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static test.util.TestResourceUtils.qualifiedResource;
import java.util.Date;
import javax.inject.Provider;
import org.junit.After;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import org.junit.Before;
import org.junit.Test;
import static test.util.TestResourceUtils.*;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.Resource;
import org.springframework.util.SerializationTestUtils;
@ -45,11 +49,12 @@ public class ObjectFactoryCreatingFactoryBeanTests {
private static final Resource CONTEXT =
qualifiedResource(ObjectFactoryCreatingFactoryBeanTests.class, "context.xml");
private XmlBeanFactory beanFactory;
private DefaultListableBeanFactory beanFactory;
@Before
public void setUp() {
this.beanFactory = new XmlBeanFactory(CONTEXT);
this.beanFactory = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(this.beanFactory).loadBeanDefinitions(CONTEXT);
this.beanFactory.setSerializationId("test");
}

View File

@ -16,11 +16,15 @@
package org.springframework.beans.factory.config;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static test.util.TestResourceUtils.qualifiedResource;
import org.junit.Test;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.Resource;
import test.beans.ITestBean;
@ -39,7 +43,8 @@ public class PropertyPathFactoryBeanTests {
@Test
public void testPropertyPathFactoryBeanWithSingletonResult() {
XmlBeanFactory xbf = new XmlBeanFactory(CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONTEXT);
assertEquals(new Integer(12), xbf.getBean("propertyPath1"));
assertEquals(new Integer(11), xbf.getBean("propertyPath2"));
assertEquals(new Integer(10), xbf.getBean("tb.age"));
@ -53,7 +58,8 @@ public class PropertyPathFactoryBeanTests {
@Test
public void testPropertyPathFactoryBeanWithPrototypeResult() {
XmlBeanFactory xbf = new XmlBeanFactory(CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONTEXT);
assertNull(xbf.getType("tb.spouse"));
assertEquals(TestBean.class, xbf.getType("propertyPath3"));
Object result1 = xbf.getBean("tb.spouse");
@ -72,14 +78,16 @@ public class PropertyPathFactoryBeanTests {
@Test
public void testPropertyPathFactoryBeanWithNullResult() {
XmlBeanFactory xbf = new XmlBeanFactory(CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONTEXT);
assertNull(xbf.getType("tb.spouse.spouse"));
assertNull(xbf.getBean("tb.spouse.spouse"));
}
@Test
public void testPropertyPathFactoryBeanAsInnerBean() {
XmlBeanFactory xbf = new XmlBeanFactory(CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONTEXT);
TestBean spouse = (TestBean) xbf.getBean("otb.spouse");
TestBean tbWithInner = (TestBean) xbf.getBean("tbWithInner");
assertSame(spouse, tbWithInner.getSpouse());

View File

@ -371,7 +371,8 @@ public final class PropertyResourceConfigurerTests {
pvs2.add("name", "name${var}${var}${");
pvs2.add("spouse", new RuntimeBeanReference("${ref}"));
pvs2.add("someMap", singletonMap);
RootBeanDefinition parent = new RootBeanDefinition(TestBean.class, pvs1);
RootBeanDefinition parent = new RootBeanDefinition(TestBean.class);
parent.setPropertyValues(pvs1);
ChildBeanDefinition bd = new ChildBeanDefinition("${parent}", pvs2);
factory.registerBeanDefinition("parent1", parent);
factory.registerBeanDefinition("tb1", bd);
@ -382,7 +383,8 @@ public final class PropertyResourceConfigurerTests {
pvs.add("name", "name${var}${var}${");
pvs.add("spouse", new RuntimeBeanReference("${ref}"));
pvs.add("someMap", singletonMap);
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, pvs);
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.setPropertyValues(pvs);
factory.registerBeanDefinition("tb1", bd);
}
@ -412,7 +414,9 @@ public final class PropertyResourceConfigurerTests {
someMap.put("key2", "${age}name");
MutablePropertyValues innerPvs = new MutablePropertyValues();
innerPvs.add("touchy", "${os.name}");
someMap.put("key3", new RootBeanDefinition(TestBean.class, innerPvs));
RootBeanDefinition innerBd = new RootBeanDefinition(TestBean.class);
innerBd.setPropertyValues(innerPvs);
someMap.put("key3", innerBd);
MutablePropertyValues innerPvs2 = new MutablePropertyValues(innerPvs);
someMap.put("${key4}", new BeanDefinitionHolder(new ChildBeanDefinition("tb1", innerPvs2), "child"));
pvs.add("someMap", someMap);

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2012 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.
@ -20,6 +20,8 @@ import java.util.Arrays;
import junit.framework.TestCase;
import org.springframework.beans.factory.config.BeanDefinition;
import test.beans.TestBean;
/**
@ -31,7 +33,8 @@ public class BeanDefinitionBuilderTests extends TestCase {
public void testBeanClassWithSimpleProperty() {
String[] dependsOn = new String[] { "A", "B", "C" };
BeanDefinitionBuilder bdb = BeanDefinitionBuilder.rootBeanDefinition(TestBean.class);
bdb.setSingleton(false).addPropertyReference("age", "15");
bdb.setScope(BeanDefinition.SCOPE_PROTOTYPE);
bdb.addPropertyReference("age", "15");
for (int i = 0; i < dependsOn.length; i++) {
bdb.addDependsOn(dependsOn[i]);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2006 the original author or authors.
* Copyright 2002-2012 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.
@ -18,6 +18,7 @@ package org.springframework.beans.factory.support;
import junit.framework.TestCase;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import test.beans.TestBean;
@ -120,10 +121,11 @@ public class BeanDefinitionTests extends TestCase {
bd.getPropertyValues().add("name", "myName");
bd.getPropertyValues().add("age", "99");
ChildBeanDefinition childBd = new ChildBeanDefinition("bd");
GenericBeanDefinition childBd = new GenericBeanDefinition();
childBd.setParentName("bd");
RootBeanDefinition mergedBd = new RootBeanDefinition(bd);
mergedBd.overrideFrom(childBd);
mergedBd.overrideFrom((BeanDefinition) childBd);
assertEquals(2, mergedBd.getConstructorArgumentValues().getArgumentCount());
assertEquals(2, mergedBd.getPropertyValues().size());
assertEquals(bd, mergedBd);

View File

@ -16,6 +16,13 @@
package org.springframework.beans.factory.support;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
@ -28,24 +35,21 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.junit.Assert.*;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.UrlResource;
import test.beans.GenericBean;
import test.beans.GenericIntegerBean;
import test.beans.GenericSetOfIntegerBean;
import test.beans.TestBean;
import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.UrlResource;
/**
* @author Juergen Hoeller
* @author Chris Beams
@ -94,7 +98,8 @@ public class BeanFactoryGenericsTests {
bf.registerSingleton("resource1", new UrlResource("http://localhost:8080"));
bf.registerSingleton("resource2", new UrlResource("http://localhost:9090"));
RootBeanDefinition rbd = new RootBeanDefinition(GenericIntegerBean.class, RootBeanDefinition.AUTOWIRE_BY_TYPE);
RootBeanDefinition rbd = new RootBeanDefinition(GenericIntegerBean.class);
rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE);
bf.registerBeanDefinition("genericBean", rbd);
GenericIntegerBean gb = (GenericIntegerBean) bf.getBean("genericBean");
@ -126,7 +131,8 @@ public class BeanFactoryGenericsTests {
public void testGenericListPropertyWithOptionalAutowiring() throws MalformedURLException {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class, RootBeanDefinition.AUTOWIRE_BY_TYPE);
RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class);
rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE);
bf.registerBeanDefinition("genericBean", rbd);
GenericBean<?> gb = (GenericBean<?>) bf.getBean("genericBean");
@ -152,7 +158,9 @@ public class BeanFactoryGenericsTests {
@Test
public void testGenericListOfArraysProperty() throws MalformedURLException {
XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("genericBeanTests.xml", getClass()));
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(
new ClassPathResource("genericBeanTests.xml", getClass()));
GenericBean<?> gb = (GenericBean<?>) bf.getBean("listOfArrays");
assertEquals(1, gb.getListOfArrays().size());
@ -186,7 +194,8 @@ public class BeanFactoryGenericsTests {
bf.registerSingleton("integer1", new Integer(4));
bf.registerSingleton("integer2", new Integer(5));
RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class, RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class);
rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
bf.registerBeanDefinition("genericBean", rbd);
GenericBean<?> gb = (GenericBean<?>) bf.getBean("genericBean");
@ -198,7 +207,8 @@ public class BeanFactoryGenericsTests {
public void testGenericSetConstructorWithOptionalAutowiring() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class, RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class);
rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
bf.registerBeanDefinition("genericBean", rbd);
GenericBean<?> gb = (GenericBean<?>) bf.getBean("genericBean");
@ -236,7 +246,8 @@ public class BeanFactoryGenericsTests {
bf.registerSingleton("resource1", new UrlResource("http://localhost:8080"));
bf.registerSingleton("resource2", new UrlResource("http://localhost:9090"));
RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class, RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class);
rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
bf.registerBeanDefinition("genericBean", rbd);
GenericBean<?> gb = (GenericBean<?>) bf.getBean("genericBean");
@ -252,7 +263,8 @@ public class BeanFactoryGenericsTests {
bf.registerSingleton("resource1", new UrlResource("http://localhost:8080"));
bf.registerSingleton("resource2", new UrlResource("http://localhost:9090"));
RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class, RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class);
rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
bf.registerBeanDefinition("genericBean", rbd);
GenericBean<?> gb = (GenericBean<?>) bf.getBean("genericBean");
@ -576,7 +588,9 @@ public class BeanFactoryGenericsTests {
@Test
public void testGenericListBean() throws Exception {
XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("genericBeanTests.xml", getClass()));
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(
new ClassPathResource("genericBeanTests.xml", getClass()));
List<?> list = (List<?>) bf.getBean("list");
assertEquals(1, list.size());
assertEquals(new URL("http://localhost:8080"), list.get(0));
@ -584,7 +598,9 @@ public class BeanFactoryGenericsTests {
@Test
public void testGenericSetBean() throws Exception {
XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("genericBeanTests.xml", getClass()));
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(
new ClassPathResource("genericBeanTests.xml", getClass()));
Set<?> set = (Set<?>) bf.getBean("set");
assertEquals(1, set.size());
assertEquals(new URL("http://localhost:8080"), set.iterator().next());
@ -592,7 +608,9 @@ public class BeanFactoryGenericsTests {
@Test
public void testGenericMapBean() throws Exception {
XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("genericBeanTests.xml", getClass()));
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(
new ClassPathResource("genericBeanTests.xml", getClass()));
Map<?, ?> map = (Map<?, ?>) bf.getBean("map");
assertEquals(1, map.size());
assertEquals(new Integer(10), map.keySet().iterator().next());
@ -601,7 +619,9 @@ public class BeanFactoryGenericsTests {
@Test
public void testGenericallyTypedIntegerBean() throws Exception {
XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("genericBeanTests.xml", getClass()));
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(
new ClassPathResource("genericBeanTests.xml", getClass()));
GenericIntegerBean gb = (GenericIntegerBean) bf.getBean("integerBean");
assertEquals(new Integer(10), gb.getGenericProperty());
assertEquals(new Integer(20), gb.getGenericListProperty().get(0));
@ -610,7 +630,9 @@ public class BeanFactoryGenericsTests {
@Test
public void testGenericallyTypedSetOfIntegerBean() throws Exception {
XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("genericBeanTests.xml", getClass()));
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(
new ClassPathResource("genericBeanTests.xml", getClass()));
GenericSetOfIntegerBean gb = (GenericSetOfIntegerBean) bf.getBean("setOfIntegerBean");
assertEquals(new Integer(10), gb.getGenericProperty().iterator().next());
assertEquals(new Integer(20), gb.getGenericListProperty().get(0).iterator().next());
@ -619,7 +641,9 @@ public class BeanFactoryGenericsTests {
@Test
public void testSetBean() throws Exception {
XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("genericBeanTests.xml", getClass()));
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(
new ClassPathResource("genericBeanTests.xml", getClass()));
UrlSet us = (UrlSet) bf.getBean("setBean");
assertEquals(1, us.size());
assertEquals(new URL("http://www.springframework.org"), us.iterator().next());

View File

@ -16,11 +16,11 @@
package org.springframework.beans.factory.support.security;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail;
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.junit.Assert.fail;
import java.lang.reflect.Method;
import java.net.URL;
@ -56,7 +56,7 @@ import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.SecurityContextProvider;
import org.springframework.beans.factory.support.security.support.ConstructorBean;
import org.springframework.beans.factory.support.security.support.CustomCallbackBean;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
@ -72,7 +72,7 @@ import org.springframework.core.io.Resource;
*/
public class CallbacksSecurityTests {
private XmlBeanFactory beanFactory;
private DefaultListableBeanFactory beanFactory;
private SecurityContextProvider provider;
private static class NonPrivilegedBean {
@ -312,7 +312,8 @@ public class CallbacksSecurityTests {
DefaultResourceLoader drl = new DefaultResourceLoader();
Resource config = drl
.getResource("/org/springframework/beans/factory/support/security/callbacks.xml");
beanFactory = new XmlBeanFactory(config);
beanFactory = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(beanFactory).loadBeanDefinitions(config);
beanFactory.setSecurityContextProvider(provider);
}

View File

@ -20,7 +20,6 @@ import java.beans.PropertyEditorSupport;
import java.util.StringTokenizer;
import junit.framework.TestCase;
import junit.framework.Assert;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyBatchUpdateException;
@ -88,7 +87,7 @@ public abstract class AbstractBeanFactoryTests extends TestCase {
*/
public void testLifecycleCallbacks() {
LifecycleBean lb = (LifecycleBean) getBeanFactory().getBean("lifecycle");
Assert.assertEquals("lifecycle", lb.getBeanName());
assertEquals("lifecycle", lb.getBeanName());
// The dummy business method will throw an exception if the
// necessary callbacks weren't invoked in the right order.
lb.businessMethod();
@ -365,4 +364,4 @@ class MustBeInitialized implements InitializingBean {
throw new RuntimeException("Factory didn't call afterPropertiesSet() on MustBeInitialized object");
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2012 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.
@ -20,8 +20,6 @@ import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.ListableBeanFactory;
import junit.framework.Assert;
import test.beans.TestBean;
/**
@ -48,24 +46,24 @@ public abstract class AbstractListableBeanFactoryTests extends AbstractBeanFacto
protected final void assertCount(int count) {
String[] defnames = getListableBeanFactory().getBeanDefinitionNames();
Assert.assertTrue("We should have " + count + " beans, not " + defnames.length, defnames.length == count);
assertTrue("We should have " + count + " beans, not " + defnames.length, defnames.length == count);
}
public void assertTestBeanCount(int count) {
String[] defNames = getListableBeanFactory().getBeanNamesForType(TestBean.class, true, false);
Assert.assertTrue("We should have " + count + " beans for class org.springframework.beans.TestBean, not " +
assertTrue("We should have " + count + " beans for class org.springframework.beans.TestBean, not " +
defNames.length, defNames.length == count);
int countIncludingFactoryBeans = count + 2;
String[] names = getListableBeanFactory().getBeanNamesForType(TestBean.class, true, true);
Assert.assertTrue("We should have " + countIncludingFactoryBeans +
assertTrue("We should have " + countIncludingFactoryBeans +
" beans for class org.springframework.beans.TestBean, not " + names.length,
names.length == countIncludingFactoryBeans);
}
public void testGetDefinitionsForNoSuchClass() {
String[] defnames = getListableBeanFactory().getBeanNamesForType(String.class);
Assert.assertTrue("No string definitions", defnames.length == 0);
assertTrue("No string definitions", defnames.length == 0);
}
/**
@ -73,18 +71,18 @@ public abstract class AbstractListableBeanFactoryTests extends AbstractBeanFacto
* what type factories may return, and it may even change over time.)
*/
public void testGetCountForFactoryClass() {
Assert.assertTrue("Should have 2 factories, not " +
assertTrue("Should have 2 factories, not " +
getListableBeanFactory().getBeanNamesForType(FactoryBean.class).length,
getListableBeanFactory().getBeanNamesForType(FactoryBean.class).length == 2);
Assert.assertTrue("Should have 2 factories, not " +
assertTrue("Should have 2 factories, not " +
getListableBeanFactory().getBeanNamesForType(FactoryBean.class).length,
getListableBeanFactory().getBeanNamesForType(FactoryBean.class).length == 2);
}
public void testContainsBeanDefinition() {
Assert.assertTrue(getListableBeanFactory().containsBeanDefinition("rod"));
Assert.assertTrue(getListableBeanFactory().containsBeanDefinition("roderick"));
assertTrue(getListableBeanFactory().containsBeanDefinition("rod"));
assertTrue(getListableBeanFactory().containsBeanDefinition("roderick"));
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2012 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.
@ -16,9 +16,7 @@
package org.springframework.beans.factory.xml;
import junit.framework.Assert;
import junit.framework.TestCase;
import test.beans.TestBean;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.beans.factory.config.RuntimeBeanReference;
@ -26,6 +24,8 @@ import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.core.io.ClassPathResource;
import test.beans.TestBean;
/**
* @author Rob Harrop
* @author Juergen Hoeller
@ -34,43 +34,45 @@ public class AutowireWithExclusionTests extends TestCase {
public void testByTypeAutowireWithAutoSelfExclusion() throws Exception {
CountingFactory.reset();
XmlBeanFactory beanFactory = getBeanFactory("autowire-with-exclusion.xml");
DefaultListableBeanFactory beanFactory = getBeanFactory("autowire-with-exclusion.xml");
beanFactory.preInstantiateSingletons();
TestBean rob = (TestBean) beanFactory.getBean("rob");
TestBean sally = (TestBean) beanFactory.getBean("sally");
assertEquals(sally, rob.getSpouse());
Assert.assertEquals(1, CountingFactory.getFactoryBeanInstanceCount());
assertEquals(1, CountingFactory.getFactoryBeanInstanceCount());
}
public void testByTypeAutowireWithExclusion() throws Exception {
CountingFactory.reset();
XmlBeanFactory beanFactory = getBeanFactory("autowire-with-exclusion.xml");
DefaultListableBeanFactory beanFactory = getBeanFactory("autowire-with-exclusion.xml");
beanFactory.preInstantiateSingletons();
TestBean rob = (TestBean) beanFactory.getBean("rob");
assertEquals("props1", rob.getSomeProperties().getProperty("name"));
Assert.assertEquals(1, CountingFactory.getFactoryBeanInstanceCount());
assertEquals(1, CountingFactory.getFactoryBeanInstanceCount());
}
public void testByTypeAutowireWithExclusionInParentFactory() throws Exception {
CountingFactory.reset();
XmlBeanFactory parent = getBeanFactory("autowire-with-exclusion.xml");
DefaultListableBeanFactory parent = getBeanFactory("autowire-with-exclusion.xml");
parent.preInstantiateSingletons();
DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
RootBeanDefinition robDef = new RootBeanDefinition(TestBean.class, RootBeanDefinition.AUTOWIRE_BY_TYPE);
RootBeanDefinition robDef = new RootBeanDefinition(TestBean.class);
robDef.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE);
robDef.getPropertyValues().add("spouse", new RuntimeBeanReference("sally"));
child.registerBeanDefinition("rob2", robDef);
TestBean rob = (TestBean) child.getBean("rob2");
assertEquals("props1", rob.getSomeProperties().getProperty("name"));
Assert.assertEquals(1, CountingFactory.getFactoryBeanInstanceCount());
assertEquals(1, CountingFactory.getFactoryBeanInstanceCount());
}
public void testByTypeAutowireWithPrimaryInParentFactory() throws Exception {
CountingFactory.reset();
XmlBeanFactory parent = getBeanFactory("autowire-with-exclusion.xml");
DefaultListableBeanFactory parent = getBeanFactory("autowire-with-exclusion.xml");
parent.getBeanDefinition("props1").setPrimary(true);
parent.preInstantiateSingletons();
DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
RootBeanDefinition robDef = new RootBeanDefinition(TestBean.class, RootBeanDefinition.AUTOWIRE_BY_TYPE);
RootBeanDefinition robDef = new RootBeanDefinition(TestBean.class);
robDef.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE);
robDef.getPropertyValues().add("spouse", new RuntimeBeanReference("sally"));
child.registerBeanDefinition("rob2", robDef);
RootBeanDefinition propsDef = new RootBeanDefinition(PropertiesFactoryBean.class);
@ -78,15 +80,16 @@ public class AutowireWithExclusionTests extends TestCase {
child.registerBeanDefinition("props3", propsDef);
TestBean rob = (TestBean) child.getBean("rob2");
assertEquals("props1", rob.getSomeProperties().getProperty("name"));
Assert.assertEquals(1, CountingFactory.getFactoryBeanInstanceCount());
assertEquals(1, CountingFactory.getFactoryBeanInstanceCount());
}
public void testByTypeAutowireWithPrimaryOverridingParentFactory() throws Exception {
CountingFactory.reset();
XmlBeanFactory parent = getBeanFactory("autowire-with-exclusion.xml");
DefaultListableBeanFactory parent = getBeanFactory("autowire-with-exclusion.xml");
parent.preInstantiateSingletons();
DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
RootBeanDefinition robDef = new RootBeanDefinition(TestBean.class, RootBeanDefinition.AUTOWIRE_BY_TYPE);
RootBeanDefinition robDef = new RootBeanDefinition(TestBean.class);
robDef.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE);
robDef.getPropertyValues().add("spouse", new RuntimeBeanReference("sally"));
child.registerBeanDefinition("rob2", robDef);
RootBeanDefinition propsDef = new RootBeanDefinition(PropertiesFactoryBean.class);
@ -95,16 +98,17 @@ public class AutowireWithExclusionTests extends TestCase {
child.registerBeanDefinition("props3", propsDef);
TestBean rob = (TestBean) child.getBean("rob2");
assertEquals("props3", rob.getSomeProperties().getProperty("name"));
Assert.assertEquals(1, CountingFactory.getFactoryBeanInstanceCount());
assertEquals(1, CountingFactory.getFactoryBeanInstanceCount());
}
public void testByTypeAutowireWithPrimaryInParentAndChild() throws Exception {
CountingFactory.reset();
XmlBeanFactory parent = getBeanFactory("autowire-with-exclusion.xml");
DefaultListableBeanFactory parent = getBeanFactory("autowire-with-exclusion.xml");
parent.getBeanDefinition("props1").setPrimary(true);
parent.preInstantiateSingletons();
DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
RootBeanDefinition robDef = new RootBeanDefinition(TestBean.class, RootBeanDefinition.AUTOWIRE_BY_TYPE);
RootBeanDefinition robDef = new RootBeanDefinition(TestBean.class);
robDef.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE);
robDef.getPropertyValues().add("spouse", new RuntimeBeanReference("sally"));
child.registerBeanDefinition("rob2", robDef);
RootBeanDefinition propsDef = new RootBeanDefinition(PropertiesFactoryBean.class);
@ -113,29 +117,29 @@ public class AutowireWithExclusionTests extends TestCase {
child.registerBeanDefinition("props3", propsDef);
TestBean rob = (TestBean) child.getBean("rob2");
assertEquals("props3", rob.getSomeProperties().getProperty("name"));
Assert.assertEquals(1, CountingFactory.getFactoryBeanInstanceCount());
assertEquals(1, CountingFactory.getFactoryBeanInstanceCount());
}
public void testByTypeAutowireWithInclusion() throws Exception {
CountingFactory.reset();
XmlBeanFactory beanFactory = getBeanFactory("autowire-with-inclusion.xml");
DefaultListableBeanFactory beanFactory = getBeanFactory("autowire-with-inclusion.xml");
beanFactory.preInstantiateSingletons();
TestBean rob = (TestBean) beanFactory.getBean("rob");
assertEquals("props1", rob.getSomeProperties().getProperty("name"));
Assert.assertEquals(1, CountingFactory.getFactoryBeanInstanceCount());
assertEquals(1, CountingFactory.getFactoryBeanInstanceCount());
}
public void testByTypeAutowireWithSelectiveInclusion() throws Exception {
CountingFactory.reset();
XmlBeanFactory beanFactory = getBeanFactory("autowire-with-selective-inclusion.xml");
DefaultListableBeanFactory beanFactory = getBeanFactory("autowire-with-selective-inclusion.xml");
beanFactory.preInstantiateSingletons();
TestBean rob = (TestBean) beanFactory.getBean("rob");
assertEquals("props1", rob.getSomeProperties().getProperty("name"));
Assert.assertEquals(1, CountingFactory.getFactoryBeanInstanceCount());
assertEquals(1, CountingFactory.getFactoryBeanInstanceCount());
}
public void testConstructorAutowireWithAutoSelfExclusion() throws Exception {
XmlBeanFactory beanFactory = getBeanFactory("autowire-constructor-with-exclusion.xml");
DefaultListableBeanFactory beanFactory = getBeanFactory("autowire-constructor-with-exclusion.xml");
TestBean rob = (TestBean) beanFactory.getBean("rob");
TestBean sally = (TestBean) beanFactory.getBean("sally");
assertEquals(sally, rob.getSpouse());
@ -147,13 +151,16 @@ public class AutowireWithExclusionTests extends TestCase {
}
public void testConstructorAutowireWithExclusion() throws Exception {
XmlBeanFactory beanFactory = getBeanFactory("autowire-constructor-with-exclusion.xml");
DefaultListableBeanFactory beanFactory = getBeanFactory("autowire-constructor-with-exclusion.xml");
TestBean rob = (TestBean) beanFactory.getBean("rob");
assertEquals("props1", rob.getSomeProperties().getProperty("name"));
}
private XmlBeanFactory getBeanFactory(String configPath) {
return new XmlBeanFactory(new ClassPathResource(configPath, getClass()));
private DefaultListableBeanFactory getBeanFactory(String configPath) {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(
new ClassPathResource(configPath, getClass()));
return bf;
}
}

View File

@ -19,6 +19,7 @@ package org.springframework.beans.factory.xml;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@ -38,9 +39,9 @@ public class CollectingReaderEventListener implements ReaderEventListener {
private final List defaults = new LinkedList();
private final Map componentDefinitions = CollectionFactory.createLinkedMapIfPossible(8);
private final Map componentDefinitions = new LinkedHashMap<>(8);
private final Map aliasMap = CollectionFactory.createLinkedMapIfPossible(8);
private final Map aliasMap = new LinkedHashMap<>(8);
private final List imports = new LinkedList();
@ -92,4 +93,4 @@ public class CollectingReaderEventListener implements ReaderEventListener {
return Collections.unmodifiableList(this.imports);
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2012 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.
@ -16,25 +16,30 @@
package org.springframework.beans.factory.xml;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.*;
import org.junit.Test;
import test.beans.TestBean;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.core.io.ClassPathResource;
import test.beans.TestBean;
/**
* @author Rob Harrop
* @author Juergen Hoeller
*/
public class CollectionsWithDefaultTypesTests {
private final XmlBeanFactory beanFactory;
private final DefaultListableBeanFactory beanFactory;
public CollectionsWithDefaultTypesTests() {
this.beanFactory = new XmlBeanFactory(new ClassPathResource("collectionsWithDefaultTypes.xml", getClass()));
this.beanFactory = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(this.beanFactory).loadBeanDefinitions(
new ClassPathResource("collectionsWithDefaultTypes.xml", getClass()));
}
@Test

View File

@ -18,6 +18,7 @@ package org.springframework.beans.factory.xml;
import junit.framework.TestCase;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.core.io.ClassPathResource;
/**
@ -25,11 +26,13 @@ import org.springframework.core.io.ClassPathResource;
*/
public class DefaultLifecycleMethodsTests extends TestCase {
private XmlBeanFactory beanFactory;
private DefaultListableBeanFactory beanFactory;
@Override
protected void setUp() throws Exception {
this.beanFactory = new XmlBeanFactory(new ClassPathResource("defaultLifecycleMethods.xml", getClass()));
this.beanFactory = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(this.beanFactory).loadBeanDefinitions(new ClassPathResource(
"defaultLifecycleMethods.xml", getClass()));
}
public void testLifecycleMethodsInvoked() {
@ -49,7 +52,9 @@ public class DefaultLifecycleMethodsTests extends TestCase {
public void testIgnoreDefaultLifecycleMethods() throws Exception {
try {
XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("ignoreDefaultLifecycleMethods.xml", getClass()));
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(
new ClassPathResource("ignoreDefaultLifecycleMethods.xml", getClass()));
bf.preInstantiateSingletons();
bf.destroySingletons();
}

View File

@ -20,6 +20,7 @@ import junit.framework.TestCase;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.core.io.ClassPathResource;
/**
@ -27,11 +28,13 @@ import org.springframework.core.io.ClassPathResource;
*/
public class MetadataAttachmentTests extends TestCase {
private XmlBeanFactory beanFactory;
private DefaultListableBeanFactory beanFactory;
@Override
protected void setUp() throws Exception {
this.beanFactory = new XmlBeanFactory(new ClassPathResource("withMeta.xml", getClass()));
this.beanFactory = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(this.beanFactory).loadBeanDefinitions(
new ClassPathResource("withMeta.xml", getClass()));
}
public void testMetadataAttachment() throws Exception {

View File

@ -21,8 +21,8 @@ import static org.junit.Assert.assertThat;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Test;
import org.junit.internal.matchers.TypeSafeMatcher;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.core.env.ConfigurableEnvironment;

View File

@ -20,6 +20,7 @@ import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.core.io.ClassPathResource;
import test.beans.DummyBean;
@ -32,7 +33,7 @@ public class SimpleConstructorNamespaceHandlerTests {
@Test
public void simpleValue() throws Exception {
XmlBeanFactory beanFactory = createFactory("simpleConstructorNamespaceHandlerTests.xml");
DefaultListableBeanFactory beanFactory = createFactory("simpleConstructorNamespaceHandlerTests.xml");
String name = "simple";
// beanFactory.getBean("simple1", DummyBean.class);
DummyBean nameValue = beanFactory.getBean(name, DummyBean.class);
@ -41,7 +42,7 @@ public class SimpleConstructorNamespaceHandlerTests {
@Test
public void simpleRef() throws Exception {
XmlBeanFactory beanFactory = createFactory("simpleConstructorNamespaceHandlerTests.xml");
DefaultListableBeanFactory beanFactory = createFactory("simpleConstructorNamespaceHandlerTests.xml");
String name = "simple-ref";
// beanFactory.getBean("name-value1", TestBean.class);
DummyBean nameValue = beanFactory.getBean(name, DummyBean.class);
@ -50,7 +51,7 @@ public class SimpleConstructorNamespaceHandlerTests {
@Test
public void nameValue() throws Exception {
XmlBeanFactory beanFactory = createFactory("simpleConstructorNamespaceHandlerTests.xml");
DefaultListableBeanFactory beanFactory = createFactory("simpleConstructorNamespaceHandlerTests.xml");
String name = "name-value";
// beanFactory.getBean("name-value1", TestBean.class);
TestBean nameValue = beanFactory.getBean(name, TestBean.class);
@ -60,7 +61,7 @@ public class SimpleConstructorNamespaceHandlerTests {
@Test
public void nameRef() throws Exception {
XmlBeanFactory beanFactory = createFactory("simpleConstructorNamespaceHandlerTests.xml");
DefaultListableBeanFactory beanFactory = createFactory("simpleConstructorNamespaceHandlerTests.xml");
TestBean nameValue = beanFactory.getBean("name-value", TestBean.class);
DummyBean nameRef = beanFactory.getBean("name-ref", DummyBean.class);
@ -70,7 +71,7 @@ public class SimpleConstructorNamespaceHandlerTests {
@Test
public void typeIndexedValue() throws Exception {
XmlBeanFactory beanFactory = createFactory("simpleConstructorNamespaceHandlerTests.xml");
DefaultListableBeanFactory beanFactory = createFactory("simpleConstructorNamespaceHandlerTests.xml");
DummyBean typeRef = beanFactory.getBean("indexed-value", DummyBean.class);
assertEquals("at", typeRef.getName());
@ -80,7 +81,7 @@ public class SimpleConstructorNamespaceHandlerTests {
@Test
public void typeIndexedRef() throws Exception {
XmlBeanFactory beanFactory = createFactory("simpleConstructorNamespaceHandlerTests.xml");
DefaultListableBeanFactory beanFactory = createFactory("simpleConstructorNamespaceHandlerTests.xml");
DummyBean typeRef = beanFactory.getBean("indexed-ref", DummyBean.class);
assertEquals("some-name", typeRef.getName());
@ -89,20 +90,23 @@ public class SimpleConstructorNamespaceHandlerTests {
@Test(expected = BeanDefinitionStoreException.class)
public void ambiguousConstructor() throws Exception {
new XmlBeanFactory(new ClassPathResource("simpleConstructorNamespaceHandlerTestsWithErrors.xml", getClass()));
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(
new ClassPathResource("simpleConstructorNamespaceHandlerTestsWithErrors.xml", getClass()));
}
@Test
public void constructorWithNameEndingInRef() throws Exception {
XmlBeanFactory beanFactory = createFactory("simpleConstructorNamespaceHandlerTests.xml");
DefaultListableBeanFactory beanFactory = createFactory("simpleConstructorNamespaceHandlerTests.xml");
DummyBean derivedBean = beanFactory.getBean("beanWithRefConstructorArg", DummyBean.class);
assertEquals(10, derivedBean.getAge());
assertEquals("silly name", derivedBean.getName());
}
private XmlBeanFactory createFactory(String resourceName) {
XmlBeanFactory fact = new XmlBeanFactory(new ClassPathResource(resourceName, getClass()));
//fact.setParameterNameDiscoverer(new LocalVariableTableParameterNameDiscoverer());
return fact;
private DefaultListableBeanFactory createFactory(String resourceName) {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(
new ClassPathResource(resourceName, getClass()));
return bf;
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2012 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.
@ -17,13 +17,15 @@
package org.springframework.beans.factory.xml;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.core.io.ClassPathResource;
import test.beans.ITestBean;
import test.beans.TestBean;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.core.io.ClassPathResource;
/**
* @author Rob Harrop
* @author Juergen Hoeller
@ -33,8 +35,9 @@ public class SimplePropertyNamespaceHandlerTests {
@Test
public void simpleBeanConfigured() throws Exception {
XmlBeanFactory beanFactory =
new XmlBeanFactory(new ClassPathResource("simplePropertyNamespaceHandlerTests.xml", getClass()));
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(beanFactory).loadBeanDefinitions(
new ClassPathResource("simplePropertyNamespaceHandlerTests.xml", getClass()));
ITestBean rob = (TestBean) beanFactory.getBean("rob");
ITestBean sally = (TestBean) beanFactory.getBean("sally");
assertEquals("Rob Harrop", rob.getName());
@ -44,8 +47,9 @@ public class SimplePropertyNamespaceHandlerTests {
@Test
public void innerBeanConfigured() throws Exception {
XmlBeanFactory beanFactory =
new XmlBeanFactory(new ClassPathResource("simplePropertyNamespaceHandlerTests.xml", getClass()));
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(beanFactory).loadBeanDefinitions(
new ClassPathResource("simplePropertyNamespaceHandlerTests.xml", getClass()));
TestBean sally = (TestBean) beanFactory.getBean("sally2");
ITestBean rob = sally.getSpouse();
assertEquals("Rob Harrop", rob.getName());
@ -55,13 +59,16 @@ public class SimplePropertyNamespaceHandlerTests {
@Test(expected = BeanDefinitionStoreException.class)
public void withPropertyDefinedTwice() throws Exception {
new XmlBeanFactory(new ClassPathResource("simplePropertyNamespaceHandlerTestsWithErrors.xml", getClass()));
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(beanFactory).loadBeanDefinitions(
new ClassPathResource("simplePropertyNamespaceHandlerTestsWithErrors.xml", getClass()));
}
@Test
public void propertyWithNameEndingInRef() throws Exception {
XmlBeanFactory beanFactory =
new XmlBeanFactory(new ClassPathResource("simplePropertyNamespaceHandlerTests.xml", getClass()));
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(beanFactory).loadBeanDefinitions(
new ClassPathResource("simplePropertyNamespaceHandlerTests.xml", getClass()));
ITestBean sally = (TestBean) beanFactory.getBean("derivedSally");
assertEquals("r", sally.getSpouse().getName());
}

View File

@ -16,7 +16,14 @@
package org.springframework.beans.factory.xml;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
@ -27,21 +34,19 @@ import java.util.Properties;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.IdentityHashMap;
import java.util.HashSet;
import java.util.concurrent.CopyOnWriteArraySet;
import org.junit.Test;
import static org.junit.Assert.*;
import test.beans.TestBean;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.config.ListFactoryBean;
import org.springframework.beans.factory.config.MapFactoryBean;
import org.springframework.beans.factory.config.SetFactoryBean;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.core.io.ClassPathResource;
import test.beans.TestBean;
/**
* Tests for collections in XML bean definitions.
*
@ -51,10 +56,12 @@ import org.springframework.core.io.ClassPathResource;
*/
public class XmlBeanCollectionTests {
private final XmlBeanFactory beanFactory;
private final DefaultListableBeanFactory beanFactory;
public XmlBeanCollectionTests() {
this.beanFactory = new XmlBeanFactory(new ClassPathResource("collections.xml", getClass()));
this.beanFactory = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(this.beanFactory).loadBeanDefinitions(
new ClassPathResource("collections.xml", getClass()));
}
@Test

View File

@ -21,8 +21,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import junit.framework.Assert;
import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.BeanFactory;
@ -44,21 +42,25 @@ public class XmlListableBeanFactoryTests extends AbstractListableBeanFactoryTest
private DefaultListableBeanFactory parent;
private XmlBeanFactory factory;
private DefaultListableBeanFactory factory;
@Override
protected void setUp() {
parent = new DefaultListableBeanFactory();
Map m = new HashMap();
m.put("name", "Albert");
parent.registerBeanDefinition("father",
new RootBeanDefinition(TestBean.class, new MutablePropertyValues(m)));
RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
bd1.setPropertyValues(new MutablePropertyValues(m));
parent.registerBeanDefinition("father", bd1);
m = new HashMap();
m.put("name", "Roderick");
parent.registerBeanDefinition("rod",
new RootBeanDefinition(TestBean.class, new MutablePropertyValues(m)));
RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
bd2.setPropertyValues(new MutablePropertyValues(m));
parent.registerBeanDefinition("rod", bd2);
this.factory = new XmlBeanFactory(new ClassPathResource("test.xml", getClass()), parent);
this.factory = new DefaultListableBeanFactory(parent);
new XmlBeanDefinitionReader(this.factory).loadBeanDefinitions(
new ClassPathResource("test.xml", getClass()));
this.factory.addBeanPostProcessor(new BeanPostProcessor() {
@Override
public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException {
@ -106,7 +108,7 @@ public class XmlListableBeanFactoryTests extends AbstractListableBeanFactoryTest
public void testDescriptionButNoProperties() throws Exception {
TestBean validEmpty = (TestBean) getBeanFactory().getBean("validEmptyWithDescription");
Assert.assertEquals(0, validEmpty.getAge());
assertEquals(0, validEmpty.getAge());
}
/**
@ -117,94 +119,94 @@ public class XmlListableBeanFactoryTests extends AbstractListableBeanFactoryTest
TestBean tb1 = (TestBean) getBeanFactory().getBean("aliased");
TestBean alias1 = (TestBean) getBeanFactory().getBean("myalias");
Assert.assertTrue(tb1 == alias1);
assertTrue(tb1 == alias1);
List tb1Aliases = Arrays.asList(getBeanFactory().getAliases("aliased"));
Assert.assertEquals(2, tb1Aliases.size());
Assert.assertTrue(tb1Aliases.contains("myalias"));
Assert.assertTrue(tb1Aliases.contains("youralias"));
Assert.assertTrue(beanNames.contains("aliased"));
Assert.assertFalse(beanNames.contains("myalias"));
Assert.assertFalse(beanNames.contains("youralias"));
assertEquals(2, tb1Aliases.size());
assertTrue(tb1Aliases.contains("myalias"));
assertTrue(tb1Aliases.contains("youralias"));
assertTrue(beanNames.contains("aliased"));
assertFalse(beanNames.contains("myalias"));
assertFalse(beanNames.contains("youralias"));
TestBean tb2 = (TestBean) getBeanFactory().getBean("multiAliased");
TestBean alias2 = (TestBean) getBeanFactory().getBean("alias1");
TestBean alias3 = (TestBean) getBeanFactory().getBean("alias2");
TestBean alias3a = (TestBean) getBeanFactory().getBean("alias3");
TestBean alias3b = (TestBean) getBeanFactory().getBean("alias4");
Assert.assertTrue(tb2 == alias2);
Assert.assertTrue(tb2 == alias3);
Assert.assertTrue(tb2 == alias3a);
Assert.assertTrue(tb2 == alias3b);
assertTrue(tb2 == alias2);
assertTrue(tb2 == alias3);
assertTrue(tb2 == alias3a);
assertTrue(tb2 == alias3b);
List tb2Aliases = Arrays.asList(getBeanFactory().getAliases("multiAliased"));
Assert.assertEquals(4, tb2Aliases.size());
Assert.assertTrue(tb2Aliases.contains("alias1"));
Assert.assertTrue(tb2Aliases.contains("alias2"));
Assert.assertTrue(tb2Aliases.contains("alias3"));
Assert.assertTrue(tb2Aliases.contains("alias4"));
Assert.assertTrue(beanNames.contains("multiAliased"));
Assert.assertFalse(beanNames.contains("alias1"));
Assert.assertFalse(beanNames.contains("alias2"));
Assert.assertFalse(beanNames.contains("alias3"));
Assert.assertFalse(beanNames.contains("alias4"));
assertEquals(4, tb2Aliases.size());
assertTrue(tb2Aliases.contains("alias1"));
assertTrue(tb2Aliases.contains("alias2"));
assertTrue(tb2Aliases.contains("alias3"));
assertTrue(tb2Aliases.contains("alias4"));
assertTrue(beanNames.contains("multiAliased"));
assertFalse(beanNames.contains("alias1"));
assertFalse(beanNames.contains("alias2"));
assertFalse(beanNames.contains("alias3"));
assertFalse(beanNames.contains("alias4"));
TestBean tb3 = (TestBean) getBeanFactory().getBean("aliasWithoutId1");
TestBean alias4 = (TestBean) getBeanFactory().getBean("aliasWithoutId2");
TestBean alias5 = (TestBean) getBeanFactory().getBean("aliasWithoutId3");
Assert.assertTrue(tb3 == alias4);
Assert.assertTrue(tb3 == alias5);
assertTrue(tb3 == alias4);
assertTrue(tb3 == alias5);
List tb3Aliases = Arrays.asList(getBeanFactory().getAliases("aliasWithoutId1"));
Assert.assertEquals(2, tb3Aliases.size());
Assert.assertTrue(tb3Aliases.contains("aliasWithoutId2"));
Assert.assertTrue(tb3Aliases.contains("aliasWithoutId3"));
Assert.assertTrue(beanNames.contains("aliasWithoutId1"));
Assert.assertFalse(beanNames.contains("aliasWithoutId2"));
Assert.assertFalse(beanNames.contains("aliasWithoutId3"));
assertEquals(2, tb3Aliases.size());
assertTrue(tb3Aliases.contains("aliasWithoutId2"));
assertTrue(tb3Aliases.contains("aliasWithoutId3"));
assertTrue(beanNames.contains("aliasWithoutId1"));
assertFalse(beanNames.contains("aliasWithoutId2"));
assertFalse(beanNames.contains("aliasWithoutId3"));
TestBean tb4 = (TestBean) getBeanFactory().getBean(TestBean.class.getName() + "#0");
Assert.assertEquals(null, tb4.getName());
assertEquals(null, tb4.getName());
Map drs = getListableBeanFactory().getBeansOfType(DummyReferencer.class, false, false);
Assert.assertEquals(5, drs.size());
Assert.assertTrue(drs.containsKey(DummyReferencer.class.getName() + "#0"));
Assert.assertTrue(drs.containsKey(DummyReferencer.class.getName() + "#1"));
Assert.assertTrue(drs.containsKey(DummyReferencer.class.getName() + "#2"));
assertEquals(5, drs.size());
assertTrue(drs.containsKey(DummyReferencer.class.getName() + "#0"));
assertTrue(drs.containsKey(DummyReferencer.class.getName() + "#1"));
assertTrue(drs.containsKey(DummyReferencer.class.getName() + "#2"));
}
public void testFactoryNesting() {
ITestBean father = (ITestBean) getBeanFactory().getBean("father");
Assert.assertTrue("Bean from root context", father != null);
assertTrue("Bean from root context", father != null);
TestBean rod = (TestBean) getBeanFactory().getBean("rod");
Assert.assertTrue("Bean from child context", "Rod".equals(rod.getName()));
Assert.assertTrue("Bean has external reference", rod.getSpouse() == father);
assertTrue("Bean from child context", "Rod".equals(rod.getName()));
assertTrue("Bean has external reference", rod.getSpouse() == father);
rod = (TestBean) parent.getBean("rod");
Assert.assertTrue("Bean from root context", "Roderick".equals(rod.getName()));
assertTrue("Bean from root context", "Roderick".equals(rod.getName()));
}
public void testFactoryReferences() {
DummyFactory factory = (DummyFactory) getBeanFactory().getBean("&singletonFactory");
DummyReferencer ref = (DummyReferencer) getBeanFactory().getBean("factoryReferencer");
Assert.assertTrue(ref.getTestBean1() == ref.getTestBean2());
Assert.assertTrue(ref.getDummyFactory() == factory);
assertTrue(ref.getTestBean1() == ref.getTestBean2());
assertTrue(ref.getDummyFactory() == factory);
DummyReferencer ref2 = (DummyReferencer) getBeanFactory().getBean("factoryReferencerWithConstructor");
Assert.assertTrue(ref2.getTestBean1() == ref2.getTestBean2());
Assert.assertTrue(ref2.getDummyFactory() == factory);
assertTrue(ref2.getTestBean1() == ref2.getTestBean2());
assertTrue(ref2.getDummyFactory() == factory);
}
public void testPrototypeReferences() {
// check that not broken by circular reference resolution mechanism
DummyReferencer ref1 = (DummyReferencer) getBeanFactory().getBean("prototypeReferencer");
Assert.assertTrue("Not referencing same bean twice", ref1.getTestBean1() != ref1.getTestBean2());
assertTrue("Not referencing same bean twice", ref1.getTestBean1() != ref1.getTestBean2());
DummyReferencer ref2 = (DummyReferencer) getBeanFactory().getBean("prototypeReferencer");
Assert.assertTrue("Not the same referencer", ref1 != ref2);
Assert.assertTrue("Not referencing same bean twice", ref2.getTestBean1() != ref2.getTestBean2());
Assert.assertTrue("Not referencing same bean twice", ref1.getTestBean1() != ref2.getTestBean1());
Assert.assertTrue("Not referencing same bean twice", ref1.getTestBean2() != ref2.getTestBean2());
Assert.assertTrue("Not referencing same bean twice", ref1.getTestBean1() != ref2.getTestBean2());
assertTrue("Not the same referencer", ref1 != ref2);
assertTrue("Not referencing same bean twice", ref2.getTestBean1() != ref2.getTestBean2());
assertTrue("Not referencing same bean twice", ref1.getTestBean1() != ref2.getTestBean1());
assertTrue("Not referencing same bean twice", ref1.getTestBean2() != ref2.getTestBean2());
assertTrue("Not referencing same bean twice", ref1.getTestBean1() != ref2.getTestBean2());
}
public void testBeanPostProcessor() throws Exception {
@ -212,22 +214,22 @@ public class XmlListableBeanFactoryTests extends AbstractListableBeanFactoryTest
TestBean kathy = (TestBean) getBeanFactory().getBean("kathy");
DummyFactory factory = (DummyFactory) getBeanFactory().getBean("&singletonFactory");
TestBean factoryCreated = (TestBean) getBeanFactory().getBean("singletonFactory");
Assert.assertTrue(kerry.isPostProcessed());
Assert.assertTrue(kathy.isPostProcessed());
Assert.assertTrue(factory.isPostProcessed());
Assert.assertTrue(factoryCreated.isPostProcessed());
assertTrue(kerry.isPostProcessed());
assertTrue(kathy.isPostProcessed());
assertTrue(factory.isPostProcessed());
assertTrue(factoryCreated.isPostProcessed());
}
public void testEmptyValues() {
TestBean rod = (TestBean) getBeanFactory().getBean("rod");
TestBean kerry = (TestBean) getBeanFactory().getBean("kerry");
Assert.assertTrue("Touchy is empty", "".equals(rod.getTouchy()));
Assert.assertTrue("Touchy is empty", "".equals(kerry.getTouchy()));
assertTrue("Touchy is empty", "".equals(rod.getTouchy()));
assertTrue("Touchy is empty", "".equals(kerry.getTouchy()));
}
public void testCommentsAndCdataInValue() {
TestBean bean = (TestBean) getBeanFactory().getBean("commentsInValue");
Assert.assertEquals("Failed to handle comments and CDATA properly", "this is a <!--comment-->", bean.getName());
assertEquals("Failed to handle comments and CDATA properly", "this is a <!--comment-->", bean.getName());
}
}

View File

@ -20,8 +20,10 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Properties;
import javax.activation.FileTypeMap;
import javax.mail.Address;
import javax.mail.Message;
@ -59,7 +61,7 @@ public class JavaMailSenderTests extends TestCase {
simpleMessage.setTo("you@mail.org");
simpleMessage.setCc(new String[] {"he@mail.org", "she@mail.org"});
simpleMessage.setBcc(new String[] {"us@mail.org", "them@mail.org"});
Date sentDate = new Date(2004, 1, 1);
Date sentDate = new GregorianCalendar(2004, 1, 1).getTime();
simpleMessage.setSentDate(sentDate);
simpleMessage.setSubject("my subject");
simpleMessage.setText("my text");
@ -334,7 +336,7 @@ public class JavaMailSenderTests extends TestCase {
MimeMessage mimeMessage = sender.createMimeMessage();
mimeMessage.setSubject("custom");
mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("you@mail.org"));
mimeMessage.setSentDate(new Date(2005, 3, 1));
mimeMessage.setSentDate(new GregorianCalendar(2005, 3, 1).getTime());
sender.send(mimeMessage);
assertEquals("host", sender.transport.getConnectedHost());
@ -559,7 +561,7 @@ public class JavaMailSenderTests extends TestCase {
throw new MessagingException("No sentDate specified");
}
if (message.getSubject() != null && message.getSubject().contains("custom")) {
assertEquals(new Date(2005, 3, 1), message.getSentDate());
assertEquals(new GregorianCalendar(2005, 3, 1).getTime(), message.getSentDate());
}
this.sentMessages.add(message);
}

View File

@ -51,7 +51,6 @@ import org.quartz.Trigger;
import org.quartz.TriggerListener;
import org.quartz.impl.SchedulerRepository;
import org.quartz.spi.JobFactory;
import org.springframework.beans.TestBean;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
@ -63,7 +62,7 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.core.io.FileSystemResourceLoader;
import org.springframework.core.task.TaskExecutor;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.TestMethodInvokingTask;
/**
@ -973,7 +972,7 @@ public class QuartzSupportTests {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
"/org/springframework/scheduling/quartz/databasePersistence.xml");
SimpleJdbcTemplate jdbcTemplate = new SimpleJdbcTemplate(ctx.getBean(DataSource.class));
JdbcTemplate jdbcTemplate = new JdbcTemplate(ctx.getBean(DataSource.class));
assertTrue("No triggers were persisted", jdbcTemplate.queryForList("SELECT * FROM qrtz_triggers").size()>0);
Thread.sleep(3000);
try {

View File

@ -198,7 +198,7 @@ public class JasperReportsUtilsTests extends TestCase {
HSSFRow row = sheet.getRow(3);
HSSFCell cell = row.getCell((short) 1);
assertNotNull("Cell should not be null", cell);
assertEquals("Cell content should be Dear Lord!", "Dear Lord!", cell.getStringCellValue());
assertEquals("Cell content should be Dear Lord!", "Dear Lord!", cell.getRichStringCellValue().getString());
}
private JasperReport getReport() throws Exception {

View File

@ -56,6 +56,7 @@ import org.springframework.instrument.classloading.websphere.WebSphereLoadTimeWe
* @since 2.5
* @see org.springframework.context.ConfigurableApplicationContext#LOAD_TIME_WEAVER_BEAN_NAME
*/
@SuppressWarnings("deprecation")
public class DefaultContextLoadTimeWeaver implements LoadTimeWeaver, BeanClassLoaderAware, DisposableBean {
protected final Log logger = LogFactory.getLog(getClass());

View File

@ -99,6 +99,7 @@ class ScriptBeanDefinitionParser extends AbstractBeanDefinitionParser {
* Registers a {@link ScriptFactoryPostProcessor} if needed.
*/
@Override
@SuppressWarnings("deprecation")
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
// Resolve the script source.
String value = resolveScriptSource(element, parserContext.getReaderContext());

View File

@ -75,6 +75,7 @@ public abstract class JRubyScriptUtils {
* @return the scripted Java object
* @throws JumpException in case of JRuby parsing failure
*/
@SuppressWarnings("deprecation")
public static Object createJRubyObject(String scriptSource, Class[] interfaces, ClassLoader classLoader) {
Ruby ruby = initializeRuntime();

View File

@ -16,8 +16,16 @@
package org.springframework.aop.framework;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.FileNotFoundException;
import java.io.IOException;
@ -45,10 +53,10 @@ import org.springframework.beans.TestBean;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationListener;
import org.springframework.context.TestListener;
import org.springframework.core.io.ClassPathResource;
@ -56,14 +64,13 @@ import org.springframework.util.SerializationTestUtils;
import test.advice.CountingBeforeAdvice;
import test.advice.MyThrowsHandler;
import test.beans.SideEffectBean;
import test.interceptor.NopInterceptor;
import test.interceptor.TimestampIntroductionInterceptor;
import test.mixin.Lockable;
import test.mixin.LockedException;
import test.util.TimeStamped;
import test.beans.SideEffectBean;
/**
* @since 13.03.2003
* @author Rod Johnson
@ -93,7 +100,9 @@ public final class ProxyFactoryBeanTests {
public void setUp() throws Exception {
DefaultListableBeanFactory parent = new DefaultListableBeanFactory();
parent.registerBeanDefinition("target2", new RootBeanDefinition(TestListener.class));
this.factory = new XmlBeanFactory(new ClassPathResource(CONTEXT, getClass()), parent);
this.factory = new DefaultListableBeanFactory(parent);
new XmlBeanDefinitionReader((BeanDefinitionRegistry) this.factory).loadBeanDefinitions(
new ClassPathResource(CONTEXT, getClass()));
}
@Test
@ -133,7 +142,8 @@ public final class ProxyFactoryBeanTests {
private void testDoubleTargetSourceIsRejected(String name) {
try {
BeanFactory bf = new XmlBeanFactory(new ClassPathResource(DBL_TARGETSOURCE_CONTEXT, CLASS));
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(DBL_TARGETSOURCE_CONTEXT, CLASS));
bf.getBean(name);
fail("Should not allow TargetSource to be specified in interceptorNames as well as targetSource property");
}
@ -147,7 +157,8 @@ public final class ProxyFactoryBeanTests {
@Test
public void testTargetSourceNotAtEndOfInterceptorNamesIsRejected() {
try {
BeanFactory bf = new XmlBeanFactory(new ClassPathResource(NOTLAST_TARGETSOURCE_CONTEXT, CLASS));
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(NOTLAST_TARGETSOURCE_CONTEXT, CLASS));
bf.getBean("targetSourceNotLast");
fail("TargetSource or non-advised object must be last in interceptorNames");
}
@ -160,7 +171,8 @@ public final class ProxyFactoryBeanTests {
@Test
public void testGetObjectTypeWithDirectTarget() {
BeanFactory bf = new XmlBeanFactory(new ClassPathResource(TARGETSOURCE_CONTEXT, CLASS));
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(TARGETSOURCE_CONTEXT, CLASS));
// We have a counting before advice here
CountingBeforeAdvice cba = (CountingBeforeAdvice) bf.getBean("countingBeforeAdvice");
@ -176,7 +188,8 @@ public final class ProxyFactoryBeanTests {
@Test
public void testGetObjectTypeWithTargetViaTargetSource() {
BeanFactory bf = new XmlBeanFactory(new ClassPathResource(TARGETSOURCE_CONTEXT, CLASS));
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(TARGETSOURCE_CONTEXT, CLASS));
ITestBean tb = (ITestBean) bf.getBean("viaTargetSource");
assertTrue(tb.getName().equals("Adam"));
ProxyFactoryBean pfb = (ProxyFactoryBean) bf.getBean("&viaTargetSource");
@ -185,7 +198,8 @@ public final class ProxyFactoryBeanTests {
@Test
public void testGetObjectTypeWithNoTargetOrTargetSource() {
BeanFactory bf = new XmlBeanFactory(new ClassPathResource(TARGETSOURCE_CONTEXT, CLASS));
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(TARGETSOURCE_CONTEXT, CLASS));
ITestBean tb = (ITestBean) bf.getBean("noTarget");
try {
@ -246,7 +260,8 @@ public final class ProxyFactoryBeanTests {
// Initial count value set in bean factory XML
int INITIAL_COUNT = 10;
BeanFactory bf = new XmlBeanFactory(new ClassPathResource(PROTOTYPE_CONTEXT, CLASS));
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(PROTOTYPE_CONTEXT, CLASS));
// Check it works without AOP
SideEffectBean raw = (SideEffectBean) bf.getBean("prototypeTarget");
@ -338,7 +353,8 @@ public final class ProxyFactoryBeanTests {
*/
@Test
public void testTargetAsInnerBean() {
ListableBeanFactory bf = new XmlBeanFactory(new ClassPathResource(INNER_BEAN_TARGET_CONTEXT, CLASS));
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(INNER_BEAN_TARGET_CONTEXT, CLASS));
ITestBean itb = (ITestBean) bf.getBean("testBean");
assertEquals("innerBeanTarget", itb.getName());
assertEquals("Only have proxy and interceptor: no target", 3, bf.getBeanDefinitionCount());
@ -441,7 +457,8 @@ public final class ProxyFactoryBeanTests {
@Test
public void testCanAddThrowsAdviceWithoutAdvisor() throws Throwable {
BeanFactory f = new XmlBeanFactory(new ClassPathResource(THROWS_ADVICE_CONTEXT, CLASS));
DefaultListableBeanFactory f = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(f).loadBeanDefinitions(new ClassPathResource(THROWS_ADVICE_CONTEXT, CLASS));
MyThrowsHandler th = (MyThrowsHandler) f.getBean("throwsAdvice");
CountingBeforeAdvice cba = (CountingBeforeAdvice) f.getBean("countingBeforeAdvice");
assertEquals(0, cba.getCalls());
@ -498,9 +515,10 @@ public final class ProxyFactoryBeanTests {
@Test
public void testEmptyInterceptorNames() {
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource(INVALID_CONTEXT, CLASS));
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(INVALID_CONTEXT, CLASS));
try {
factory.getBean("emptyInterceptorNames");
bf.getBean("emptyInterceptorNames");
fail("Interceptor names cannot be empty");
}
catch (BeanCreationException ex) {
@ -513,9 +531,10 @@ public final class ProxyFactoryBeanTests {
*/
@Test
public void testGlobalsWithoutTarget() {
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource(INVALID_CONTEXT, CLASS));
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(INVALID_CONTEXT, CLASS));
try {
factory.getBean("globalsWithoutTarget");
bf.getBean("globalsWithoutTarget");
fail("Should require target name");
}
catch (BeanCreationException ex) {
@ -554,7 +573,8 @@ public final class ProxyFactoryBeanTests {
@Test
public void testSerializableSingletonProxy() throws Exception {
BeanFactory bf = new XmlBeanFactory(new ClassPathResource(SERIALIZATION_CONTEXT, CLASS));
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(SERIALIZATION_CONTEXT, CLASS));
Person p = (Person) bf.getBean("serializableSingleton");
assertSame("Should be a Singleton", p, bf.getBean("serializableSingleton"));
Person p2 = (Person) SerializationTestUtils.serializeAndDeserialize(p);
@ -576,7 +596,8 @@ public final class ProxyFactoryBeanTests {
@Test
public void testSerializablePrototypeProxy() throws Exception {
BeanFactory bf = new XmlBeanFactory(new ClassPathResource(SERIALIZATION_CONTEXT, CLASS));
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(SERIALIZATION_CONTEXT, CLASS));
Person p = (Person) bf.getBean("serializablePrototype");
assertNotSame("Should not be a Singleton", p, bf.getBean("serializablePrototype"));
Person p2 = (Person) SerializationTestUtils.serializeAndDeserialize(p);
@ -587,7 +608,8 @@ public final class ProxyFactoryBeanTests {
@Test
public void testSerializableSingletonProxyFactoryBean() throws Exception {
BeanFactory bf = new XmlBeanFactory(new ClassPathResource(SERIALIZATION_CONTEXT, CLASS));
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(SERIALIZATION_CONTEXT, CLASS));
Person p = (Person) bf.getBean("serializableSingleton");
ProxyFactoryBean pfb = (ProxyFactoryBean) bf.getBean("&serializableSingleton");
ProxyFactoryBean pfb2 = (ProxyFactoryBean) SerializationTestUtils.serializeAndDeserialize(pfb);
@ -599,14 +621,16 @@ public final class ProxyFactoryBeanTests {
@Test
public void testProxyNotSerializableBecauseOfAdvice() throws Exception {
BeanFactory bf = new XmlBeanFactory(new ClassPathResource(SERIALIZATION_CONTEXT, CLASS));
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(SERIALIZATION_CONTEXT, CLASS));
Person p = (Person) bf.getBean("interceptorNotSerializableSingleton");
assertFalse("Not serializable because an interceptor isn't serializable", SerializationTestUtils.isSerializable(p));
}
@Test
public void testPrototypeAdvisor() {
BeanFactory bf = new XmlBeanFactory(new ClassPathResource(CONTEXT, CLASS));
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(CONTEXT, CLASS));
ITestBean bean1 = (ITestBean) bf.getBean("prototypeTestBeanProxy");
ITestBean bean2 = (ITestBean) bf.getBean("prototypeTestBeanProxy");
@ -637,7 +661,8 @@ public final class ProxyFactoryBeanTests {
@Test
public void testPrototypeInterceptorSingletonTarget() {
BeanFactory bf = new XmlBeanFactory(new ClassPathResource(CONTEXT, CLASS));
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(CONTEXT, CLASS));
ITestBean bean1 = (ITestBean) bf.getBean("prototypeTestBeanProxySingletonTarget");
ITestBean bean2 = (ITestBean) bf.getBean("prototypeTestBeanProxySingletonTarget");
@ -671,13 +696,15 @@ public final class ProxyFactoryBeanTests {
*/
@Test
public void testInnerBeanTargetUsingAutowiring() {
BeanFactory bf = new XmlBeanFactory(new ClassPathResource(AUTOWIRING_CONTEXT, CLASS));
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(AUTOWIRING_CONTEXT, CLASS));
bf.getBean("testBean");
}
@Test
public void testFrozenFactoryBean() {
BeanFactory bf = new XmlBeanFactory(new ClassPathResource(FROZEN_CONTEXT, CLASS));
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(FROZEN_CONTEXT, CLASS));
Advised advised = (Advised)bf.getBean("frozen");
assertTrue("The proxy should be frozen", advised.isFrozen());

View File

@ -55,7 +55,8 @@ public final class AutoProxyCreatorTests {
proxyCreator.getPropertyValues().add("beanNames", "singletonToBeProxied,innerBean,singletonFactoryToBeProxied");
sac.getDefaultListableBeanFactory().registerBeanDefinition("beanNameAutoProxyCreator", proxyCreator);
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, RootBeanDefinition.AUTOWIRE_BY_TYPE);
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE);
RootBeanDefinition innerBean = new RootBeanDefinition(TestBean.class);
bd.getPropertyValues().add("spouse", new BeanDefinitionHolder(innerBean, "innerBean"));
sac.getDefaultListableBeanFactory().registerBeanDefinition("singletonToBeProxied", bd);

View File

@ -16,19 +16,21 @@
package org.springframework.aop.scope;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.ITestBean;
import org.springframework.beans.TestBean;
import org.springframework.beans.factory.config.SimpleMapScope;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.SerializationTestUtils;
@ -51,14 +53,16 @@ public class ScopedProxyTests {
/* SPR-2108 */
@Test
public void testProxyAssignable() throws Exception {
XmlBeanFactory bf = new XmlBeanFactory(MAP_CONTEXT);
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(MAP_CONTEXT);
Object baseMap = bf.getBean("singletonMap");
assertTrue(baseMap instanceof Map);
}
@Test
public void testSimpleProxy() throws Exception {
XmlBeanFactory bf = new XmlBeanFactory(MAP_CONTEXT);
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(MAP_CONTEXT);
Object simpleMap = bf.getBean("simpleMap");
assertTrue(simpleMap instanceof Map);
assertTrue(simpleMap instanceof HashMap);
@ -82,7 +86,8 @@ public class ScopedProxyTests {
@Test
public void testJdkScopedProxy() throws Exception {
XmlBeanFactory bf = new XmlBeanFactory(TESTBEAN_CONTEXT);
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(TESTBEAN_CONTEXT);
bf.setSerializationId("X");
SimpleMapScope scope = new SimpleMapScope();
bf.registerScope("request", scope);
@ -111,7 +116,8 @@ public class ScopedProxyTests {
@Test
public void testCglibScopedProxy() throws Exception {
XmlBeanFactory bf = new XmlBeanFactory(LIST_CONTEXT);
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(LIST_CONTEXT);
bf.setSerializationId("Y");
SimpleMapScope scope = new SimpleMapScope();
bf.registerScope("request", scope);

View File

@ -16,7 +16,10 @@
package org.springframework.aop.target;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.NoSuchElementException;
@ -27,7 +30,8 @@ import org.junit.Test;
import org.springframework.aop.framework.Advised;
import org.springframework.beans.Person;
import org.springframework.beans.SerializablePerson;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.SerializationTestUtils;
@ -50,11 +54,13 @@ public class CommonsPoolTargetSourceTests {
*/
private static final int INITIAL_COUNT = 10;
private XmlBeanFactory beanFactory;
private DefaultListableBeanFactory beanFactory;
@Before
public void setUp() throws Exception {
this.beanFactory = new XmlBeanFactory(new ClassPathResource(getClass().getSimpleName() + "-context.xml", getClass()));
this.beanFactory = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(this.beanFactory).loadBeanDefinitions(
new ClassPathResource(getClass().getSimpleName() + "-context.xml", getClass()));
}
/**

View File

@ -20,7 +20,6 @@ import java.beans.PropertyEditorSupport;
import java.util.StringTokenizer;
import junit.framework.TestCase;
import junit.framework.Assert;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyBatchUpdateException;
@ -79,7 +78,7 @@ public abstract class AbstractBeanFactoryTests extends TestCase {
*/
public void testLifecycleCallbacks() {
LifecycleBean lb = (LifecycleBean) getBeanFactory().getBean("lifecycle");
Assert.assertEquals("lifecycle", lb.getBeanName());
assertEquals("lifecycle", lb.getBeanName());
// The dummy business method will throw an exception if the
// necessary callbacks weren't invoked in the right order.
lb.businessMethod();
@ -327,4 +326,4 @@ public abstract class AbstractBeanFactoryTests extends TestCase {
}
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2012 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.
@ -16,8 +16,6 @@
package org.springframework.beans.factory;
import junit.framework.Assert;
import org.springframework.beans.TestBean;
/**
@ -44,24 +42,24 @@ public abstract class AbstractListableBeanFactoryTests extends AbstractBeanFacto
protected final void assertCount(int count) {
String[] defnames = getListableBeanFactory().getBeanDefinitionNames();
Assert.assertTrue("We should have " + count + " beans, not " + defnames.length, defnames.length == count);
assertTrue("We should have " + count + " beans, not " + defnames.length, defnames.length == count);
}
public void assertTestBeanCount(int count) {
String[] defNames = getListableBeanFactory().getBeanNamesForType(TestBean.class, true, false);
Assert.assertTrue("We should have " + count + " beans for class org.springframework.beans.TestBean, not " +
assertTrue("We should have " + count + " beans for class org.springframework.beans.TestBean, not " +
defNames.length, defNames.length == count);
int countIncludingFactoryBeans = count + 2;
String[] names = getListableBeanFactory().getBeanNamesForType(TestBean.class, true, true);
Assert.assertTrue("We should have " + countIncludingFactoryBeans +
assertTrue("We should have " + countIncludingFactoryBeans +
" beans for class org.springframework.beans.TestBean, not " + names.length,
names.length == countIncludingFactoryBeans);
}
public void testGetDefinitionsForNoSuchClass() {
String[] defnames = getListableBeanFactory().getBeanNamesForType(String.class);
Assert.assertTrue("No string definitions", defnames.length == 0);
assertTrue("No string definitions", defnames.length == 0);
}
/**
@ -69,18 +67,18 @@ public abstract class AbstractListableBeanFactoryTests extends AbstractBeanFacto
* what type factories may return, and it may even change over time.)
*/
public void testGetCountForFactoryClass() {
Assert.assertTrue("Should have 2 factories, not " +
assertTrue("Should have 2 factories, not " +
getListableBeanFactory().getBeanNamesForType(FactoryBean.class).length,
getListableBeanFactory().getBeanNamesForType(FactoryBean.class).length == 2);
Assert.assertTrue("Should have 2 factories, not " +
assertTrue("Should have 2 factories, not " +
getListableBeanFactory().getBeanNamesForType(FactoryBean.class).length,
getListableBeanFactory().getBeanNamesForType(FactoryBean.class).length == 2);
}
public void testContainsBeanDefinition() {
Assert.assertTrue(getListableBeanFactory().containsBeanDefinition("rod"));
Assert.assertTrue(getListableBeanFactory().containsBeanDefinition("roderick"));
assertTrue(getListableBeanFactory().containsBeanDefinition("rod"));
assertTrue(getListableBeanFactory().containsBeanDefinition("roderick"));
}
}
}

View File

@ -19,12 +19,11 @@ package org.springframework.beans.factory.parsing;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.springframework.core.CollectionFactory;
/**
* @author Rob Harrop
* @author Juergen Hoeller
@ -33,9 +32,9 @@ public class CollectingReaderEventListener implements ReaderEventListener {
private final List<DefaultsDefinition> defaults = new LinkedList<DefaultsDefinition>();
private final Map<String, ComponentDefinition> componentDefinitions = CollectionFactory.createLinkedMapIfPossible(8);
private final Map<String, ComponentDefinition> componentDefinitions = new LinkedHashMap<>(8);
private final Map<String, List<AliasDefinition>> aliasMap = CollectionFactory.createLinkedMapIfPossible(8);
private final Map<String, List<AliasDefinition>> aliasMap = new LinkedHashMap<>(8);
private final List<ImportDefinition> imports = new LinkedList<ImportDefinition>();

View File

@ -116,7 +116,6 @@ public final class XmlBeanFactoryTests {
private static final ClassPathResource NO_SUCH_FACTORY_METHOD_CONTEXT = classPathResource("-noSuchFactoryMethod.xml");
private static final ClassPathResource RECURSIVE_IMPORT_CONTEXT = classPathResource("-recursiveImport.xml");
private static final ClassPathResource RESOURCE_CONTEXT = classPathResource("-resource.xml");
private static final ClassPathResource RESOURCE_IMPORT_CONTEXT = classPathResource("-resourceImport.xml");
private static final ClassPathResource SATISFIED_ALL_DEP_CONTEXT = classPathResource("-satisfiedAllDepCheck.xml");
private static final ClassPathResource SATISFIED_OBJECT_DEP_CONTEXT = classPathResource("-satisfiedObjectDepCheck.xml");
private static final ClassPathResource SATISFIED_SIMPLE_DEP_CONTEXT = classPathResource("-satisfiedSimpleDepCheck.xml");
@ -135,7 +134,8 @@ public final class XmlBeanFactoryTests {
/* SPR-2368 */
public @Test void testCollectionsReferredToAsRefLocals() throws Exception {
XmlBeanFactory factory = new XmlBeanFactory(COLLECTIONS_XSD_CONTEXT);
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(factory).loadBeanDefinitions(COLLECTIONS_XSD_CONTEXT);
factory.preInstantiateSingletons();
}
@ -296,8 +296,10 @@ public final class XmlBeanFactoryTests {
}
public @Test void testInheritanceFromParentFactoryPrototype() throws Exception {
XmlBeanFactory parent = new XmlBeanFactory(PARENT_CONTEXT);
XmlBeanFactory child = new XmlBeanFactory(CHILD_CONTEXT, parent);
DefaultListableBeanFactory parent = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(parent).loadBeanDefinitions(PARENT_CONTEXT);
DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
new XmlBeanDefinitionReader(child).loadBeanDefinitions(CHILD_CONTEXT);
assertEquals(TestBean.class, child.getType("inheritsFromParentFactory"));
TestBean inherits = (TestBean) child.getBean("inheritsFromParentFactory");
// Name property value is overridden
@ -309,8 +311,10 @@ public final class XmlBeanFactoryTests {
}
public @Test void testInheritanceWithDifferentClass() throws Exception {
XmlBeanFactory parent = new XmlBeanFactory(PARENT_CONTEXT);
XmlBeanFactory child = new XmlBeanFactory(CHILD_CONTEXT, parent);
DefaultListableBeanFactory parent = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(parent).loadBeanDefinitions(PARENT_CONTEXT);
DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
new XmlBeanDefinitionReader(child).loadBeanDefinitions(CHILD_CONTEXT);
assertEquals(DerivedTestBean.class, child.getType("inheritsWithClass"));
DerivedTestBean inherits = (DerivedTestBean) child.getBean("inheritsWithDifferentClass");
// Name property value is overridden
@ -321,8 +325,10 @@ public final class XmlBeanFactoryTests {
}
public @Test void testInheritanceWithClass() throws Exception {
XmlBeanFactory parent = new XmlBeanFactory(PARENT_CONTEXT);
XmlBeanFactory child = new XmlBeanFactory(CHILD_CONTEXT, parent);
DefaultListableBeanFactory parent = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(parent).loadBeanDefinitions(PARENT_CONTEXT);
DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
new XmlBeanDefinitionReader(child).loadBeanDefinitions(CHILD_CONTEXT);
assertEquals(DerivedTestBean.class, child.getType("inheritsWithClass"));
DerivedTestBean inherits = (DerivedTestBean) child.getBean("inheritsWithClass");
// Name property value is overridden
@ -333,8 +339,10 @@ public final class XmlBeanFactoryTests {
}
public @Test void testPrototypeInheritanceFromParentFactoryPrototype() throws Exception {
XmlBeanFactory parent = new XmlBeanFactory(PARENT_CONTEXT);
XmlBeanFactory child = new XmlBeanFactory(CHILD_CONTEXT, parent);
DefaultListableBeanFactory parent = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(parent).loadBeanDefinitions(PARENT_CONTEXT);
DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
new XmlBeanDefinitionReader(child).loadBeanDefinitions(CHILD_CONTEXT);
assertEquals(TestBean.class, child.getType("prototypeInheritsFromParentFactoryPrototype"));
TestBean inherits = (TestBean) child.getBean("prototypeInheritsFromParentFactoryPrototype");
// Name property value is overridden
@ -350,8 +358,10 @@ public final class XmlBeanFactoryTests {
}
public @Test void testPrototypeInheritanceFromParentFactorySingleton() throws Exception {
XmlBeanFactory parent = new XmlBeanFactory(PARENT_CONTEXT);
XmlBeanFactory child = new XmlBeanFactory(CHILD_CONTEXT, parent);
DefaultListableBeanFactory parent = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(parent).loadBeanDefinitions(PARENT_CONTEXT);
DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
new XmlBeanDefinitionReader(child).loadBeanDefinitions(CHILD_CONTEXT);
TestBean inherits = (TestBean) child.getBean("protoypeInheritsFromParentFactorySingleton");
// Name property value is overridden
assertTrue(inherits.getName().equals("prototypeOverridesInheritedSingleton"));
@ -380,7 +390,8 @@ public final class XmlBeanFactoryTests {
}
public @Test void testAbstractParentBeans() {
XmlBeanFactory parent = new XmlBeanFactory(PARENT_CONTEXT);
DefaultListableBeanFactory parent = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(parent).loadBeanDefinitions(PARENT_CONTEXT);
parent.preInstantiateSingletons();
assertTrue(parent.isSingleton("inheritedTestBeanWithoutClass"));
@ -404,7 +415,8 @@ public final class XmlBeanFactoryTests {
}
public @Test void testDependenciesMaterializeThis() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(DEP_MATERIALIZE_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(DEP_MATERIALIZE_CONTEXT);
assertEquals(2, xbf.getBeansOfType(DummyBo.class, true, false).size());
assertEquals(3, xbf.getBeansOfType(DummyBo.class, true, true).size());
@ -421,8 +433,10 @@ public final class XmlBeanFactoryTests {
}
public @Test void testChildOverridesParentBean() throws Exception {
XmlBeanFactory parent = new XmlBeanFactory(PARENT_CONTEXT);
XmlBeanFactory child = new XmlBeanFactory(CHILD_CONTEXT, parent);
DefaultListableBeanFactory parent = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(parent).loadBeanDefinitions(PARENT_CONTEXT);
DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
new XmlBeanDefinitionReader(child).loadBeanDefinitions(CHILD_CONTEXT);
TestBean inherits = (TestBean) child.getBean("inheritedTestBean");
// Name property value is overridden
assertTrue(inherits.getName().equals("overrideParentBean"));
@ -437,8 +451,10 @@ public final class XmlBeanFactoryTests {
* If a singleton does this the factory will fail to load.
*/
public @Test void testBogusParentageFromParentFactory() throws Exception {
XmlBeanFactory parent = new XmlBeanFactory(PARENT_CONTEXT);
XmlBeanFactory child = new XmlBeanFactory(CHILD_CONTEXT, parent);
DefaultListableBeanFactory parent = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(parent).loadBeanDefinitions(PARENT_CONTEXT);
DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
new XmlBeanDefinitionReader(child).loadBeanDefinitions(CHILD_CONTEXT);
try {
child.getBean("bogusParent", TestBean.class);
fail();
@ -456,8 +472,10 @@ public final class XmlBeanFactoryTests {
* instances even if derived from a prototype
*/
public @Test void testSingletonInheritsFromParentFactoryPrototype() throws Exception {
XmlBeanFactory parent = new XmlBeanFactory(PARENT_CONTEXT);
XmlBeanFactory child = new XmlBeanFactory(CHILD_CONTEXT, parent);
DefaultListableBeanFactory parent = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(parent).loadBeanDefinitions(PARENT_CONTEXT);
DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
new XmlBeanDefinitionReader(child).loadBeanDefinitions(CHILD_CONTEXT);
TestBean inherits = (TestBean) child.getBean("singletonInheritsFromParentFactoryPrototype");
// Name property value is overriden
assertTrue(inherits.getName().equals("prototype-override"));
@ -468,16 +486,20 @@ public final class XmlBeanFactoryTests {
}
public @Test void testSingletonFromParent() {
XmlBeanFactory parent = new XmlBeanFactory(PARENT_CONTEXT);
DefaultListableBeanFactory parent = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(parent).loadBeanDefinitions(PARENT_CONTEXT);
TestBean beanFromParent = (TestBean) parent.getBean("inheritedTestBeanSingleton");
XmlBeanFactory child = new XmlBeanFactory(CHILD_CONTEXT, parent);
DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
new XmlBeanDefinitionReader(child).loadBeanDefinitions(CHILD_CONTEXT);
TestBean beanFromChild = (TestBean) child.getBean("inheritedTestBeanSingleton");
assertTrue("singleton from parent and child is the same", beanFromParent == beanFromChild);
}
public @Test void testNestedPropertyValue() {
XmlBeanFactory parent = new XmlBeanFactory(PARENT_CONTEXT);
XmlBeanFactory child = new XmlBeanFactory(CHILD_CONTEXT, parent);
DefaultListableBeanFactory parent = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(parent).loadBeanDefinitions(PARENT_CONTEXT);
DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
new XmlBeanDefinitionReader(child).loadBeanDefinitions(CHILD_CONTEXT);
IndexedTestBean bean = (IndexedTestBean) child.getBean("indexedTestBean");
assertEquals("name applied correctly", "myname", bean.getArray()[0].getName());
}
@ -571,19 +593,22 @@ public final class XmlBeanFactoryTests {
}
public @Test void testFactoryReferenceCircle() {
XmlBeanFactory xbf = new XmlBeanFactory(FACTORY_CIRCLE_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(FACTORY_CIRCLE_CONTEXT);
TestBean tb = (TestBean) xbf.getBean("singletonFactory");
DummyFactory db = (DummyFactory) xbf.getBean("&singletonFactory");
assertTrue(tb == db.getOtherTestBean());
}
public @Test void testFactoryReferenceWithDoublePrefix() {
XmlBeanFactory xbf = new XmlBeanFactory(FACTORY_CIRCLE_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(FACTORY_CIRCLE_CONTEXT);
assertThat(xbf.getBean("&&singletonFactory"), instanceOf(DummyFactory.class));
}
public @Test void testComplexFactoryReferenceCircle() {
XmlBeanFactory xbf = new XmlBeanFactory(COMPLEX_FACTORY_CIRCLE_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(COMPLEX_FACTORY_CIRCLE_CONTEXT);
xbf.getBean("proxy1");
// check that unused instances from autowiring got removed
assertEquals(4, xbf.getSingletonCount());
@ -594,7 +619,8 @@ public final class XmlBeanFactoryTests {
public @Test void testNoSuchFactoryBeanMethod() {
try {
XmlBeanFactory xbf = new XmlBeanFactory(NO_SUCH_FACTORY_METHOD_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(NO_SUCH_FACTORY_METHOD_CONTEXT);
assertNotNull(xbf.getBean("defaultTestBean"));
fail("Should not get invalid bean");
}
@ -604,7 +630,8 @@ public final class XmlBeanFactoryTests {
}
public @Test void testInitMethodIsInvoked() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(INITIALIZERS_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(INITIALIZERS_CONTEXT);
DoubleInitializer in = (DoubleInitializer) xbf.getBean("init-method1");
// Initializer should have doubled value
assertEquals(14, in.getNum());
@ -614,7 +641,8 @@ public final class XmlBeanFactoryTests {
* Test that if a custom initializer throws an exception, it's handled correctly
*/
public @Test void testInitMethodThrowsException() {
XmlBeanFactory xbf = new XmlBeanFactory(INITIALIZERS_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(INITIALIZERS_CONTEXT);
try {
xbf.getBean("init-method2");
fail();
@ -627,7 +655,8 @@ public final class XmlBeanFactoryTests {
}
public @Test void testNoSuchInitMethod() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(INITIALIZERS_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(INITIALIZERS_CONTEXT);
try {
xbf.getBean("init-method3");
fail();
@ -645,7 +674,8 @@ public final class XmlBeanFactoryTests {
*/
public @Test void testInitializingBeanAndInitMethod() throws Exception {
InitAndIB.constructed = false;
XmlBeanFactory xbf = new XmlBeanFactory(INITIALIZERS_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(INITIALIZERS_CONTEXT);
assertFalse(InitAndIB.constructed);
xbf.preInstantiateSingletons();
assertFalse(InitAndIB.constructed);
@ -664,7 +694,8 @@ public final class XmlBeanFactoryTests {
*/
public @Test void testInitializingBeanAndSameInitMethod() throws Exception {
InitAndIB.constructed = false;
XmlBeanFactory xbf = new XmlBeanFactory(INITIALIZERS_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(INITIALIZERS_CONTEXT);
assertFalse(InitAndIB.constructed);
xbf.preInstantiateSingletons();
assertFalse(InitAndIB.constructed);
@ -680,7 +711,8 @@ public final class XmlBeanFactoryTests {
public @Test void testDefaultLazyInit() throws Exception {
InitAndIB.constructed = false;
XmlBeanFactory xbf = new XmlBeanFactory(DEFAULT_LAZY_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(DEFAULT_LAZY_CONTEXT);
assertFalse(InitAndIB.constructed);
xbf.preInstantiateSingletons();
assertTrue(InitAndIB.constructed);
@ -693,8 +725,9 @@ public final class XmlBeanFactoryTests {
}
public @Test void testNoSuchXmlFile() throws Exception {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
try {
new XmlBeanFactory(MISSING_CONTEXT);
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(MISSING_CONTEXT);
fail("Must not create factory from missing XML");
}
catch (BeanDefinitionStoreException expected) {
@ -702,8 +735,9 @@ public final class XmlBeanFactoryTests {
}
public @Test void testInvalidXmlFile() throws Exception {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
try {
new XmlBeanFactory(INVALID_CONTEXT);
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(INVALID_CONTEXT);
fail("Must not create factory from invalid XML");
}
catch (BeanDefinitionStoreException expected) {
@ -711,8 +745,9 @@ public final class XmlBeanFactoryTests {
}
public @Test void testUnsatisfiedObjectDependencyCheck() throws Exception {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
try {
XmlBeanFactory xbf = new XmlBeanFactory(UNSATISFIED_OBJECT_DEP_CONTEXT);
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(UNSATISFIED_OBJECT_DEP_CONTEXT);
xbf.getBean("a", DependenciesBean.class);
fail("Must have thrown an UnsatisfiedDependencyException");
}
@ -721,8 +756,9 @@ public final class XmlBeanFactoryTests {
}
public @Test void testUnsatisfiedSimpleDependencyCheck() throws Exception {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
try {
XmlBeanFactory xbf = new XmlBeanFactory(UNSATISFIED_SIMPLE_DEP_CONTEXT);
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(UNSATISFIED_SIMPLE_DEP_CONTEXT);
xbf.getBean("a", DependenciesBean.class);
fail("Must have thrown an UnsatisfiedDependencyException");
}
@ -731,21 +767,24 @@ public final class XmlBeanFactoryTests {
}
public @Test void testSatisfiedObjectDependencyCheck() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(SATISFIED_OBJECT_DEP_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(SATISFIED_OBJECT_DEP_CONTEXT);
DependenciesBean a = (DependenciesBean) xbf.getBean("a");
assertNotNull(a.getSpouse());
assertEquals(xbf, a.getBeanFactory());
}
public @Test void testSatisfiedSimpleDependencyCheck() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(SATISFIED_SIMPLE_DEP_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(SATISFIED_SIMPLE_DEP_CONTEXT);
DependenciesBean a = (DependenciesBean) xbf.getBean("a");
assertEquals(a.getAge(), 33);
}
public @Test void testUnsatisfiedAllDependencyCheck() throws Exception {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
try {
XmlBeanFactory xbf = new XmlBeanFactory(UNSATISFIED_ALL_DEP_CONTEXT);
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(UNSATISFIED_ALL_DEP_CONTEXT);
xbf.getBean("a", DependenciesBean.class);
fail("Must have thrown an UnsatisfiedDependencyException");
}
@ -754,7 +793,8 @@ public final class XmlBeanFactoryTests {
}
public @Test void testSatisfiedAllDependencyCheck() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(SATISFIED_ALL_DEP_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(SATISFIED_ALL_DEP_CONTEXT);
DependenciesBean a = (DependenciesBean) xbf.getBean("a");
assertEquals(a.getAge(), 33);
assertNotNull(a.getName());
@ -762,23 +802,27 @@ public final class XmlBeanFactoryTests {
}
public @Test void testAutowire() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(AUTOWIRE_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(AUTOWIRE_CONTEXT);
TestBean spouse = new TestBean("kerry", 0);
xbf.registerSingleton("spouse", spouse);
doTestAutowire(xbf);
}
public @Test void testAutowireWithParent() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(AUTOWIRE_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(AUTOWIRE_CONTEXT);
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("name", "kerry");
lbf.registerBeanDefinition("spouse", new RootBeanDefinition(TestBean.class, pvs));
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.setPropertyValues(pvs);
lbf.registerBeanDefinition("spouse", bd);
xbf.setParentBeanFactory(lbf);
doTestAutowire(xbf);
}
private void doTestAutowire(XmlBeanFactory xbf) throws Exception {
private void doTestAutowire(DefaultListableBeanFactory xbf) throws Exception {
DependenciesBean rod1 = (DependenciesBean) xbf.getBean("rod1");
TestBean kerry = (TestBean) xbf.getBean("spouse");
// should have been autowired
@ -827,7 +871,8 @@ public final class XmlBeanFactoryTests {
}
public @Test void testAutowireWithDefault() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(DEFAULT_AUTOWIRE_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(DEFAULT_AUTOWIRE_CONTEXT);
DependenciesBean rod1 = (DependenciesBean) xbf.getBean("rod1");
// should have been autowired
@ -848,7 +893,8 @@ public final class XmlBeanFactoryTests {
}
public @Test void testAutowireByConstructor() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(CONSTRUCTOR_ARG_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
ConstructorDependenciesBean rod1 = (ConstructorDependenciesBean) xbf.getBean("rod1");
TestBean kerry = (TestBean) xbf.getBean("kerry2");
// should have been autowired
@ -884,7 +930,8 @@ public final class XmlBeanFactoryTests {
}
public @Test void testAutowireByConstructorWithSimpleValues() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(CONSTRUCTOR_ARG_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
ConstructorDependenciesBean rod5 = (ConstructorDependenciesBean) xbf.getBean("rod5");
TestBean kerry1 = (TestBean) xbf.getBean("kerry1");
@ -912,7 +959,8 @@ public final class XmlBeanFactoryTests {
}
public @Test void testRelatedCausesFromConstructorResolution() {
XmlBeanFactory xbf = new XmlBeanFactory(CONSTRUCTOR_ARG_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
try {
xbf.getBean("rod2Accessor");
@ -925,7 +973,8 @@ public final class XmlBeanFactoryTests {
}
public @Test void testConstructorArgResolution() {
XmlBeanFactory xbf = new XmlBeanFactory(CONSTRUCTOR_ARG_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
TestBean kerry1 = (TestBean) xbf.getBean("kerry1");
TestBean kerry2 = (TestBean) xbf.getBean("kerry2");
@ -972,7 +1021,8 @@ public final class XmlBeanFactoryTests {
}
public @Test void testPrototypeWithExplicitArguments() {
XmlBeanFactory xbf = new XmlBeanFactory(CONSTRUCTOR_ARG_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
SimpleConstructorArgBean cd1 = (SimpleConstructorArgBean) xbf.getBean("rod18");
assertEquals(0, cd1.getAge());
SimpleConstructorArgBean cd2 = (SimpleConstructorArgBean) xbf.getBean("rod18", 98);
@ -986,13 +1036,15 @@ public final class XmlBeanFactoryTests {
}
public @Test void testConstructorArgWithSingleMatch() {
XmlBeanFactory xbf = new XmlBeanFactory(CONSTRUCTOR_ARG_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
File file = (File) xbf.getBean("file");
assertEquals(File.separator + "test", file.getPath());
}
public @Test void testThrowsExceptionOnTooManyArguments() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(CONSTRUCTOR_ARG_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
try {
xbf.getBean("rod7", ConstructorDependenciesBean.class);
fail("Should have thrown BeanCreationException");
@ -1002,7 +1054,8 @@ public final class XmlBeanFactoryTests {
}
public @Test void testThrowsExceptionOnAmbiguousResolution() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(CONSTRUCTOR_ARG_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
try {
xbf.getBean("rod8", ConstructorDependenciesBean.class);
fail("Must have thrown UnsatisfiedDependencyException");
@ -1058,7 +1111,8 @@ public final class XmlBeanFactoryTests {
PreparingBean2.destroyed = false;
DependingBean.destroyCount = 0;
HoldingBean.destroyCount = 0;
XmlBeanFactory xbf = new XmlBeanFactory(resource);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(resource);
xbf.preInstantiateSingletons();
xbf.destroySingletons();
assertTrue(PreparingBean1.prepared);
@ -1078,7 +1132,8 @@ public final class XmlBeanFactoryTests {
* must rather only be picked up when the bean is instantiated.
*/
public @Test void testClassNotFoundWithDefaultBeanClassLoader() {
BeanFactory factory = new XmlBeanFactory(CLASS_NOT_FOUND_CONTEXT);
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(factory).loadBeanDefinitions(CLASS_NOT_FOUND_CONTEXT);
// cool, no errors, so the rubbish class name in the bean def was not resolved
try {
// let's resolve the bean definition; must blow up
@ -1100,7 +1155,8 @@ public final class XmlBeanFactoryTests {
}
public @Test void testResourceAndInputStream() throws IOException {
XmlBeanFactory xbf = new XmlBeanFactory(RESOURCE_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(RESOURCE_CONTEXT);
// comes from "resourceImport.xml"
ResourceTestBean resource1 = (ResourceTestBean) xbf.getBean("resource1");
// comes from "resource.xml"
@ -1122,7 +1178,8 @@ public final class XmlBeanFactoryTests {
}
public @Test void testClassPathResourceWithImport() {
XmlBeanFactory xbf = new XmlBeanFactory(RESOURCE_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(RESOURCE_CONTEXT);
// comes from "resourceImport.xml"
xbf.getBean("resource1", ResourceTestBean.class);
// comes from "resource.xml"
@ -1131,7 +1188,8 @@ public final class XmlBeanFactoryTests {
public @Test void testUrlResourceWithImport() {
URL url = getClass().getResource(RESOURCE_CONTEXT.getPath());
XmlBeanFactory xbf = new XmlBeanFactory(new UrlResource(url));
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(new UrlResource(url));
// comes from "resourceImport.xml"
xbf.getBean("resource1", ResourceTestBean.class);
// comes from "resource.xml"
@ -1140,7 +1198,8 @@ public final class XmlBeanFactoryTests {
public @Test void testFileSystemResourceWithImport() throws URISyntaxException {
String file = getClass().getResource(RESOURCE_CONTEXT.getPath()).toURI().getPath();
XmlBeanFactory xbf = new XmlBeanFactory(new FileSystemResource(file));
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(new FileSystemResource(file));
// comes from "resourceImport.xml"
xbf.getBean("resource1", ResourceTestBean.class);
// comes from "resource.xml"
@ -1148,8 +1207,9 @@ public final class XmlBeanFactoryTests {
}
public @Test void testRecursiveImport() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
try {
new XmlBeanFactory(RECURSIVE_IMPORT_CONTEXT);
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(RECURSIVE_IMPORT_CONTEXT);
fail("Should have thrown BeanDefinitionStoreException");
}
catch (BeanDefinitionStoreException ex) {
@ -1344,7 +1404,8 @@ public final class XmlBeanFactoryTests {
}
public @Test void testConstructorArgWithSingleSimpleTypeMatch() {
XmlBeanFactory xbf = new XmlBeanFactory(CONSTRUCTOR_ARG_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
SingleSimpleTypeConstructorBean bean = (SingleSimpleTypeConstructorBean) xbf.getBean("beanWithBoolean");
assertTrue(bean.isSingleBoolean());
@ -1354,7 +1415,8 @@ public final class XmlBeanFactoryTests {
}
public @Test void testConstructorArgWithDoubleSimpleTypeMatch() {
XmlBeanFactory xbf = new XmlBeanFactory(CONSTRUCTOR_ARG_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
SingleSimpleTypeConstructorBean bean = (SingleSimpleTypeConstructorBean) xbf.getBean("beanWithBooleanAndString");
assertTrue(bean.isSecondBoolean());
@ -1366,33 +1428,38 @@ public final class XmlBeanFactoryTests {
}
public @Test void testDoubleBooleanAutowire() {
XmlBeanFactory xbf = new XmlBeanFactory(CONSTRUCTOR_ARG_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
DoubleBooleanConstructorBean bean = (DoubleBooleanConstructorBean) xbf.getBean("beanWithDoubleBoolean");
assertEquals(Boolean.TRUE, bean.boolean1);
assertEquals(Boolean.FALSE, bean.boolean2);
}
public @Test void testDoubleBooleanAutowireWithIndex() {
XmlBeanFactory xbf = new XmlBeanFactory(CONSTRUCTOR_ARG_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
DoubleBooleanConstructorBean bean = (DoubleBooleanConstructorBean) xbf.getBean("beanWithDoubleBooleanAndIndex");
assertEquals(Boolean.FALSE, bean.boolean1);
assertEquals(Boolean.TRUE, bean.boolean2);
}
public @Test void testLenientDependencyMatching() {
XmlBeanFactory xbf = new XmlBeanFactory(CONSTRUCTOR_ARG_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
LenientDependencyTestBean bean = (LenientDependencyTestBean) xbf.getBean("lenientDependencyTestBean");
assertTrue(bean.tb instanceof DerivedTestBean);
}
public @Test void testLenientDependencyMatchingFactoryMethod() {
XmlBeanFactory xbf = new XmlBeanFactory(CONSTRUCTOR_ARG_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
LenientDependencyTestBean bean = (LenientDependencyTestBean) xbf.getBean("lenientDependencyTestBeanFactoryMethod");
assertTrue(bean.tb instanceof DerivedTestBean);
}
public @Test void testNonLenientDependencyMatching() {
XmlBeanFactory xbf = new XmlBeanFactory(CONSTRUCTOR_ARG_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
AbstractBeanDefinition bd = (AbstractBeanDefinition) xbf.getBeanDefinition("lenientDependencyTestBean");
bd.setLenientConstructorResolution(false);
try {
@ -1407,7 +1474,8 @@ public final class XmlBeanFactoryTests {
}
public @Test void testNonLenientDependencyMatchingFactoryMethod() {
XmlBeanFactory xbf = new XmlBeanFactory(CONSTRUCTOR_ARG_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
AbstractBeanDefinition bd = (AbstractBeanDefinition) xbf.getBeanDefinition("lenientDependencyTestBeanFactoryMethod");
bd.setLenientConstructorResolution(false);
try {
@ -1422,7 +1490,8 @@ public final class XmlBeanFactoryTests {
}
public @Test void testJavaLangStringConstructor() {
XmlBeanFactory xbf = new XmlBeanFactory(CONSTRUCTOR_ARG_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
AbstractBeanDefinition bd = (AbstractBeanDefinition) xbf.getBeanDefinition("string");
bd.setLenientConstructorResolution(false);
String str = (String) xbf.getBean("string");
@ -1430,7 +1499,8 @@ public final class XmlBeanFactoryTests {
}
public @Test void testCustomStringConstructor() {
XmlBeanFactory xbf = new XmlBeanFactory(CONSTRUCTOR_ARG_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
AbstractBeanDefinition bd = (AbstractBeanDefinition) xbf.getBeanDefinition("stringConstructor");
bd.setLenientConstructorResolution(false);
StringConstructorTestBean tb = (StringConstructorTestBean) xbf.getBean("stringConstructor");
@ -1438,7 +1508,8 @@ public final class XmlBeanFactoryTests {
}
public @Test void testPrimitiveConstructorArray() {
XmlBeanFactory xbf = new XmlBeanFactory(CONSTRUCTOR_ARG_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
ConstructorArrayTestBean bean = (ConstructorArrayTestBean) xbf.getBean("constructorArray");
assertTrue(bean.array instanceof int[]);
assertEquals(1, ((int[]) bean.array).length);
@ -1446,7 +1517,8 @@ public final class XmlBeanFactoryTests {
}
public @Test void testIndexedPrimitiveConstructorArray() {
XmlBeanFactory xbf = new XmlBeanFactory(CONSTRUCTOR_ARG_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
ConstructorArrayTestBean bean = (ConstructorArrayTestBean) xbf.getBean("indexedConstructorArray");
assertTrue(bean.array instanceof int[]);
assertEquals(1, ((int[]) bean.array).length);
@ -1454,14 +1526,16 @@ public final class XmlBeanFactoryTests {
}
public @Test void testStringConstructorArrayNoType() {
XmlBeanFactory xbf = new XmlBeanFactory(CONSTRUCTOR_ARG_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
ConstructorArrayTestBean bean = (ConstructorArrayTestBean) xbf.getBean("constructorArrayNoType");
assertTrue(bean.array instanceof String[]);
assertEquals(0, ((String[]) bean.array).length);
}
public @Test void testStringConstructorArrayNoTypeNonLenient() {
XmlBeanFactory xbf = new XmlBeanFactory(CONSTRUCTOR_ARG_CONTEXT);
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
AbstractBeanDefinition bd = (AbstractBeanDefinition) xbf.getBeanDefinition("constructorArrayNoType");
bd.setLenientConstructorResolution(false);
ConstructorArrayTestBean bean = (ConstructorArrayTestBean) xbf.getBean("constructorArrayNoType");
@ -1470,8 +1544,9 @@ public final class XmlBeanFactoryTests {
}
public @Test void testWithDuplicateName() throws Exception {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
try {
new XmlBeanFactory(TEST_WITH_DUP_NAMES_CONTEXT);
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(TEST_WITH_DUP_NAMES_CONTEXT);
fail("Duplicate name not detected");
}
catch (BeansException ex) {
@ -1480,8 +1555,9 @@ public final class XmlBeanFactoryTests {
}
public @Test void testWithDuplicateNameInAlias() throws Exception {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
try {
new XmlBeanFactory(TEST_WITH_DUP_NAME_IN_ALIAS_CONTEXT);
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(TEST_WITH_DUP_NAME_IN_ALIAS_CONTEXT);
fail("Duplicate name not detected");
}
catch (BeansException e) {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2010-2011 the original author or authors.
* Copyright 2002-2012 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.
@ -16,7 +16,7 @@
package org.springframework.cache.config;
import junit.framework.Assert;
import static org.junit.Assert.assertSame;
import org.junit.Test;
import org.springframework.cache.interceptor.CacheInterceptor;
@ -39,6 +39,6 @@ public class AnnotationNamespaceDrivenTests extends AbstractAnnotationTests {
public void testKeyStrategy() throws Exception {
CacheInterceptor ci = ctx.getBean("org.springframework.cache.interceptor.CacheInterceptor#0",
CacheInterceptor.class);
Assert.assertSame(ctx.getBean("keyGenerator"), ci.getKeyGenerator());
assertSame(ctx.getBean("keyGenerator"), ci.getKeyGenerator());
}
}

View File

@ -16,12 +16,11 @@
package org.springframework.cache.config;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import junit.framework.Assert;
import org.junit.Test;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.cache.CacheManager;
@ -53,7 +52,7 @@ public class EnableCachingTests extends AbstractAnnotationTests {
@Test
public void testKeyStrategy() throws Exception {
CacheInterceptor ci = ctx.getBean(CacheInterceptor.class);
Assert.assertSame(ctx.getBean(KeyGenerator.class), ci.getKeyGenerator());
assertSame(ctx.getBean(KeyGenerator.class), ci.getKeyGenerator());
}
// --- local tests -------

View File

@ -16,14 +16,16 @@
package org.springframework.context.access;
import static org.junit.Assert.*;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.access.BeanFactoryLocator;
import org.springframework.beans.factory.access.BeanFactoryReference;
import org.springframework.beans.factory.access.SingletonBeanFactoryLocatorTests;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ClassUtils;
@ -43,8 +45,10 @@ public class ContextSingletonBeanFactoryLocatorTests extends SingletonBeanFactor
public void testBaseBeanFactoryDefs() {
// Just test the base BeanFactory/AppContext defs we are going to work
// with in other tests.
new XmlBeanFactory(new ClassPathResource("/org/springframework/beans/factory/access/beans1.xml"));
new XmlBeanFactory(new ClassPathResource("/org/springframework/beans/factory/access/beans2.xml"));
new XmlBeanDefinitionReader(new DefaultListableBeanFactory()).loadBeanDefinitions(new ClassPathResource(
"/org/springframework/beans/factory/access/beans1.xml"));
new XmlBeanDefinitionReader(new DefaultListableBeanFactory()).loadBeanDefinitions(new ClassPathResource(
"/org/springframework/beans/factory/access/beans2.xml"));
}
@Override

View File

@ -34,6 +34,7 @@ import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
@ -171,9 +172,15 @@ public class CommonAnnotationBeanPostProcessorTests {
CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
bpp.setResourceFactory(bf);
bf.addBeanPostProcessor(bpp);
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ResourceInjectionBean.class, false));
bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class, false));
bf.registerBeanDefinition("testBean2", new RootBeanDefinition(TestBean.class, false));
RootBeanDefinition abd = new RootBeanDefinition(ResourceInjectionBean.class);
abd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
bf.registerBeanDefinition("annotatedBean", abd);
RootBeanDefinition tbd1 = new RootBeanDefinition(TestBean.class);
tbd1.setScope(BeanDefinition.SCOPE_PROTOTYPE);
bf.registerBeanDefinition("testBean", tbd1);
RootBeanDefinition tbd2 = new RootBeanDefinition(TestBean.class);
tbd2.setScope(BeanDefinition.SCOPE_PROTOTYPE);
bf.registerBeanDefinition("testBean2", tbd2);
ResourceInjectionBean bean = (ResourceInjectionBean) bf.getBean("annotatedBean");
assertTrue(bean.initCalled);
@ -202,8 +209,12 @@ public class CommonAnnotationBeanPostProcessorTests {
CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
bpp.setBeanFactory(bf);
bf.addBeanPostProcessor(bpp);
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ExtendedResourceInjectionBean.class, false));
bf.registerBeanDefinition("testBean4", new RootBeanDefinition(TestBean.class, false));
RootBeanDefinition abd = new RootBeanDefinition(ExtendedResourceInjectionBean.class);
abd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
bf.registerBeanDefinition("annotatedBean", abd);
RootBeanDefinition tbd = new RootBeanDefinition(TestBean.class);
tbd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
bf.registerBeanDefinition("testBean4", tbd);
bf.registerResolvableDependency(BeanFactory.class, bf);
bf.registerResolvableDependency(INestedTestBean.class, new ObjectFactory<Object>() {

View File

@ -25,8 +25,9 @@ import test.beans.TestBean;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
@ -79,7 +80,9 @@ public class AutowiredConfigurationTests {
*/
@Test(expected=BeanCreationException.class)
public void testAutowiredConfigurationConstructorsAreNotSupported() {
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("annotation-config.xml", AutowiredConstructorConfig.class));
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(factory).loadBeanDefinitions(
new ClassPathResource("annotation-config.xml", AutowiredConstructorConfig.class));
GenericApplicationContext ctx = new GenericApplicationContext(factory);
ctx.registerBeanDefinition("config1", new RootBeanDefinition(AutowiredConstructorConfig.class));
ctx.registerBeanDefinition("config2", new RootBeanDefinition(ColorConfig.class));

View File

@ -16,14 +16,15 @@
package org.springframework.context.annotation.configuration;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.junit.Test;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ConfigurationClassPostProcessor;
@ -47,8 +48,10 @@ import test.beans.TestBean;
*/
public class ConfigurationClassAspectIntegrationTests {
private void assertAdviceWasApplied(Class<?> configClass) {
GenericApplicationContext ctx = new GenericApplicationContext(
new XmlBeanFactory(new ClassPathResource("aspectj-autoproxy-config.xml", ConfigurationClassAspectIntegrationTests.class)));
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(factory).loadBeanDefinitions(
new ClassPathResource("aspectj-autoproxy-config.xml", ConfigurationClassAspectIntegrationTests.class));
GenericApplicationContext ctx = new GenericApplicationContext(factory);
ctx.addBeanFactoryPostProcessor(new ConfigurationClassPostProcessor());
ctx.registerBeanDefinition("config", new RootBeanDefinition(configClass));
ctx.refresh();

View File

@ -86,7 +86,7 @@ public class EventPublicationInterceptorTests {
class TestContext extends StaticApplicationContext {
@Override
protected void onRefresh() throws BeansException {
addListener(listener);
addApplicationListener(listener);
}
}

View File

@ -54,7 +54,7 @@ public class StaticApplicationContextMulticasterTests extends AbstractApplicatio
parent.registerSingleton(StaticApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME,
TestApplicationEventMulticaster.class, null);
parent.refresh();
parent.addListener(parentListener) ;
parent.addApplicationListener(parentListener) ;
parent.getStaticMessageSource().addMessage("code1", Locale.getDefault(), "message1");
@ -66,7 +66,7 @@ public class StaticApplicationContextMulticasterTests extends AbstractApplicatio
Resource resource = new ClassPathResource("testBeans.properties", getClass());
reader.loadBeanDefinitions(new EncodedResource(resource, "ISO-8859-1"));
sac.refresh();
sac.addListener(listener);
sac.addApplicationListener(listener);
sac.getStaticMessageSource().addMessage("code2", Locale.getDefault(), "message2");

View File

@ -48,7 +48,7 @@ public class StaticApplicationContextTests extends AbstractApplicationContextTes
m.put("name", "Albert");
parent.registerPrototype("father", TestBean.class, new MutablePropertyValues(m));
parent.refresh();
parent.addListener(parentListener) ;
parent.addApplicationListener(parentListener) ;
parent.getStaticMessageSource().addMessage("code1", Locale.getDefault(), "message1");
@ -59,7 +59,7 @@ public class StaticApplicationContextTests extends AbstractApplicationContextTes
PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(sac.getDefaultListableBeanFactory());
reader.loadBeanDefinitions(new ClassPathResource("testBeans.properties", getClass()));
sac.refresh();
sac.addListener(listener);
sac.addApplicationListener(listener);
sac.getStaticMessageSource().addMessage("code2", Locale.getDefault(), "message2");

View File

@ -208,7 +208,7 @@ public class StaticMessageSourceTests extends AbstractApplicationContextTests {
parent.registerPrototype("father", org.springframework.beans.TestBean.class, new MutablePropertyValues(m));
parent.refresh();
parent.addListener(parentListener);
parent.addApplicationListener(parentListener);
this.sac = new StaticApplicationContext(parent);
@ -221,7 +221,7 @@ public class StaticMessageSourceTests extends AbstractApplicationContextTests {
PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(sac.getDefaultListableBeanFactory());
reader.loadBeanDefinitions(new ClassPathResource("testBeans.properties", getClass()));
sac.refresh();
sac.addListener(listener);
sac.addApplicationListener(listener);
StaticMessageSource messageSource = sac.getStaticMessageSource();
Map<String, String> usMessages = new HashMap<String, String>(3);

View File

@ -36,6 +36,7 @@ import org.springframework.beans.BeanUtils;
import org.springframework.beans.ConfigurablePropertyAccessor;
import org.springframework.beans.PropertyAccessorFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@ -112,7 +113,9 @@ public class FormattingConversionServiceTests {
@Test
public void testFormatFieldForValueInjectionUsingMetaAnnotations() {
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();
ac.registerBeanDefinition("valueBean", new RootBeanDefinition(MetaValueBean.class, false));
RootBeanDefinition bd = new RootBeanDefinition(MetaValueBean.class);
bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
ac.registerBeanDefinition("valueBean", bd);
ac.registerBeanDefinition("conversionService", new RootBeanDefinition(FormattingConversionServiceFactoryBean.class));
ac.registerBeanDefinition("ppc", new RootBeanDefinition(PropertyPlaceholderConfigurer.class));
ac.refresh();

View File

@ -37,7 +37,7 @@ import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.TestBean;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jmx.AbstractMBeanServerTests;
import org.springframework.jmx.IJmxTestBean;
@ -155,7 +155,8 @@ public final class MBeanExporterTests extends AbstractMBeanServerTests {
}
public void testAutodetectMBeans() throws Exception {
XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("autodetectMBeans.xml", getClass()));
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource("autodetectMBeans.xml", getClass()));
try {
bf.getBean("exporter");
MBeanServer server = (MBeanServer) bf.getBean("server");
@ -171,7 +172,8 @@ public final class MBeanExporterTests extends AbstractMBeanServerTests {
}
public void testAutodetectWithExclude() throws Exception {
XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("autodetectMBeans.xml", getClass()));
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource("autodetectMBeans.xml", getClass()));
try {
bf.getBean("exporter");
MBeanServer server = (MBeanServer) bf.getBean("server");
@ -189,7 +191,8 @@ public final class MBeanExporterTests extends AbstractMBeanServerTests {
}
public void testAutodetectLazyMBeans() throws Exception {
XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("autodetectLazyMBeans.xml", getClass()));
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource("autodetectLazyMBeans.xml", getClass()));
try {
bf.getBean("exporter");
MBeanServer server = (MBeanServer) bf.getBean("server");
@ -209,7 +212,8 @@ public final class MBeanExporterTests extends AbstractMBeanServerTests {
}
public void testAutodetectNoMBeans() throws Exception {
XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("autodetectNoMBeans.xml", getClass()));
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource("autodetectNoMBeans.xml", getClass()));
try {
bf.getBean("exporter");
} finally {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2012 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.
@ -24,6 +24,7 @@ import org.springframework.core.task.NoOpRunnable;
* @author Rick Evans
* @author Juergen Hoeller
*/
@Deprecated
public class ConcurrentTaskExecutorTests extends TestCase {
public void testZeroArgCtorResultsInDefaultTaskExecutorBeingUsed() throws Exception {

View File

@ -29,6 +29,7 @@ import org.springframework.scheduling.TestMethodInvokingTask;
* @author Juergen Hoeller
* @since 20.02.2004
*/
@Deprecated
public class TimerSupportTests extends TestCase {
public void testTimerFactoryBean() throws Exception {

View File

@ -28,6 +28,7 @@ import org.junit.Test;
* @author Rick Evans
* @author Chris Beams
*/
@Deprecated
public final class TimerTaskExecutorTests {
@Test(expected=IllegalArgumentException.class)

View File

@ -19,7 +19,6 @@ package org.springframework.scripting.jruby;
import java.util.Map;
import junit.framework.TestCase;
import junit.framework.Assert;
import org.springframework.aop.support.AopUtils;
import org.springframework.aop.target.dynamic.Refreshable;
@ -55,14 +54,14 @@ public class JRubyScriptFactoryTests extends TestCase {
assertFalse("Scripted object should not be instance of Refreshable", calc instanceof Refreshable);
assertFalse("Scripted object should not be instance of Refreshable", messenger instanceof Refreshable);
Assert.assertEquals(calc, calc);
Assert.assertEquals(messenger, messenger);
assertEquals(calc, calc);
assertEquals(messenger, messenger);
assertTrue(!messenger.equals(calc));
assertNotSame(messenger.hashCode(), calc.hashCode());
assertTrue(!messenger.toString().equals(calc.toString()));
String desiredMessage = "Hello World!";
Assert.assertEquals("Message is incorrect", desiredMessage, messenger.getMessage());
assertEquals("Message is incorrect", desiredMessage, messenger.getMessage());
}
public void testNonStaticScript() throws Exception {
@ -73,12 +72,12 @@ public class JRubyScriptFactoryTests extends TestCase {
assertTrue("Should be an instance of Refreshable", messenger instanceof Refreshable);
String desiredMessage = "Hello World!";
Assert.assertEquals("Message is incorrect.", desiredMessage, messenger.getMessage());
assertEquals("Message is incorrect.", desiredMessage, messenger.getMessage());
Refreshable refreshable = (Refreshable) messenger;
refreshable.refresh();
Assert.assertEquals("Message is incorrect after refresh.", desiredMessage, messenger.getMessage());
assertEquals("Message is incorrect after refresh.", desiredMessage, messenger.getMessage());
assertEquals("Incorrect refresh count", 2, refreshable.getRefreshCount());
}
@ -142,14 +141,14 @@ public class JRubyScriptFactoryTests extends TestCase {
TestBean testBean = (TestBean) ctx.getBean("testBean");
Messenger messenger = (Messenger) ctx.getBean("messenger");
Assert.assertEquals("Hello World!", messenger.getMessage());
assertEquals("Hello World!", messenger.getMessage());
assertFalse(messenger instanceof Refreshable);
TestBeanAwareMessenger messengerByType = (TestBeanAwareMessenger) ctx.getBean("messengerByType");
Assert.assertEquals(testBean, messengerByType.getTestBean());
assertEquals(testBean, messengerByType.getTestBean());
TestBeanAwareMessenger messengerByName = (TestBeanAwareMessenger) ctx.getBean("messengerByName");
Assert.assertEquals(testBean, messengerByName.getTestBean());
assertEquals(testBean, messengerByName.getTestBean());
}
public void testPrototypeScriptFromTag() throws Exception {
@ -159,12 +158,12 @@ public class JRubyScriptFactoryTests extends TestCase {
assertNotSame(messenger, messenger2);
assertSame(messenger.getClass(), messenger2.getClass());
Assert.assertEquals("Hello World!", messenger.getMessage());
Assert.assertEquals("Hello World!", messenger2.getMessage());
assertEquals("Hello World!", messenger.getMessage());
assertEquals("Hello World!", messenger2.getMessage());
messenger.setMessage("Bye World!");
messenger2.setMessage("Byebye World!");
Assert.assertEquals("Bye World!", messenger.getMessage());
Assert.assertEquals("Byebye World!", messenger2.getMessage());
assertEquals("Bye World!", messenger.getMessage());
assertEquals("Byebye World!", messenger2.getMessage());
}
public void testInlineScriptFromTag() throws Exception {
@ -177,18 +176,18 @@ public class JRubyScriptFactoryTests extends TestCase {
public void testRefreshableFromTag() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("jruby-with-xsd.xml", getClass());
Messenger messenger = (Messenger) ctx.getBean("refreshableMessenger");
Assert.assertEquals("Hello World!", messenger.getMessage());
assertEquals("Hello World!", messenger.getMessage());
assertTrue("Messenger should be Refreshable", messenger instanceof Refreshable);
}
public void testThatMultipleScriptInterfacesAreSupported() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("jruby-with-xsd.xml", getClass());
Messenger messenger = (Messenger) ctx.getBean("calculatingMessenger");
Assert.assertEquals("Hello World!", messenger.getMessage());
assertEquals("Hello World!", messenger.getMessage());
// cool, now check that the Calculator interface is also exposed
Calculator calc = (Calculator) messenger;
Assert.assertEquals(0, calc.add(2, -2));
assertEquals(0, calc.add(2, -2));
}
public void testWithComplexArg() throws Exception {
@ -270,7 +269,7 @@ public class JRubyScriptFactoryTests extends TestCase {
public void testAOP() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("jruby-aop.xml", getClass());
Messenger messenger = (Messenger) ctx.getBean("messenger");
Assert.assertEquals(new StringBuffer("Hello World!").reverse().toString(), messenger.getMessage());
assertEquals(new StringBuffer("Hello World!").reverse().toString(), messenger.getMessage());
}

View File

@ -205,7 +205,7 @@ public class ScriptFactoryPostProcessorTests extends TestCase {
ctx.registerBeanDefinition("messenger", BeanDefinitionBuilder.rootBeanDefinition(StubMessenger.class).getBeanDefinition());
BeanDefinitionBuilder scriptedBeanBuilder = BeanDefinitionBuilder.rootBeanDefinition(GroovyScriptFactory.class);
scriptedBeanBuilder.setSingleton(false);
scriptedBeanBuilder.setScope(BeanDefinition.SCOPE_PROTOTYPE);
scriptedBeanBuilder.addConstructorArgValue(DELEGATING_SCRIPT);
scriptedBeanBuilder.addPropertyReference("messenger", "messenger");

View File

@ -20,7 +20,6 @@ import java.beans.PropertyEditorSupport;
import java.util.Map;
import junit.framework.TestCase;
import junit.framework.Assert;
import org.springframework.beans.FieldAccessBean;
import org.springframework.beans.MutablePropertyValues;
@ -106,7 +105,7 @@ public class DataBinderFieldAccessTests extends TestCase {
assertTrue("Correct number of age errors", br.getFieldErrorCount("age") == 1);
assertEquals("32x", binder.getBindingResult().getFieldValue("age"));
assertEquals("32x", binder.getBindingResult().getFieldError("age").getRejectedValue());
Assert.assertEquals(0, tb.getAge());
assertEquals(0, tb.getAge());
}
}
@ -152,7 +151,7 @@ public class DataBinderFieldAccessTests extends TestCase {
assertTrue("Correct number of age errors", br.getFieldErrorCount("age") == 1);
assertEquals("32x", binder.getBindingResult().getFieldValue("age"));
assertEquals("32x", binder.getBindingResult().getFieldError("age").getRejectedValue());
Assert.assertEquals(0, tb.getAge());
assertEquals(0, tb.getAge());
assertTrue("Does not have spouse errors", !br.hasFieldErrors("spouse"));
assertEquals("Kerry", binder.getBindingResult().getFieldValue("spouse"));

View File

@ -33,21 +33,25 @@ import org.springframework.util.MultiValueMap;
*/
public class CollectionFactoryTests extends TestCase {
@SuppressWarnings("deprecation")
public void testLinkedSet() {
Set set = CollectionFactory.createLinkedSetIfPossible(16);
assertTrue(set instanceof LinkedHashSet);
}
@SuppressWarnings("deprecation")
public void testLinkedMap() {
Map map = CollectionFactory.createLinkedMapIfPossible(16);
assertTrue(map instanceof LinkedHashMap);
}
@SuppressWarnings("deprecation")
public void testIdentityMap() {
Map map = CollectionFactory.createIdentityMapIfPossible(16);
assertTrue(map instanceof IdentityHashMap);
}
@SuppressWarnings("deprecation")
public void testConcurrentMap() {
Map map = CollectionFactory.createConcurrentMapIfPossible(16);
assertTrue(map.getClass().getName().endsWith("ConcurrentHashMap"));
@ -58,6 +62,7 @@ public class CollectionFactoryTests extends TestCase {
assertTrue(map.getClass().getName().endsWith("MultiValueMap"));
}
@SuppressWarnings("deprecation")
public void testConcurrentMapWithExplicitInterface() {
ConcurrentMap map = CollectionFactory.createConcurrentMap(16);
assertTrue(map.getClass().getSuperclass().getName().endsWith("ConcurrentHashMap"));

View File

@ -25,8 +25,6 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import junit.framework.Assert;
import org.springframework.beans.GenericBean;
import org.springframework.core.io.Resource;
@ -96,11 +94,11 @@ public class GenericCollectionTypeResolverTests extends AbstractGenericsTests {
public void testProgrammaticListIntrospection() throws Exception {
Method setter = GenericBean.class.getMethod("setResourceList", List.class);
Assert.assertEquals(Resource.class,
assertEquals(Resource.class,
GenericCollectionTypeResolver.getCollectionParameterType(new MethodParameter(setter, 0)));
Method getter = GenericBean.class.getMethod("getResourceList");
Assert.assertEquals(Resource.class,
assertEquals(Resource.class,
GenericCollectionTypeResolver.getCollectionReturnType(getter));
}

View File

@ -20,7 +20,6 @@ import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import junit.framework.TestCase;
import junit.framework.Assert;
/**
* @author Rod Johnson
@ -52,7 +51,7 @@ public class NestedExceptionTests extends TestCase {
Exception rootCause = new Exception(rootCauseMesg);
// Making a class abstract doesn't _really_ prevent instantiation :-)
NestedRuntimeException nex = new NestedRuntimeException(myMessage, rootCause) {};
Assert.assertEquals(nex.getCause(), rootCause);
assertEquals(nex.getCause(), rootCause);
assertTrue(nex.getMessage().indexOf(myMessage) != -1);
assertTrue(nex.getMessage().indexOf(rootCauseMesg) != -1);
@ -90,7 +89,7 @@ public class NestedExceptionTests extends TestCase {
Exception rootCause = new Exception(rootCauseMesg);
// Making a class abstract doesn't _really_ prevent instantiation :-)
NestedCheckedException nex = new NestedCheckedException(myMessage, rootCause) {};
Assert.assertEquals(nex.getCause(), rootCause);
assertEquals(nex.getCause(), rootCause);
assertTrue(nex.getMessage().indexOf(myMessage) != -1);
assertTrue(nex.getMessage().indexOf(rootCauseMesg) != -1);

View File

@ -401,7 +401,7 @@ public class DefaultConversionTests {
public void convertArrayToObjectAssignableTargetType() {
Long[] array = new Long[] { 3L };
Long[] result = (Long[]) conversionService.convert(array, Object.class);
assertEquals(array, result);
assertArrayEquals(array, result);
}
@Test

View File

@ -29,6 +29,7 @@ import junit.framework.TestCase;
* @author Juergen Hoeller
* @author Sam Brannen
*/
@Deprecated
public class LabeledEnumTests extends TestCase {
private byte[] serializeObject(final Object obj) throws IOException {

View File

@ -21,6 +21,7 @@ import java.beans.PropertyEditor;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.Resource;
/**
@ -67,7 +68,9 @@ public class ResourceArrayPropertyEditorTests {
@Test(expected=IllegalArgumentException.class)
public void testStrictSystemPropertyReplacement() {
PropertyEditor editor = new ResourceArrayPropertyEditor(new PathMatchingResourcePatternResolver(), false);
PropertyEditor editor = new ResourceArrayPropertyEditor(
new PathMatchingResourcePatternResolver(), new StandardEnvironment(),
false);
System.setProperty("test.prop", "foo");
try {
editor.setAsText("${test.prop}-${bar}");

View File

@ -17,13 +17,14 @@
package org.springframework.core.style;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import junit.framework.TestCase;
import org.springframework.core.CollectionFactory;
import org.springframework.util.ObjectUtils;
/**
@ -65,7 +66,7 @@ public class ToStringCreatorTests extends TestCase {
}
private Map getMap() {
Map map = CollectionFactory.createLinkedMapIfPossible(3);
Map map = new LinkedHashMap(3);
map.put("Keri", "Softball");
map.put("Scot", "Fishing");
map.put("Keith", "Flag Football");
@ -96,7 +97,7 @@ public class ToStringCreatorTests extends TestCase {
}
public void testSet() {
Set set = CollectionFactory.createLinkedSetIfPossible(3);
Set set = new LinkedHashSet<>(3);
set.add(s1);
set.add(s2);
set.add(s3);

View File

@ -19,7 +19,6 @@ package org.springframework.util;
import java.util.LinkedList;
import junit.framework.*;
import junit.framework.Assert;
import org.springframework.beans.TestBean;
@ -72,14 +71,14 @@ public class AutoPopulatingListTests extends TestCase {
for(int x = 0; x < list.size(); x++) {
Object element = list.get(x);
if(element instanceof TestBean) {
junit.framework.Assert.assertEquals(x, ((TestBean) element).getAge());
assertEquals(x, ((TestBean) element).getAge());
}
}
}
public void testSerialization() throws Exception {
AutoPopulatingList<?> list = new AutoPopulatingList<Object>(TestBean.class);
Assert.assertEquals(list, SerializationTestUtils.serializeAndDeserialize(list));
assertEquals(list, SerializationTestUtils.serializeAndDeserialize(list));
}

View File

@ -27,6 +27,7 @@ import junit.framework.TestCase;
* @author Keith Donald
* @author Juergen Hoeller
*/
@Deprecated
public class CachingMapDecoratorTests extends TestCase {
public void testValidCache() {

View File

@ -41,6 +41,8 @@ import org.springframework.beans.TestBean;
*/
public class ClassUtilsTests extends TestCase {
private ClassLoader classLoader = getClass().getClassLoader();
@Override
public void setUp() {
InnerClass.noArgCalled = false;
@ -49,56 +51,56 @@ public class ClassUtilsTests extends TestCase {
}
public void testIsPresent() throws Exception {
assertTrue(ClassUtils.isPresent("java.lang.String"));
assertFalse(ClassUtils.isPresent("java.lang.MySpecialString"));
assertTrue(ClassUtils.isPresent("java.lang.String", classLoader));
assertFalse(ClassUtils.isPresent("java.lang.MySpecialString", classLoader));
}
public void testForName() throws ClassNotFoundException {
assertEquals(String.class, ClassUtils.forName("java.lang.String"));
assertEquals(String[].class, ClassUtils.forName("java.lang.String[]"));
assertEquals(String[].class, ClassUtils.forName(String[].class.getName()));
assertEquals(String[][].class, ClassUtils.forName(String[][].class.getName()));
assertEquals(String[][][].class, ClassUtils.forName(String[][][].class.getName()));
assertEquals(TestBean.class, ClassUtils.forName("org.springframework.beans.TestBean"));
assertEquals(TestBean[].class, ClassUtils.forName("org.springframework.beans.TestBean[]"));
assertEquals(TestBean[].class, ClassUtils.forName(TestBean[].class.getName()));
assertEquals(TestBean[][].class, ClassUtils.forName("org.springframework.beans.TestBean[][]"));
assertEquals(TestBean[][].class, ClassUtils.forName(TestBean[][].class.getName()));
assertEquals(short[][][].class, ClassUtils.forName("[[[S"));
assertEquals(String.class, ClassUtils.forName("java.lang.String", classLoader));
assertEquals(String[].class, ClassUtils.forName("java.lang.String[]", classLoader));
assertEquals(String[].class, ClassUtils.forName(String[].class.getName(), classLoader));
assertEquals(String[][].class, ClassUtils.forName(String[][].class.getName(), classLoader));
assertEquals(String[][][].class, ClassUtils.forName(String[][][].class.getName(), classLoader));
assertEquals(TestBean.class, ClassUtils.forName("org.springframework.beans.TestBean", classLoader));
assertEquals(TestBean[].class, ClassUtils.forName("org.springframework.beans.TestBean[]", classLoader));
assertEquals(TestBean[].class, ClassUtils.forName(TestBean[].class.getName(), classLoader));
assertEquals(TestBean[][].class, ClassUtils.forName("org.springframework.beans.TestBean[][]", classLoader));
assertEquals(TestBean[][].class, ClassUtils.forName(TestBean[][].class.getName(), classLoader));
assertEquals(short[][][].class, ClassUtils.forName("[[[S", classLoader));
}
public void testForNameWithPrimitiveClasses() throws ClassNotFoundException {
assertEquals(boolean.class, ClassUtils.forName("boolean"));
assertEquals(byte.class, ClassUtils.forName("byte"));
assertEquals(char.class, ClassUtils.forName("char"));
assertEquals(short.class, ClassUtils.forName("short"));
assertEquals(int.class, ClassUtils.forName("int"));
assertEquals(long.class, ClassUtils.forName("long"));
assertEquals(float.class, ClassUtils.forName("float"));
assertEquals(double.class, ClassUtils.forName("double"));
assertEquals(void.class, ClassUtils.forName("void"));
assertEquals(boolean.class, ClassUtils.forName("boolean", classLoader));
assertEquals(byte.class, ClassUtils.forName("byte", classLoader));
assertEquals(char.class, ClassUtils.forName("char", classLoader));
assertEquals(short.class, ClassUtils.forName("short", classLoader));
assertEquals(int.class, ClassUtils.forName("int", classLoader));
assertEquals(long.class, ClassUtils.forName("long", classLoader));
assertEquals(float.class, ClassUtils.forName("float", classLoader));
assertEquals(double.class, ClassUtils.forName("double", classLoader));
assertEquals(void.class, ClassUtils.forName("void", classLoader));
}
public void testForNameWithPrimitiveArrays() throws ClassNotFoundException {
assertEquals(boolean[].class, ClassUtils.forName("boolean[]"));
assertEquals(byte[].class, ClassUtils.forName("byte[]"));
assertEquals(char[].class, ClassUtils.forName("char[]"));
assertEquals(short[].class, ClassUtils.forName("short[]"));
assertEquals(int[].class, ClassUtils.forName("int[]"));
assertEquals(long[].class, ClassUtils.forName("long[]"));
assertEquals(float[].class, ClassUtils.forName("float[]"));
assertEquals(double[].class, ClassUtils.forName("double[]"));
assertEquals(boolean[].class, ClassUtils.forName("boolean[]", classLoader));
assertEquals(byte[].class, ClassUtils.forName("byte[]", classLoader));
assertEquals(char[].class, ClassUtils.forName("char[]", classLoader));
assertEquals(short[].class, ClassUtils.forName("short[]", classLoader));
assertEquals(int[].class, ClassUtils.forName("int[]", classLoader));
assertEquals(long[].class, ClassUtils.forName("long[]", classLoader));
assertEquals(float[].class, ClassUtils.forName("float[]", classLoader));
assertEquals(double[].class, ClassUtils.forName("double[]", classLoader));
}
public void testForNameWithPrimitiveArraysInternalName() throws ClassNotFoundException {
assertEquals(boolean[].class, ClassUtils.forName(boolean[].class.getName()));
assertEquals(byte[].class, ClassUtils.forName(byte[].class.getName()));
assertEquals(char[].class, ClassUtils.forName(char[].class.getName()));
assertEquals(short[].class, ClassUtils.forName(short[].class.getName()));
assertEquals(int[].class, ClassUtils.forName(int[].class.getName()));
assertEquals(long[].class, ClassUtils.forName(long[].class.getName()));
assertEquals(float[].class, ClassUtils.forName(float[].class.getName()));
assertEquals(double[].class, ClassUtils.forName(double[].class.getName()));
assertEquals(boolean[].class, ClassUtils.forName(boolean[].class.getName(), classLoader));
assertEquals(byte[].class, ClassUtils.forName(byte[].class.getName(), classLoader));
assertEquals(char[].class, ClassUtils.forName(char[].class.getName(), classLoader));
assertEquals(short[].class, ClassUtils.forName(short[].class.getName(), classLoader));
assertEquals(int[].class, ClassUtils.forName(int[].class.getName(), classLoader));
assertEquals(long[].class, ClassUtils.forName(long[].class.getName(), classLoader));
assertEquals(float[].class, ClassUtils.forName(float[].class.getName(), classLoader));
assertEquals(double[].class, ClassUtils.forName(double[].class.getName(), classLoader));
}
public void testGetShortName() {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2012 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.
@ -16,8 +16,13 @@
package org.springframework.util.xml;
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.stream.XMLEventReader;
@ -28,7 +33,6 @@ import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.stream.StreamResult;
import static org.custommonkey.xmlunit.XMLAssert.*;
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Document;
@ -101,4 +105,4 @@ public class StaxSourceTests {
transformer.transform(source, new DOMResult(result));
assertXMLEqual("Invalid result", expected, result);
}
}
}

View File

@ -15,7 +15,8 @@
*/
package org.springframework.expression.spel;
import junit.framework.Assert;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.expression.EvaluationException;
@ -33,57 +34,57 @@ public class DefaultComparatorUnitTests {
public void testPrimitives() throws EvaluationException {
TypeComparator comparator = new StandardTypeComparator();
// primitive int
Assert.assertTrue(comparator.compare(1, 2) < 0);
Assert.assertTrue(comparator.compare(1, 1) == 0);
Assert.assertTrue(comparator.compare(2, 1) > 0);
assertTrue(comparator.compare(1, 2) < 0);
assertTrue(comparator.compare(1, 1) == 0);
assertTrue(comparator.compare(2, 1) > 0);
Assert.assertTrue(comparator.compare(1.0d, 2) < 0);
Assert.assertTrue(comparator.compare(1.0d, 1) == 0);
Assert.assertTrue(comparator.compare(2.0d, 1) > 0);
assertTrue(comparator.compare(1.0d, 2) < 0);
assertTrue(comparator.compare(1.0d, 1) == 0);
assertTrue(comparator.compare(2.0d, 1) > 0);
Assert.assertTrue(comparator.compare(1.0f, 2) < 0);
Assert.assertTrue(comparator.compare(1.0f, 1) == 0);
Assert.assertTrue(comparator.compare(2.0f, 1) > 0);
assertTrue(comparator.compare(1.0f, 2) < 0);
assertTrue(comparator.compare(1.0f, 1) == 0);
assertTrue(comparator.compare(2.0f, 1) > 0);
Assert.assertTrue(comparator.compare(1L, 2) < 0);
Assert.assertTrue(comparator.compare(1L, 1) == 0);
Assert.assertTrue(comparator.compare(2L, 1) > 0);
assertTrue(comparator.compare(1L, 2) < 0);
assertTrue(comparator.compare(1L, 1) == 0);
assertTrue(comparator.compare(2L, 1) > 0);
Assert.assertTrue(comparator.compare(1, 2L) < 0);
Assert.assertTrue(comparator.compare(1, 1L) == 0);
Assert.assertTrue(comparator.compare(2, 1L) > 0);
assertTrue(comparator.compare(1, 2L) < 0);
assertTrue(comparator.compare(1, 1L) == 0);
assertTrue(comparator.compare(2, 1L) > 0);
Assert.assertTrue(comparator.compare(1L, 2L) < 0);
Assert.assertTrue(comparator.compare(1L, 1L) == 0);
Assert.assertTrue(comparator.compare(2L, 1L) > 0);
assertTrue(comparator.compare(1L, 2L) < 0);
assertTrue(comparator.compare(1L, 1L) == 0);
assertTrue(comparator.compare(2L, 1L) > 0);
}
@Test
public void testNulls() throws EvaluationException {
TypeComparator comparator = new StandardTypeComparator();
Assert.assertTrue(comparator.compare(null,"abc")<0);
Assert.assertTrue(comparator.compare(null,null)==0);
Assert.assertTrue(comparator.compare("abc",null)>0);
assertTrue(comparator.compare(null,"abc")<0);
assertTrue(comparator.compare(null,null)==0);
assertTrue(comparator.compare("abc",null)>0);
}
@Test
public void testObjects() throws EvaluationException {
TypeComparator comparator = new StandardTypeComparator();
Assert.assertTrue(comparator.compare("a","a")==0);
Assert.assertTrue(comparator.compare("a","b")<0);
Assert.assertTrue(comparator.compare("b","a")>0);
assertTrue(comparator.compare("a","a")==0);
assertTrue(comparator.compare("a","b")<0);
assertTrue(comparator.compare("b","a")>0);
}
@Test
public void testCanCompare() throws EvaluationException {
TypeComparator comparator = new StandardTypeComparator();
Assert.assertTrue(comparator.canCompare(null,1));
Assert.assertTrue(comparator.canCompare(1,null));
assertTrue(comparator.canCompare(null,1));
assertTrue(comparator.canCompare(1,null));
Assert.assertTrue(comparator.canCompare(2,1));
Assert.assertTrue(comparator.canCompare("abc","def"));
Assert.assertTrue(comparator.canCompare("abc",3));
Assert.assertFalse(comparator.canCompare(String.class,3));
assertTrue(comparator.canCompare(2,1));
assertTrue(comparator.canCompare("abc","def"));
assertTrue(comparator.canCompare("abc",3));
assertFalse(comparator.canCompare(String.class,3));
}
}

View File

@ -16,6 +16,9 @@
package org.springframework.expression.spel;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Arrays;
@ -23,8 +26,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import junit.framework.Assert;
import org.junit.Test;
import org.springframework.expression.AccessException;
import org.springframework.expression.EvaluationContext;
@ -76,14 +77,14 @@ public class ExpressionLanguageScenarioTests extends ExpressionTestCase {
// They are reusable
value = expr.getValue();
Assert.assertEquals("hello world", value);
Assert.assertEquals(String.class, value.getClass());
assertEquals("hello world", value);
assertEquals(String.class, value.getClass());
} catch (EvaluationException ee) {
ee.printStackTrace();
Assert.fail("Unexpected Exception: " + ee.getMessage());
fail("Unexpected Exception: " + ee.getMessage());
} catch (ParseException pe) {
pe.printStackTrace();
Assert.fail("Unexpected Exception: " + pe.getMessage());
fail("Unexpected Exception: " + pe.getMessage());
}
}
@ -103,16 +104,16 @@ public class ExpressionLanguageScenarioTests extends ExpressionTestCase {
Expression expr = parser.parseRaw("#favouriteColour");
Object value = expr.getValue(ctx);
Assert.assertEquals("blue", value);
assertEquals("blue", value);
expr = parser.parseRaw("#primes.get(1)");
value = expr.getValue(ctx);
Assert.assertEquals(3, value);
assertEquals(3, value);
// all prime numbers > 10 from the list (using selection ?{...})
expr = parser.parseRaw("#primes.?[#this>10]");
value = expr.getValue(ctx);
Assert.assertEquals("[11, 13, 17]", value.toString());
assertEquals("[11, 13, 17]", value.toString());
}
@ -141,30 +142,30 @@ public class ExpressionLanguageScenarioTests extends ExpressionTestCase {
// read it, set it, read it again
Expression expr = parser.parseRaw("str");
Object value = expr.getValue(ctx);
Assert.assertEquals("wibble", value);
assertEquals("wibble", value);
expr = parser.parseRaw("str");
expr.setValue(ctx, "wobble");
expr = parser.parseRaw("str");
value = expr.getValue(ctx);
Assert.assertEquals("wobble", value);
assertEquals("wobble", value);
// or using assignment within the expression
expr = parser.parseRaw("str='wabble'");
value = expr.getValue(ctx);
expr = parser.parseRaw("str");
value = expr.getValue(ctx);
Assert.assertEquals("wabble", value);
assertEquals("wabble", value);
// private property will be accessed through getter()
expr = parser.parseRaw("property");
value = expr.getValue(ctx);
Assert.assertEquals(42, value);
assertEquals(42, value);
// ... and set through setter
expr = parser.parseRaw("property=4");
value = expr.getValue(ctx);
expr = parser.parseRaw("property");
value = expr.getValue(ctx);
Assert.assertEquals(4,value);
assertEquals(4,value);
}
public static String repeat(String s) { return s+s; }
@ -183,14 +184,14 @@ public class ExpressionLanguageScenarioTests extends ExpressionTestCase {
Expression expr = parser.parseRaw("#repeat('hello')");
Object value = expr.getValue(ctx);
Assert.assertEquals("hellohello", value);
assertEquals("hellohello", value);
} catch (EvaluationException ee) {
ee.printStackTrace();
Assert.fail("Unexpected Exception: " + ee.getMessage());
fail("Unexpected Exception: " + ee.getMessage());
} catch (ParseException pe) {
pe.printStackTrace();
Assert.fail("Unexpected Exception: " + pe.getMessage());
fail("Unexpected Exception: " + pe.getMessage());
}
}
@ -207,13 +208,13 @@ public class ExpressionLanguageScenarioTests extends ExpressionTestCase {
ctx.addPropertyAccessor(new FruitColourAccessor());
Expression expr = parser.parseRaw("orange");
Object value = expr.getValue(ctx);
Assert.assertEquals(Color.orange, value);
assertEquals(Color.orange, value);
try {
expr.setValue(ctx, Color.blue);
Assert.fail("Should not be allowed to set oranges to be blue !");
fail("Should not be allowed to set oranges to be blue !");
} catch (SpelEvaluationException ee) {
Assert.assertEquals(ee.getMessageCode(), SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE_ON_NULL);
assertEquals(ee.getMessageCode(), SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE_ON_NULL);
}
}
@ -227,14 +228,14 @@ public class ExpressionLanguageScenarioTests extends ExpressionTestCase {
ctx.addPropertyAccessor(new VegetableColourAccessor());
Expression expr = parser.parseRaw("pea");
Object value = expr.getValue(ctx);
Assert.assertEquals(Color.green, value);
assertEquals(Color.green, value);
try {
expr.setValue(ctx, Color.blue);
Assert.fail("Should not be allowed to set peas to be blue !");
fail("Should not be allowed to set peas to be blue !");
}
catch (SpelEvaluationException ee) {
Assert.assertEquals(ee.getMessageCode(), SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE_ON_NULL);
assertEquals(ee.getMessageCode(), SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE_ON_NULL);
}
}

View File

@ -16,12 +16,15 @@
package org.springframework.expression.spel;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import java.util.EmptyStackException;
import java.util.HashMap;
import java.util.Map;
import junit.framework.Assert;
import org.junit.Test;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.expression.EvaluationContext;
@ -42,7 +45,7 @@ public class ExpressionStateTests extends ExpressionTestCase {
public void testConstruction() {
EvaluationContext context = TestScenarioCreator.getTestEvaluationContext();
ExpressionState state = new ExpressionState(context);
Assert.assertEquals(context,state.getEvaluationContext());
assertEquals(context,state.getEvaluationContext());
}
// Local variables are in variable scopes which come and go during evaluation. Normal variables are
@ -53,129 +56,129 @@ public class ExpressionStateTests extends ExpressionTestCase {
ExpressionState state = getState();
Object value = state.lookupLocalVariable("foo");
Assert.assertNull(value);
assertNull(value);
state.setLocalVariable("foo",34);
value = state.lookupLocalVariable("foo");
Assert.assertEquals(34,value);
assertEquals(34,value);
state.setLocalVariable("foo",null);
value = state.lookupLocalVariable("foo");
Assert.assertEquals(null,value);
assertEquals(null,value);
}
@Test
public void testVariables() {
ExpressionState state = getState();
TypedValue typedValue = state.lookupVariable("foo");
Assert.assertEquals(TypedValue.NULL,typedValue);
assertEquals(TypedValue.NULL,typedValue);
state.setVariable("foo",34);
typedValue = state.lookupVariable("foo");
Assert.assertEquals(34,typedValue.getValue());
Assert.assertEquals(Integer.class,typedValue.getTypeDescriptor().getType());
assertEquals(34,typedValue.getValue());
assertEquals(Integer.class,typedValue.getTypeDescriptor().getType());
state.setVariable("foo","abc");
typedValue = state.lookupVariable("foo");
Assert.assertEquals("abc",typedValue.getValue());
Assert.assertEquals(String.class,typedValue.getTypeDescriptor().getType());
assertEquals("abc",typedValue.getValue());
assertEquals(String.class,typedValue.getTypeDescriptor().getType());
}
@Test
public void testNoVariableInteference() {
ExpressionState state = getState();
TypedValue typedValue = state.lookupVariable("foo");
Assert.assertEquals(TypedValue.NULL,typedValue);
assertEquals(TypedValue.NULL,typedValue);
state.setLocalVariable("foo",34);
typedValue = state.lookupVariable("foo");
Assert.assertEquals(TypedValue.NULL,typedValue);
assertEquals(TypedValue.NULL,typedValue);
state.setVariable("goo","hello");
Assert.assertNull(state.lookupLocalVariable("goo"));
assertNull(state.lookupLocalVariable("goo"));
}
@Test
public void testLocalVariableNestedScopes() {
ExpressionState state = getState();
Assert.assertEquals(null,state.lookupLocalVariable("foo"));
assertEquals(null,state.lookupLocalVariable("foo"));
state.setLocalVariable("foo",12);
Assert.assertEquals(12,state.lookupLocalVariable("foo"));
assertEquals(12,state.lookupLocalVariable("foo"));
state.enterScope(null);
Assert.assertEquals(12,state.lookupLocalVariable("foo")); // found in upper scope
assertEquals(12,state.lookupLocalVariable("foo")); // found in upper scope
state.setLocalVariable("foo","abc");
Assert.assertEquals("abc",state.lookupLocalVariable("foo")); // found in nested scope
assertEquals("abc",state.lookupLocalVariable("foo")); // found in nested scope
state.exitScope();
Assert.assertEquals(12,state.lookupLocalVariable("foo")); // found in nested scope
assertEquals(12,state.lookupLocalVariable("foo")); // found in nested scope
}
@Test
public void testRootContextObject() {
ExpressionState state = getState();
Assert.assertEquals(Inventor.class,state.getRootContextObject().getValue().getClass());
assertEquals(Inventor.class,state.getRootContextObject().getValue().getClass());
// although the root object is being set on the evaluation context, the value in the 'state' remains what it was when constructed
((StandardEvaluationContext) state.getEvaluationContext()).setRootObject(null);
Assert.assertEquals(Inventor.class,state.getRootContextObject().getValue().getClass());
// Assert.assertEquals(null, state.getRootContextObject().getValue());
assertEquals(Inventor.class,state.getRootContextObject().getValue().getClass());
// assertEquals(null, state.getRootContextObject().getValue());
state = new ExpressionState(new StandardEvaluationContext());
Assert.assertEquals(TypedValue.NULL,state.getRootContextObject());
assertEquals(TypedValue.NULL,state.getRootContextObject());
((StandardEvaluationContext)state.getEvaluationContext()).setRootObject(null);
Assert.assertEquals(null,state.getRootContextObject().getValue());
assertEquals(null,state.getRootContextObject().getValue());
}
@Test
public void testActiveContextObject() {
ExpressionState state = getState();
Assert.assertEquals(state.getRootContextObject().getValue(),state.getActiveContextObject().getValue());
assertEquals(state.getRootContextObject().getValue(),state.getActiveContextObject().getValue());
try {
state.popActiveContextObject();
Assert.fail("stack should be empty...");
fail("stack should be empty...");
} catch (EmptyStackException ese) {
// success
}
state.pushActiveContextObject(new TypedValue(34));
Assert.assertEquals(34,state.getActiveContextObject().getValue());
assertEquals(34,state.getActiveContextObject().getValue());
state.pushActiveContextObject(new TypedValue("hello"));
Assert.assertEquals("hello",state.getActiveContextObject().getValue());
assertEquals("hello",state.getActiveContextObject().getValue());
state.popActiveContextObject();
Assert.assertEquals(34,state.getActiveContextObject().getValue());
assertEquals(34,state.getActiveContextObject().getValue());
state.popActiveContextObject();
Assert.assertEquals(state.getRootContextObject().getValue(),state.getActiveContextObject().getValue());
assertEquals(state.getRootContextObject().getValue(),state.getActiveContextObject().getValue());
state = new ExpressionState(new StandardEvaluationContext());
Assert.assertEquals(TypedValue.NULL,state.getActiveContextObject());
assertEquals(TypedValue.NULL,state.getActiveContextObject());
}
@Test
public void testPopulatedNestedScopes() {
ExpressionState state = getState();
Assert.assertNull(state.lookupLocalVariable("foo"));
assertNull(state.lookupLocalVariable("foo"));
state.enterScope("foo",34);
Assert.assertEquals(34,state.lookupLocalVariable("foo"));
assertEquals(34,state.lookupLocalVariable("foo"));
state.enterScope(null);
state.setLocalVariable("foo",12);
Assert.assertEquals(12,state.lookupLocalVariable("foo"));
assertEquals(12,state.lookupLocalVariable("foo"));
state.exitScope();
Assert.assertEquals(34,state.lookupLocalVariable("foo"));
assertEquals(34,state.lookupLocalVariable("foo"));
state.exitScope();
Assert.assertNull(state.lookupLocalVariable("goo"));
assertNull(state.lookupLocalVariable("goo"));
}
@Test
@ -185,33 +188,33 @@ public class ExpressionStateTests extends ExpressionTestCase {
// supplied should override root on context
ExpressionState state = new ExpressionState(ctx,new TypedValue("i am a string"));
TypedValue stateRoot = state.getRootContextObject();
Assert.assertEquals(String.class,stateRoot.getTypeDescriptor().getType());
Assert.assertEquals("i am a string",stateRoot.getValue());
assertEquals(String.class,stateRoot.getTypeDescriptor().getType());
assertEquals("i am a string",stateRoot.getValue());
}
@Test
public void testPopulatedNestedScopesMap() {
ExpressionState state = getState();
Assert.assertNull(state.lookupLocalVariable("foo"));
Assert.assertNull(state.lookupLocalVariable("goo"));
assertNull(state.lookupLocalVariable("foo"));
assertNull(state.lookupLocalVariable("goo"));
Map<String,Object> m = new HashMap<String,Object>();
m.put("foo",34);
m.put("goo","abc");
state.enterScope(m);
Assert.assertEquals(34,state.lookupLocalVariable("foo"));
Assert.assertEquals("abc",state.lookupLocalVariable("goo"));
assertEquals(34,state.lookupLocalVariable("foo"));
assertEquals("abc",state.lookupLocalVariable("goo"));
state.enterScope(null);
state.setLocalVariable("foo",12);
Assert.assertEquals(12,state.lookupLocalVariable("foo"));
Assert.assertEquals("abc",state.lookupLocalVariable("goo"));
assertEquals(12,state.lookupLocalVariable("foo"));
assertEquals("abc",state.lookupLocalVariable("goo"));
state.exitScope();
state.exitScope();
Assert.assertNull(state.lookupLocalVariable("foo"));
Assert.assertNull(state.lookupLocalVariable("goo"));
assertNull(state.lookupLocalVariable("foo"));
assertNull(state.lookupLocalVariable("goo"));
}
@Test
@ -219,38 +222,38 @@ public class ExpressionStateTests extends ExpressionTestCase {
ExpressionState state = getState();
try {
state.operate(Operation.ADD,1,2);
Assert.fail("should have failed");
fail("should have failed");
} catch (EvaluationException ee) {
SpelEvaluationException sEx = (SpelEvaluationException)ee;
Assert.assertEquals(SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES,sEx.getMessageCode());
assertEquals(SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES,sEx.getMessageCode());
}
try {
state.operate(Operation.ADD,null,null);
Assert.fail("should have failed");
fail("should have failed");
} catch (EvaluationException ee) {
SpelEvaluationException sEx = (SpelEvaluationException)ee;
Assert.assertEquals(SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES,sEx.getMessageCode());
assertEquals(SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES,sEx.getMessageCode());
}
}
@Test
public void testComparator() {
ExpressionState state = getState();
Assert.assertEquals(state.getEvaluationContext().getTypeComparator(),state.getTypeComparator());
assertEquals(state.getEvaluationContext().getTypeComparator(),state.getTypeComparator());
}
@Test
public void testTypeLocator() throws EvaluationException {
ExpressionState state = getState();
Assert.assertNotNull(state.getEvaluationContext().getTypeLocator());
Assert.assertEquals(Integer.class,state.findType("java.lang.Integer"));
assertNotNull(state.getEvaluationContext().getTypeLocator());
assertEquals(Integer.class,state.findType("java.lang.Integer"));
try {
state.findType("someMadeUpName");
Assert.fail("Should have failed to find it");
fail("Should have failed to find it");
} catch (EvaluationException ee) {
SpelEvaluationException sEx = (SpelEvaluationException)ee;
Assert.assertEquals(SpelMessage.TYPE_NOT_FOUND,sEx.getMessageCode());
assertEquals(SpelMessage.TYPE_NOT_FOUND,sEx.getMessageCode());
}
}
@ -258,16 +261,16 @@ public class ExpressionStateTests extends ExpressionTestCase {
public void testTypeConversion() throws EvaluationException {
ExpressionState state = getState();
String s = (String)state.convertValue(34, TypeDescriptor.valueOf(String.class));
Assert.assertEquals("34",s);
assertEquals("34",s);
s = (String)state.convertValue(new TypedValue(34), TypeDescriptor.valueOf(String.class));
Assert.assertEquals("34",s);
assertEquals("34",s);
}
@Test
public void testPropertyAccessors() {
ExpressionState state = getState();
Assert.assertEquals(state.getEvaluationContext().getPropertyAccessors(),state.getPropertyAccessors());
assertEquals(state.getEvaluationContext().getPropertyAccessors(),state.getPropertyAccessors());
}
/**

View File

@ -16,11 +16,12 @@
package org.springframework.expression.spel;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.util.Arrays;
import java.util.List;
import junit.framework.Assert;
import org.springframework.expression.EvaluationException;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
@ -54,13 +55,13 @@ public abstract class ExpressionTestCase {
try {
Expression expr = parser.parseExpression(expression);
if (expr == null) {
Assert.fail("Parser returned null for expression");
fail("Parser returned null for expression");
}
if (DEBUG) {
SpelUtilities.printAbstractSyntaxTree(System.out, expr);
}
// Class<?> expressionType = expr.getValueType();
// Assert.assertEquals("Type of the expression is not as expected. Should be '"+expectedResultType+"' but is
// assertEquals("Type of the expression is not as expected. Should be '"+expectedResultType+"' but is
// '"+expressionType+"'",
// expectedResultType,expressionType);
@ -71,12 +72,12 @@ public abstract class ExpressionTestCase {
if (expectedValue == null) {
return; // no point doing other checks
}
Assert.assertEquals("Expression returned null value, but expected '" + expectedValue + "'", expectedValue,
assertEquals("Expression returned null value, but expected '" + expectedValue + "'", expectedValue,
null);
}
Class<?> resultType = value.getClass();
Assert.assertEquals("Type of the actual result was not as expected. Expected '" + expectedResultType
assertEquals("Type of the actual result was not as expected. Expected '" + expectedResultType
+ "' but result was of type '" + resultType + "'", expectedResultType, resultType);
// .equals/* isAssignableFrom */(resultType), truers);
@ -84,17 +85,17 @@ public abstract class ExpressionTestCase {
// in the above expression...
if (expectedValue instanceof String) {
Assert.assertEquals("Did not get expected value for expression '" + expression + "'.", expectedValue,
assertEquals("Did not get expected value for expression '" + expression + "'.", expectedValue,
ExpressionTestCase.stringValueOf(value));
} else {
Assert.assertEquals("Did not get expected value for expression '" + expression + "'.", expectedValue, value);
assertEquals("Did not get expected value for expression '" + expression + "'.", expectedValue, value);
}
} catch (EvaluationException ee) {
ee.printStackTrace();
Assert.fail("Unexpected Exception: " + ee.getMessage());
fail("Unexpected Exception: " + ee.getMessage());
} catch (ParseException pe) {
pe.printStackTrace();
Assert.fail("Unexpected Exception: " + pe.getMessage());
fail("Unexpected Exception: " + pe.getMessage());
}
}
@ -102,13 +103,13 @@ public abstract class ExpressionTestCase {
try {
Expression expr = parser.parseExpression(expression);
if (expr == null) {
Assert.fail("Parser returned null for expression");
fail("Parser returned null for expression");
}
if (DEBUG) {
SpelUtilities.printAbstractSyntaxTree(System.out, expr);
}
// Class<?> expressionType = expr.getValueType();
// Assert.assertEquals("Type of the expression is not as expected. Should be '"+expectedResultType+"' but is
// assertEquals("Type of the expression is not as expected. Should be '"+expectedResultType+"' but is
// '"+expressionType+"'",
// expectedResultType,expressionType);
@ -116,23 +117,23 @@ public abstract class ExpressionTestCase {
if (value == null) {
if (expectedValue == null)
return; // no point doing other checks
Assert.assertEquals("Expression returned null value, but expected '" + expectedValue + "'", expectedValue,
assertEquals("Expression returned null value, but expected '" + expectedValue + "'", expectedValue,
null);
}
Class<?> resultType = value.getClass();
Assert.assertEquals("Type of the actual result was not as expected. Expected '" + expectedResultType
assertEquals("Type of the actual result was not as expected. Expected '" + expectedResultType
+ "' but result was of type '" + resultType + "'", expectedResultType, resultType);
// .equals/* isAssignableFrom */(resultType), truers);
Assert.assertEquals("Did not get expected value for expression '" + expression + "'.", expectedValue, value);
assertEquals("Did not get expected value for expression '" + expression + "'.", expectedValue, value);
// isAssignableFrom would allow some room for compatibility
// in the above expression...
} catch (EvaluationException ee) {
SpelEvaluationException ex = (SpelEvaluationException) ee;
ex.printStackTrace();
Assert.fail("Unexpected EvaluationException: " + ex.getMessage());
fail("Unexpected EvaluationException: " + ex.getMessage());
} catch (ParseException pe) {
Assert.fail("Unexpected ParseException: " + pe.getMessage());
fail("Unexpected ParseException: " + pe.getMessage());
}
}
@ -151,7 +152,7 @@ public abstract class ExpressionTestCase {
try {
Expression e = parser.parseExpression(expression);
if (e == null) {
Assert.fail("Parser returned null for expression");
fail("Parser returned null for expression");
}
if (DEBUG) {
SpelUtilities.printAbstractSyntaxTree(System.out, e);
@ -161,19 +162,19 @@ public abstract class ExpressionTestCase {
if (expectedValue == null)
return; // no point doing other
// checks
Assert.assertEquals("Expression returned null value, but expected '" + expectedValue + "'", expectedValue,
assertEquals("Expression returned null value, but expected '" + expectedValue + "'", expectedValue,
null);
}
Class<? extends Object> resultType = value.getClass();
if (expectedValue instanceof String) {
Assert.assertEquals("Did not get expected value for expression '" + expression + "'.", expectedValue,
assertEquals("Did not get expected value for expression '" + expression + "'.", expectedValue,
ExpressionTestCase.stringValueOf(value));
} else {
Assert.assertEquals("Did not get expected value for expression '" + expression + "'.", expectedValue, value);
assertEquals("Did not get expected value for expression '" + expression + "'.", expectedValue, value);
}
// Assert.assertEquals("Did not get expected value for expression '" + expression + "'.", expectedValue,
// assertEquals("Did not get expected value for expression '" + expression + "'.", expectedValue,
// ExpressionTestCase.stringValueOf(value));
Assert.assertEquals("Type of the result was not as expected. Expected '" + expectedClassOfResult
assertEquals("Type of the result was not as expected. Expected '" + expectedClassOfResult
+ "' but result was of type '" + resultType + "'", expectedClassOfResult
.equals/* isAssignableFrom */(resultType), true);
// TODO isAssignableFrom would allow some room for compatibility
@ -182,16 +183,16 @@ public abstract class ExpressionTestCase {
boolean isWritable = e.isWritable(eContext);
if (isWritable != shouldBeWritable) {
if (shouldBeWritable)
Assert.fail("Expected the expression to be writable but it is not");
fail("Expected the expression to be writable but it is not");
else
Assert.fail("Expected the expression to be readonly but it is not");
fail("Expected the expression to be readonly but it is not");
}
} catch (EvaluationException ee) {
ee.printStackTrace();
Assert.fail("Unexpected Exception: " + ee.getMessage());
fail("Unexpected Exception: " + ee.getMessage());
} catch (ParseException pe) {
pe.printStackTrace();
Assert.fail("Unexpected Exception: " + pe.getMessage());
fail("Unexpected Exception: " + pe.getMessage());
}
}
@ -222,7 +223,7 @@ public abstract class ExpressionTestCase {
try {
Expression expr = parser.parseExpression(expression);
if (expr == null) {
Assert.fail("Parser returned null for expression");
fail("Parser returned null for expression");
}
if (expectedReturnType != null) {
@SuppressWarnings("unused")
@ -231,18 +232,18 @@ public abstract class ExpressionTestCase {
@SuppressWarnings("unused")
Object value = expr.getValue(eContext);
}
Assert.fail("Should have failed with message " + expectedMessage);
fail("Should have failed with message " + expectedMessage);
} catch (EvaluationException ee) {
SpelEvaluationException ex = (SpelEvaluationException) ee;
if (ex.getMessageCode() != expectedMessage) {
// System.out.println(ex.getMessage());
ex.printStackTrace();
Assert.assertEquals("Failed to get expected message", expectedMessage, ex.getMessageCode());
assertEquals("Failed to get expected message", expectedMessage, ex.getMessageCode());
}
if (otherProperties != null && otherProperties.length != 0) {
// first one is expected position of the error within the string
int pos = ((Integer) otherProperties[0]).intValue();
Assert.assertEquals("Did not get correct position reported in error ", pos, ex.getPosition());
assertEquals("Did not get correct position reported in error ", pos, ex.getPosition());
if (otherProperties.length > 1) {
// Check inserts match
Object[] inserts = ex.getInserts();
@ -251,25 +252,25 @@ public abstract class ExpressionTestCase {
}
if (inserts.length < otherProperties.length - 1) {
ex.printStackTrace();
Assert.fail("Cannot check " + (otherProperties.length - 1)
fail("Cannot check " + (otherProperties.length - 1)
+ " properties of the exception, it only has " + inserts.length + " inserts");
}
for (int i = 1; i < otherProperties.length; i++) {
if (otherProperties[i] == null) {
if (inserts[i - 1] != null) {
ex.printStackTrace();
Assert.fail("Insert does not match, expected 'null' but insert value was '" + inserts[i - 1]
fail("Insert does not match, expected 'null' but insert value was '" + inserts[i - 1]
+ "'");
}
} else if (inserts[i - 1] == null) {
if (otherProperties[i] != null) {
ex.printStackTrace();
Assert.fail("Insert does not match, expected '" + otherProperties[i]
fail("Insert does not match, expected '" + otherProperties[i]
+ "' but insert value was 'null'");
}
} else if (!inserts[i - 1].equals(otherProperties[i])) {
ex.printStackTrace();
Assert.fail("Insert does not match, expected '" + otherProperties[i] + "' but insert value was '"
fail("Insert does not match, expected '" + otherProperties[i] + "' but insert value was '"
+ inserts[i - 1] + "'");
}
}
@ -277,7 +278,7 @@ public abstract class ExpressionTestCase {
}
} catch (ParseException pe) {
pe.printStackTrace();
Assert.fail("Unexpected Exception: " + pe.getMessage());
fail("Unexpected Exception: " + pe.getMessage());
}
}
@ -293,16 +294,16 @@ public abstract class ExpressionTestCase {
try {
Expression expr = parser.parseExpression(expression);
SpelUtilities.printAbstractSyntaxTree(System.out, expr);
Assert.fail("Parsing should have failed!");
fail("Parsing should have failed!");
} catch (ParseException pe) {
// pe.printStackTrace();
// Throwable t = pe.getCause();
// if (t == null) {
// Assert.fail("ParseException caught with no defined cause");
// fail("ParseException caught with no defined cause");
// }
// if (!(t instanceof SpelEvaluationException)) {
// t.printStackTrace();
// Assert.fail("Cause of parse exception is not a SpelException");
// fail("Cause of parse exception is not a SpelException");
// }
// SpelEvaluationException ex = (SpelEvaluationException) t;
// pe.printStackTrace();
@ -310,12 +311,12 @@ public abstract class ExpressionTestCase {
if (ex.getMessageCode() != expectedMessage) {
// System.out.println(ex.getMessage());
ex.printStackTrace();
Assert.assertEquals("Failed to get expected message", expectedMessage, ex.getMessageCode());
assertEquals("Failed to get expected message", expectedMessage, ex.getMessageCode());
}
if (otherProperties != null && otherProperties.length != 0) {
// first one is expected position of the error within the string
int pos = ((Integer) otherProperties[0]).intValue();
Assert.assertEquals("Did not get correct position reported in error ", pos, ex.getPosition());
assertEquals("Did not get correct position reported in error ", pos, ex.getPosition());
if (otherProperties.length > 1) {
// Check inserts match
Object[] inserts = ex.getInserts();
@ -324,13 +325,13 @@ public abstract class ExpressionTestCase {
}
if (inserts.length < otherProperties.length - 1) {
ex.printStackTrace();
Assert.fail("Cannot check " + (otherProperties.length - 1)
fail("Cannot check " + (otherProperties.length - 1)
+ " properties of the exception, it only has " + inserts.length + " inserts");
}
for (int i = 1; i < otherProperties.length; i++) {
if (!inserts[i - 1].equals(otherProperties[i])) {
ex.printStackTrace();
Assert.fail("Insert does not match, expected '" + otherProperties[i] + "' but insert value was '"
fail("Insert does not match, expected '" + otherProperties[i] + "' but insert value was '"
+ inserts[i - 1] + "'");
}
}

View File

@ -16,9 +16,9 @@
package org.springframework.expression.spel;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.Collection;

View File

@ -16,11 +16,11 @@
package org.springframework.expression.spel;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.HashMap;
import junit.framework.Assert;
import org.junit.Test;
import org.springframework.expression.spel.standard.SpelExpression;
@ -78,7 +78,7 @@ public class InProgressTests extends ExpressionTestCase {
@Test
public void testProjection06() throws Exception {
SpelExpression expr = (SpelExpression) parser.parseExpression("'abc'.![true]");
Assert.assertEquals("'abc'.![true]", expr.toStringAST());
assertEquals("'abc'.![true]", expr.toStringAST());
}
// SELECTION
@ -141,11 +141,11 @@ public class InProgressTests extends ExpressionTestCase {
@Test
public void testSelectionAST() throws Exception {
SpelExpression expr = (SpelExpression) parser.parseExpression("'abc'.^[true]");
Assert.assertEquals("'abc'.^[true]", expr.toStringAST());
assertEquals("'abc'.^[true]", expr.toStringAST());
expr = (SpelExpression) parser.parseExpression("'abc'.?[true]");
Assert.assertEquals("'abc'.?[true]", expr.toStringAST());
assertEquals("'abc'.?[true]", expr.toStringAST());
expr = (SpelExpression) parser.parseExpression("'abc'.$[true]");
Assert.assertEquals("'abc'.$[true]", expr.toStringAST());
assertEquals("'abc'.$[true]", expr.toStringAST());
}
// Constructor invocation

View File

@ -16,7 +16,9 @@
package org.springframework.expression.spel;
import junit.framework.Assert;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;
import org.junit.Test;
import org.springframework.expression.EvaluationContext;
@ -41,10 +43,10 @@ public class LiteralExpressionTests {
checkString("somevalue", lEx.getValue(new Rooty(), String.class));
checkString("somevalue", lEx.getValue(ctx, new Rooty()));
checkString("somevalue", lEx.getValue(ctx, new Rooty(),String.class));
Assert.assertEquals("somevalue", lEx.getExpressionString());
Assert.assertFalse(lEx.isWritable(new StandardEvaluationContext()));
Assert.assertFalse(lEx.isWritable(new Rooty()));
Assert.assertFalse(lEx.isWritable(new StandardEvaluationContext(), new Rooty()));
assertEquals("somevalue", lEx.getExpressionString());
assertFalse(lEx.isWritable(new StandardEvaluationContext()));
assertFalse(lEx.isWritable(new Rooty()));
assertFalse(lEx.isWritable(new StandardEvaluationContext(), new Rooty()));
}
static class Rooty {}
@ -54,51 +56,51 @@ public class LiteralExpressionTests {
try {
LiteralExpression lEx = new LiteralExpression("somevalue");
lEx.setValue(new StandardEvaluationContext(), "flibble");
Assert.fail("Should have got an exception that the value cannot be set");
fail("Should have got an exception that the value cannot be set");
}
catch (EvaluationException ee) {
// success, not allowed - whilst here, check the expression value in the exception
Assert.assertEquals(ee.getExpressionString(), "somevalue");
assertEquals(ee.getExpressionString(), "somevalue");
}
try {
LiteralExpression lEx = new LiteralExpression("somevalue");
lEx.setValue(new Rooty(), "flibble");
Assert.fail("Should have got an exception that the value cannot be set");
fail("Should have got an exception that the value cannot be set");
}
catch (EvaluationException ee) {
// success, not allowed - whilst here, check the expression value in the exception
Assert.assertEquals(ee.getExpressionString(), "somevalue");
assertEquals(ee.getExpressionString(), "somevalue");
}
try {
LiteralExpression lEx = new LiteralExpression("somevalue");
lEx.setValue(new StandardEvaluationContext(), new Rooty(), "flibble");
Assert.fail("Should have got an exception that the value cannot be set");
fail("Should have got an exception that the value cannot be set");
}
catch (EvaluationException ee) {
// success, not allowed - whilst here, check the expression value in the exception
Assert.assertEquals(ee.getExpressionString(), "somevalue");
assertEquals(ee.getExpressionString(), "somevalue");
}
}
@Test
public void testGetValueType() throws Exception {
LiteralExpression lEx = new LiteralExpression("somevalue");
Assert.assertEquals(String.class, lEx.getValueType());
Assert.assertEquals(String.class, lEx.getValueType(new StandardEvaluationContext()));
Assert.assertEquals(String.class, lEx.getValueType(new Rooty()));
Assert.assertEquals(String.class, lEx.getValueType(new StandardEvaluationContext(), new Rooty()));
Assert.assertEquals(String.class, lEx.getValueTypeDescriptor().getType());
Assert.assertEquals(String.class, lEx.getValueTypeDescriptor(new StandardEvaluationContext()).getType());
Assert.assertEquals(String.class, lEx.getValueTypeDescriptor(new Rooty()).getType());
Assert.assertEquals(String.class, lEx.getValueTypeDescriptor(new StandardEvaluationContext(), new Rooty()).getType());
assertEquals(String.class, lEx.getValueType());
assertEquals(String.class, lEx.getValueType(new StandardEvaluationContext()));
assertEquals(String.class, lEx.getValueType(new Rooty()));
assertEquals(String.class, lEx.getValueType(new StandardEvaluationContext(), new Rooty()));
assertEquals(String.class, lEx.getValueTypeDescriptor().getType());
assertEquals(String.class, lEx.getValueTypeDescriptor(new StandardEvaluationContext()).getType());
assertEquals(String.class, lEx.getValueTypeDescriptor(new Rooty()).getType());
assertEquals(String.class, lEx.getValueTypeDescriptor(new StandardEvaluationContext(), new Rooty()).getType());
}
private void checkString(String expectedString, Object value) {
if (!(value instanceof String)) {
Assert.fail("Result was not a string, it was of type " + value.getClass() + " (value=" + value + ")");
fail("Result was not a string, it was of type " + value.getClass() + " (value=" + value + ")");
}
if (!((String) value).equals(expectedString)) {
Assert.fail("Did not get expected result. Should have been '" + expectedString + "' but was '" + value + "'");
fail("Did not get expected result. Should have been '" + expectedString + "' but was '" + value + "'");
}
}

View File

@ -16,7 +16,8 @@
package org.springframework.expression.spel;
import junit.framework.Assert;
import static org.junit.Assert.assertFalse;
import org.junit.Test;
import org.springframework.expression.spel.standard.SpelExpression;
import org.springframework.expression.spel.support.StandardEvaluationContext;
@ -163,10 +164,10 @@ public class LiteralTests extends ExpressionTestCase {
@Test
public void testNotWritable() throws Exception {
SpelExpression expr = (SpelExpression)parser.parseExpression("37");
Assert.assertFalse(expr.isWritable(new StandardEvaluationContext()));
assertFalse(expr.isWritable(new StandardEvaluationContext()));
expr = (SpelExpression)parser.parseExpression("37L");
Assert.assertFalse(expr.isWritable(new StandardEvaluationContext()));
assertFalse(expr.isWritable(new StandardEvaluationContext()));
expr = (SpelExpression)parser.parseExpression("true");
Assert.assertFalse(expr.isWritable(new StandardEvaluationContext()));
assertFalse(expr.isWritable(new StandardEvaluationContext()));
}
}

View File

@ -16,10 +16,10 @@
package org.springframework.expression.spel;
import java.util.Map;
import java.util.HashMap;
import static org.junit.Assert.assertEquals;
import junit.framework.Assert;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import org.springframework.expression.AccessException;
@ -56,7 +56,7 @@ public class MapAccessTests extends ExpressionTestCase {
Expression expr = parser.parseExpression("testMap.monday");
Object value = expr.getValue(ctx, String.class);
Assert.assertEquals("montag", value);
assertEquals("montag", value);
}
@Test
@ -67,7 +67,7 @@ public class MapAccessTests extends ExpressionTestCase {
Expression expr = parser.parseExpression("testMap[#day]");
Object value = expr.getValue(ctx, String.class);
Assert.assertEquals("samstag", value);
assertEquals("samstag", value);
}
@Test

View File

@ -16,7 +16,7 @@
package org.springframework.expression.spel;
import junit.framework.Assert;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.expression.EvaluationException;
@ -64,12 +64,12 @@ public class OperatorOverloaderTests extends ExpressionTestCase {
eContext.setOperatorOverloader(new StringAndBooleanAddition());
SpelExpression expr = (SpelExpression)parser.parseExpression("'abc'+true");
Assert.assertEquals("abctrue",expr.getValue(eContext));
assertEquals("abctrue",expr.getValue(eContext));
expr = (SpelExpression)parser.parseExpression("'abc'-true");
Assert.assertEquals("abc",expr.getValue(eContext));
assertEquals("abc",expr.getValue(eContext));
expr = (SpelExpression)parser.parseExpression("'abc'+null");
Assert.assertEquals("abcnull",expr.getValue(eContext));
assertEquals("abcnull",expr.getValue(eContext));
}
}

View File

@ -16,7 +16,8 @@
package org.springframework.expression.spel;
import junit.framework.Assert;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.expression.spel.ast.Operator;
import org.springframework.expression.spel.standard.SpelExpression;
@ -208,9 +209,9 @@ public class OperatorTests extends ExpressionTestCase {
// AST:
SpelExpression expr = (SpelExpression)parser.parseExpression("+3");
Assert.assertEquals("+3",expr.toStringAST());
assertEquals("+3",expr.toStringAST());
expr = (SpelExpression)parser.parseExpression("2+3");
Assert.assertEquals("(2 + 3)",expr.toStringAST());
assertEquals("(2 + 3)",expr.toStringAST());
// use as a unary operator
evaluate("+5d",5d,Double.class);
@ -232,9 +233,9 @@ public class OperatorTests extends ExpressionTestCase {
evaluateAndCheckError("'ab' - 2", SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
evaluateAndCheckError("2-'ab'",SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
SpelExpression expr = (SpelExpression)parser.parseExpression("-3");
Assert.assertEquals("-3",expr.toStringAST());
assertEquals("-3",expr.toStringAST());
expr = (SpelExpression)parser.parseExpression("2-3");
Assert.assertEquals("(2 - 3)",expr.toStringAST());
assertEquals("(2 - 3)",expr.toStringAST());
evaluate("-5d",-5d,Double.class);
evaluate("-5L",-5L,Long.class);
@ -286,40 +287,40 @@ public class OperatorTests extends ExpressionTestCase {
@Test
public void testOperatorNames() throws Exception {
Operator node = getOperatorNode((SpelExpression)parser.parseExpression("1==3"));
Assert.assertEquals("==",node.getOperatorName());
assertEquals("==",node.getOperatorName());
node = getOperatorNode((SpelExpression)parser.parseExpression("1!=3"));
Assert.assertEquals("!=",node.getOperatorName());
assertEquals("!=",node.getOperatorName());
node = getOperatorNode((SpelExpression)parser.parseExpression("3/3"));
Assert.assertEquals("/",node.getOperatorName());
assertEquals("/",node.getOperatorName());
node = getOperatorNode((SpelExpression)parser.parseExpression("3+3"));
Assert.assertEquals("+",node.getOperatorName());
assertEquals("+",node.getOperatorName());
node = getOperatorNode((SpelExpression)parser.parseExpression("3-3"));
Assert.assertEquals("-",node.getOperatorName());
assertEquals("-",node.getOperatorName());
node = getOperatorNode((SpelExpression)parser.parseExpression("3<4"));
Assert.assertEquals("<",node.getOperatorName());
assertEquals("<",node.getOperatorName());
node = getOperatorNode((SpelExpression)parser.parseExpression("3<=4"));
Assert.assertEquals("<=",node.getOperatorName());
assertEquals("<=",node.getOperatorName());
node = getOperatorNode((SpelExpression)parser.parseExpression("3*4"));
Assert.assertEquals("*",node.getOperatorName());
assertEquals("*",node.getOperatorName());
node = getOperatorNode((SpelExpression)parser.parseExpression("3%4"));
Assert.assertEquals("%",node.getOperatorName());
assertEquals("%",node.getOperatorName());
node = getOperatorNode((SpelExpression)parser.parseExpression("3>=4"));
Assert.assertEquals(">=",node.getOperatorName());
assertEquals(">=",node.getOperatorName());
node = getOperatorNode((SpelExpression)parser.parseExpression("3 between 4"));
Assert.assertEquals("between",node.getOperatorName());
assertEquals("between",node.getOperatorName());
node = getOperatorNode((SpelExpression)parser.parseExpression("3 ^ 4"));
Assert.assertEquals("^",node.getOperatorName());
assertEquals("^",node.getOperatorName());
}
@Test

View File

@ -16,7 +16,8 @@
package org.springframework.expression.spel;
import junit.framework.Assert;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.junit.Test;
import org.springframework.expression.ParseException;
@ -462,12 +463,12 @@ public class ParsingTests {
SpelUtilities.printAbstractSyntaxTree(System.err, e);
}
if (e == null) {
Assert.fail("Parsed exception was null");
fail("Parsed exception was null");
}
Assert.assertEquals("String form of AST does not match expected output", expectedStringFormOfAST, e.toStringAST());
assertEquals("String form of AST does not match expected output", expectedStringFormOfAST, e.toStringAST());
} catch (ParseException ee) {
ee.printStackTrace();
Assert.fail("Unexpected Exception: " + ee.getMessage());
fail("Unexpected Exception: " + ee.getMessage());
}
}

Some files were not shown because too many files have changed in this diff Show More