[jboss-cvs] JBossAS SVN: r75194 - in projects/aop/branches/joinpoint_graph/aop/src: test/org/jboss/aop/joinpoint/graph and 1 other directory.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Mon Jun 30 01:24:19 EDT 2008
Author: flavia.rainone at jboss.com
Date: 2008-06-30 01:24:18 -0400 (Mon, 30 Jun 2008)
New Revision: 75194
Added:
projects/aop/branches/joinpoint_graph/aop/src/test/org/jboss/aop/joinpoint/graph/FieldFilterTest.java
projects/aop/branches/joinpoint_graph/aop/src/test/org/jboss/aop/joinpoint/graph/TypeFilterTest.java
Modified:
projects/aop/branches/joinpoint_graph/aop/src/main/org/jboss/aop/joinpoint/graph/FieldFilter.java
projects/aop/branches/joinpoint_graph/aop/src/main/org/jboss/aop/joinpoint/graph/SimpleSearchKey.java
projects/aop/branches/joinpoint_graph/aop/src/test/org/jboss/aop/joinpoint/graph/DummyAnnotation.java
projects/aop/branches/joinpoint_graph/aop/src/test/org/jboss/aop/joinpoint/graph/Pojo5.java
Log:
[JBAOP-508] Tests for TypeFilter and FieldFilters.
Modified: projects/aop/branches/joinpoint_graph/aop/src/main/org/jboss/aop/joinpoint/graph/FieldFilter.java
===================================================================
--- projects/aop/branches/joinpoint_graph/aop/src/main/org/jboss/aop/joinpoint/graph/FieldFilter.java 2008-06-30 04:58:29 UTC (rev 75193)
+++ projects/aop/branches/joinpoint_graph/aop/src/main/org/jboss/aop/joinpoint/graph/FieldFilter.java 2008-06-30 05:24:18 UTC (rev 75194)
@@ -23,6 +23,7 @@
import java.lang.reflect.Field;
+import org.jboss.aop.AspectManager;
import org.jboss.aop.pointcut.Util;
import org.jboss.aop.pointcut.ast.ClassExpression;
@@ -65,6 +66,7 @@
public boolean accept(Field field)
{
- return Util.matchesClassExpr(classExpression, field.getType());
+ return Util.matchesClassExpr(classExpression, field.getType(),
+ AspectManager.instance().getTempClassAdvisorIfNotExist(field.getType()));
}
}
\ No newline at end of file
Modified: projects/aop/branches/joinpoint_graph/aop/src/main/org/jboss/aop/joinpoint/graph/SimpleSearchKey.java
===================================================================
--- projects/aop/branches/joinpoint_graph/aop/src/main/org/jboss/aop/joinpoint/graph/SimpleSearchKey.java 2008-06-30 04:58:29 UTC (rev 75193)
+++ projects/aop/branches/joinpoint_graph/aop/src/main/org/jboss/aop/joinpoint/graph/SimpleSearchKey.java 2008-06-30 05:24:18 UTC (rev 75194)
@@ -24,7 +24,7 @@
import java.util.ArrayList;
/**
- * A search key that does not wraps an internal search key.
+ * A search key that does not wrap an internal search key.
*
* @author <a href="flavia.rainone at jboss.com">Flavia Rainone</a>
*/
Modified: projects/aop/branches/joinpoint_graph/aop/src/test/org/jboss/aop/joinpoint/graph/DummyAnnotation.java
===================================================================
--- projects/aop/branches/joinpoint_graph/aop/src/test/org/jboss/aop/joinpoint/graph/DummyAnnotation.java 2008-06-30 04:58:29 UTC (rev 75193)
+++ projects/aop/branches/joinpoint_graph/aop/src/test/org/jboss/aop/joinpoint/graph/DummyAnnotation.java 2008-06-30 05:24:18 UTC (rev 75194)
@@ -21,9 +21,16 @@
*/
package org.jboss.aop.joinpoint.graph;
+import static java.lang.annotation.ElementType.*;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
/**
* Test scenario.
*
* @author <a href="flavia.rainone at jboss.com">Flavia Rainone</a>
*/
+ at Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
+ at Retention(RetentionPolicy.RUNTIME)
@interface DummyAnnotation {}
\ No newline at end of file
Added: projects/aop/branches/joinpoint_graph/aop/src/test/org/jboss/aop/joinpoint/graph/FieldFilterTest.java
===================================================================
--- projects/aop/branches/joinpoint_graph/aop/src/test/org/jboss/aop/joinpoint/graph/FieldFilterTest.java (rev 0)
+++ projects/aop/branches/joinpoint_graph/aop/src/test/org/jboss/aop/joinpoint/graph/FieldFilterTest.java 2008-06-30 05:24:18 UTC (rev 75194)
@@ -0,0 +1,109 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.aop.joinpoint.graph;
+
+import java.lang.reflect.Field;
+
+import junit.framework.TestCase;
+
+import org.jboss.aop.AspectManager;
+import org.jboss.aop.pointcut.Typedef;
+import org.jboss.aop.pointcut.TypedefExpression;
+import org.jboss.aop.pointcut.ast.ClassExpression;
+
+/**
+ * Tests {@code FieldFilter}.
+ *
+ * @author <a href="flavia.rainone at jboss.com">Flavia Rainone</a>
+ */
+public class FieldFilterTest extends TestCase
+{
+ private Field field1;
+ private Field field2;
+ private Field field3;
+ private Field field4;
+ private Field field5;
+ private Field field6;
+
+ @SuppressWarnings("deprecation")
+ public void setUp() throws Exception
+ {
+ field1 = Pojo1.class.getDeclaredField("field");
+ field2 = Pojo2.class.getDeclaredField("pojo3");
+ field3 = Pojo3.class.getDeclaredField("intField");
+ field4 = Pojo3.class.getDeclaredField("dumbField");
+ field5 = Pojo3.class.getDeclaredField("ANY_CONSTANT");
+ field6 = Pojo3.class.getDeclaredField("field");
+
+ }
+
+ public void test1() throws Exception
+ {
+ ClassExpression classExpr = new ClassExpression("java.lang.String");
+ FieldFilter fieldFilter = new FieldTypeFilter(classExpr);
+ assertFilter(fieldFilter, true, false, false, true, false, false);
+ }
+
+ public void test2() throws Exception
+ {
+ ClassExpression classExpr = new ClassExpression("@" +
+ DummyAnnotation.class.getName());
+ FieldFilter fieldFilter = new FieldTypeFilter(classExpr);
+ assertFilter(fieldFilter, false, true, false, false, false, false);
+ }
+
+ public void test3() throws Exception
+ {
+ Typedef typedef = new TypedefExpression("hasSize", "has(int *->size())");
+ AspectManager.instance().addTypedef(typedef);
+ ClassExpression classExpr = new ClassExpression("$typedef{hasSize}");
+ FieldFilter fieldFilter = new FieldTypeFilter(classExpr);
+ assertFilter(fieldFilter, false, false, false, false, false, true);
+ }
+
+ public void test4() throws Exception
+ {
+ Typedef typedef = new TypedefExpression("hasSizeOrLength",
+ "has(int *->size()) OR has(int *->length())");
+ AspectManager.instance().addTypedef(typedef);
+ ClassExpression classExpr = new ClassExpression("$typedef{hasSizeOrLength}");
+ FieldFilter fieldFilter = new FieldTypeFilter(classExpr);
+ assertFilter(fieldFilter, true, false, false, true, false, true);
+ }
+
+ private void assertFilter(FieldFilter filter, boolean accept1, boolean accept2,
+ boolean accept3, boolean accept4, boolean accept5, boolean accept6)
+ {
+ assertEquals("Filter " + (accept1? "does not accept ": "accepts ") + field1,
+ accept1, filter.accept(field1));
+ assertEquals("Filter " + (accept2? "does not accept ": "accepts ") + field2,
+ accept2, filter.accept(field2));
+ assertEquals("Filter " + (accept3? "does not accept ": "accepts ") + field3,
+ accept3, filter.accept(field3));
+ assertEquals("Filter " + (accept4? "does not accept ": "accepts ") + field4,
+ accept4, filter.accept(field4));
+ assertEquals("Filter " + (accept5? "does not accept ": "accepts ") + field5,
+ accept5, filter.accept(field5));
+ assertEquals("Filter " + (accept6? "does not accept ": "accepts ") + field6,
+ accept6, filter.accept(field6));
+ }
+}
\ No newline at end of file
Modified: projects/aop/branches/joinpoint_graph/aop/src/test/org/jboss/aop/joinpoint/graph/Pojo5.java
===================================================================
--- projects/aop/branches/joinpoint_graph/aop/src/test/org/jboss/aop/joinpoint/graph/Pojo5.java 2008-06-30 04:58:29 UTC (rev 75193)
+++ projects/aop/branches/joinpoint_graph/aop/src/test/org/jboss/aop/joinpoint/graph/Pojo5.java 2008-06-30 05:24:18 UTC (rev 75194)
@@ -33,7 +33,8 @@
* @author <a href="flavia.rainone at jboss.com">Flavia Rainone</a>
*/
@Aspect
-public class Pojo5
+ at SuppressWarnings("deprecation")
+public class Pojo5 extends Pojo2
{
public Pojo5() {}
Added: projects/aop/branches/joinpoint_graph/aop/src/test/org/jboss/aop/joinpoint/graph/TypeFilterTest.java
===================================================================
--- projects/aop/branches/joinpoint_graph/aop/src/test/org/jboss/aop/joinpoint/graph/TypeFilterTest.java (rev 0)
+++ projects/aop/branches/joinpoint_graph/aop/src/test/org/jboss/aop/joinpoint/graph/TypeFilterTest.java 2008-06-30 05:24:18 UTC (rev 75194)
@@ -0,0 +1,207 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.aop.joinpoint.graph;
+
+import java.io.StringReader;
+
+import junit.framework.TestCase;
+
+import org.jboss.aop.Aspect;
+import org.jboss.aop.AspectManager;
+import org.jboss.aop.Mixin;
+import org.jboss.aop.pointcut.Typedef;
+import org.jboss.aop.pointcut.TypedefExpression;
+import org.jboss.aop.pointcut.ast.ASTBehavior;
+import org.jboss.aop.pointcut.ast.ASTField;
+import org.jboss.aop.pointcut.ast.ASTHas;
+import org.jboss.aop.pointcut.ast.ASTHasField;
+import org.jboss.aop.pointcut.ast.ASTStart;
+import org.jboss.aop.pointcut.ast.ParseException;
+import org.jboss.aop.pointcut.ast.TypeExpressionParser;
+
+/**
+ * Tests {@code TypeFilter}.
+ *
+ * @author <a href="flavia.rainone at jboss.com">Flavia Rainone</a>
+ */
+public class TypeFilterTest extends TestCase
+{
+ public void testHasField1() throws Exception
+ {
+ ASTField astField = getASTField("* *->field");
+ assertFilter(new HasFieldFilter(astField), true, false, true, false, false);
+ }
+
+ public void testHasField2() throws Exception
+ {
+ ASTField astField = getASTField("java.util.Collection *->*");
+ assertFilter(new HasFieldFilter(astField), false, false, true, false, false);
+ }
+
+ public void testHasBehavior1() throws Exception
+ {
+ ASTBehavior astBehavior = getASTBehavior("java.util.Collection *->*(..)");
+ assertFilter(new HasBehaviorFilter(astBehavior), false, false, false, false,
+ false);
+ }
+
+ public void testHasBehavior2() throws Exception
+ {
+ ASTBehavior astBehavior = getASTBehavior("private void *->*(..)");
+ assertFilter(new HasBehaviorFilter(astBehavior), true, true, true, true,
+ true);
+ }
+
+ public void testHasBehavior3() throws Exception
+ {
+ ASTBehavior astBehavior = getASTBehavior("private void *->privateMethod(..)");
+ assertFilter(new HasBehaviorFilter(astBehavior), true, false, false, false,
+ false);
+ }
+
+ public void testNegative1() throws Exception
+ {
+ ASTBehavior astBehavior = getASTBehavior("private void *->privateMethod(..)");
+ assertFilter(new NegativeTypeFilter(new HasBehaviorFilter(astBehavior)), false,
+ true, true, true, true);
+ }
+
+ public void testNegative2() throws Exception
+ {
+ ASTField astField = getASTField("* *->field");
+ assertFilter(new NegativeTypeFilter(new HasFieldFilter(astField)), false,
+ true, false, true, true);
+ }
+
+
+
+ public void testTypeDef1() throws Exception
+ {
+ Typedef typeDef = new TypedefExpression("typeDef", "class(" +
+ Pojo1.class.getName() + ")");
+ assertFilter(new TypedefFilter(typeDef), true, false, false, false, false);
+ }
+
+ public void testTypeDef2() throws Exception
+ {
+ Typedef typeDef = new TypedefExpression("typeDef", "class(@" +
+ Aspect.class.getName() + ")");
+ assertFilter(new TypedefFilter(typeDef), false, false, false, false, true);
+ }
+
+ public void testTypeDef3() throws Exception
+ {
+ Typedef typeDef = new TypedefExpression("typeDef", "class(@" +
+ Aspect.class.getName() + ") AND !has(public *->new($instanceof{" +
+ Pojo3.class.getName() + "}))");
+ assertFilter(new TypedefFilter(typeDef), false, false, false, false, false);
+ }
+
+ public void testDisjunctive1() throws Exception
+ {
+ ASTBehavior astBehavior = getASTBehavior("private void *->privateMethod(..)");
+ TypeFilter typeFilter1 = new HasBehaviorFilter(astBehavior);
+ Typedef typeDef = new TypedefExpression("typeDef", "class(@" +
+ Aspect.class.getName() + ")");
+ TypeFilter typeFilter2 = new TypedefFilter(typeDef);
+ assertFilter(new DisjunctiveTypeFilter(typeFilter1, typeFilter2), true,
+ false, false, false, true);
+ }
+
+ public void testDisjunctive2() throws Exception
+ {
+ ASTBehavior astBehavior = getASTBehavior("* *->@" + Mixin.class.getName() +
+ "(..)");
+ TypeFilter typeFilter1 = new HasBehaviorFilter(astBehavior);
+ astBehavior = getASTBehavior("* *->call*()");
+ TypeFilter typeFilter2 = new HasBehaviorFilter(astBehavior);
+ assertFilter(new DisjunctiveTypeFilter(typeFilter1, typeFilter2), false,
+ false, false, true, true);
+ }
+
+ @SuppressWarnings("deprecation")
+ private void assertFilter(TypeFilter filter, boolean acceptPojo1,
+ boolean acceptPojo2, boolean acceptPojo3, boolean acceptPojo4,
+ boolean acceptPojo5)
+ {
+
+ assertEquals("Filter " + (acceptPojo1? "does not accept ": "accepts ") +
+ "Pojo1.class", acceptPojo1, filter.accept(Pojo1.class,
+ AspectManager.instance().getTempClassAdvisorIfNotExist(Pojo1.class)));
+ assertEquals("Filter " + (acceptPojo2? "does not accept ": "accepts ") +
+ "Pojo2.class", acceptPojo2, filter.accept(Pojo2.class,
+ AspectManager.instance().getTempClassAdvisorIfNotExist(Pojo2.class)));
+ assertEquals("Filter " + (acceptPojo3? " does not accept ": "accepts ") +
+ "Pojo3.class", acceptPojo3, filter.accept(Pojo3.class,
+ AspectManager.instance().getTempClassAdvisorIfNotExist(Pojo3.class)));
+ assertEquals("Filter " + (acceptPojo4? " does not accept ": "accepts ") +
+ "Pojo4.class", acceptPojo4, filter.accept(Pojo4.class,
+ AspectManager.instance().getTempClassAdvisorIfNotExist(Pojo4.class)));
+ assertEquals("Filter " + (acceptPojo5? " does not accept ": "accepts ") +
+ "Pojo5.class", acceptPojo5, filter.accept(Pojo5.class,
+ AspectManager.instance().getTempClassAdvisorIfNotExist(Pojo5.class)));
+ }
+
+ private ASTField getASTField(String fieldExpression) throws ParseException
+ {
+ ASTStart parsed = new TypeExpressionParser(new StringReader("hasfield(" +
+ fieldExpression + ")")).Start();
+ int numChildren = parsed.jjtGetNumChildren();
+ for (int i = 0; i < numChildren; i++)
+ {
+ if (parsed.jjtGetChild(i) instanceof ASTHasField)
+ {
+ ASTHasField astHas = (ASTHasField) parsed.jjtGetChild(i);
+ for (int j = 0; j < astHas.jjtGetNumChildren(); j++)
+ {
+ if (astHas.jjtGetChild(j) instanceof ASTField)
+ {
+ return (ASTField) astHas.jjtGetChild(j);
+ }
+ }
+ }
+ }
+ throw new RuntimeException("ASTField not found");
+ }
+
+ private ASTBehavior getASTBehavior(String behaviorExpression) throws ParseException
+ {
+ ASTStart parsed = new TypeExpressionParser(new StringReader("has(" +
+ behaviorExpression + ")")).Start();
+ int numChildren = parsed.jjtGetNumChildren();
+ for (int i = 0; i < numChildren; i++)
+ {
+ if (parsed.jjtGetChild(i) instanceof ASTHas)
+ {
+ ASTHas astHas = (ASTHas) parsed.jjtGetChild(i);
+ for (int j = 0; j < astHas.jjtGetNumChildren(); j++)
+ {
+ if (astHas.jjtGetChild(j) instanceof ASTBehavior)
+ {
+ return (ASTBehavior) astHas.jjtGetChild(j);
+ }
+ }
+ }
+ }
+ throw new RuntimeException("ASTBehavior not found");
+ }
+}
\ No newline at end of file
More information about the jboss-cvs-commits
mailing list