001: /**************************************************************************************
002: * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
003: * http://aspectwerkz.codehaus.org *
004: * ---------------------------------------------------------------------------------- *
005: * The software in this package is published under the terms of the LGPL license *
006: * a copy of which has been included with this distribution in the license.txt file. *
007: **************************************************************************************/package test.expression;
008:
009: import junit.framework.TestCase;
010:
011: import org.codehaus.aspectwerkz.expression.ast.ExpressionParser;
012: import org.codehaus.aspectwerkz.expression.ast.ParseException;
013: import org.codehaus.aspectwerkz.expression.ast.TokenMgrError;
014: import org.codehaus.aspectwerkz.expression.ExpressionInfo;
015:
016: /**
017: * Unit test for expression parser.
018: *
019: * @author <a href="mailto:the_mindstorm@evolva.ro">Alex Popescu</a>
020: */
021: public class ExpressionParserTest extends TestCase {
022: private static final ExpressionParser PARSER = ExpressionInfo
023: .getParser();
024:
025: public void testMethodPatternJP() {
026: for (int i = 0; i < s_methodLikeExpressions.length; i++) {
027: parse("call(" + s_methodLikeExpressions[i] + ")");
028: parse("!call(" + s_methodLikeExpressions[i] + ")");
029: parse("execution(" + s_methodLikeExpressions[i] + ")");
030: parse("!execution(" + s_methodLikeExpressions[i] + ")");
031: parse("withincode(" + s_methodLikeExpressions[i] + ")");
032: parse("! withincode(" + s_methodLikeExpressions[i] + ")");
033: }
034: }
035:
036: public void testHasmethodJP() {
037: for (int i = 0; i < s_methodLikeExpressions.length; i++) {
038: parse("! hasmethod(" + s_methodLikeExpressions[i] + ")");
039: }
040:
041: for (int i = 0; i < s_methodLikeExpressions.length; i++) {
042: parse("hasmethod(" + s_methodLikeExpressions[i] + ")");
043: }
044:
045: }
046:
047: public void testComplexMethodLevelJP() {
048: for (int i = 0; i < s_methodLikeExpressions.length; i++) {
049: for (int j = 0; j < METHOD_EXPRESSIONS.length; j++) {
050: parse("call(" + s_methodLikeExpressions[i] + ") AND "
051: + "hasmethod(" + METHOD_EXPRESSIONS[j] + ")");
052:
053: parse("call(" + s_methodLikeExpressions[i] + ") AND "
054: + "!hasmethod(" + METHOD_EXPRESSIONS[j] + ")");
055:
056: parse("call(" + s_methodLikeExpressions[i] + ") && "
057: + "!hasmethod(" + METHOD_EXPRESSIONS[j] + ")");
058:
059: parse("call(" + s_methodLikeExpressions[i] + ") OR "
060: + "!hasmethod(" + METHOD_EXPRESSIONS[j] + ")");
061:
062: parse("call(" + s_methodLikeExpressions[i] + ") || "
063: + "!hasmethod(" + METHOD_EXPRESSIONS[j] + ")");
064: }
065: }
066: }
067:
068: public void testFieldPatternJP() {
069: for (int i = 0; i < FIELD_EXPRESSIONS.length; i++) {
070: parse("set(" + FIELD_EXPRESSIONS[i] + ")");
071:
072: parse("get(" + FIELD_EXPRESSIONS[i] + ")");
073:
074: parse("hasfield(" + FIELD_EXPRESSIONS[i] + ")");
075: }
076: }
077:
078: public void testSpecial() {
079: parse("(set(* foo.bar.*) || get(* foo.bar.*)) && withincode(* foo.bar.Buzz.*(..))");
080:
081: parse("execution(* foo.bar.Baz.*(..)) || call(* foo.bar.Baz.*(..))");
082:
083: parse("handler(java.lang.Exception+)");
084:
085: parse("!cflow(call(* foo.bar.Buzz.*(..)))");
086:
087: parse("handler(java.lang.Exception+) && !cflow(call(* foo.bar.Buzz.*(..)))");
088:
089: parse("call(!public !static * *..*.*(..))");
090: }
091:
092: public void testClassPatternJP() {
093: for (int i = 0; i < TYPE_EXPRESSIONS.length; i++) {
094: parse("within(" + TYPE_EXPRESSIONS[i] + ")");
095:
096: parse("staticinitialization(" + TYPE_EXPRESSIONS[i] + ")");
097: }
098: }
099:
100: /* FIXME ArgParameters()
101: public void testArgsAndPointcutrefs() {
102: assertNull("args()", parseString("args()"));
103:
104: assertNotNull("args(,, String)", parseString("args(,, String)"));
105:
106: assertNotNull("args(, , String, , int)", parseString("args(, , String, , int)"));
107:
108: assertNull("pointcutref()", parseString("pointcutref()"));
109:
110: assertNull("pointcutref(arg1)", parseString("pointcutref(arg1)"));
111:
112: assertNull("pointcutref(..)", parseString("pointcutref(..)"));
113:
114: assertNull("pointcutref(arg1, ..)", parseString("pointcutref(arg1, ..)"));
115:
116: assertNotNull("pointcutref(, arg2)", parseString("pointcutref(, arg2)"));
117:
118: assertNotNull("pointcutref(, arg2, , arg4)", parseString("pointcutref(, arg2, , arg4)"));
119: }
120: */
121:
122: public void testWithincodeStaticinitialization() {
123: for (int i = 0; i < TYPE_EXPRESSIONS.length; i++) {
124: Throwable cause = parseString("withincode(staticinitialization("
125: + TYPE_EXPRESSIONS[i] + "))");
126: if (null != cause) {
127: cause.printStackTrace();
128: }
129: assertNull(TYPE_EXPRESSIONS[i] + cause, cause);
130: }
131: }
132:
133: private void parse(String expression) {
134: try {
135: PARSER.parse(expression);
136: } catch (ParseException e) {
137: fail("parsing [" + expression + "] failed because:\n"
138: + e.getMessage());
139: }
140: }
141:
142: private Throwable parseString(String expression) {
143: try {
144: PARSER.parse(expression);
145: } catch (ParseException pe) {
146: return pe;
147: } catch (TokenMgrError tokenerr) {
148: return tokenerr;
149: } catch (Exception ex) {
150: return ex;
151: }
152:
153: return null;
154: }
155:
156: private static final String[] METHOD_EXPRESSIONS = {
157: "int foo.*.Bar.method()", "int *.method(*)",
158: "* method(..)", "int foo.*.*.method(*,int)",
159: "int foo.*.Bar.method(..)", "int foo.*.Bar.method(int,..)",
160: "int foo.*.Bar.method(java.lang.*)",
161: "int foo.*.Bar.me*o*()", "* foo.*.Bar.method()",
162: "java.lang.* foo.*.Bar.method()",
163: "static int foo.*.Bar.method()",
164: "synchronized static int foo.*.Bar.method()",
165: "!synchronized !static int foo.*.Bar.method()",
166: "@Transaction * foo.*.*.*(..)",
167: "@Transaction static * foo.*.*.*(..)", "@Transaction",
168: "! @Transaction", "@Transaction !static * foo.*.*.*(..)",
169: "!@Transaction !static * foo.*.*.*(..)" };
170:
171: private static final String[] CONSTRUCTOR_EXPRESSIONS = {
172: "foo.*.Bar.new()", "*.new(*)", "foo.*.*.new(*,int)",
173: "foo.*.Bar.new(..)", "foo.*.Bar.new(int,..)",
174: "foo.*.Bar.new(java.lang.*)", "foo.*.Bar.new()",
175: "@Transaction foo.*.*.new(..)", };
176:
177: private static final String[] FIELD_EXPRESSIONS = {
178: "int foo.*.Bar.m_foo",
179: "* m_field",
180: "* foo.*.Bar.m_foo",
181: "java.lang.* foo.*.Bar.m_foo",
182: "int foo.*.Bar.m_*",
183: "int foo.*.Bar.m_*oo*",
184: "int foo.bar.Baz.*",
185: "int foo.*.Bar+.m_foo",
186: "* foo.*.Bar+.m_foo",
187: "java.lang.* foo.*.Bar+.m_foo",
188: "private static final String foo.*.bar....m_field",
189: "private static final String foo.*.bar.*.m_field",
190: "private static final String foo.*.bar.*.*",
191: "@Persistent",
192: "@Persistent * m_field",
193: "@Persistent int foo.*.Bar.m_foo",
194: "@Persistent * foo.*.Bar.m_foo",
195: "@Persistent java.lang.* foo.*.Bar.m_foo",
196: "@Persistent int foo.*.Bar.m_*",
197: "@Persistent int foo.*.Bar.m_*oo*",
198: "@Persistent int foo.bar.Baz.*",
199: "@Persistent * foo.bar.Baz.*",
200: "@Persistent private static final String foo.*.bar....m_field",
201: "@Persistent private static final String foo.*.bar.*.m_field",
202: "@Persistent private static final String foo.*.bar.*.*",
203: "@Persistent @Instrumentable",
204: "@Persistent @Instrumentable * m_field",
205: "@Persistent @Instrumentable int foo.*.Bar.m_foo",
206: "@Persistent @Instrumentable * foo.*.Bar.m_foo",
207: "@Persistent @Instrumentable java.lang.* foo.*.Bar.m_foo",
208: "@Persistent @Instrumentable int foo.*.Bar.m_*",
209: "@Persistent @Instrumentable int foo.*.Bar.m_*oo*",
210: "@Persistent @Instrumentable int foo.bar.Baz.*",
211: "@Persistent @Instrumentable int foo.bar.Baz+.*",
212: "@Persistent @Instrumentable * foo.bar.Baz.*",
213: "@Persistent @Instrumentable private static final String foo.*.bar....m_field",
214: "@Persistent @Instrumentable private static final String foo.*.bar.*.m_field",
215: "@Persistent @Instrumentable private static final String foo.*.bar.*.*"
216:
217: };
218:
219: private static final String[] TYPE_EXPRESSIONS = { "foo.bar.*",
220: "foo.*.FooBar", "foo.*.FooB*", "foo..", "public foo.bar.*",
221: "@Session foo.bar.*", "@Session",
222: "@Session @Service foo....bar.*",
223: "@Session @Service public abstract foo....bar.*"
224:
225: };
226:
227: private static String[] s_methodLikeExpressions;
228:
229: static {
230: s_methodLikeExpressions = new String[METHOD_EXPRESSIONS.length
231: + CONSTRUCTOR_EXPRESSIONS.length];
232: for (int i = 0; i < METHOD_EXPRESSIONS.length; i++) {
233: s_methodLikeExpressions[i] = METHOD_EXPRESSIONS[i];
234: }
235: for (int i = 0; i < CONSTRUCTOR_EXPRESSIONS.length; i++) {
236: s_methodLikeExpressions[METHOD_EXPRESSIONS.length + i] = CONSTRUCTOR_EXPRESSIONS[i];
237: }
238: }
239:
240: public static void main(String[] args) {
241: junit.textui.TestRunner.run(suite());
242: }
243:
244: public static junit.framework.Test suite() {
245: return new junit.framework.TestSuite(ExpressionParserTest.class);
246: }
247:
248: }
|