From 2e5f3559d3bd2f9166d3a6cde7fc4c402bc0b802 Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Sat, 3 Sep 2011 22:37:16 +0000 Subject: [PATCH] Fix handling of @EnableLoadTimeWeaving AUTODETECT Issue: SPR-8643 --- .../LoadTimeWeavingConfiguration.java | 2 +- .../EnableLoadTimeWeavingTests.java | 63 ++++++++++++++++--- 2 files changed, 57 insertions(+), 8 deletions(-) diff --git a/org.springframework.context/src/main/java/org/springframework/context/annotation/LoadTimeWeavingConfiguration.java b/org.springframework.context/src/main/java/org/springframework/context/annotation/LoadTimeWeavingConfiguration.java index e206a855ad6..7acf3abbf0f 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/annotation/LoadTimeWeavingConfiguration.java +++ b/org.springframework.context/src/main/java/org/springframework/context/annotation/LoadTimeWeavingConfiguration.java @@ -84,7 +84,7 @@ public class LoadTimeWeavingConfiguration implements ImportAware, BeanClassLoade // AJ weaving is disabled -> do nothing break; case AUTODETECT: - if (this.beanClassLoader.getResource(ASPECTJ_AOP_XML_RESOURCE) != null) { + if (this.beanClassLoader.getResource(ASPECTJ_AOP_XML_RESOURCE) == null) { // No aop.xml present on the classpath -> treat as 'disabled' break; } diff --git a/org.springframework.context/src/test/java/org/springframework/context/annotation/EnableLoadTimeWeavingTests.java b/org.springframework.context/src/test/java/org/springframework/context/annotation/EnableLoadTimeWeavingTests.java index 33ddc066a7f..df6b5fb8e96 100644 --- a/org.springframework.context/src/test/java/org/springframework/context/annotation/EnableLoadTimeWeavingTests.java +++ b/org.springframework.context/src/test/java/org/springframework/context/annotation/EnableLoadTimeWeavingTests.java @@ -16,10 +16,17 @@ package org.springframework.context.annotation; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expectLastCall; +import static org.easymock.EasyMock.isA; +import static org.easymock.EasyMock.replay; + +import java.lang.instrument.ClassFileTransformer; + import org.junit.Test; +import org.springframework.context.annotation.EnableLoadTimeWeaving.AspectJWeaving; import org.springframework.context.support.GenericXmlApplicationContext; import org.springframework.instrument.classloading.LoadTimeWeaver; -import org.springframework.instrument.classloading.SimpleLoadTimeWeaver; /** * Unit tests for @EnableLoadTimeWeaving @@ -37,19 +44,61 @@ public class EnableLoadTimeWeavingTests { } @Test - public void test() { + public void enableLTW_withAjWeavingDisabled() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); - ctx.register(Config.class); + ctx.register(EnableLTWConfig_withAjWeavingDisabled.class); + ctx.refresh(); + ctx.getBean("loadTimeWeaver"); + } + + @Test + public void enableLTW_withAjWeavingAutodetect() { + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); + ctx.register(EnableLTWConfig_withAjWeavingAutodetect.class); + ctx.refresh(); + ctx.getBean("loadTimeWeaver"); + } + + @Test + public void enableLTW_withAjWeavingEnabled() { + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); + ctx.register(EnableLTWConfig_withAjWeavingEnabled.class); ctx.refresh(); ctx.getBean("loadTimeWeaver"); } @Configuration - @EnableLoadTimeWeaving - static class Config implements LoadTimeWeavingConfigurer { - + @EnableLoadTimeWeaving(aspectjWeaving=AspectJWeaving.DISABLED) + static class EnableLTWConfig_withAjWeavingDisabled implements LoadTimeWeavingConfigurer { public LoadTimeWeaver getLoadTimeWeaver() { - return new SimpleLoadTimeWeaver(); + LoadTimeWeaver mockLTW = createMock(LoadTimeWeaver.class); + // no expectations -> a class file transformer should NOT be added + replay(mockLTW); + return mockLTW; + } + } + + @Configuration + @EnableLoadTimeWeaving(aspectjWeaving=AspectJWeaving.AUTODETECT) + static class EnableLTWConfig_withAjWeavingAutodetect implements LoadTimeWeavingConfigurer { + public LoadTimeWeaver getLoadTimeWeaver() { + LoadTimeWeaver mockLTW = createMock(LoadTimeWeaver.class); + // no expectations -> a class file transformer should NOT be added + // because no META-INF/aop.xml is present on the classpath + replay(mockLTW); + return mockLTW; + } + } + + @Configuration + @EnableLoadTimeWeaving(aspectjWeaving=AspectJWeaving.ENABLED) + static class EnableLTWConfig_withAjWeavingEnabled implements LoadTimeWeavingConfigurer { + public LoadTimeWeaver getLoadTimeWeaver() { + LoadTimeWeaver mockLTW = createMock(LoadTimeWeaver.class); + mockLTW.addTransformer(isA(ClassFileTransformer.class)); + expectLastCall(); + replay(mockLTW); + return mockLTW; } } }