moving unit tests from .testsuite -> .context.support

This commit is contained in:
Chris Beams
2008-12-14 17:35:21 +00:00
parent 7432202b6a
commit 0a47beb647
4 changed files with 13 additions and 6 deletions

View File

@@ -0,0 +1,104 @@
/*
* 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.context.support;
import static org.junit.Assert.*;
import org.junit.Test;
/**
* @author Mark Fisher
* @author Chris Beams
*/
public class ApplicationContextLifecycleTests {
@Test
public void testBeansStart() {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("lifecycleTests.xml", getClass());
context.start();
LifecycleTestBean bean1 = (LifecycleTestBean) context.getBean("bean1");
LifecycleTestBean bean2 = (LifecycleTestBean) context.getBean("bean2");
LifecycleTestBean bean3 = (LifecycleTestBean) context.getBean("bean3");
LifecycleTestBean bean4 = (LifecycleTestBean) context.getBean("bean4");
String error = "bean was not started";
assertTrue(error, bean1.isRunning());
assertTrue(error, bean2.isRunning());
assertTrue(error, bean3.isRunning());
assertTrue(error, bean4.isRunning());
}
@Test
public void testBeansStop() {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("lifecycleTests.xml", getClass());
context.start();
LifecycleTestBean bean1 = (LifecycleTestBean) context.getBean("bean1");
LifecycleTestBean bean2 = (LifecycleTestBean) context.getBean("bean2");
LifecycleTestBean bean3 = (LifecycleTestBean) context.getBean("bean3");
LifecycleTestBean bean4 = (LifecycleTestBean) context.getBean("bean4");
String startError = "bean was not started";
assertTrue(startError, bean1.isRunning());
assertTrue(startError, bean2.isRunning());
assertTrue(startError, bean3.isRunning());
assertTrue(startError, bean4.isRunning());
context.stop();
String stopError = "bean was not stopped";
assertFalse(stopError, bean1.isRunning());
assertFalse(stopError, bean2.isRunning());
assertFalse(stopError, bean3.isRunning());
assertFalse(stopError, bean4.isRunning());
}
@Test
public void testStartOrder() {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("lifecycleTests.xml", getClass());
context.start();
LifecycleTestBean bean1 = (LifecycleTestBean) context.getBean("bean1");
LifecycleTestBean bean2 = (LifecycleTestBean) context.getBean("bean2");
LifecycleTestBean bean3 = (LifecycleTestBean) context.getBean("bean3");
LifecycleTestBean bean4 = (LifecycleTestBean) context.getBean("bean4");
String notStartedError = "bean was not started";
assertTrue(notStartedError, bean1.getStartOrder() > 0);
assertTrue(notStartedError, bean2.getStartOrder() > 0);
assertTrue(notStartedError, bean3.getStartOrder() > 0);
assertTrue(notStartedError, bean4.getStartOrder() > 0);
String orderError = "dependent bean must start after the bean it depends on";
assertTrue(orderError, bean2.getStartOrder() > bean1.getStartOrder());
assertTrue(orderError, bean3.getStartOrder() > bean2.getStartOrder());
assertTrue(orderError, bean4.getStartOrder() > bean2.getStartOrder());
}
@Test
public void testStopOrder() {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("lifecycleTests.xml", getClass());
context.start();
context.stop();
LifecycleTestBean bean1 = (LifecycleTestBean) context.getBean("bean1");
LifecycleTestBean bean2 = (LifecycleTestBean) context.getBean("bean2");
LifecycleTestBean bean3 = (LifecycleTestBean) context.getBean("bean3");
LifecycleTestBean bean4 = (LifecycleTestBean) context.getBean("bean4");
String notStoppedError = "bean was not stopped";
assertTrue(notStoppedError, bean1.getStopOrder() > 0);
assertTrue(notStoppedError, bean2.getStopOrder() > 0);
assertTrue(notStoppedError, bean3.getStopOrder() > 0);
assertTrue(notStoppedError, bean4.getStopOrder() > 0);
String orderError = "dependent bean must stop before the bean it depends on";
assertTrue(orderError, bean2.getStopOrder() < bean1.getStopOrder());
assertTrue(orderError, bean3.getStopOrder() < bean2.getStopOrder());
assertTrue(orderError, bean4.getStopOrder() < bean2.getStopOrder());
}
}

View File

@@ -0,0 +1,60 @@
/*
* 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.context.support;
import org.springframework.context.Lifecycle;
/**
* @author Mark Fisher
*/
public class LifecycleTestBean implements Lifecycle {
private static int startCounter;
private static int stopCounter;
private int startOrder;
private int stopOrder;
private boolean running;
public int getStartOrder() {
return startOrder;
}
public int getStopOrder() {
return stopOrder;
}
public boolean isRunning() {
return this.running;
}
public void start() {
this.startOrder = ++startCounter;
this.running = true;
}
public void stop() {
this.stopOrder = ++stopCounter;
this.running = false;
}
}

View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="bean4" class="org.springframework.context.support.LifecycleTestBean" depends-on="bean2"/>
<bean id="bean3" class="org.springframework.context.support.LifecycleTestBean" depends-on="bean2"/>
<bean id="bean1" class="org.springframework.context.support.LifecycleTestBean"/>
<bean id="bean2" class="org.springframework.context.support.LifecycleTestBean" depends-on="bean1"/>
<bean id="bean2Proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="bean2"/>
</bean>
</beans>