moving unit tests from .testsuite -> .aop

This commit is contained in:
Chris Beams
2008-12-13 00:35:22 +00:00
parent be3ecf5fe7
commit 40016fc902
12 changed files with 182 additions and 831 deletions

View File

@@ -22,9 +22,6 @@ import org.aopalliance.intercept.MethodInvocation;
import org.springframework.beans.ITestBean;
/**
*
*/
public class InvocationCheckExposedInvocationTestBean extends ExposedInvocationTestBean {
protected void assertions(MethodInvocation invocation) {
TestCase.assertTrue(invocation.getThis() == this);

View File

@@ -1,292 +0,0 @@
/*
* 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.aop.framework;
import junit.framework.TestCase;
import org.springframework.aop.Advisor;
import org.springframework.aop.interceptor.DebugInterceptor;
import org.springframework.aop.interceptor.NopInterceptor;
import org.springframework.aop.support.AopUtils;
import org.springframework.aop.support.DefaultIntroductionAdvisor;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.IOther;
import org.springframework.beans.ITestBean;
import org.springframework.beans.TestBean;
import org.springframework.transaction.interceptor.TransactionInterceptor;
/**
* Also tests AdvisedSupport and ProxyCreatorSupport superclasses.
*
* @author Rod Johnson
* @author Juergen Hoeller
* @since 14.05.2003
*/
public class ProxyFactoryTests extends TestCase {
public void testIndexOfMethods() {
TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
NopInterceptor nop = new NopInterceptor();
Advisor advisor = new DefaultPointcutAdvisor(new CountingBeforeAdvice());
Advised advised = (Advised) pf.getProxy();
// Can use advised and ProxyFactory interchangeably
advised.addAdvice(nop);
pf.addAdvisor(advisor);
assertEquals(-1, pf.indexOf(new NopInterceptor()));
assertEquals(0, pf.indexOf(nop));
assertEquals(1, pf.indexOf(advisor));
assertEquals(-1, advised.indexOf(new DefaultPointcutAdvisor(null)));
}
public void testRemoveAdvisorByReference() {
TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
NopInterceptor nop = new NopInterceptor();
CountingBeforeAdvice cba = new CountingBeforeAdvice();
Advisor advisor = new DefaultPointcutAdvisor(cba);
pf.addAdvice(nop);
pf.addAdvisor(advisor);
ITestBean proxied = (ITestBean) pf.getProxy();
proxied.setAge(5);
assertEquals(1, cba.getCalls());
assertEquals(1, nop.getCount());
assertTrue(pf.removeAdvisor(advisor));
assertEquals(5, proxied.getAge());
assertEquals(1, cba.getCalls());
assertEquals(2, nop.getCount());
assertFalse(pf.removeAdvisor(new DefaultPointcutAdvisor(null)));
}
public void testRemoveAdvisorByIndex() {
TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
NopInterceptor nop = new NopInterceptor();
CountingBeforeAdvice cba = new CountingBeforeAdvice();
Advisor advisor = new DefaultPointcutAdvisor(cba);
pf.addAdvice(nop);
pf.addAdvisor(advisor);
NopInterceptor nop2 = new NopInterceptor();
pf.addAdvice(nop2);
ITestBean proxied = (ITestBean) pf.getProxy();
proxied.setAge(5);
assertEquals(1, cba.getCalls());
assertEquals(1, nop.getCount());
assertEquals(1, nop2.getCount());
// Removes counting before advisor
pf.removeAdvisor(1);
assertEquals(5, proxied.getAge());
assertEquals(1, cba.getCalls());
assertEquals(2, nop.getCount());
assertEquals(2, nop2.getCount());
// Removes Nop1
pf.removeAdvisor(0);
assertEquals(5, proxied.getAge());
assertEquals(1, cba.getCalls());
assertEquals(2, nop.getCount());
assertEquals(3, nop2.getCount());
// Check out of bounds
try {
pf.removeAdvisor(-1);
}
catch (AopConfigException ex) {
// Ok
}
try {
pf.removeAdvisor(2);
}
catch (AopConfigException ex) {
// Ok
}
assertEquals(5, proxied.getAge());
assertEquals(4, nop2.getCount());
}
public void testReplaceAdvisor() {
TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
NopInterceptor nop = new NopInterceptor();
CountingBeforeAdvice cba1 = new CountingBeforeAdvice();
CountingBeforeAdvice cba2 = new CountingBeforeAdvice();
Advisor advisor1 = new DefaultPointcutAdvisor(cba1);
Advisor advisor2 = new DefaultPointcutAdvisor(cba2);
pf.addAdvisor(advisor1);
pf.addAdvice(nop);
ITestBean proxied = (ITestBean) pf.getProxy();
// Use the type cast feature
// Replace etc methods on advised should be same as on ProxyFactory
Advised advised = (Advised) proxied;
proxied.setAge(5);
assertEquals(1, cba1.getCalls());
assertEquals(0, cba2.getCalls());
assertEquals(1, nop.getCount());
assertFalse(advised.replaceAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()), advisor2));
assertTrue(advised.replaceAdvisor(advisor1, advisor2));
assertEquals(advisor2, pf.getAdvisors()[0]);
assertEquals(5, proxied.getAge());
assertEquals(1, cba1.getCalls());
assertEquals(2, nop.getCount());
assertEquals(1, cba2.getCalls());
assertFalse(pf.replaceAdvisor(new DefaultPointcutAdvisor(null), advisor1));
}
public void testAddRepeatedInterface() {
TimeStamped tst = new TimeStamped() {
public long getTimeStamp() {
throw new UnsupportedOperationException("getTimeStamp");
}
};
ProxyFactory pf = new ProxyFactory(tst);
// We've already implicitly added this interface.
// This call should be ignored without error
pf.addInterface(TimeStamped.class);
// All cool
TimeStamped ts = (TimeStamped) pf.getProxy();
}
public void testGetsAllInterfaces() throws Exception {
// Extend to get new interface
class TestBeanSubclass extends TestBean implements Comparable {
public int compareTo(Object arg0) {
throw new UnsupportedOperationException("compareTo");
}
}
TestBeanSubclass raw = new TestBeanSubclass();
ProxyFactory factory = new ProxyFactory(raw);
assertEquals("Found correct number of interfaces", 5, factory.getProxiedInterfaces().length);
//System.out.println("Proxied interfaces are " + StringUtils.arrayToDelimitedString(factory.getProxiedInterfaces(), ","));
ITestBean tb = (ITestBean) factory.getProxy();
assertTrue("Picked up secondary interface", tb instanceof IOther);
raw.setAge(25);
assertTrue(tb.getAge() == raw.getAge());
long t = 555555L;
TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor(t);
Class[] oldProxiedInterfaces = factory.getProxiedInterfaces();
factory.addAdvisor(0, new DefaultIntroductionAdvisor(ti, TimeStamped.class));
Class[] newProxiedInterfaces = factory.getProxiedInterfaces();
assertEquals("Advisor proxies one more interface after introduction", oldProxiedInterfaces.length + 1, newProxiedInterfaces.length);
TimeStamped ts = (TimeStamped) factory.getProxy();
assertTrue(ts.getTimeStamp() == t);
// Shouldn't fail;
((IOther) ts).absquatulate();
}
public void testInterceptorInclusionMethods() {
NopInterceptor di = new NopInterceptor();
NopInterceptor diUnused = new NopInterceptor();
ProxyFactory factory = new ProxyFactory(new TestBean());
factory.addAdvice(0, di);
ITestBean tb = (ITestBean) factory.getProxy();
assertTrue(factory.adviceIncluded(di));
assertTrue(!factory.adviceIncluded(diUnused));
assertTrue(factory.countAdvicesOfType(NopInterceptor.class) == 1);
assertTrue(factory.countAdvicesOfType(TransactionInterceptor.class) == 0);
factory.addAdvice(0, diUnused);
assertTrue(factory.adviceIncluded(diUnused));
assertTrue(factory.countAdvicesOfType(NopInterceptor.class) == 2);
}
/**
* Should see effect immediately on behavior.
*/
public void testCanAddAndRemoveAspectInterfacesOnSingleton() {
ProxyFactory config = new ProxyFactory(new TestBean());
assertFalse("Shouldn't implement TimeStamped before manipulation",
config.getProxy() instanceof TimeStamped);
long time = 666L;
TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor();
ti.setTime(time);
// Add to front of interceptor chain
int oldCount = config.getAdvisors().length;
config.addAdvisor(0, new DefaultIntroductionAdvisor(ti, TimeStamped.class));
assertTrue(config.getAdvisors().length == oldCount + 1);
TimeStamped ts = (TimeStamped) config.getProxy();
assertTrue(ts.getTimeStamp() == time);
// Can remove
config.removeAdvice(ti);
assertTrue(config.getAdvisors().length == oldCount);
try {
// Existing reference will fail
ts.getTimeStamp();
fail("Existing object won't implement this interface any more");
}
catch (RuntimeException ex) {
}
assertFalse("Should no longer implement TimeStamped",
config.getProxy() instanceof TimeStamped);
// Now check non-effect of removing interceptor that isn't there
config.removeAdvice(new DebugInterceptor());
assertTrue(config.getAdvisors().length == oldCount);
ITestBean it = (ITestBean) ts;
DebugInterceptor debugInterceptor = new DebugInterceptor();
config.addAdvice(0, debugInterceptor);
it.getSpouse();
assertEquals(1, debugInterceptor.getCount());
config.removeAdvice(debugInterceptor);
it.getSpouse();
// not invoked again
assertTrue(debugInterceptor.getCount() == 1);
}
public void testProxyTargetClassWithInterfaceAsTarget() {
ProxyFactory pf = new ProxyFactory();
pf.setTargetClass(ITestBean.class);
Object proxy = pf.getProxy();
assertTrue("Proxy is a JDK proxy", AopUtils.isJdkDynamicProxy(proxy));
assertTrue(proxy instanceof ITestBean);
}
public void testProxyTargetClassWithConcreteClassAsTarget() {
ProxyFactory pf = new ProxyFactory();
pf.setTargetClass(TestBean.class);
Object proxy = pf.getProxy();
assertTrue("Proxy is a CGLIB proxy", AopUtils.isCglibProxy(proxy));
assertTrue(proxy instanceof TestBean);
}
public static class Concrete {
public void foo() {
}
}
}

View File

@@ -1,153 +0,0 @@
/*
* Copyright 2002-2005 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.aop.interceptor;
import junit.framework.TestCase;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.DerivedTestBean;
import org.springframework.beans.ITestBean;
import org.springframework.beans.TestBean;
import org.springframework.util.SerializationTestUtils;
/**
* @author Juergen Hoeller
* @since 06.04.2004
*/
public class ConcurrencyThrottleInterceptorTests extends TestCase {
protected static final Log logger = LogFactory.getLog(ConcurrencyThrottleInterceptorTests.class);
public static final int NR_OF_THREADS = 100;
public static final int NR_OF_ITERATIONS = 1000;
public void testSerializable() throws Exception {
DerivedTestBean tb = new DerivedTestBean();
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setInterfaces(new Class[] {ITestBean.class});
ConcurrencyThrottleInterceptor cti = new ConcurrencyThrottleInterceptor();
proxyFactory.addAdvice(cti);
proxyFactory.setTarget(tb);
ITestBean proxy = (ITestBean) proxyFactory.getProxy();
proxy.getAge();
ITestBean serializedProxy = (ITestBean) SerializationTestUtils.serializeAndDeserialize(proxy);
Advised advised = (Advised) serializedProxy;
ConcurrencyThrottleInterceptor serializedCti =
(ConcurrencyThrottleInterceptor) advised.getAdvisors()[0].getAdvice();
assertEquals(cti.getConcurrencyLimit(), serializedCti.getConcurrencyLimit());
serializedProxy.getAge();
}
public void testMultipleThreadsWithLimit1() {
testMultipleThreads(1);
}
public void testMultipleThreadsWithLimit10() {
testMultipleThreads(10);
}
private void testMultipleThreads(int concurrencyLimit) {
TestBean tb = new TestBean();
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setInterfaces(new Class[] {ITestBean.class});
ConcurrencyThrottleInterceptor cti = new ConcurrencyThrottleInterceptor();
cti.setConcurrencyLimit(concurrencyLimit);
proxyFactory.addAdvice(cti);
proxyFactory.setTarget(tb);
ITestBean proxy = (ITestBean) proxyFactory.getProxy();
Thread[] threads = new Thread[NR_OF_THREADS];
for (int i = 0; i < NR_OF_THREADS; i++) {
threads[i] = new ConcurrencyThread(proxy, null);
threads[i].start();
}
for (int i = 0; i < NR_OF_THREADS / 10; i++) {
try {
Thread.sleep(5);
}
catch (InterruptedException ex) {
ex.printStackTrace();
}
threads[i] = new ConcurrencyThread(proxy,
i % 2 == 0 ? (Throwable) new OutOfMemoryError() : (Throwable) new IllegalStateException());
threads[i].start();
}
for (int i = 0; i < NR_OF_THREADS; i++) {
try {
threads[i].join();
}
catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
private static class ConcurrencyThread extends Thread {
private ITestBean proxy;
private Throwable ex;
public ConcurrencyThread(ITestBean proxy, Throwable ex) {
this.proxy = proxy;
this.ex = ex;
}
public void run() {
if (this.ex != null) {
try {
this.proxy.exceptional(this.ex);
}
catch (RuntimeException ex) {
if (ex == this.ex) {
logger.debug("Expected exception thrown", ex);
}
else {
// should never happen
ex.printStackTrace();
}
}
catch (Error err) {
if (err == this.ex) {
logger.debug("Expected exception thrown", err);
}
else {
// should never happen
ex.printStackTrace();
}
}
catch (Throwable ex) {
// should never happen
ex.printStackTrace();
}
}
else {
for (int i = 0; i < NR_OF_ITERATIONS; i++) {
this.proxy.getName();
}
}
logger.debug("finished");
}
}
}

View File

@@ -1,224 +0,0 @@
/*
* Copyright 2002-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.aop.interceptor;
import java.lang.reflect.Method;
import junit.framework.TestCase;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.easymock.MockControl;
import org.springframework.test.AssertThrows;
/**
* @author Rob Harrop
* @author Rick Evans
* @author Juergen Hoeller
*/
public class CustomizableTraceInterceptorTests extends TestCase {
public void testSetEmptyEnterMessage() {
new AssertThrows(IllegalArgumentException.class, "Must not be able to set empty enter message") {
public void test() throws Exception {
new CustomizableTraceInterceptor().setEnterMessage("");
}
}.runTest();
}
public void testSetEnterMessageWithReturnValuePlaceholder() {
new AssertThrows(IllegalArgumentException.class, "Must not be able to set enter message with return value placeholder") {
public void test() throws Exception {
new CustomizableTraceInterceptor().setEnterMessage(CustomizableTraceInterceptor.PLACEHOLDER_RETURN_VALUE);
}
}.runTest();
}
public void testSetEnterMessageWithExceptionPlaceholder() {
new AssertThrows(IllegalArgumentException.class, "Must not be able to set enter message with exception placeholder") {
public void test() throws Exception {
new CustomizableTraceInterceptor().setEnterMessage(CustomizableTraceInterceptor.PLACEHOLDER_EXCEPTION);
}
}.runTest();
}
public void testSetEnterMessageWithInvocationTimePlaceholder() {
new AssertThrows(IllegalArgumentException.class, "Must not be able to set enter message with invocation time placeholder") {
public void test() throws Exception {
new CustomizableTraceInterceptor().setEnterMessage(CustomizableTraceInterceptor.PLACEHOLDER_INVOCATION_TIME);
}
}.runTest();
}
public void testSetEmptyExitMessage() {
new AssertThrows(IllegalArgumentException.class, "Must not be able to set empty exit message") {
public void test() throws Exception {
new CustomizableTraceInterceptor().setExitMessage("");
}
}.runTest();
}
public void testSetExitMessageWithExceptionPlaceholder() {
new AssertThrows(IllegalArgumentException.class, "Must not be able to set exit message with exception placeholder") {
public void test() throws Exception {
new CustomizableTraceInterceptor().setExitMessage(CustomizableTraceInterceptor.PLACEHOLDER_EXCEPTION);
}
}.runTest();
}
public void testSetEmptyExceptionMessage() {
new AssertThrows(IllegalArgumentException.class, "Must not be able to set empty exception message") {
public void test() throws Exception {
new CustomizableTraceInterceptor().setExceptionMessage("");
}
}.runTest();
}
public void testSetExceptionMethodWithReturnValuePlaceholder() {
new AssertThrows(IllegalArgumentException.class, "Must not be able to set exception message with return value placeholder") {
public void test() throws Exception {
new CustomizableTraceInterceptor().setExceptionMessage(CustomizableTraceInterceptor.PLACEHOLDER_RETURN_VALUE);
}
}.runTest();
}
public void testSunnyDayPathLogsCorrectly() throws Throwable {
MockControl mockLog = MockControl.createControl(Log.class);
Log log = (Log) mockLog.getMock();
MockControl mockMethodInvocation = MockControl.createControl(MethodInvocation.class);
MethodInvocation methodInvocation = (MethodInvocation) mockMethodInvocation.getMock();
Method toString = String.class.getMethod("toString", new Class[]{});
log.isTraceEnabled();
mockLog.setReturnValue(true);
methodInvocation.getMethod();
mockMethodInvocation.setReturnValue(toString, 4);
methodInvocation.getThis();
mockMethodInvocation.setReturnValue(this, 2);
log.trace("Some tracing output");
mockLog.setMatcher(MockControl.ALWAYS_MATCHER);
methodInvocation.proceed();
mockMethodInvocation.setReturnValue(null);
log.trace("Some more tracing output");
mockLog.setMatcher(MockControl.ALWAYS_MATCHER);
mockLog.setVoidCallable();
mockMethodInvocation.replay();
mockLog.replay();
CustomizableTraceInterceptor interceptor = new StubCustomizableTraceInterceptor(log);
interceptor.invoke(methodInvocation);
mockLog.verify();
mockMethodInvocation.verify();
}
public void testExceptionPathLogsCorrectly() throws Throwable {
MockControl mockLog = MockControl.createControl(Log.class);
Log log = (Log) mockLog.getMock();
MockControl mockMethodInvocation = MockControl.createControl(MethodInvocation.class);
MethodInvocation methodInvocation = (MethodInvocation) mockMethodInvocation.getMock();
Method toString = String.class.getMethod("toString", new Class[]{});
log.isTraceEnabled();
mockLog.setReturnValue(true);
methodInvocation.getMethod();
mockMethodInvocation.setReturnValue(toString, 4);
methodInvocation.getThis();
mockMethodInvocation.setReturnValue(this, 2);
log.trace("Some tracing output");
mockLog.setMatcher(MockControl.ALWAYS_MATCHER);
methodInvocation.proceed();
IllegalArgumentException exception = new IllegalArgumentException();
mockMethodInvocation.setThrowable(exception);
log.trace("Some more tracing output", exception);
mockLog.setMatcher(MockControl.ALWAYS_MATCHER);
mockLog.setVoidCallable();
mockMethodInvocation.replay();
mockLog.replay();
CustomizableTraceInterceptor interceptor = new StubCustomizableTraceInterceptor(log);
try {
interceptor.invoke(methodInvocation);
fail("Must have propagated the IllegalArgumentException.");
}
catch (IllegalArgumentException expected) {
}
mockLog.verify();
mockMethodInvocation.verify();
}
public void testSunnyDayPathLogsCorrectlyWithPrettyMuchAllPlaceholdersMatching() throws Throwable {
MockControl mockLog = MockControl.createControl(Log.class);
Log log = (Log) mockLog.getMock();
MockControl mockMethodInvocation = MockControl.createControl(MethodInvocation.class);
MethodInvocation methodInvocation = (MethodInvocation) mockMethodInvocation.getMock();
Method toString = String.class.getMethod("toString", new Class[0]);
Object[] arguments = new Object[]{"$ One \\$", new Long(2)};
log.isTraceEnabled();
mockLog.setReturnValue(true);
methodInvocation.getMethod();
mockMethodInvocation.setReturnValue(toString, 7);
methodInvocation.getThis();
mockMethodInvocation.setReturnValue(this, 2);
methodInvocation.getArguments();
mockMethodInvocation.setReturnValue(arguments, 2);
log.trace("Some tracing output");
mockLog.setMatcher(MockControl.ALWAYS_MATCHER);
methodInvocation.proceed();
mockMethodInvocation.setReturnValue("Hello!");
log.trace("Some more tracing output");
mockLog.setMatcher(MockControl.ALWAYS_MATCHER);
mockLog.setVoidCallable();
mockMethodInvocation.replay();
mockLog.replay();
CustomizableTraceInterceptor interceptor = new StubCustomizableTraceInterceptor(log);
interceptor.setEnterMessage(new StringBuffer().append("Entering the '").append(CustomizableTraceInterceptor.PLACEHOLDER_METHOD_NAME).append("' method of the [").append(CustomizableTraceInterceptor.PLACEHOLDER_TARGET_CLASS_NAME).append("] class with the following args (").append(CustomizableTraceInterceptor.PLACEHOLDER_ARGUMENTS).append(") and arg types (").append(CustomizableTraceInterceptor.PLACEHOLDER_ARGUMENT_TYPES).append(").").toString());
interceptor.setExitMessage(new StringBuffer().append("Exiting the '").append(CustomizableTraceInterceptor.PLACEHOLDER_METHOD_NAME).append("' method of the [").append(CustomizableTraceInterceptor.PLACEHOLDER_TARGET_CLASS_SHORT_NAME).append("] class with the following args (").append(CustomizableTraceInterceptor.PLACEHOLDER_ARGUMENTS).append(") and arg types (").append(CustomizableTraceInterceptor.PLACEHOLDER_ARGUMENT_TYPES).append("), returning '").append(CustomizableTraceInterceptor.PLACEHOLDER_RETURN_VALUE).append("' and taking '").append(CustomizableTraceInterceptor.PLACEHOLDER_INVOCATION_TIME).append("' this long.").toString());
interceptor.invoke(methodInvocation);
mockLog.verify();
mockMethodInvocation.verify();
}
private static class StubCustomizableTraceInterceptor extends CustomizableTraceInterceptor {
private final Log log;
public StubCustomizableTraceInterceptor(Log log) {
super.setUseDynamicLogger(false);
this.log = log;
}
protected Log getLoggerForInvocation(MethodInvocation invocation) {
return this.log;
}
}
}

View File

@@ -1,114 +0,0 @@
/*
* 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.aop.interceptor;
import junit.framework.TestCase;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.easymock.MockControl;
/**
* Unit tests for the {@link DebugInterceptor} class.
*
* @author Rick Evans
*/
public final class DebugInterceptorTests extends TestCase {
public void testSunnyDayPathLogsCorrectly() throws Throwable {
MockControl mockLog = MockControl.createControl(Log.class);
final Log log = (Log) mockLog.getMock();
MockControl mockMethodInvocation = MockControl.createControl(MethodInvocation.class);
MethodInvocation methodInvocation = (MethodInvocation) mockMethodInvocation.getMock();
log.isTraceEnabled();
mockLog.setReturnValue(true);
log.trace("Some tracing output");
mockLog.setMatcher(MockControl.ALWAYS_MATCHER);
methodInvocation.proceed();
mockMethodInvocation.setReturnValue(null);
log.trace("Some more tracing output");
mockLog.setMatcher(MockControl.ALWAYS_MATCHER);
mockLog.setVoidCallable();
mockMethodInvocation.replay();
mockLog.replay();
DebugInterceptor interceptor = new StubDebugInterceptor(log);
interceptor.invoke(methodInvocation);
checkCallCountTotal(interceptor);
mockLog.verify();
mockMethodInvocation.verify();
}
public void testExceptionPathStillLogsCorrectly() throws Throwable {
MockControl mockLog = MockControl.createControl(Log.class);
final Log log = (Log) mockLog.getMock();
MockControl mockMethodInvocation = MockControl.createControl(MethodInvocation.class);
final MethodInvocation methodInvocation = (MethodInvocation) mockMethodInvocation.getMock();
log.isTraceEnabled();
mockLog.setReturnValue(true);
log.trace("Some tracing output");
mockLog.setMatcher(MockControl.ALWAYS_MATCHER);
methodInvocation.proceed();
IllegalArgumentException exception = new IllegalArgumentException();
mockMethodInvocation.setThrowable(exception);
log.trace("Some more tracing output", exception);
mockLog.setMatcher(MockControl.ALWAYS_MATCHER);
mockLog.setVoidCallable();
mockMethodInvocation.replay();
mockLog.replay();
DebugInterceptor interceptor = new StubDebugInterceptor(log);
try {
interceptor.invoke(methodInvocation);
fail("Must have propagated the IllegalArgumentException.");
} catch (IllegalArgumentException expected) {
}
checkCallCountTotal(interceptor);
mockLog.verify();
mockMethodInvocation.verify();
}
private void checkCallCountTotal(DebugInterceptor interceptor) {
assertEquals("Intercepted call count not being incremented correctly", 1, interceptor.getCount());
}
private static final class StubDebugInterceptor extends DebugInterceptor {
private final Log log;
public StubDebugInterceptor(Log log) {
super(true);
this.log = log;
}
protected Log getLoggerForInvocation(MethodInvocation invocation) {
return log;
}
}
}

View File

@@ -1,75 +0,0 @@
/*
* 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.aop.interceptor;
import junit.framework.TestCase;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.ITestBean;
import org.springframework.beans.TestBean;
import org.springframework.beans.factory.NamedBean;
/**
*
* @author Rod Johnson
*
*/
public class ExposeBeanNameAdvisorsTests extends TestCase {
private class RequiresBeanNameBoundTestBean extends TestBean {
private final String beanName;
public RequiresBeanNameBoundTestBean(String beanName) {
this.beanName = beanName;
}
public int getAge() {
assertEquals(beanName, ExposeBeanNameAdvisors.getBeanName());
return super.getAge();
}
}
public void testNoIntroduction() {
String beanName = "foo";
TestBean target = new RequiresBeanNameBoundTestBean(beanName);
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
pf.addAdvisor(ExposeBeanNameAdvisors.createAdvisorWithoutIntroduction(beanName));
ITestBean proxy = (ITestBean) pf.getProxy();
assertFalse("No introduction", proxy instanceof NamedBean);
// Requires binding
proxy.getAge();
}
public void testWithIntroduction() {
String beanName = "foo";
TestBean target = new RequiresBeanNameBoundTestBean(beanName);
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
pf.addAdvisor(ExposeBeanNameAdvisors.createAdvisorIntroducingNamedBean(beanName));
ITestBean proxy = (ITestBean) pf.getProxy();
assertTrue("Introduction was made", proxy instanceof NamedBean);
// Requires binding
proxy.getAge();
NamedBean nb = (NamedBean) proxy;
assertEquals("Name returned correctly", beanName, nb.getBeanName());
}
}

View File

@@ -1,39 +0,0 @@
/*
* Copyright 2002-2005 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.aop.interceptor;
import junit.framework.TestCase;
import org.springframework.beans.ITestBean;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Non-XML tests are in AbstractAopProxyTests
* @author Rod Johnson
*/
public class ExposeInvocationInterceptorTests extends TestCase {
public void testXmlConfig() {
ClassPathXmlApplicationContext xac = new ClassPathXmlApplicationContext("org/springframework/aop/interceptor/exposeInvocation.xml");
ITestBean tb = (ITestBean) xac.getBean("proxy");
String name= "tony";
tb.setName(name);
// Fires context checks
assertEquals(name, tb.getName());
}
}

View File

@@ -1,104 +0,0 @@
/*
* 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.aop.interceptor;
import java.lang.reflect.Method;
import junit.framework.TestCase;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.easymock.MockControl;
/**
* @author Rob Harrop
* @author Rick Evans
*/
public class PerformanceMonitorInterceptorTests extends TestCase {
public void testSuffixAndPrefixAssignment() {
PerformanceMonitorInterceptor interceptor = new PerformanceMonitorInterceptor();
assertNotNull(interceptor.getPrefix());
assertNotNull(interceptor.getSuffix());
interceptor.setPrefix(null);
interceptor.setSuffix(null);
assertNotNull(interceptor.getPrefix());
assertNotNull(interceptor.getSuffix());
}
public void testSunnyDayPathLogsPerformanceMetricsCorrectly() throws Throwable {
MockControl mockLog = MockControl.createControl(Log.class);
Log log = (Log) mockLog.getMock();
MockControl mockMethodInvocation = MockControl.createControl(MethodInvocation.class);
MethodInvocation methodInvocation = (MethodInvocation) mockMethodInvocation.getMock();
Method toString = String.class.getMethod("toString", new Class[0]);
methodInvocation.getMethod();
mockMethodInvocation.setReturnValue(toString);
methodInvocation.proceed();
mockMethodInvocation.setReturnValue(null);
log.trace("Some performance metric");
mockLog.setMatcher(MockControl.ALWAYS_MATCHER);
mockLog.setVoidCallable();
mockMethodInvocation.replay();
mockLog.replay();
PerformanceMonitorInterceptor interceptor = new PerformanceMonitorInterceptor(true);
interceptor.invokeUnderTrace(methodInvocation, log);
mockLog.verify();
mockMethodInvocation.verify();
}
public void testExceptionPathStillLogsPerformanceMetricsCorrectly() throws Throwable {
MockControl mockLog = MockControl.createControl(Log.class);
Log log = (Log) mockLog.getMock();
MockControl mockMethodInvocation = MockControl.createControl(MethodInvocation.class);
MethodInvocation methodInvocation = (MethodInvocation) mockMethodInvocation.getMock();
Method toString = String.class.getMethod("toString", new Class[0]);
methodInvocation.getMethod();
mockMethodInvocation.setReturnValue(toString);
methodInvocation.proceed();
mockMethodInvocation.setThrowable(new IllegalArgumentException());
log.trace("Some performance metric");
mockLog.setMatcher(MockControl.ALWAYS_MATCHER);
mockLog.setVoidCallable();
mockMethodInvocation.replay();
mockLog.replay();
PerformanceMonitorInterceptor interceptor = new PerformanceMonitorInterceptor(true);
try {
interceptor.invokeUnderTrace(methodInvocation, log);
fail("Must have propagated the IllegalArgumentException.");
}
catch (IllegalArgumentException expected) {
}
mockLog.verify();
mockMethodInvocation.verify();
}
}

View File

@@ -1,101 +0,0 @@
/*
* 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.aop.interceptor;
import junit.framework.TestCase;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.easymock.MockControl;
import java.lang.reflect.Method;
/**
* Unit tests for the {@link SimpleTraceInterceptor} class.
*
* @author Rick Evans
*/
public final class SimpleTraceInterceptorTests extends TestCase {
public void testSunnyDayPathLogsCorrectly() throws Throwable {
MockControl mockLog = MockControl.createControl(Log.class);
Log log = (Log) mockLog.getMock();
MockControl mockMethodInvocation = MockControl.createControl(MethodInvocation.class);
MethodInvocation methodInvocation = (MethodInvocation) mockMethodInvocation.getMock();
Method toString = String.class.getMethod("toString", new Class[]{});
methodInvocation.getMethod();
mockMethodInvocation.setReturnValue(toString);
methodInvocation.getThis();
mockMethodInvocation.setReturnValue(this);
log.trace("Some tracing output");
mockLog.setMatcher(MockControl.ALWAYS_MATCHER);
methodInvocation.proceed();
mockMethodInvocation.setReturnValue(null);
log.trace("Some more tracing output");
mockLog.setMatcher(MockControl.ALWAYS_MATCHER);
mockLog.setVoidCallable();
mockMethodInvocation.replay();
mockLog.replay();
SimpleTraceInterceptor interceptor = new SimpleTraceInterceptor(true);
interceptor.invokeUnderTrace(methodInvocation, log);
mockLog.verify();
mockMethodInvocation.verify();
}
public void testExceptionPathStillLogsCorrectly() throws Throwable {
MockControl mockLog = MockControl.createControl(Log.class);
final Log log = (Log) mockLog.getMock();
MockControl mockMethodInvocation = MockControl.createControl(MethodInvocation.class);
final MethodInvocation methodInvocation = (MethodInvocation) mockMethodInvocation.getMock();
Method toString = String.class.getMethod("toString", new Class[]{});
methodInvocation.getMethod();
mockMethodInvocation.setReturnValue(toString);
methodInvocation.getThis();
mockMethodInvocation.setReturnValue(this);
log.trace("Some tracing output");
mockLog.setMatcher(MockControl.ALWAYS_MATCHER);
methodInvocation.proceed();
IllegalArgumentException exception = new IllegalArgumentException();
mockMethodInvocation.setThrowable(exception);
log.trace("Some more tracing output", exception);
mockLog.setMatcher(MockControl.ALWAYS_MATCHER);
mockLog.setVoidCallable();
mockMethodInvocation.replay();
mockLog.replay();
final SimpleTraceInterceptor interceptor = new SimpleTraceInterceptor(true);
try {
interceptor.invokeUnderTrace(methodInvocation, log);
fail("Must have propagated the IllegalArgumentException.");
} catch (IllegalArgumentException expected) {
}
mockLog.verify();
mockMethodInvocation.verify();
}
}

View File

@@ -1,29 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<!--
Tests for throws advice.
-->
<beans>
<bean id="nopInterceptor" class="org.springframework.aop.interceptor.NopInterceptor"/>
<bean id="exposeInvocation" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
<property name="targetClass">
<value>org.springframework.aop.interceptor.ExposeInvocationInterceptor</value>
</property>
<property name="targetField"><value>INSTANCE</value></property>
</bean>
<bean id="countingBeforeAdvice" class="org.springframework.aop.framework.CountingBeforeAdvice"/>
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<bean class="org.springframework.aop.framework.InvocationCheckExposedInvocationTestBean" />
</property>
<property name="interceptorNames">
<value>exposeInvocation,countingBeforeAdvice,nopInterceptor</value>
</property>
</bean>
</beans>