1 package net.sourceforge.pmd.jaxen; 2 3 import static org.junit.Assert.assertTrue; 4 5 import java.util.ArrayList; 6 import java.util.List; 7 8 import net.sourceforge.pmd.lang.ast.AbstractNode; 9 import net.sourceforge.pmd.lang.ast.xpath.Attribute; 10 import net.sourceforge.pmd.lang.xpath.MatchesFunction; 11 12 import org.jaxen.Context; 13 import org.jaxen.FunctionCallException; 14 import org.junit.Test; 15 16 public class MatchesFunctionTest { 17 18 public static class MyNode extends AbstractNode 19 { 20 private String className; 21 public MyNode() { 22 super(1); 23 } 24 public String toString() { 25 return "MyNode"; 26 } 27 public void setClassName(String className) { 28 this.className = className; 29 } 30 public String getClassName() { 31 return className; 32 } 33 }; 34 35 @Test 36 public void testMatch() throws FunctionCallException, NoSuchMethodException { 37 MyNode myNode = new MyNode(); 38 myNode.setClassName("Foo"); 39 assertTrue(tryRegexp(myNode, "Foo") instanceof List); 40 } 41 42 @Test 43 public void testNoMatch() throws FunctionCallException, NoSuchMethodException { 44 MyNode myNode = new MyNode(); 45 myNode.setClassName("bar"); 46 assertTrue(tryRegexp(myNode, "Foo") instanceof Boolean); 47 myNode.setClassName("FobboBar"); 48 assertTrue(tryRegexp(myNode, "Foo") instanceof Boolean); 49 } 50 51 private Object tryRegexp(MyNode myNode, String exp) throws FunctionCallException, NoSuchMethodException { 52 MatchesFunction function = new MatchesFunction(); 53 List<Object> list = new ArrayList<Object>(); 54 List<Attribute> attrs = new ArrayList<Attribute>(); 55 attrs.add(new Attribute(myNode, "matches", myNode.getClass().getMethod("getClassName", new Class[0]))); 56 list.add(attrs); 57 list.add(exp); 58 Context c = new Context(null); 59 c.setNodeSet(new ArrayList()); 60 return function.call(c, list); 61 } 62 63 public static junit.framework.Test suite() { 64 return new junit.framework.JUnit4TestAdapter(MatchesFunctionTest.class); 65 } 66 } 67 68