Moved tests from testsuite to beans

This commit is contained in:
Arjen Poutsma
2008-10-29 18:08:07 +00:00
parent dcaf024e76
commit e788cef856
7 changed files with 148 additions and 0 deletions

View File

@@ -0,0 +1,86 @@
/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.factory.parsing;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.springframework.core.CollectionFactory;
/**
* @author Rob Harrop
* @author Juergen Hoeller
*/
public class CollectingReaderEventListener implements ReaderEventListener {
private final List defaults = new LinkedList();
private final Map componentDefinitions = CollectionFactory.createLinkedMapIfPossible(8);
private final Map aliasMap = CollectionFactory.createLinkedMapIfPossible(8);
private final List imports = new LinkedList();
public void defaultsRegistered(DefaultsDefinition defaultsDefinition) {
this.defaults.add(defaultsDefinition);
}
public List getDefaults() {
return Collections.unmodifiableList(this.defaults);
}
public void componentRegistered(ComponentDefinition componentDefinition) {
this.componentDefinitions.put(componentDefinition.getName(), componentDefinition);
}
public ComponentDefinition getComponentDefinition(String name) {
return (ComponentDefinition) this.componentDefinitions.get(name);
}
public ComponentDefinition[] getComponentDefinitions() {
Collection collection = this.componentDefinitions.values();
return (ComponentDefinition[]) collection.toArray(new ComponentDefinition[collection.size()]);
}
public void aliasRegistered(AliasDefinition aliasDefinition) {
List aliases = (List) this.aliasMap.get(aliasDefinition.getBeanName());
if(aliases == null) {
aliases = new ArrayList();
this.aliasMap.put(aliasDefinition.getBeanName(), aliases);
}
aliases.add(aliasDefinition);
}
public List getAliases(String beanName) {
List aliases = (List) this.aliasMap.get(beanName);
return aliases == null ? null : Collections.unmodifiableList(aliases);
}
public void importProcessed(ImportDefinition importDefinition) {
this.imports.add(importDefinition);
}
public List getImports() {
return Collections.unmodifiableList(this.imports);
}
}

View File

@@ -0,0 +1,114 @@
/*
* Copyright 2002-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.factory.xml;
import junit.framework.TestCase;
import org.springframework.core.io.ClassPathResource;
/**
* @author Rob Harrop
*/
public class DefaultLifecycleMethodsTests extends TestCase {
private XmlBeanFactory beanFactory;
protected void setUp() throws Exception {
this.beanFactory = new XmlBeanFactory(new ClassPathResource("defaultLifecycleMethods.xml", getClass()));
}
public void testLifecycleMethodsInvoked() {
LifecycleAwareBean bean = (LifecycleAwareBean) this.beanFactory.getBean("lifecycleAware");
assertTrue("Bean not initialized", bean.isInitCalled());
assertFalse("Bean destroyed too early", bean.isDestroyCalled());
this.beanFactory.destroySingletons();
assertTrue("Bean not destroyed", bean.isDestroyCalled());
}
public void testLifecycleMethodsDisabled() throws Exception {
LifecycleAwareBean bean = (LifecycleAwareBean) this.beanFactory.getBean("lifecycleMethodsDisabled");
assertFalse("Bean init method called incorrectly", bean.isInitCalled());
this.beanFactory.destroySingletons();
assertFalse("Bean destroy method called incorrectly", bean.isDestroyCalled());
}
public void testIgnoreDefaultLifecycleMethods() throws Exception {
try {
XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("ignoreDefaultLifecycleMethods.xml", getClass()));
bf.preInstantiateSingletons();
bf.destroySingletons();
}
catch (Exception ex) {
ex.printStackTrace();
fail("Should ignore non-existent default lifecycle methods");
}
}
public void testOverrideDefaultLifecycleMethods() throws Exception {
LifecycleAwareBean bean = (LifecycleAwareBean) this.beanFactory.getBean("overrideLifecycleMethods");
assertFalse("Default init method called incorrectly.", bean.isInitCalled());
assertTrue("Custom init method not called.", bean.isCustomInitCalled());
this.beanFactory.destroySingletons();
assertFalse("Default destory method called incorrectly.", bean.isDestroyCalled());
assertTrue("Custom destory method not called.", bean.isCustomDestroyCalled());
}
public static class LifecycleAwareBean {
private boolean initCalled;
private boolean destroyCalled;
private boolean customInitCalled;
private boolean customDestroyCalled;
public void init() {
this.initCalled = true;
}
public void destroy() {
this.destroyCalled = true;
}
public void customInit() {
this.customInitCalled = true;
}
public void customDestroy() {
this.customDestroyCalled = true;
}
public boolean isInitCalled() {
return initCalled;
}
public boolean isDestroyCalled() {
return destroyCalled;
}
public boolean isCustomInitCalled() {
return customInitCalled;
}
public boolean isCustomDestroyCalled() {
return customDestroyCalled;
}
}
}

View File

@@ -0,0 +1,115 @@
/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.factory.xml;
import java.util.List;
import junit.framework.TestCase;
import org.w3c.dom.Element;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.TypedStringValue;
import org.springframework.beans.factory.parsing.AliasDefinition;
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
import org.springframework.beans.factory.parsing.CollectingReaderEventListener;
import org.springframework.beans.factory.parsing.ComponentDefinition;
import org.springframework.beans.factory.parsing.ImportDefinition;
import org.springframework.beans.factory.parsing.PassThroughSourceExtractor;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.core.io.ClassPathResource;
/**
* @author Rob Harrop
* @author Juergen Hoeller
*/
public class EventPublicationTests extends TestCase {
private final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
private final CollectingReaderEventListener eventListener = new CollectingReaderEventListener();
protected void setUp() throws Exception {
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this.beanFactory);
reader.setEventListener(this.eventListener);
reader.setSourceExtractor(new PassThroughSourceExtractor());
reader.loadBeanDefinitions(new ClassPathResource("beanEvents.xml", getClass()));
}
public void testDefaultsEventReceived() throws Exception {
List defaultsList = this.eventListener.getDefaults();
assertTrue(!defaultsList.isEmpty());
assertTrue(defaultsList.get(0) instanceof DocumentDefaultsDefinition);
DocumentDefaultsDefinition defaults = (DocumentDefaultsDefinition) defaultsList.get(0);
assertEquals("true", defaults.getLazyInit());
assertEquals("constructor", defaults.getAutowire());
assertEquals("objects", defaults.getDependencyCheck());
assertEquals("myInit", defaults.getInitMethod());
assertEquals("myDestroy", defaults.getDestroyMethod());
assertEquals("true", defaults.getMerge());
assertTrue(defaults.getSource() instanceof Element);
}
public void testBeanEventReceived() throws Exception {
ComponentDefinition componentDefinition1 = this.eventListener.getComponentDefinition("testBean");
assertTrue(componentDefinition1 instanceof BeanComponentDefinition);
assertEquals(1, componentDefinition1.getBeanDefinitions().length);
BeanDefinition beanDefinition1 = componentDefinition1.getBeanDefinitions()[0];
assertEquals(new TypedStringValue("Rob Harrop"),
beanDefinition1.getConstructorArgumentValues().getGenericArgumentValue(String.class).getValue());
assertEquals(1, componentDefinition1.getBeanReferences().length);
assertEquals("testBean2", componentDefinition1.getBeanReferences()[0].getBeanName());
assertEquals(1, componentDefinition1.getInnerBeanDefinitions().length);
BeanDefinition innerBd1 = componentDefinition1.getInnerBeanDefinitions()[0];
assertEquals(new TypedStringValue("ACME"),
innerBd1.getConstructorArgumentValues().getGenericArgumentValue(String.class).getValue());
assertTrue(componentDefinition1.getSource() instanceof Element);
ComponentDefinition componentDefinition2 = this.eventListener.getComponentDefinition("testBean2");
assertTrue(componentDefinition2 instanceof BeanComponentDefinition);
assertEquals(1, componentDefinition1.getBeanDefinitions().length);
BeanDefinition beanDefinition2 = componentDefinition2.getBeanDefinitions()[0];
assertEquals(new TypedStringValue("Juergen Hoeller"),
beanDefinition2.getPropertyValues().getPropertyValue("name").getValue());
assertEquals(0, componentDefinition2.getBeanReferences().length);
assertEquals(1, componentDefinition2.getInnerBeanDefinitions().length);
BeanDefinition innerBd2 = componentDefinition2.getInnerBeanDefinitions()[0];
assertEquals(new TypedStringValue("Eva Schallmeiner"),
innerBd2.getPropertyValues().getPropertyValue("name").getValue());
assertTrue(componentDefinition2.getSource() instanceof Element);
}
public void testAliasEventReceived() throws Exception {
List aliases = this.eventListener.getAliases("testBean");
assertEquals(2, aliases.size());
AliasDefinition aliasDefinition1 = (AliasDefinition) aliases.get(0);
assertEquals("testBeanAlias1", aliasDefinition1.getAlias());
assertTrue(aliasDefinition1.getSource() instanceof Element);
AliasDefinition aliasDefinition2 = (AliasDefinition) aliases.get(1);
assertEquals("testBeanAlias2", aliasDefinition2.getAlias());
assertTrue(aliasDefinition2.getSource() instanceof Element);
}
public void testImportEventReceived() throws Exception {
List imports = this.eventListener.getImports();
assertEquals(1, imports.size());
ImportDefinition importDefinition = (ImportDefinition) imports.get(0);
assertEquals("beanEventsImported.xml", importDefinition.getImportedResource());
assertTrue(importDefinition.getSource() instanceof Element);
}
}