1   package test.net.sourceforge.pmd.jaxen;
2   
3   import static org.junit.Assert.assertTrue;
4   import net.sourceforge.pmd.ast.JavaParserVisitor;
5   import net.sourceforge.pmd.ast.Node;
6   import net.sourceforge.pmd.jaxen.Attribute;
7   import net.sourceforge.pmd.jaxen.MatchesFunction;
8   
9   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, NoSuchMethodException {
54          className = "Foo";
55          assertTrue(tryRegexp("Foo") instanceof List);
56      }
57  
58      @Test
59      public void testNoMatch() throws FunctionCallException, NoSuchMethodException {
60          className = "bar";
61          assertTrue(tryRegexp("Foo") instanceof Boolean);
62          className = "FobboBar";
63          assertTrue(tryRegexp("Foo") instanceof Boolean);
64      }
65  
66      private Object tryRegexp(String exp) throws FunctionCallException, NoSuchMethodException {
67          MatchesFunction function = new MatchesFunction();
68          List<Object> list = new ArrayList<Object>();
69          List<Attribute> attrs = new ArrayList<Attribute>();
70          attrs.add(new Attribute(this, "matches", getClass().getMethod("getValue", new Class[0])));
71          list.add(attrs);
72          list.add(exp);
73          Context c = new Context(null);
74          c.setNodeSet(new ArrayList());
75          return function.call(c, list);
76      }
77  
78      public static junit.framework.Test suite() {
79          return new junit.framework.JUnit4TestAdapter(MatchesFunctionTest.class);
80      }
81  }
82  
83