01: package test.net.sourceforge.pmd.jaxen;
02:
03: import static org.junit.Assert.assertTrue;
04: import net.sourceforge.pmd.ast.JavaParserVisitor;
05: import net.sourceforge.pmd.ast.Node;
06: import net.sourceforge.pmd.jaxen.Attribute;
07: import net.sourceforge.pmd.jaxen.MatchesFunction;
08:
09: import org.jaxen.Context;
10: import org.jaxen.FunctionCallException;
11: import org.junit.Test;
12:
13: import java.util.ArrayList;
14: import java.util.List;
15:
16: public class MatchesFunctionTest implements Node {
17:
18: public void jjtOpen() {
19: }
20:
21: public void jjtClose() {
22: }
23:
24: public void jjtSetParent(Node n) {
25: }
26:
27: public Node jjtGetParent() {
28: return null;
29: }
30:
31: public void jjtAddChild(Node n, int i) {
32: }
33:
34: public Node jjtGetChild(int i) {
35: return null;
36: }
37:
38: public int jjtGetNumChildren() {
39: return 0;
40: }
41:
42: public Object jjtAccept(JavaParserVisitor visitor, Object data) {
43: return null;
44: }
45:
46: private String className;
47:
48: public String getValue() {
49: return className;
50: }
51:
52: @Test
53: public void testMatch() throws FunctionCallException,
54: NoSuchMethodException {
55: className = "Foo";
56: assertTrue(tryRegexp("Foo") instanceof List);
57: }
58:
59: @Test
60: public void testNoMatch() throws FunctionCallException,
61: NoSuchMethodException {
62: className = "bar";
63: assertTrue(tryRegexp("Foo") instanceof Boolean);
64: className = "FobboBar";
65: assertTrue(tryRegexp("Foo") instanceof Boolean);
66: }
67:
68: private Object tryRegexp(String exp) throws FunctionCallException,
69: NoSuchMethodException {
70: MatchesFunction function = new MatchesFunction();
71: List<Object> list = new ArrayList<Object>();
72: List<Attribute> attrs = new ArrayList<Attribute>();
73: attrs.add(new Attribute(this , "matches", getClass().getMethod(
74: "getValue", new Class[0])));
75: list.add(attrs);
76: list.add(exp);
77: Context c = new Context(null);
78: c.setNodeSet(new ArrayList());
79: return function.call(c, list);
80: }
81:
82: public static junit.framework.Test suite() {
83: return new junit.framework.JUnit4TestAdapter(
84: MatchesFunctionTest.class);
85: }
86: }
|