polishing .aop tests

This commit is contained in:
Chris Beams 2008-12-21 00:12:12 +00:00
parent 4671fd7141
commit 6966099d85
18 changed files with 122 additions and 84 deletions

View File

@ -19,13 +19,14 @@ package org.springframework.aop.aspectj;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.aop.framework.autoproxy.CountingTestBean;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import test.beans.CountingTestBean;
import test.beans.IOther;
import test.beans.ITestBean;
import test.beans.TestBean;
import test.beans.subpkg.DeepBean;
/**
* Unit tests for the {@link TypePatternClassFilter} class.
@ -48,7 +49,7 @@ public final class TypePatternClassFilterTests {
assertTrue("Must match: in package", tpcf.matches(TestBean.class));
assertTrue("Must match: in package", tpcf.matches(ITestBean.class));
assertTrue("Must match: in package", tpcf.matches(IOther.class));
assertFalse("Must be excluded: in wrong package", tpcf.matches(CountingTestBean.class));
assertFalse("Must be excluded: in wrong package", tpcf.matches(DeepBean.class));
assertFalse("Must be excluded: in wrong package", tpcf.matches(BeanFactory.class));
assertFalse("Must be excluded: in wrong package", tpcf.matches(DefaultListableBeanFactory.class));
}

View File

@ -17,6 +17,7 @@
package org.springframework.aop.config;
import static org.junit.Assert.*;
import static test.util.TestResourceUtils.qualifiedResource;
import java.util.HashSet;
import java.util.Set;
@ -30,7 +31,7 @@ import org.springframework.beans.factory.parsing.ComponentDefinition;
import org.springframework.beans.factory.parsing.CompositeComponentDefinition;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import test.parsing.CollectingReaderEventListener;
@ -42,12 +43,11 @@ import test.parsing.CollectingReaderEventListener;
public final class AopNamespaceHandlerEventTests {
private static final Class<?> CLASS = AopNamespaceHandlerEventTests.class;
private static final String CLASSNAME = CLASS.getSimpleName();
private static final String CONTEXT = CLASSNAME + "-context.xml";
private static final String POINTCUT_EVENTS_CONTEXT = CLASSNAME + "-pointcutEvents.xml";
private static final String POINTCUT_REF_CONTEXT = CLASSNAME + "-pointcutRefEvents.xml";
private static final String DIRECT_POINTCUT_EVENTS_CONTEXT = CLASSNAME + "-directPointcutEvents.xml";
private static final Resource CONTEXT = qualifiedResource(CLASS, "context.xml");
private static final Resource POINTCUT_EVENTS_CONTEXT = qualifiedResource(CLASS, "pointcutEvents.xml");
private static final Resource POINTCUT_REF_CONTEXT = qualifiedResource(CLASS, "pointcutRefEvents.xml");
private static final Resource DIRECT_POINTCUT_EVENTS_CONTEXT = qualifiedResource(CLASS, "directPointcutEvents.xml");
private CollectingReaderEventListener eventListener = new CollectingReaderEventListener();
@ -65,7 +65,7 @@ public final class AopNamespaceHandlerEventTests {
@Test
public void testPointcutEvents() throws Exception {
loadBeansFrom(POINTCUT_EVENTS_CONTEXT);
this.reader.loadBeanDefinitions(POINTCUT_EVENTS_CONTEXT);
ComponentDefinition[] componentDefinitions = this.eventListener.getComponentDefinitions();
assertEquals("Incorrect number of events fired", 1, componentDefinitions.length);
assertTrue("No holder with nested components", componentDefinitions[0] instanceof CompositeComponentDefinition);
@ -89,7 +89,7 @@ public final class AopNamespaceHandlerEventTests {
@Test
public void testAdvisorEventsWithPointcutRef() throws Exception {
loadBeansFrom(POINTCUT_REF_CONTEXT);
this.reader.loadBeanDefinitions(POINTCUT_REF_CONTEXT);
ComponentDefinition[] componentDefinitions = this.eventListener.getComponentDefinitions();
assertEquals("Incorrect number of events fired", 2, componentDefinitions.length);
@ -118,7 +118,7 @@ public final class AopNamespaceHandlerEventTests {
@Test
public void testAdvisorEventsWithDirectPointcut() throws Exception {
loadBeansFrom(DIRECT_POINTCUT_EVENTS_CONTEXT);
this.reader.loadBeanDefinitions(DIRECT_POINTCUT_EVENTS_CONTEXT);
ComponentDefinition[] componentDefinitions = this.eventListener.getComponentDefinitions();
assertEquals("Incorrect number of events fired", 2, componentDefinitions.length);
@ -147,7 +147,7 @@ public final class AopNamespaceHandlerEventTests {
@Test
public void testAspectEvent() throws Exception {
loadBeansFrom(CONTEXT);
this.reader.loadBeanDefinitions(CONTEXT);
ComponentDefinition[] componentDefinitions = this.eventListener.getComponentDefinitions();
assertEquals("Incorrect number of events fired", 5, componentDefinitions.length);
@ -192,8 +192,4 @@ public final class AopNamespaceHandlerEventTests {
assertEquals("Incorrect number of BeanDefinitions", 1, pcd.getBeanDefinitions().length);
}
private void loadBeansFrom(String path) {
this.reader.loadBeanDefinitions(new ClassPathResource(path, getClass()));
}
}

View File

@ -17,12 +17,12 @@
package org.springframework.aop.config;
import static org.junit.Assert.*;
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.core.io.ClassPathResource;
/**
* @author Mark Fisher
@ -30,19 +30,10 @@ import org.springframework.core.io.ClassPathResource;
*/
public final class AopNamespaceHandlerPointcutErrorTests {
private static final Class<?> CLASS = AopNamespaceHandlerPointcutErrorTests.class;
private static final String CLASSNAME = CLASS.getSimpleName();
private static final ClassPathResource DUPLICATION_CONTEXT =
new ClassPathResource(CLASSNAME + "-pointcutDuplication.xml", CLASS);
private static final ClassPathResource MISSING_CONTEXT =
new ClassPathResource(CLASSNAME + "-pointcutMissing.xml", CLASS);
@Test
public void testDuplicatePointcutConfig() {
try {
new XmlBeanFactory(DUPLICATION_CONTEXT);
new XmlBeanFactory(qualifiedResource(getClass(), "pointcutDuplication.xml"));
fail("parsing should have caused a BeanDefinitionStoreException");
}
catch (BeanDefinitionStoreException ex) {
@ -53,7 +44,7 @@ public final class AopNamespaceHandlerPointcutErrorTests {
@Test
public void testMissingPointcutConfig() {
try {
new XmlBeanFactory(MISSING_CONTEXT);
new XmlBeanFactory(qualifiedResource(getClass(), "pointcutMissing.xml"));
fail("parsing should have caused a BeanDefinitionStoreException");
}
catch (BeanDefinitionStoreException ex) {

View File

@ -17,11 +17,12 @@
package org.springframework.aop.config;
import static org.junit.Assert.assertTrue;
import static test.util.TestResourceUtils.qualifiedResource;
import org.junit.Test;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
/**
* Tests that the &lt;aop:config/&gt; element can be used as a top level element.
@ -31,9 +32,7 @@ import org.springframework.core.io.ClassPathResource;
*/
public final class TopLevelAopTagTests {
private static final Class<?> CLASS = TopLevelAopTagTests.class;
private static final ClassPathResource CONTEXT =
new ClassPathResource(CLASS.getSimpleName() + "-context.xml", CLASS);
private static final Resource CONTEXT = qualifiedResource(TopLevelAopTagTests.class, "context.xml");
@Test
public void testParse() throws Exception {

View File

@ -16,14 +16,14 @@
package org.springframework.aop.framework;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
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.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
/**
* @author Juergen Hoeller
@ -32,9 +32,7 @@ import org.springframework.core.io.ClassPathResource;
*/
public final class PrototypeTargetTests {
private static final Class<?> CLASS = PrototypeTargetTests.class;
private static final String CLASSNAME = CLASS.getSimpleName();
private static final ClassPathResource CONTEXT = new ClassPathResource(CLASSNAME + "-context.xml", CLASS);
private static final Resource CONTEXT = qualifiedResource(PrototypeTargetTests.class, "context.xml");
@Test
public void testPrototypeProxyWithPrototypeTarget() {

View File

@ -16,13 +16,13 @@
package org.springframework.aop.interceptor;
import static org.junit.Assert.assertEquals;
import junit.framework.TestCase;
import static org.junit.Assert.*;
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.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import test.beans.ITestBean;
import test.beans.TestBean;
@ -33,11 +33,10 @@ import test.beans.TestBean;
* @author Rod Johnson
* @author Chris Beams
*/
public class ExposeInvocationInterceptorTests {
public final class ExposeInvocationInterceptorTests {
private static final Class<?> CLASS = ExposeInvocationInterceptorTests.class;
private static final ClassPathResource CONTEXT =
new ClassPathResource(CLASS.getSimpleName() + "-context.xml", CLASS);
private static final Resource CONTEXT =
qualifiedResource(ExposeInvocationInterceptorTests.class, "context.xml");
@Test
public void testXmlConfig() {
@ -72,8 +71,8 @@ abstract class ExposedInvocationTestBean extends TestBean {
class InvocationCheckExposedInvocationTestBean extends ExposedInvocationTestBean {
protected void assertions(MethodInvocation invocation) {
TestCase.assertTrue(invocation.getThis() == this);
TestCase.assertTrue("Invocation should be on ITestBean: " + invocation.getMethod(),
assertTrue(invocation.getThis() == this);
assertTrue("Invocation should be on ITestBean: " + invocation.getMethod(),
ITestBean.class.isAssignableFrom(invocation.getMethod().getDeclaringClass()));
}
}

View File

@ -17,10 +17,11 @@
package org.springframework.aop.scope;
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.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
/**
* @author Mark Fisher
@ -29,12 +30,9 @@ import org.springframework.core.io.ClassPathResource;
public final class ScopedProxyAutowireTests {
private static final Class<?> CLASS = ScopedProxyAutowireTests.class;
private static final String CLASSNAME = CLASS.getSimpleName();
private static final ClassPathResource SCOPED_AUTOWIRE_TRUE_CONTEXT =
new ClassPathResource(CLASSNAME + "-scopedAutowireTrue.xml", CLASS);
private static final ClassPathResource SCOPED_AUTOWIRE_FALSE_CONTEXT =
new ClassPathResource(CLASSNAME + "-scopedAutowireFalse.xml", CLASS);
private static final Resource SCOPED_AUTOWIRE_TRUE_CONTEXT = qualifiedResource(CLASS, "scopedAutowireTrue.xml");
private static final Resource SCOPED_AUTOWIRE_FALSE_CONTEXT = qualifiedResource(CLASS, "scopedAutowireFalse.xml");
@Test
public void testScopedProxyInheritsAutowireCandidateFalse() {

View File

@ -43,7 +43,6 @@ public final class NameMatchMethodPointcutTests {
/**
* Create an empty pointcut, populating instance variables.
* @see junit.framework.TestCase#setUp()
*/
@Before
public void setUp() {

View File

@ -17,12 +17,13 @@
package org.springframework.aop.support;
import static org.junit.Assert.assertEquals;
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.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import test.aop.NopInterceptor;
import test.aop.SerializableNopInterceptor;
@ -37,11 +38,8 @@ import test.util.SerializationTestUtils;
*/
public final class RegexpMethodPointcutAdvisorIntegrationTests {
private static final Class<?> CLASS = RegexpMethodPointcutAdvisorIntegrationTests.class;
private static final String CLASSNAME = CLASS.getSimpleName();
private static final ClassPathResource CONTEXT =
new ClassPathResource(CLASSNAME + "-context.xml", CLASS);
private static final Resource CONTEXT =
qualifiedResource(RegexpMethodPointcutAdvisorIntegrationTests.class, "context.xml");
@Test
public void testSinglePattern() throws Throwable {

View File

@ -17,12 +17,13 @@
package org.springframework.aop.target;
import static org.junit.Assert.assertTrue;
import static test.util.TestResourceUtils.qualifiedResource;
import org.junit.Test;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import test.beans.ITestBean;
@ -33,11 +34,14 @@ import test.beans.ITestBean;
*/
public final class CommonsPoolTargetSourceProxyTests {
private static final Resource CONTEXT =
qualifiedResource(CommonsPoolTargetSourceProxyTests.class, "context.xml");
@Test
public void testProxy() throws Exception {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
reader.loadBeanDefinitions(new ClassPathResource("CommonsPoolTargetSourceProxyTests-context.xml", getClass()));
reader.loadBeanDefinitions(CONTEXT);
beanFactory.preInstantiateSingletons();
ITestBean bean = (ITestBean)beanFactory.getBean("testBean");
assertTrue(AopUtils.isAopProxy(bean));

View File

@ -17,6 +17,7 @@
package org.springframework.aop.target;
import static org.junit.Assert.*;
import static test.util.TestResourceUtils.qualifiedResource;
import org.junit.After;
import org.junit.Before;
@ -25,7 +26,7 @@ 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.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import test.aop.SerializableNopInterceptor;
import test.beans.Person;
@ -40,6 +41,8 @@ import test.util.SerializationTestUtils;
*/
public final class HotSwappableTargetSourceTests {
private static final Resource CONTEXT = qualifiedResource(HotSwappableTargetSourceTests.class, "context.xml");
/** Initial count value set in bean factory XML */
private static final int INITIAL_COUNT = 10;
@ -47,8 +50,7 @@ public final class HotSwappableTargetSourceTests {
@Before
public void setUp() throws Exception {
this.beanFactory = new XmlBeanFactory(
new ClassPathResource("HotSwappableTargetSourceTests-context.xml", getClass()));
this.beanFactory = new XmlBeanFactory(CONTEXT);
}
/**

View File

@ -17,12 +17,13 @@
package org.springframework.aop.target;
import static org.junit.Assert.*;
import static test.util.TestResourceUtils.qualifiedResource;
import java.util.Set;
import org.junit.Test;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import test.beans.ITestBean;
@ -35,14 +36,10 @@ import test.beans.ITestBean;
public final class LazyInitTargetSourceTests {
private static final Class<?> CLASS = LazyInitTargetSourceTests.class;
private static final String CLASSNAME = CLASS.getSimpleName();
private static final ClassPathResource SINGLETON_CONTEXT =
new ClassPathResource(CLASSNAME + "-singleton.xml", CLASS);
private static final ClassPathResource CUSTOM_TARGET_CONTEXT =
new ClassPathResource(CLASSNAME + "-customTarget.xml", CLASS);
private static final ClassPathResource FACTORY_BEAN_CONTEXT =
new ClassPathResource(CLASSNAME + "-factoryBean.xml", CLASS);
private static final Resource SINGLETON_CONTEXT = qualifiedResource(CLASS, "singleton.xml");
private static final Resource CUSTOM_TARGET_CONTEXT = qualifiedResource(CLASS, "customTarget.xml");
private static final Resource FACTORY_BEAN_CONTEXT = qualifiedResource(CLASS, "factoryBean.xml");
@Test
public void testLazyInitSingletonTargetSource() {

View File

@ -17,12 +17,13 @@
package org.springframework.aop.target;
import static org.junit.Assert.assertEquals;
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.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import test.beans.SideEffectBean;
@ -33,8 +34,7 @@ import test.beans.SideEffectBean;
*/
public final class PrototypeTargetSourceTests {
private static final ClassPathResource CONTEXT =
new ClassPathResource("PrototypeTargetSourceTests-context.xml", PrototypeTargetSourceTests.class);
private static final Resource CONTEXT = qualifiedResource(PrototypeTargetSourceTests.class, "context.xml");
/** Initial count value set in bean factory XML */
private static final int INITIAL_COUNT = 10;

View File

@ -17,11 +17,12 @@
package org.springframework.aop.target;
import static org.junit.Assert.*;
import static test.util.TestResourceUtils.qualifiedResource;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import test.beans.ITestBean;
import test.beans.SideEffectBean;
@ -33,8 +34,7 @@ import test.beans.SideEffectBean;
*/
public class ThreadLocalTargetSourceTests {
private static final ClassPathResource CONTEXT =
new ClassPathResource("ThreadLocalTargetSourceTests-context.xml", ThreadLocalTargetSourceTests.class);
private static final Resource CONTEXT = qualifiedResource(ThreadLocalTargetSourceTests.class, "context.xml");
/** Initial count value set in bean factory XML */
private static final int INITIAL_COUNT = 10;

View File

@ -18,9 +18,8 @@
* limitations under the License.
*/
package org.springframework.aop.framework.autoproxy;
package test.beans;
import test.beans.TestBean;
/**
* @author Juergen Hoeller

View File

@ -1,6 +1,17 @@
/*
* The Spring Framework is published under the terms
* of the Apache Software License.
* Copyright 2002-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package test.util;

View File

@ -0,0 +1,46 @@
/*
* Copyright 2002-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package test.util;
import static java.lang.String.format;
import org.springframework.core.io.ClassPathResource;
/**
* Convenience utilities for common operations with test resources.
*
* @author Chris Beams
*/
public class TestResourceUtils {
/**
* Loads a {@link ClassPathResource} qualified by the simple name of clazz,
* and relative to the package for clazz.
*
* <p>Example: given a clazz 'com.foo.BarTests' and a resourceSuffix of 'context.xml',
* this method will return a ClassPathResource representing com/foo/BarTests-context.xml
*
* <p>Intended for use loading context configuration XML files within JUnit tests.
*
* @param clazz
* @param resourceSuffix
*/
public static ClassPathResource qualifiedResource(Class<?> clazz, String resourceSuffix) {
return new ClassPathResource(format("%s-%s", clazz.getSimpleName(), resourceSuffix), clazz);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2005 the original author or authors.
* Copyright 2002-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.