Author: hlship
Date: Wed Aug 10 00:50:41 2011
New Revision: 1155983
URL: http://svn.apache.org/viewvc?rev=1155983&view=rev
Log:
Add a number of tests to prove that methods with advice still have references
to fields transformed when the field has a FieldConduit
Added:
tapestry/tapestry5/trunk/plastic/src/test/java/testannotations/FieldAnnotation.java
tapestry/tapestry5/trunk/plastic/src/test/java/testannotations/MethodAnnotation.java
tapestry/tapestry5/trunk/plastic/src/test/java/testinterfaces/MagicContainer.java
tapestry/tapestry5/trunk/plastic/src/test/java/testsubjects/FieldConduitInsideAdvisedMethod.java
Modified:
tapestry/tapestry5/trunk/plastic/src/test/groovy/org/apache/tapestry5/plastic/FieldConduitTests.groovy
tapestry/tapestry5/trunk/plastic/src/test/groovy/org/apache/tapestry5/plastic/MethodAdviceTests.groovy
Modified:
tapestry/tapestry5/trunk/plastic/src/test/groovy/org/apache/tapestry5/plastic/FieldConduitTests.groovy
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/plastic/src/test/groovy/org/apache/tapestry5/plastic/FieldConduitTests.groovy?rev=1155983&r1=1155982&r2=1155983&view=diff
==============================================================================
---
tapestry/tapestry5/trunk/plastic/src/test/groovy/org/apache/tapestry5/plastic/FieldConduitTests.groovy
(original)
+++
tapestry/tapestry5/trunk/plastic/src/test/groovy/org/apache/tapestry5/plastic/FieldConduitTests.groovy
Wed Aug 10 00:50:41 2011
@@ -215,11 +215,16 @@ class FieldConduitTests extends Abstract
when:
def mgr = createMgr(transformer);
- mgr.addPlasticClassListener({ PlasticClassEvent event ->
- print(event.getDissasembledBytecode())
+ // Needed this when debugging an issue:
+ if (false)
+ {
+ mgr.addPlasticClassListener({ PlasticClassEvent event ->
- } as PlasticClassListener)
+ print(event.getDissasembledBytecode())
+
+ } as PlasticClassListener)
+ }
def o =
mgr.getClassInstantiator("testsubjects.InjectSubClass").newInstance()
Modified:
tapestry/tapestry5/trunk/plastic/src/test/groovy/org/apache/tapestry5/plastic/MethodAdviceTests.groovy
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/plastic/src/test/groovy/org/apache/tapestry5/plastic/MethodAdviceTests.groovy?rev=1155983&r1=1155982&r2=1155983&view=diff
==============================================================================
---
tapestry/tapestry5/trunk/plastic/src/test/groovy/org/apache/tapestry5/plastic/MethodAdviceTests.groovy
(original)
+++
tapestry/tapestry5/trunk/plastic/src/test/groovy/org/apache/tapestry5/plastic/MethodAdviceTests.groovy
Wed Aug 10 00:50:41 2011
@@ -1,26 +1,29 @@
package org.apache.tapestry5.plastic
import java.sql.SQLException
-
import org.apache.tapestry5.plastic.test.NoopAdvice
-
+import testannotations.FieldAnnotation
import testannotations.Maybe
+import testannotations.MethodAnnotation
import testannotations.Truth
+import testinterfaces.MagicContainer
-class MethodAdviceTests extends AbstractPlasticSpecification {
- def "advice for a void method"() {
+class MethodAdviceTests extends AbstractPlasticSpecification
+{
+ def "advice for a void method"()
+ {
setup:
def didInvoke = false
def methodId;
- def mgr = createMgr( { PlasticClass pc ->
+ def mgr = createMgr({ PlasticClass pc ->
def method = findMethod(pc, "aSingleMethod")
methodId = method.methodIdentifier
- findMethod(pc, "aSingleMethod").addAdvice ({
+ findMethod(pc, "aSingleMethod").addAdvice({
didInvoke = true
assert it.method.name == "aSingleMethod"
@@ -55,16 +58,17 @@ class MethodAdviceTests extends Abstract
didInvoke == true
}
- def "multiple advice on method with parameters and return values"() {
+ def "multiple advice on method with parameters and return values"()
+ {
setup:
- def mgr = createMgr( { PlasticClass pc ->
- findMethod(pc, "dupe").addAdvice( {
+ def mgr = createMgr({ PlasticClass pc ->
+ findMethod(pc, "dupe").addAdvice({
it.setParameter(0, it.getParameter(0) + 2)
it.proceed()
- } as MethodAdvice).addAdvice ( {
+ } as MethodAdvice).addAdvice({
it.setParameter(0, it.getParameter(0) * 3)
it.proceed()
@@ -80,7 +84,8 @@ class MethodAdviceTests extends Abstract
o.dupe(2, "Fam") == "FAM FAM FAM FAM FAM FAM FAM FAM FAM FAM FAM FAM"
}
- def "method that throws exceptions"() {
+ def "method that throws exceptions"()
+ {
setup:
@@ -103,13 +108,15 @@ class MethodAdviceTests extends Abstract
thrown(SQLException)
}
- def "setting return value clears checked exceptions"() {
+ def "setting return value clears checked exceptions"()
+ {
def mgr = createMgr({ PlasticClass pc ->
findMethod(pc, "maybeThrow").addAdvice({ MethodInvocation mi ->
mi.proceed()
- if (mi.didThrowCheckedException()) {
+ if (mi.didThrowCheckedException())
+ {
mi.setReturnValue(-1L)
}
} as MethodAdvice)
@@ -126,10 +133,11 @@ class MethodAdviceTests extends Abstract
/**
* This is important because each double/long takes up two local variable
slots.
- *
+ *
* @return
*/
- def "method with long and double parameters"() {
+ def "method with long and double parameters"()
+ {
setup:
def mgr = createMgr({ PlasticClass pc ->
@@ -143,4 +151,91 @@ class MethodAdviceTests extends Abstract
o.doMath(2l, 4.0d, 5, 6l) == 38d
}
+
+ def "method advice does not interfere with field instrumentation (advice
first)"()
+ {
+ FieldConduit fc = [get: { instance, context ->
+ return "via conduit"
+ }, set: { instance, context -> }] as FieldConduit
+
+ MethodAdvice justProceed = { inv -> inv.proceed() } as MethodAdvice
+
+ def mgr = createMgr({ PlasticClass pc ->
+
+ pc.getMethodsWithAnnotation(MethodAnnotation.class).each({ m ->
+ m.addAdvice(justProceed)
+ })
+
+ pc.getFieldsWithAnnotation(FieldAnnotation.class).each({ f ->
+ f.setConduit(fc)
+ })
+ } as PlasticClassTransformer)
+
+ def o =
mgr.getClassInstantiator("testsubjects.FieldConduitInsideAdvisedMethod").newInstance()
+
+ expect:
+
+ o.magic == "via conduit"
+ }
+
+ def "method advice does not interfere with field instrumentation (conduit
first)"()
+ {
+ FieldConduit fc = [get: { instance, context ->
+ return "via conduit"
+ }, set: { instance, context -> }] as FieldConduit
+
+ MethodAdvice justProceed = { inv -> inv.proceed() } as MethodAdvice
+
+ def mgr = createMgr({ PlasticClass pc ->
+
+ pc.getFieldsWithAnnotation(FieldAnnotation.class).each({ f ->
+ f.setConduit(fc)
+ })
+
+ pc.getMethodsWithAnnotation(MethodAnnotation.class).each({ m ->
+ m.addAdvice(justProceed)
+ })
+
+ } as PlasticClassTransformer)
+
+ def o =
mgr.getClassInstantiator("testsubjects.FieldConduitInsideAdvisedMethod").newInstance()
+
+ expect:
+
+ o.magic == "via conduit"
+ }
+
+ def "method advice does not interfere with field instrumentation (instance
context version)"()
+ {
+ MagicContainer container = Mock()
+
+ FieldConduit fc = [get: { instance, context ->
+
+ return context.get(MagicContainer.class).magic()
+
+ }, set: { instance, context -> }] as FieldConduit
+
+ MethodAdvice justProceed = { inv -> inv.proceed() } as MethodAdvice
+
+ def mgr = createMgr({ PlasticClass pc ->
+
+ pc.getMethodsWithAnnotation(MethodAnnotation.class).each({ m ->
+ m.addAdvice(justProceed)
+ })
+
+ pc.getFieldsWithAnnotation(FieldAnnotation.class).each({ f ->
+ f.setConduit(fc)
+ })
+ } as PlasticClassTransformer)
+
+ def o =
mgr.getClassInstantiator("testsubjects.FieldConduitInsideAdvisedMethod").with(MagicContainer.class,
container).newInstance()
+
+ when:
+
+ o.magic == "via context and mock"
+
+ then:
+
+ 1 * container.magic() >> "via context and mock"
+ }
}
Added:
tapestry/tapestry5/trunk/plastic/src/test/java/testannotations/FieldAnnotation.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/plastic/src/test/java/testannotations/FieldAnnotation.java?rev=1155983&view=auto
==============================================================================
---
tapestry/tapestry5/trunk/plastic/src/test/java/testannotations/FieldAnnotation.java
(added)
+++
tapestry/tapestry5/trunk/plastic/src/test/java/testannotations/FieldAnnotation.java
Wed Aug 10 00:50:41 2011
@@ -0,0 +1,12 @@
+package testannotations;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target(ElementType.FIELD)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface FieldAnnotation
+{
+}
Added:
tapestry/tapestry5/trunk/plastic/src/test/java/testannotations/MethodAnnotation.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/plastic/src/test/java/testannotations/MethodAnnotation.java?rev=1155983&view=auto
==============================================================================
---
tapestry/tapestry5/trunk/plastic/src/test/java/testannotations/MethodAnnotation.java
(added)
+++
tapestry/tapestry5/trunk/plastic/src/test/java/testannotations/MethodAnnotation.java
Wed Aug 10 00:50:41 2011
@@ -0,0 +1,12 @@
+package testannotations;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target(ElementType.METHOD)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface MethodAnnotation
+{
+}
Added:
tapestry/tapestry5/trunk/plastic/src/test/java/testinterfaces/MagicContainer.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/plastic/src/test/java/testinterfaces/MagicContainer.java?rev=1155983&view=auto
==============================================================================
---
tapestry/tapestry5/trunk/plastic/src/test/java/testinterfaces/MagicContainer.java
(added)
+++
tapestry/tapestry5/trunk/plastic/src/test/java/testinterfaces/MagicContainer.java
Wed Aug 10 00:50:41 2011
@@ -0,0 +1,6 @@
+package testinterfaces;
+
+public interface MagicContainer
+{
+ String magic();
+}
Added:
tapestry/tapestry5/trunk/plastic/src/test/java/testsubjects/FieldConduitInsideAdvisedMethod.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/plastic/src/test/java/testsubjects/FieldConduitInsideAdvisedMethod.java?rev=1155983&view=auto
==============================================================================
---
tapestry/tapestry5/trunk/plastic/src/test/java/testsubjects/FieldConduitInsideAdvisedMethod.java
(added)
+++
tapestry/tapestry5/trunk/plastic/src/test/java/testsubjects/FieldConduitInsideAdvisedMethod.java
Wed Aug 10 00:50:41 2011
@@ -0,0 +1,16 @@
+package testsubjects;
+
+import testannotations.FieldAnnotation;
+import testannotations.MethodAnnotation;
+
+public class FieldConduitInsideAdvisedMethod
+{
+ @FieldAnnotation
+ private String magic;
+
+ @MethodAnnotation
+ public String getMagic()
+ {
+ return magic;
+ }
+}