Polishing

This commit is contained in:
Sam Brannen 2024-02-16 15:49:13 +01:00
parent a85bf3185e
commit 71dfebbfe5
12 changed files with 269 additions and 266 deletions

View File

@ -19,16 +19,10 @@ package org.springframework.aop.framework;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.accessibility.Accessible;
import javax.swing.JFrame;
import javax.swing.RootPaneContainer;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.aop.Advisor;
@ -60,7 +54,7 @@ import static org.assertj.core.api.Assertions.assertThatRuntimeException;
class ProxyFactoryTests {
@Test
void testIndexOfMethods() {
void indexOfMethods() {
TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
NopInterceptor nop = new NopInterceptor();
@ -76,7 +70,7 @@ class ProxyFactoryTests {
}
@Test
void testRemoveAdvisorByReference() {
void removeAdvisorByReference() {
TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
NopInterceptor nop = new NopInterceptor();
@ -96,7 +90,7 @@ class ProxyFactoryTests {
}
@Test
void testRemoveAdvisorByIndex() {
void removeAdvisorByIndex() {
TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
NopInterceptor nop = new NopInterceptor();
@ -144,7 +138,7 @@ class ProxyFactoryTests {
}
@Test
void testReplaceAdvisor() {
void replaceAdvisor() {
TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
NopInterceptor nop = new NopInterceptor();
@ -173,7 +167,7 @@ class ProxyFactoryTests {
}
@Test
void testAddRepeatedInterface() {
void addRepeatedInterface() {
TimeStamped tst = () -> {
throw new UnsupportedOperationException("getTimeStamp");
};
@ -186,7 +180,7 @@ class ProxyFactoryTests {
}
@Test
void testGetsAllInterfaces() {
void getsAllInterfaces() {
// Extend to get new interface
class TestBeanSubclass extends TestBean implements Comparable<Object> {
@Override
@ -220,7 +214,7 @@ class ProxyFactoryTests {
}
@Test
void testInterceptorInclusionMethods() {
void interceptorInclusionMethods() {
class MyInterceptor implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) {
@ -244,7 +238,7 @@ class ProxyFactoryTests {
}
@Test
void testSealedInterfaceExclusion() {
void sealedInterfaceExclusion() {
// String implements ConstantDesc on JDK 12+, sealed as of JDK 17
ProxyFactory factory = new ProxyFactory("");
NopInterceptor di = new NopInterceptor();
@ -257,7 +251,7 @@ class ProxyFactoryTests {
* Should see effect immediately on behavior.
*/
@Test
void testCanAddAndRemoveAspectInterfacesOnSingleton() {
void canAddAndRemoveAspectInterfacesOnSingleton() {
ProxyFactory config = new ProxyFactory(new TestBean());
assertThat(config.getProxy()).as("Shouldn't implement TimeStamped before manipulation")
@ -304,7 +298,7 @@ class ProxyFactoryTests {
}
@Test
void testProxyTargetClassWithInterfaceAsTarget() {
void proxyTargetClassWithInterfaceAsTarget() {
ProxyFactory pf = new ProxyFactory();
pf.setTargetClass(ITestBean.class);
Object proxy = pf.getProxy();
@ -320,7 +314,7 @@ class ProxyFactoryTests {
}
@Test
void testProxyTargetClassWithConcreteClassAsTarget() {
void proxyTargetClassWithConcreteClassAsTarget() {
ProxyFactory pf = new ProxyFactory();
pf.setTargetClass(TestBean.class);
Object proxy = pf.getProxy();
@ -337,17 +331,7 @@ class ProxyFactoryTests {
}
@Test
@Disabled("Not implemented yet, see https://jira.springframework.org/browse/SPR-5708")
public void testExclusionOfNonPublicInterfaces() {
JFrame frame = new JFrame();
ProxyFactory proxyFactory = new ProxyFactory(frame);
Object proxy = proxyFactory.getProxy();
assertThat(proxy).isInstanceOf(RootPaneContainer.class);
assertThat(proxy).isInstanceOf(Accessible.class);
}
@Test
void testInterfaceProxiesCanBeOrderedThroughAnnotations() {
void interfaceProxiesCanBeOrderedThroughAnnotations() {
Object proxy1 = new ProxyFactory(new A()).getProxy();
Object proxy2 = new ProxyFactory(new B()).getProxy();
List<Object> list = new ArrayList<>(2);
@ -358,7 +342,7 @@ class ProxyFactoryTests {
}
@Test
void testTargetClassProxiesCanBeOrderedThroughAnnotations() {
void targetClassProxiesCanBeOrderedThroughAnnotations() {
ProxyFactory pf1 = new ProxyFactory(new A());
pf1.setProxyTargetClass(true);
ProxyFactory pf2 = new ProxyFactory(new B());
@ -373,7 +357,7 @@ class ProxyFactoryTests {
}
@Test
void testInterceptorWithoutJoinpoint() {
void interceptorWithoutJoinpoint() {
final TestBean target = new TestBean("tb");
ITestBean proxy = ProxyFactory.getProxy(ITestBean.class, (MethodInterceptor) invocation -> {
assertThat(invocation.getThis()).isNull();
@ -383,28 +367,28 @@ class ProxyFactoryTests {
}
@Test
void testCharSequenceProxy() {
void interfaceProxy() {
CharSequence target = "test";
ProxyFactory pf = new ProxyFactory(target);
ClassLoader cl = target.getClass().getClassLoader();
CharSequence proxy = (CharSequence) pf.getProxy(cl);
assertThat(proxy.toString()).isEqualTo(target);
assertThat(proxy).asString().isEqualTo(target);
assertThat(pf.getProxyClass(cl)).isSameAs(proxy.getClass());
}
@Test
void testDateProxy() {
Date target = new Date();
void dateProxy() {
MyDate target = new MyDate();
ProxyFactory pf = new ProxyFactory(target);
pf.setProxyTargetClass(true);
ClassLoader cl = target.getClass().getClassLoader();
Date proxy = (Date) pf.getProxy(cl);
MyDate proxy = (MyDate) pf.getProxy(cl);
assertThat(proxy.getTime()).isEqualTo(target.getTime());
assertThat(pf.getProxyClass(cl)).isSameAs(proxy.getClass());
}
@Test
void testJdbcSavepointProxy() throws SQLException {
void jdbcSavepointProxy() throws SQLException {
Savepoint target = new Savepoint() {
@Override
public int getSavepointId() {
@ -423,8 +407,20 @@ class ProxyFactoryTests {
}
// Emulates java.util.Date locally, since we cannot automatically proxy the
// java.util.Date class.
static class MyDate {
private final long time = System.currentTimeMillis();
public long getTime() {
return time;
}
}
@Order(2)
public static class A implements Runnable {
static class A implements Runnable {
@Override
public void run() {
@ -433,7 +429,7 @@ class ProxyFactoryTests {
@Order(1)
public static class B implements Runnable {
static class B implements Runnable {
@Override
public void run() {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
@ -36,26 +36,24 @@ public class Pet {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return getName();
}
@Override
public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Pet pet = (Pet) o;
return Objects.equals(this.name, pet.name);
public boolean equals(@Nullable Object obj) {
return (this == obj) ||
(obj instanceof Pet that && Objects.equals(this.name, that.name));
}
@Override
public int hashCode() {
return (name != null ? name.hashCode() : 0);
return (this.name != null ? this.name.hashCode() : 0);
}
}

View File

@ -85,7 +85,7 @@ import static org.assertj.core.api.Assertions.assertThatRuntimeException;
* @author Chris Beams
* @since 13.03.2003
*/
public abstract class AbstractAopProxyTests {
abstract class AbstractAopProxyTests {
protected final MockTargetSource mockTargetSource = new MockTargetSource();
@ -125,7 +125,7 @@ public abstract class AbstractAopProxyTests {
* Simple test that if we set values we can get them out again.
*/
@Test
void testValuesStick() {
void valuesStick() {
int age1 = 33;
int age2 = 37;
String name = "tony";
@ -146,7 +146,7 @@ public abstract class AbstractAopProxyTests {
}
@Test
void testSerializationAdviceAndTargetNotSerializable() throws Exception {
void serializationAdviceAndTargetNotSerializable() throws Exception {
TestBean tb = new TestBean();
assertThat(SerializationTestUtils.isSerializable(tb)).isFalse();
@ -159,7 +159,7 @@ public abstract class AbstractAopProxyTests {
}
@Test
void testSerializationAdviceNotSerializable() throws Exception {
void serializationAdviceNotSerializable() throws Exception {
SerializablePerson sp = new SerializablePerson();
assertThat(SerializationTestUtils.isSerializable(sp)).isTrue();
@ -175,7 +175,7 @@ public abstract class AbstractAopProxyTests {
}
@Test
void testSerializableTargetAndAdvice() throws Throwable {
void serializableTargetAndAdvice() throws Throwable {
SerializablePerson personTarget = new SerializablePerson();
personTarget.setName("jim");
personTarget.setAge(26);
@ -245,7 +245,7 @@ public abstract class AbstractAopProxyTests {
* Check also proxy exposure.
*/
@Test
void testOneAdvisedObjectCallsAnother() {
void oneAdvisedObjectCallsAnother() {
int age1 = 33;
int age2 = 37;
@ -290,7 +290,7 @@ public abstract class AbstractAopProxyTests {
@Test
void testReentrance() {
void reentrance() {
int age1 = 33;
TestBean target1 = new TestBean();
@ -314,7 +314,7 @@ public abstract class AbstractAopProxyTests {
}
@Test
void testTargetCanGetProxy() {
void targetCanGetProxy() {
NopInterceptor di = new NopInterceptor();
INeedsToSeeProxy target = new TargetChecker();
ProxyFactory proxyFactory = new ProxyFactory(target);
@ -338,7 +338,7 @@ public abstract class AbstractAopProxyTests {
@Test
// Should fail to get proxy as exposeProxy wasn't set to true
public void testTargetCantGetProxyByDefault() {
public void targetCantGetProxyByDefault() {
NeedsToSeeProxy et = new NeedsToSeeProxy();
ProxyFactory pf1 = new ProxyFactory(et);
assertThat(pf1.isExposeProxy()).isFalse();
@ -347,12 +347,12 @@ public abstract class AbstractAopProxyTests {
}
@Test
void testContext() {
void context() {
testContext(true);
}
@Test
void testNoContext() {
void noContext() {
testContext(false);
}
@ -393,7 +393,7 @@ public abstract class AbstractAopProxyTests {
* target returns {@code this}
*/
@Test
void testTargetReturnsThis() {
void targetReturnsThis() {
// Test return value
TestBean raw = new OwnSpouse();
@ -406,7 +406,7 @@ public abstract class AbstractAopProxyTests {
}
@Test
void testDeclaredException() {
void declaredException() {
final Exception expectedException = new Exception();
// Test return value
MethodInterceptor mi = invocation -> {
@ -434,7 +434,7 @@ public abstract class AbstractAopProxyTests {
* org.springframework.cglib UndeclaredThrowableException
*/
@Test
void testUndeclaredCheckedException() {
void undeclaredCheckedException() {
final Exception unexpectedException = new Exception();
// Test return value
MethodInterceptor mi = invocation -> {
@ -454,7 +454,7 @@ public abstract class AbstractAopProxyTests {
}
@Test
void testUndeclaredUncheckedException() {
void undeclaredUncheckedException() {
final RuntimeException unexpectedException = new RuntimeException();
// Test return value
MethodInterceptor mi = invocation -> {
@ -480,7 +480,7 @@ public abstract class AbstractAopProxyTests {
* so as to guarantee a consistent programming model.
*/
@Test
void testTargetCanGetInvocationEvenIfNoAdviceChain() {
void targetCanGetInvocationEvenIfNoAdviceChain() {
NeedsToSeeProxy target = new NeedsToSeeProxy();
AdvisedSupport pc = new AdvisedSupport(INeedsToSeeProxy.class);
pc.setTarget(target);
@ -494,7 +494,7 @@ public abstract class AbstractAopProxyTests {
}
@Test
void testTargetCanGetInvocation() {
void targetCanGetInvocation() {
final InvocationCheckExposedInvocationTestBean expectedTarget = new InvocationCheckExposedInvocationTestBean();
AdvisedSupport pc = new AdvisedSupport(ITestBean.class, IOther.class);
@ -527,7 +527,7 @@ public abstract class AbstractAopProxyTests {
* Test stateful interceptor
*/
@Test
void testMixinWithIntroductionAdvisor() {
void mixinWithIntroductionAdvisor() {
TestBean tb = new TestBean();
ProxyFactory pc = new ProxyFactory();
pc.addInterface(ITestBean.class);
@ -538,7 +538,7 @@ public abstract class AbstractAopProxyTests {
}
@Test
void testMixinWithIntroductionInfo() {
void mixinWithIntroductionInfo() {
TestBean tb = new TestBean();
ProxyFactory pc = new ProxyFactory();
pc.addInterface(ITestBean.class);
@ -571,7 +571,7 @@ public abstract class AbstractAopProxyTests {
}
@Test
void testReplaceArgument() {
void replaceArgument() {
TestBean tb = new TestBean();
ProxyFactory pc = new ProxyFactory();
pc.addInterface(ITestBean.class);
@ -592,7 +592,7 @@ public abstract class AbstractAopProxyTests {
}
@Test
void testCanCastProxyToProxyConfig() {
void canCastProxyToProxyConfig() {
TestBean tb = new TestBean();
ProxyFactory pc = new ProxyFactory(tb);
NopInterceptor di = new NopInterceptor();
@ -628,7 +628,7 @@ public abstract class AbstractAopProxyTests {
}
@Test
void testAdviceImplementsIntroductionInfo() {
void adviceImplementsIntroductionInfo() {
TestBean tb = new TestBean();
String name = "tony";
tb.setName(name);
@ -645,7 +645,7 @@ public abstract class AbstractAopProxyTests {
}
@Test
void testCannotAddDynamicIntroductionAdviceExceptInIntroductionAdvice() {
void cannotAddDynamicIntroductionAdviceExceptInIntroductionAdvice() {
TestBean target = new TestBean();
target.setAge(21);
ProxyFactory pc = new ProxyFactory(target);
@ -658,7 +658,7 @@ public abstract class AbstractAopProxyTests {
}
@Test
void testRejectsBogusDynamicIntroductionAdviceWithNoAdapter() {
void rejectsBogusDynamicIntroductionAdviceWithNoAdapter() {
TestBean target = new TestBean();
target.setAge(21);
ProxyFactory pc = new ProxyFactory(target);
@ -679,7 +679,7 @@ public abstract class AbstractAopProxyTests {
* that are unsupported by the IntroductionInterceptor.
*/
@Test
void testCannotAddIntroductionAdviceWithUnimplementedInterface() {
void cannotAddIntroductionAdviceWithUnimplementedInterface() {
TestBean target = new TestBean();
target.setAge(21);
ProxyFactory pc = new ProxyFactory(target);
@ -695,7 +695,7 @@ public abstract class AbstractAopProxyTests {
* as it's constrained by the interface.
*/
@Test
void testIntroductionThrowsUncheckedException() {
void introductionThrowsUncheckedException() {
TestBean target = new TestBean();
target.setAge(21);
ProxyFactory pc = new ProxyFactory(target);
@ -720,7 +720,7 @@ public abstract class AbstractAopProxyTests {
* Should only be able to introduce interfaces, not classes.
*/
@Test
void testCannotAddIntroductionAdviceToIntroduceClass() {
void cannotAddIntroductionAdviceToIntroduceClass() {
TestBean target = new TestBean();
target.setAge(21);
ProxyFactory pc = new ProxyFactory(target);
@ -733,7 +733,7 @@ public abstract class AbstractAopProxyTests {
}
@Test
void testCannotAddInterceptorWhenFrozen() {
void cannotAddInterceptorWhenFrozen() {
TestBean target = new TestBean();
target.setAge(21);
ProxyFactory pc = new ProxyFactory(target);
@ -753,7 +753,7 @@ public abstract class AbstractAopProxyTests {
* Check that casting to Advised can't get around advice freeze.
*/
@Test
void testCannotAddAdvisorWhenFrozenUsingCast() {
void cannotAddAdvisorWhenFrozenUsingCast() {
TestBean target = new TestBean();
target.setAge(21);
ProxyFactory pc = new ProxyFactory(target);
@ -773,7 +773,7 @@ public abstract class AbstractAopProxyTests {
}
@Test
void testCannotRemoveAdvisorWhenFrozen() {
void cannotRemoveAdvisorWhenFrozen() {
TestBean target = new TestBean();
target.setAge(21);
ProxyFactory pc = new ProxyFactory(target);
@ -798,7 +798,7 @@ public abstract class AbstractAopProxyTests {
}
@Test
void testUseAsHashKey() {
void useAsHashKey() {
TestBean target1 = new TestBean();
ProxyFactory pf1 = new ProxyFactory(target1);
pf1.addAdvice(new NopInterceptor());
@ -823,7 +823,7 @@ public abstract class AbstractAopProxyTests {
* Check that the string is informative.
*/
@Test
void testProxyConfigString() {
void proxyConfigString() {
TestBean target = new TestBean();
ProxyFactory pc = new ProxyFactory(target);
pc.setInterfaces(ITestBean.class);
@ -839,7 +839,7 @@ public abstract class AbstractAopProxyTests {
}
@Test
void testCanPreventCastToAdvisedUsingOpaque() {
void canPreventCastToAdvisedUsingOpaque() {
TestBean target = new TestBean();
ProxyFactory pc = new ProxyFactory(target);
pc.setInterfaces(ITestBean.class);
@ -860,7 +860,7 @@ public abstract class AbstractAopProxyTests {
}
@Test
void testAdviceSupportListeners() {
void adviceSupportListeners() {
TestBean target = new TestBean();
target.setAge(21);
@ -899,7 +899,7 @@ public abstract class AbstractAopProxyTests {
}
@Test
void testExistingProxyChangesTarget() {
void existingProxyChangesTarget() {
TestBean tb1 = new TestBean();
tb1.setAge(33);
@ -942,7 +942,7 @@ public abstract class AbstractAopProxyTests {
}
@Test
void testDynamicMethodPointcutThatAlwaysAppliesStatically() {
void dynamicMethodPointcutThatAlwaysAppliesStatically() {
TestBean tb = new TestBean();
ProxyFactory pc = new ProxyFactory();
pc.addInterface(ITestBean.class);
@ -959,7 +959,7 @@ public abstract class AbstractAopProxyTests {
}
@Test
void testDynamicMethodPointcutThatAppliesStaticallyOnlyToSetters() {
void dynamicMethodPointcutThatAppliesStaticallyOnlyToSetters() {
TestBean tb = new TestBean();
ProxyFactory pc = new ProxyFactory();
pc.addInterface(ITestBean.class);
@ -982,7 +982,7 @@ public abstract class AbstractAopProxyTests {
}
@Test
void testStaticMethodPointcut() {
void staticMethodPointcut() {
TestBean tb = new TestBean();
ProxyFactory pc = new ProxyFactory();
pc.addInterface(ITestBean.class);
@ -1004,7 +1004,7 @@ public abstract class AbstractAopProxyTests {
* We can do this if we clone the invocation.
*/
@Test
void testCloneInvocationToProceedThreeTimes() {
void cloneInvocationToProceedThreeTimes() {
TestBean tb = new TestBean();
ProxyFactory pc = new ProxyFactory(tb);
pc.addInterface(ITestBean.class);
@ -1041,7 +1041,7 @@ public abstract class AbstractAopProxyTests {
* We want to change the arguments on a clone: it shouldn't affect the original.
*/
@Test
void testCanChangeArgumentsIndependentlyOnClonedInvocation() {
void canChangeArgumentsIndependentlyOnClonedInvocation() {
TestBean tb = new TestBean();
ProxyFactory pc = new ProxyFactory(tb);
pc.addInterface(ITestBean.class);
@ -1085,7 +1085,7 @@ public abstract class AbstractAopProxyTests {
@SuppressWarnings("serial")
@Test
void testOverloadedMethodsWithDifferentAdvice() {
void overloadedMethodsWithDifferentAdvice() {
Overloads target = new Overloads();
ProxyFactory pc = new ProxyFactory(target);
@ -1121,7 +1121,7 @@ public abstract class AbstractAopProxyTests {
}
@Test
void testProxyIsBoundBeforeTargetSourceInvoked() {
void proxyIsBoundBeforeTargetSourceInvoked() {
final TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvice(new DebugInterceptor());
@ -1147,7 +1147,7 @@ public abstract class AbstractAopProxyTests {
}
@Test
void testEquals() {
void equals() {
IOther a = new AllInstancesAreEqual();
IOther b = new AllInstancesAreEqual();
NopInterceptor i1 = new NopInterceptor();
@ -1177,7 +1177,7 @@ public abstract class AbstractAopProxyTests {
}
@Test
void testBeforeAdvisorIsInvoked() {
void beforeAdvisorIsInvoked() {
CountingBeforeAdvice cba = new CountingBeforeAdvice();
@SuppressWarnings("serial")
Advisor matchesNoArgs = new StaticMethodMatcherPointcutAdvisor(cba) {
@ -1206,7 +1206,7 @@ public abstract class AbstractAopProxyTests {
}
@Test
void testUserAttributes() {
void userAttributes() {
class MapAwareMethodInterceptor implements MethodInterceptor {
private final Map<String, String> expectedValues;
private final Map<String, String> valuesToAdd;
@ -1257,7 +1257,7 @@ public abstract class AbstractAopProxyTests {
}
@Test
void testMultiAdvice() {
void multiAdvice() {
CountingMultiAdvice cca = new CountingMultiAdvice();
@SuppressWarnings("serial")
Advisor matchesNoArgs = new StaticMethodMatcherPointcutAdvisor(cca) {
@ -1291,7 +1291,7 @@ public abstract class AbstractAopProxyTests {
}
@Test
void testBeforeAdviceThrowsException() {
void beforeAdviceThrowsException() {
final RuntimeException rex = new RuntimeException();
@SuppressWarnings("serial")
CountingBeforeAdvice ba = new CountingBeforeAdvice() {
@ -1333,7 +1333,7 @@ public abstract class AbstractAopProxyTests {
@Test
void testAfterReturningAdvisorIsInvoked() {
void afterReturningAdvisorIsInvoked() {
class SummingAfterAdvice implements AfterReturningAdvice {
public int sum;
@Override
@ -1370,7 +1370,7 @@ public abstract class AbstractAopProxyTests {
}
@Test
void testAfterReturningAdvisorIsNotInvokedOnException() {
void afterReturningAdvisorIsNotInvokedOnException() {
CountingAfterReturningAdvice car = new CountingAfterReturningAdvice();
TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
@ -1392,7 +1392,7 @@ public abstract class AbstractAopProxyTests {
@Test
void testThrowsAdvisorIsInvoked() {
void throwsAdvisorIsInvoked() {
// Reacts to ServletException and RemoteException
MyThrowsHandler th = new MyThrowsHandler();
@SuppressWarnings("serial")
@ -1425,7 +1425,7 @@ public abstract class AbstractAopProxyTests {
}
@Test
void testAddThrowsAdviceWithoutAdvisor() {
void addThrowsAdviceWithoutAdvisor() {
// Reacts to ServletException and RemoteException
MyThrowsHandler th = new MyThrowsHandler();
@ -1850,26 +1850,17 @@ public abstract class AbstractAopProxyTests {
this.target = target;
}
/**
* @see org.springframework.aop.TargetSource#getTargetClass()
*/
@Override
public Class<?> getTargetClass() {
return target.getClass();
}
/**
* @see org.springframework.aop.TargetSource#getTarget()
*/
@Override
public Object getTarget() {
++gets;
return target;
}
/**
* @see org.springframework.aop.TargetSource#releaseTarget(java.lang.Object)
*/
@Override
public void releaseTarget(Object pTarget) {
if (pTarget != this.target) {
@ -1879,8 +1870,7 @@ public abstract class AbstractAopProxyTests {
}
/**
* Check that gets and releases match
*
* Check that gets and releases match.
*/
public void verify() {
if (gets != releases) {

View File

@ -78,13 +78,13 @@ class CglibProxyTests extends AbstractAopProxyTests {
@Test
void testNullConfig() {
void nullConfig() {
assertThatIllegalArgumentException().isThrownBy(() ->
new CglibAopProxy(null));
}
@Test
void testNoTarget() {
void noTarget() {
AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
pc.addAdvice(new NopInterceptor());
AopProxy aop = createAopProxy(pc);
@ -92,7 +92,7 @@ class CglibProxyTests extends AbstractAopProxyTests {
}
@Test
void testProtectedMethodInvocation() {
void protectedMethodInvocation() {
ProtectedMethodTestBean bean = new ProtectedMethodTestBean();
bean.value = "foo";
mockTargetSource.setTarget(bean);
@ -109,7 +109,7 @@ class CglibProxyTests extends AbstractAopProxyTests {
}
@Test
void testPackageMethodInvocation() {
void packageMethodInvocation() {
PackageMethodTestBean bean = new PackageMethodTestBean();
bean.value = "foo";
mockTargetSource.setTarget(bean);
@ -126,7 +126,7 @@ class CglibProxyTests extends AbstractAopProxyTests {
}
@Test
void testProxyCanBeClassNotInterface() {
void proxyCanBeClassNotInterface() {
TestBean raw = new TestBean();
raw.setAge(32);
mockTargetSource.setTarget(raw);
@ -144,7 +144,7 @@ class CglibProxyTests extends AbstractAopProxyTests {
}
@Test
void testMethodInvocationDuringConstructor() {
void methodInvocationDuringConstructor() {
CglibTestBean bean = new CglibTestBean();
bean.setName("Rob Harrop");
@ -158,7 +158,7 @@ class CglibProxyTests extends AbstractAopProxyTests {
}
@Test
void testToStringInvocation() {
void toStringInvocation() {
PrivateCglibTestBean bean = new PrivateCglibTestBean();
bean.setName("Rob Harrop");
@ -172,7 +172,7 @@ class CglibProxyTests extends AbstractAopProxyTests {
}
@Test
void testUnadvisedProxyCreationWithCallDuringConstructor() {
void unadvisedProxyCreationWithCallDuringConstructor() {
CglibTestBean target = new CglibTestBean();
target.setName("Rob Harrop");
@ -187,7 +187,7 @@ class CglibProxyTests extends AbstractAopProxyTests {
}
@Test
void testMultipleProxies() {
void multipleProxies() {
TestBean target = new TestBean();
target.setAge(20);
TestBean target2 = new TestBean();
@ -233,7 +233,7 @@ class CglibProxyTests extends AbstractAopProxyTests {
}
@Test
void testMultipleProxiesForIntroductionAdvisor() {
void multipleProxiesForIntroductionAdvisor() {
TestBean target1 = new TestBean();
target1.setAge(20);
TestBean target2 = new TestBean();
@ -257,7 +257,7 @@ class CglibProxyTests extends AbstractAopProxyTests {
}
@Test
void testWithNoArgConstructor() {
void withNoArgConstructor() {
NoArgCtorTestBean target = new NoArgCtorTestBean("b", 1);
target.reset();
@ -272,7 +272,7 @@ class CglibProxyTests extends AbstractAopProxyTests {
}
@Test
void testProxyAProxy() {
void proxyAProxy() {
ITestBean target = new TestBean();
mockTargetSource.setTarget(target);
@ -293,7 +293,7 @@ class CglibProxyTests extends AbstractAopProxyTests {
}
@Test
void testProxyAProxyWithAdditionalInterface() {
void proxyAProxyWithAdditionalInterface() {
ITestBean target = new TestBean();
mockTargetSource.setTarget(target);
@ -351,7 +351,7 @@ class CglibProxyTests extends AbstractAopProxyTests {
}
@Test
void testExceptionHandling() {
void exceptionHandling() {
ExceptionThrower bean = new ExceptionThrower();
mockTargetSource.setTarget(bean);
@ -374,14 +374,14 @@ class CglibProxyTests extends AbstractAopProxyTests {
}
@Test
void testWithDependencyChecking() {
void withDependencyChecking() {
try (ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(DEPENDENCY_CHECK_CONTEXT, getClass())) {
ctx.getBean("testBean");
}
}
@Test
void testAddAdviceAtRuntime() {
void addAdviceAtRuntime() {
TestBean bean = new TestBean();
CountingBeforeAdvice cba = new CountingBeforeAdvice();
@ -403,7 +403,7 @@ class CglibProxyTests extends AbstractAopProxyTests {
}
@Test
void testProxyProtectedMethod() {
void proxyProtectedMethod() {
CountingBeforeAdvice advice = new CountingBeforeAdvice();
ProxyFactory proxyFactory = new ProxyFactory(new MyBean());
proxyFactory.addAdvice(advice);
@ -415,14 +415,14 @@ class CglibProxyTests extends AbstractAopProxyTests {
}
@Test
void testProxyTargetClassInCaseOfNoInterfaces() {
void proxyTargetClassInCaseOfNoInterfaces() {
ProxyFactory proxyFactory = new ProxyFactory(new MyBean());
MyBean proxy = (MyBean) proxyFactory.getProxy();
assertThat(proxy.add(1, 3)).isEqualTo(4);
}
@Test // SPR-13328
void testVarargsWithEnumArray() {
void varargsWithEnumArray() {
ProxyFactory proxyFactory = new ProxyFactory(new MyBean());
MyBean proxy = (MyBean) proxyFactory.getProxy();
assertThat(proxy.doWithVarargs(MyEnum.A, MyOtherEnum.C)).isTrue();

View File

@ -53,12 +53,12 @@ class JdkDynamicProxyTests extends AbstractAopProxyTests {
@Test
void testNullConfig() {
void nullConfig() {
assertThatIllegalArgumentException().isThrownBy(() -> new JdkDynamicAopProxy(null));
}
@Test
void testProxyIsJustInterface() {
void proxyIsJustInterface() {
TestBean raw = new TestBean();
raw.setAge(32);
AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
@ -66,12 +66,11 @@ class JdkDynamicProxyTests extends AbstractAopProxyTests {
JdkDynamicAopProxy aop = new JdkDynamicAopProxy(pc);
Object proxy = aop.getProxy();
assertThat(proxy instanceof ITestBean).isTrue();
assertThat(proxy instanceof TestBean).isFalse();
assertThat(proxy).isInstanceOf(ITestBean.class).isNotInstanceOf(TestBean.class);
}
@Test
void testInterceptorIsInvokedWithNoTarget() {
void interceptorIsInvokedWithNoTarget() {
// Test return value
final int age = 25;
MethodInterceptor mi = (invocation -> age);
@ -85,12 +84,14 @@ class JdkDynamicProxyTests extends AbstractAopProxyTests {
}
@Test
void testTargetCanGetInvocationWithPrivateClass() {
void targetCanGetInvocationWithPrivateClass() {
final ExposedInvocationTestBean expectedTarget = new ExposedInvocationTestBean() {
@Override
protected void assertions(MethodInvocation invocation) {
assertThat(invocation.getThis()).isEqualTo(this);
assertThat(invocation.getMethod().getDeclaringClass()).as("Invocation should be on ITestBean: " + invocation.getMethod()).isEqualTo(ITestBean.class);
assertThat(invocation.getMethod().getDeclaringClass())
.as("Invocation should be on ITestBean: " + invocation.getMethod())
.isEqualTo(ITestBean.class);
}
};
@ -113,7 +114,7 @@ class JdkDynamicProxyTests extends AbstractAopProxyTests {
}
@Test
void testProxyNotWrappedIfIncompatible() {
void proxyNotWrappedIfIncompatible() {
FooBar bean = new FooBar();
ProxyCreatorSupport as = new ProxyCreatorSupport();
as.setInterfaces(Foo.class);
@ -125,22 +126,22 @@ class JdkDynamicProxyTests extends AbstractAopProxyTests {
}
@Test
void testEqualsAndHashCodeDefined() {
void equalsAndHashCodeDefined() {
Named named = new Person();
AdvisedSupport as = new AdvisedSupport(Named.class);
as.setTarget(named);
Named proxy = (Named) new JdkDynamicAopProxy(as).getProxy();
assertThat(proxy).isEqualTo(named);
assertThat(named.hashCode()).isEqualTo(proxy.hashCode());
assertThat(named).hasSameHashCodeAs(proxy);
proxy = (Named) new JdkDynamicAopProxy(as).getProxy();
assertThat(proxy).isEqualTo(named);
assertThat(named.hashCode()).isEqualTo(proxy.hashCode());
assertThat(named).hasSameHashCodeAs(proxy);
}
@Test // SPR-13328
void testVarargsWithEnumArray() {
void varargsWithEnumArray() {
ProxyFactory proxyFactory = new ProxyFactory(new VarargTestBean());
VarargTestInterface proxy = (VarargTestInterface) proxyFactory.getProxy();
assertThat(proxy.doWithVarargs(MyEnum.A, MyOtherEnum.C)).isTrue();

View File

@ -30,7 +30,6 @@ import org.springframework.aop.target.ThreadLocalTargetSource;
import org.springframework.aop.testfixture.advice.CountingBeforeAdvice;
import org.springframework.aop.testfixture.interceptor.NopInterceptor;
import org.springframework.aop.testfixture.mixin.Lockable;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.testfixture.beans.CountingTestBean;
import org.springframework.beans.testfixture.beans.ITestBean;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@ -40,48 +39,36 @@ import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for auto proxy creation by advisor recognition.
*
* @see org.springframework.aop.framework.autoproxy.AdvisorAutoProxyCreatorIntegrationTests
*
* @author Rod Johnson
* @author Dave Syer
* @author Chris Beams
* @see org.springframework.aop.framework.autoproxy.AdvisorAutoProxyCreatorIntegrationTests
*/
class AdvisorAutoProxyCreatorTests {
private static final Class<?> CLASS = AdvisorAutoProxyCreatorTests.class;
private static final String CLASSNAME = CLASS.getSimpleName();
private static final String DEFAULT_CONTEXT = CLASSNAME + "-context.xml";
private static final String CLASSNAME = AdvisorAutoProxyCreatorTests.class.getSimpleName();
private static final String COMMON_INTERCEPTORS_CONTEXT = CLASSNAME + "-common-interceptors.xml";
private static final String CUSTOM_TARGETSOURCE_CONTEXT = CLASSNAME + "-custom-targetsource.xml";
private static final String QUICK_TARGETSOURCE_CONTEXT = CLASSNAME + "-quick-targetsource.xml";
private static final String OPTIMIZED_CONTEXT = CLASSNAME + "-optimized.xml";
/**
* Return a bean factory with attributes and EnterpriseServices configured.
*/
protected BeanFactory getBeanFactory() {
return new ClassPathXmlApplicationContext(DEFAULT_CONTEXT, CLASS);
}
/**
* Check that we can provide a common interceptor that will
* appear in the chain before "specific" interceptors,
* which are sourced from matching advisors
*/
@Test
void testCommonInterceptorAndAdvisor() {
BeanFactory bf = new ClassPathXmlApplicationContext(COMMON_INTERCEPTORS_CONTEXT, CLASS);
ITestBean test1 = (ITestBean) bf.getBean("test1");
void commonInterceptorAndAdvisor() {
ClassPathXmlApplicationContext ctx = context(COMMON_INTERCEPTORS_CONTEXT);
ITestBean test1 = (ITestBean) ctx.getBean("test1");
assertThat(AopUtils.isAopProxy(test1)).isTrue();
Lockable lockable1 = (Lockable) test1;
NopInterceptor nop1 = (NopInterceptor) bf.getBean("nopInterceptor");
NopInterceptor nop2 = (NopInterceptor) bf.getBean("pointcutAdvisor", Advisor.class).getAdvice();
NopInterceptor nop1 = (NopInterceptor) ctx.getBean("nopInterceptor");
NopInterceptor nop2 = (NopInterceptor) ctx.getBean("pointcutAdvisor", Advisor.class).getAdvice();
ITestBean test2 = (ITestBean) bf.getBean("test2");
ITestBean test2 = (ITestBean) ctx.getBean("test2");
Lockable lockable2 = (Lockable) test2;
// Locking should be independent; nop is shared
@ -97,19 +84,19 @@ class AdvisorAutoProxyCreatorTests {
assertThat(nop1.getCount()).isEqualTo(5);
assertThat(nop2.getCount()).isEqualTo(0);
PackageVisibleMethod packageVisibleMethod = (PackageVisibleMethod) bf.getBean("packageVisibleMethod");
PackageVisibleMethod packageVisibleMethod = (PackageVisibleMethod) ctx.getBean("packageVisibleMethod");
assertThat(nop1.getCount()).isEqualTo(5);
assertThat(nop2.getCount()).isEqualTo(0);
packageVisibleMethod.doSomething();
assertThat(nop1.getCount()).isEqualTo(6);
assertThat(nop2.getCount()).isEqualTo(1);
boolean condition = packageVisibleMethod instanceof Lockable;
assertThat(condition).isTrue();
assertThat(packageVisibleMethod).isInstanceOf(Lockable.class);
Lockable lockable3 = (Lockable) packageVisibleMethod;
lockable3.lock();
assertThat(lockable3.locked()).isTrue();
lockable3.unlock();
assertThat(lockable3.locked()).isFalse();
ctx.close();
}
/**
@ -117,107 +104,108 @@ class AdvisorAutoProxyCreatorTests {
* hence no proxying, for this bean
*/
@Test
void testCustomTargetSourceNoMatch() {
BeanFactory bf = new ClassPathXmlApplicationContext(CUSTOM_TARGETSOURCE_CONTEXT, CLASS);
ITestBean test = (ITestBean) bf.getBean("test");
void customTargetSourceNoMatch() {
ClassPathXmlApplicationContext ctx = context(CUSTOM_TARGETSOURCE_CONTEXT);
ITestBean test = (ITestBean) ctx.getBean("test");
assertThat(AopUtils.isAopProxy(test)).isFalse();
assertThat(test.getName()).isEqualTo("Rod");
assertThat(test.getSpouse().getName()).isEqualTo("Kerry");
ctx.close();
}
@Test
void testCustomPrototypeTargetSource() {
void customPrototypeTargetSource() {
CountingTestBean.count = 0;
BeanFactory bf = new ClassPathXmlApplicationContext(CUSTOM_TARGETSOURCE_CONTEXT, CLASS);
ITestBean test = (ITestBean) bf.getBean("prototypeTest");
ClassPathXmlApplicationContext ctx = context(CUSTOM_TARGETSOURCE_CONTEXT);
ITestBean test = (ITestBean) ctx.getBean("prototypeTest");
assertThat(AopUtils.isAopProxy(test)).isTrue();
Advised advised = (Advised) test;
boolean condition = advised.getTargetSource() instanceof PrototypeTargetSource;
assertThat(condition).isTrue();
assertThat(advised.getTargetSource()).isInstanceOf(PrototypeTargetSource.class);
assertThat(test.getName()).isEqualTo("Rod");
// Check that references survived prototype creation
assertThat(test.getSpouse().getName()).isEqualTo("Kerry");
assertThat(CountingTestBean.count).as("Only 2 CountingTestBeans instantiated").isEqualTo(2);
CountingTestBean.count = 0;
ctx.close();
}
@Test
void testLazyInitTargetSource() {
void lazyInitTargetSource() {
CountingTestBean.count = 0;
BeanFactory bf = new ClassPathXmlApplicationContext(CUSTOM_TARGETSOURCE_CONTEXT, CLASS);
ITestBean test = (ITestBean) bf.getBean("lazyInitTest");
ClassPathXmlApplicationContext ctx = context(CUSTOM_TARGETSOURCE_CONTEXT);
ITestBean test = (ITestBean) ctx.getBean("lazyInitTest");
assertThat(AopUtils.isAopProxy(test)).isTrue();
Advised advised = (Advised) test;
boolean condition = advised.getTargetSource() instanceof LazyInitTargetSource;
assertThat(condition).isTrue();
assertThat(advised.getTargetSource()).isInstanceOf(LazyInitTargetSource.class);
assertThat(CountingTestBean.count).as("No CountingTestBean instantiated yet").isEqualTo(0);
assertThat(test.getName()).isEqualTo("Rod");
assertThat(test.getSpouse().getName()).isEqualTo("Kerry");
assertThat(CountingTestBean.count).as("Only 1 CountingTestBean instantiated").isEqualTo(1);
CountingTestBean.count = 0;
ctx.close();
}
@Test
void testQuickTargetSourceCreator() {
ClassPathXmlApplicationContext bf =
new ClassPathXmlApplicationContext(QUICK_TARGETSOURCE_CONTEXT, CLASS);
ITestBean test = (ITestBean) bf.getBean("test");
void quickTargetSourceCreator() {
ClassPathXmlApplicationContext ctx = context(QUICK_TARGETSOURCE_CONTEXT);
ITestBean test = (ITestBean) ctx.getBean("test");
assertThat(AopUtils.isAopProxy(test)).isFalse();
assertThat(test.getName()).isEqualTo("Rod");
// Check that references survived pooling
assertThat(test.getSpouse().getName()).isEqualTo("Kerry");
// Now test the pooled one
test = (ITestBean) bf.getBean(":test");
test = (ITestBean) ctx.getBean(":test");
assertThat(AopUtils.isAopProxy(test)).isTrue();
Advised advised = (Advised) test;
boolean condition2 = advised.getTargetSource() instanceof CommonsPool2TargetSource;
assertThat(condition2).isTrue();
assertThat(advised.getTargetSource()).isInstanceOf(CommonsPool2TargetSource.class);
assertThat(test.getName()).isEqualTo("Rod");
// Check that references survived pooling
assertThat(test.getSpouse().getName()).isEqualTo("Kerry");
// Now test the ThreadLocal one
test = (ITestBean) bf.getBean("%test");
test = (ITestBean) ctx.getBean("%test");
assertThat(AopUtils.isAopProxy(test)).isTrue();
advised = (Advised) test;
boolean condition1 = advised.getTargetSource() instanceof ThreadLocalTargetSource;
assertThat(condition1).isTrue();
assertThat(advised.getTargetSource()).isInstanceOf(ThreadLocalTargetSource.class);
assertThat(test.getName()).isEqualTo("Rod");
// Check that references survived pooling
assertThat(test.getSpouse().getName()).isEqualTo("Kerry");
// Now test the Prototype TargetSource
test = (ITestBean) bf.getBean("!test");
test = (ITestBean) ctx.getBean("!test");
assertThat(AopUtils.isAopProxy(test)).isTrue();
advised = (Advised) test;
boolean condition = advised.getTargetSource() instanceof PrototypeTargetSource;
assertThat(condition).isTrue();
assertThat(advised.getTargetSource()).isInstanceOf(PrototypeTargetSource.class);
assertThat(test.getName()).isEqualTo("Rod");
// Check that references survived pooling
assertThat(test.getSpouse().getName()).isEqualTo("Kerry");
ITestBean test2 = (ITestBean) bf.getBean("!test");
ITestBean test2 = (ITestBean) ctx.getBean("!test");
assertThat(test).as("Prototypes cannot be the same object").isNotSameAs(test2);
assertThat(test2.getName()).isEqualTo("Rod");
assertThat(test2.getSpouse().getName()).isEqualTo("Kerry");
bf.close();
ctx.close();
}
@Test
void testWithOptimizedProxy() {
BeanFactory beanFactory = new ClassPathXmlApplicationContext(OPTIMIZED_CONTEXT, CLASS);
void withOptimizedProxy() {
ClassPathXmlApplicationContext ctx = context(OPTIMIZED_CONTEXT);
ITestBean testBean = (ITestBean) beanFactory.getBean("optimizedTestBean");
ITestBean testBean = (ITestBean) ctx.getBean("optimizedTestBean");
assertThat(AopUtils.isAopProxy(testBean)).isTrue();
CountingBeforeAdvice beforeAdvice = (CountingBeforeAdvice) beanFactory.getBean("countingAdvice");
testBean.setAge(23);
testBean.getAge();
CountingBeforeAdvice beforeAdvice = (CountingBeforeAdvice) ctx.getBean("countingAdvice");
assertThat(beforeAdvice.getCalls()).as("Incorrect number of calls to proxy").isEqualTo(2);
ctx.close();
}
private ClassPathXmlApplicationContext context(String filename) {
return new ClassPathXmlApplicationContext(filename, getClass());
}
}

View File

@ -58,7 +58,7 @@ import static org.assertj.core.api.Assertions.assertThat;
class AutoProxyCreatorTests {
@Test
void testBeanNameAutoProxyCreator() {
void beanNameAutoProxyCreator() {
StaticApplicationContext sac = new StaticApplicationContext();
sac.registerSingleton("testInterceptor", TestInterceptor.class);
@ -105,10 +105,12 @@ class AutoProxyCreatorTests {
assertThat(ti.nrOfInvocations).isEqualTo(6);
tb2.getAge();
assertThat(ti.nrOfInvocations).isEqualTo(7);
sac.close();
}
@Test
void testBeanNameAutoProxyCreatorWithFactoryBeanProxy() {
void beanNameAutoProxyCreatorWithFactoryBeanProxy() {
StaticApplicationContext sac = new StaticApplicationContext();
sac.registerSingleton("testInterceptor", TestInterceptor.class);
@ -139,12 +141,14 @@ class AutoProxyCreatorTests {
assertThat(ti.nrOfInvocations).isEqualTo((initialNr + 3));
tb.getAge();
assertThat(ti.nrOfInvocations).isEqualTo((initialNr + 3));
sac.close();
}
@Test
void testCustomAutoProxyCreator() {
void customAutoProxyCreator() {
StaticApplicationContext sac = new StaticApplicationContext();
sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class);
sac.registerSingleton("autoProxyCreator", TestAutoProxyCreator.class);
sac.registerSingleton("noInterfaces", NoInterfaces.class);
sac.registerSingleton("containerCallbackInterfacesOnly", ContainerCallbackInterfacesOnly.class);
sac.registerSingleton("singletonNoInterceptor", TestBean.class);
@ -166,7 +170,7 @@ class AutoProxyCreatorTests {
assertThat(AopUtils.isCglibProxy(singletonToBeProxied)).isTrue();
assertThat(AopUtils.isCglibProxy(prototypeToBeProxied)).isTrue();
TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("autoProxyCreator");
assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(0);
singletonNoInterceptor.getName();
assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(0);
@ -174,12 +178,14 @@ class AutoProxyCreatorTests {
assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(1);
prototypeToBeProxied.getSpouse();
assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(2);
sac.close();
}
@Test
void testAutoProxyCreatorWithFallbackToTargetClass() {
void autoProxyCreatorWithFallbackToTargetClass() {
StaticApplicationContext sac = new StaticApplicationContext();
sac.registerSingleton("testAutoProxyCreator", FallbackTestAutoProxyCreator.class);
sac.registerSingleton("autoProxyCreator", FallbackTestAutoProxyCreator.class);
sac.registerSingleton("noInterfaces", NoInterfaces.class);
sac.registerSingleton("containerCallbackInterfacesOnly", ContainerCallbackInterfacesOnly.class);
sac.registerSingleton("singletonNoInterceptor", TestBean.class);
@ -201,7 +207,7 @@ class AutoProxyCreatorTests {
assertThat(AopUtils.isCglibProxy(singletonToBeProxied)).isFalse();
assertThat(AopUtils.isCglibProxy(prototypeToBeProxied)).isFalse();
TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("autoProxyCreator");
assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(0);
singletonNoInterceptor.getName();
assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(0);
@ -209,15 +215,17 @@ class AutoProxyCreatorTests {
assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(1);
prototypeToBeProxied.getSpouse();
assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(2);
sac.close();
}
@Test
void testAutoProxyCreatorWithFallbackToDynamicProxy() {
void autoProxyCreatorWithFallbackToDynamicProxy() {
StaticApplicationContext sac = new StaticApplicationContext();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("proxyFactoryBean", "false");
sac.registerSingleton("testAutoProxyCreator", IntroductionTestAutoProxyCreator.class, pvs);
sac.registerSingleton("autoProxyCreator", IntroductionTestAutoProxyCreator.class, pvs);
sac.registerSingleton("noInterfaces", NoInterfaces.class);
sac.registerSingleton("containerCallbackInterfacesOnly", ContainerCallbackInterfacesOnly.class);
@ -241,7 +249,7 @@ class AutoProxyCreatorTests {
assertThat(AopUtils.isCglibProxy(singletonToBeProxied)).isFalse();
assertThat(AopUtils.isCglibProxy(prototypeToBeProxied)).isFalse();
TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("autoProxyCreator");
assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(0);
singletonNoInterceptor.getName();
assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(0);
@ -249,16 +257,18 @@ class AutoProxyCreatorTests {
assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(2);
prototypeToBeProxied.getSpouse();
assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(4);
sac.close();
}
@Test
void testAutoProxyCreatorWithPackageVisibleMethod() {
void autoProxyCreatorWithPackageVisibleMethod() {
StaticApplicationContext sac = new StaticApplicationContext();
sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class);
sac.registerSingleton("autoProxyCreator", TestAutoProxyCreator.class);
sac.registerSingleton("packageVisibleMethodToBeProxied", PackageVisibleMethod.class);
sac.refresh();
TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("autoProxyCreator");
tapc.testInterceptor.nrOfInvocations = 0;
PackageVisibleMethod tb = (PackageVisibleMethod) sac.getBean("packageVisibleMethodToBeProxied");
@ -266,16 +276,18 @@ class AutoProxyCreatorTests {
assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(0);
tb.doSomething();
assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(1);
sac.close();
}
@Test
void testAutoProxyCreatorWithFactoryBean() {
void autoProxyCreatorWithFactoryBean() {
StaticApplicationContext sac = new StaticApplicationContext();
sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class);
sac.registerSingleton("autoProxyCreator", TestAutoProxyCreator.class);
sac.registerSingleton("singletonFactoryToBeProxied", DummyFactory.class);
sac.refresh();
TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("autoProxyCreator");
tapc.testInterceptor.nrOfInvocations = 0;
FactoryBean<?> factory = (FactoryBean<?>) sac.getBean("&singletonFactoryToBeProxied");
@ -286,12 +298,14 @@ class AutoProxyCreatorTests {
assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(2);
tb.getAge();
assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(3);
sac.close();
}
@Test
void testAutoProxyCreatorWithFactoryBeanAndPrototype() {
void autoProxyCreatorWithFactoryBeanAndPrototype() {
StaticApplicationContext sac = new StaticApplicationContext();
sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class);
sac.registerSingleton("autoProxyCreator", TestAutoProxyCreator.class);
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("singleton", "false");
@ -299,7 +313,7 @@ class AutoProxyCreatorTests {
sac.refresh();
TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("autoProxyCreator");
tapc.testInterceptor.nrOfInvocations = 0;
FactoryBean<?> prototypeFactory = (FactoryBean<?>) sac.getBean("&prototypeFactoryToBeProxied");
@ -310,21 +324,23 @@ class AutoProxyCreatorTests {
assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(2);
tb.getAge();
assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(3);
sac.close();
}
@Test
void testAutoProxyCreatorWithFactoryBeanAndProxyObjectOnly() {
void autoProxyCreatorWithFactoryBeanAndProxyObjectOnly() {
StaticApplicationContext sac = new StaticApplicationContext();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("proxyFactoryBean", "false");
sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class, pvs);
sac.registerSingleton("autoProxyCreator", TestAutoProxyCreator.class, pvs);
sac.registerSingleton("singletonFactoryToBeProxied", DummyFactory.class);
sac.refresh();
TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("autoProxyCreator");
tapc.testInterceptor.nrOfInvocations = 0;
FactoryBean<?> factory = (FactoryBean<?>) sac.getBean("&singletonFactoryToBeProxied");
@ -341,15 +357,17 @@ class AutoProxyCreatorTests {
assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(1);
tb2.getAge();
assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(2);
sac.close();
}
@Test
void testAutoProxyCreatorWithFactoryBeanAndProxyFactoryBeanOnly() {
void autoProxyCreatorWithFactoryBeanAndProxyFactoryBeanOnly() {
StaticApplicationContext sac = new StaticApplicationContext();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("proxyObject", "false");
sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class, pvs);
sac.registerSingleton("autoProxyCreator", TestAutoProxyCreator.class, pvs);
pvs = new MutablePropertyValues();
pvs.add("singleton", "false");
@ -357,7 +375,7 @@ class AutoProxyCreatorTests {
sac.refresh();
TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("autoProxyCreator");
tapc.testInterceptor.nrOfInvocations = 0;
FactoryBean<?> prototypeFactory = (FactoryBean<?>) sac.getBean("&prototypeFactoryToBeProxied");
@ -368,6 +386,8 @@ class AutoProxyCreatorTests {
assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(2);
tb.getAge();
assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(2);
sac.close();
}

View File

@ -21,7 +21,7 @@ import java.lang.reflect.Method;
import org.junit.jupiter.api.Test;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.beans.testfixture.beans.TestBean;
import org.springframework.beans.testfixture.beans.Pet;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.lang.Nullable;
@ -38,17 +38,16 @@ class BeanNameAutoProxyCreatorInitTests {
@Test
void ignoreAdvisorThatIsCurrentlyInCreation() {
ClassPathXmlApplicationContext ctx =
new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-context.xml", getClass());
TestBean bean = ctx.getBean(TestBean.class);
bean.setName("foo");
assertThat(bean.getName()).isEqualTo("foo");
assertThatIllegalArgumentException()
.isThrownBy(() -> bean.setName(null))
.withMessage("Null argument at position 0");
ctx.close();
String path = getClass().getSimpleName() + "-context.xml";
try (ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(path, getClass())) {
Pet pet = ctx.getBean(Pet.class);
assertThat(pet.getName()).isEqualTo("Simba");
pet.setName("Tiger");
assertThat(pet.getName()).isEqualTo("Tiger");
assertThatIllegalArgumentException()
.isThrownBy(() -> pet.setName(null))
.withMessage("Null argument at position 0");
}
}
}

View File

@ -76,8 +76,7 @@ class BeanNameAutoProxyCreatorTests {
int age = 5;
tb.setAge(age);
assertThat(tb.getAge()).isEqualTo(age);
boolean condition = tb instanceof TimeStamped;
assertThat(condition).as("Introduction was made").isTrue();
assertThat(tb).as("Introduction was made").isInstanceOf(TimeStamped.class);
assertThat(((TimeStamped) tb).getTimeStamp()).isEqualTo(0);
assertThat(nop.getCount()).isEqualTo(3);
assertThat(tb.getName()).isEqualTo("introductionUsingJdk");
@ -98,8 +97,9 @@ class BeanNameAutoProxyCreatorTests {
// Can still mod second object
tb2.setAge(12);
// But can't mod first
assertThatExceptionOfType(LockedException.class).as("mixin should have locked this object").isThrownBy(() ->
tb.setAge(6));
assertThatExceptionOfType(LockedException.class)
.as("mixin should have locked this object")
.isThrownBy(() -> tb.setAge(6));
}
@Test
@ -131,8 +131,9 @@ class BeanNameAutoProxyCreatorTests {
// Can still mod second object
tb2.setAge(12);
// But can't mod first
assertThatExceptionOfType(LockedException.class).as("mixin should have locked this object").isThrownBy(() ->
tb.setAge(6));
assertThatExceptionOfType(LockedException.class)
.as("mixin should have locked this object")
.isThrownBy(() -> tb.setAge(6));
}
@Test

View File

@ -4,7 +4,7 @@
<beans>
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames" value="*"/>
<property name="beanNames" value="pet*"/>
<property name="proxyTargetClass" value="true"/>
<property name="interceptorNames" value="checker"/>
</bean>
@ -17,13 +17,15 @@
<bean class="org.springframework.beans.factory.config.ListFactoryBean">
<property name="sourceList">
<list>
<value>.*\.set[a-zA-Z]*(.*)</value>
</list>
<value>.*\.setName*(.*)</value>
</list>
</property>
</bean>
</property>
</bean>
<bean id="bean" class="org.springframework.beans.testfixture.beans.TestBean"/>
<bean id="pet" class="org.springframework.beans.testfixture.beans.Pet">
<constructor-arg value="Simba" />
</bean>
</beans>

View File

@ -26,8 +26,11 @@ import org.springframework.http.client.ClientHttpRequestFactory;
/**
* Documented {@link io.micrometer.common.KeyValue KeyValues} for {@link ClientHttpRequestFactory HTTP client} observations.
* <p>This class is used by automated tools to document KeyValues attached to the HTTP client observations.
* Documented {@link io.micrometer.common.KeyValue KeyValues} for
* {@link ClientHttpRequestFactory HTTP client} observations.
*
* <p>This class is used by automated tools to document KeyValues attached to the
* HTTP client observations.
*
* @author Brian Clozel
* @since 6.0
@ -52,25 +55,26 @@ public enum ClientHttpObservationDocumentation implements ObservationDocumentati
public KeyName[] getHighCardinalityKeyNames() {
return new KeyName[] {HighCardinalityKeyNames.HTTP_URL};
}
};
public enum LowCardinalityKeyNames implements KeyName {
/**
* Name of HTTP request method or {@value KeyValue#NONE_VALUE} if the request could not be created.
* Name of HTTP request method or {@value KeyValue#NONE_VALUE} if the
* request could not be created.
*/
METHOD {
@Override
public String asString() {
return "method";
}
},
/**
* URI template used for HTTP request, or {@value KeyValue#NONE_VALUE} if none was provided.
* Only the path part of the URI is considered.
* URI template used for HTTP request, or {@value KeyValue#NONE_VALUE} if
* none was provided.
* <p>Only the path part of the URI is considered.
*/
URI {
@Override
@ -90,7 +94,6 @@ public enum ClientHttpObservationDocumentation implements ObservationDocumentati
}
},
/**
* Client name derived from the request URI host.
* @since 6.0.5
@ -103,7 +106,8 @@ public enum ClientHttpObservationDocumentation implements ObservationDocumentati
},
/**
* Name of the exception thrown during the exchange, or {@value KeyValue#NONE_VALUE} if no exception happened.
* Name of the exception thrown during the exchange, or
* {@value KeyValue#NONE_VALUE} if no exception happened.
*/
EXCEPTION {
@Override

View File

@ -23,8 +23,11 @@ import io.micrometer.observation.ObservationConvention;
import io.micrometer.observation.docs.ObservationDocumentation;
/**
* Documented {@link io.micrometer.common.KeyValue KeyValues} for the {@link WebClient HTTP client} observations.
* <p>This class is used by automated tools to document KeyValues attached to the HTTP client observations.
* Documented {@link io.micrometer.common.KeyValue KeyValues} for the
* {@link WebClient HTTP client} observations.
*
* <p>This class is used by automated tools to document KeyValues attached to the
* HTTP client observations.
*
* @author Brian Clozel
* @since 6.0
@ -49,25 +52,26 @@ public enum ClientHttpObservationDocumentation implements ObservationDocumentati
public KeyName[] getHighCardinalityKeyNames() {
return new KeyName[] {HighCardinalityKeyNames.HTTP_URL};
}
};
public enum LowCardinalityKeyNames implements KeyName {
/**
* Name of HTTP request method or {@value KeyValue#NONE_VALUE} if the request could not be created.
* Name of HTTP request method or {@value KeyValue#NONE_VALUE} if the
* request could not be created.
*/
METHOD {
@Override
public String asString() {
return "method";
}
},
/**
* URI template used for HTTP request, or {@value KeyValue#NONE_VALUE} if none was provided.
* Only the path part of the URI is considered.
* URI template used for HTTP request, or {@value KeyValue#NONE_VALUE} if
* none was provided.
* <p>Only the path part of the URI is considered.
*/
URI {
@Override
@ -99,7 +103,8 @@ public enum ClientHttpObservationDocumentation implements ObservationDocumentati
},
/**
* Name of the exception thrown during the exchange, or {@value KeyValue#NONE_VALUE} if no exception happened.
* Name of the exception thrown during the exchange, or
* {@value KeyValue#NONE_VALUE} if no exception happened.
*/
EXCEPTION {
@Override
@ -110,7 +115,6 @@ public enum ClientHttpObservationDocumentation implements ObservationDocumentati
/**
* Outcome of the HTTP client exchange.
*
* @see org.springframework.http.HttpStatus.Series
*/
OUTCOME {