View Javadoc
1   package net.sourceforge.pmd.properties;
2   
3   import static org.junit.Assert.assertNotNull;
4   import static org.junit.Assert.assertTrue;
5   
6   import java.lang.reflect.Method;
7   import java.util.HashMap;
8   
9   import net.sourceforge.pmd.PropertyDescriptor;
10  import net.sourceforge.pmd.lang.rule.properties.MethodMultiProperty;
11  import net.sourceforge.pmd.lang.rule.properties.MethodProperty;
12  import net.sourceforge.pmd.util.ClassUtil;
13  
14  import org.junit.Test;
15  
16  /**
17   * Evaluates the functionality of the MethodProperty descriptor by testing its
18   * ability to catch creation errors (illegal args), flag invalid methods per the
19   * allowable packages, and serialize/deserialize groups of methods onto/from a
20   * string buffer.
21   * 
22   * We're using methods from java.lang classes for 'normal' constructors and
23   * applying ones from java.util types as ones we expect to fail.
24   * 
25   * @author Brian Remedios
26   */
27  public class MethodPropertyTest extends AbstractPropertyDescriptorTester {
28  
29      private static final String[] methodSignatures = new String[] { "String#indexOf(int)", "String#substring(int,int)",
30              "java.lang.String#substring(int,int)", "Integer#parseInt(String)", "java.util.HashMap#put(Object,Object)",
31              "HashMap#containsKey(Object)" };
32  
33      public MethodPropertyTest() {
34          super("Method");
35      }
36  
37      @Test
38      public void testAsStringOn() {
39  
40          Method method = null;
41  
42          for (int i = 0; i < methodSignatures.length; i++) {
43              method = MethodProperty.methodFrom(methodSignatures[i], MethodProperty.CLASS_METHOD_DELIMITER,
44                      MethodProperty.METHOD_ARG_DELIMITER);
45              assertNotNull("Unable to identify method: " + methodSignatures[i], method);
46          }
47      }
48  
49      @Test
50      public void testAsMethodOn() {
51  
52          Method[] methods = new Method[methodSignatures.length];
53  
54          for (int i = 0; i < methodSignatures.length; i++) {
55              methods[i] = MethodProperty.methodFrom(methodSignatures[i], MethodProperty.CLASS_METHOD_DELIMITER,
56                      MethodProperty.METHOD_ARG_DELIMITER);
57              assertNotNull("Unable to identify method: " + methodSignatures[i], methods[i]);
58          }
59  
60          String translatedMethod = null;
61          for (int i = 0; i < methods.length; i++) {
62              translatedMethod = MethodProperty.asStringFor(methods[i]);
63              assertTrue(
64                      "Translated method does not match",
65                      ClassUtil.withoutPackageName(methodSignatures[i]).equals(
66                              ClassUtil.withoutPackageName(translatedMethod)));
67          }
68      }
69  
70      @Override
71      protected PropertyDescriptor createBadProperty(boolean multiValue) {
72  
73          Method[] methods = String.class.getDeclaredMethods();
74  
75          return multiValue ? new MethodMultiProperty("methodProperty", "asdf", new Method[] { methods[2], methods[3] },
76                  new String[] { "java.util" }, 1.0f) : new MethodProperty("methodProperty", "asdf", methods[1],
77                  new String[] { "java.util" }, 1.0f);
78      }
79  
80      @Override
81      protected Object createBadValue(int count) {
82  
83          Method[] allMethods = HashMap.class.getDeclaredMethods();
84  
85          if (count == 1) {
86              return (Method) randomChoice(allMethods);
87          }
88  
89          Method[] methods = new Method[count];
90          for (int i = 0; i < count; i++) {
91              methods[i] = allMethods[i];
92          }
93  
94          return methods;
95      }
96  
97      @Override
98      protected PropertyDescriptor createProperty(boolean multiValue) {
99  
100         Method[] methods = String.class.getDeclaredMethods();
101 
102         return multiValue ? new MethodMultiProperty("methodProperty", "asdf", new Method[] { methods[2], methods[3] },
103                 new String[] { "java.lang" }, 1.0f) : new MethodProperty("methodProperty", "asdf", methods[1],
104                 new String[] { "java.lang" }, 1.0f);
105     }
106 
107     @Override
108     protected Object createValue(int count) {
109 
110         Method[] allMethods = String.class.getDeclaredMethods();
111 
112         if (count == 1) {
113             return (Method) randomChoice(allMethods);
114         }
115 
116         Method[] methods = new Method[count];
117         for (int i = 0; i < count; i++) {
118             methods[i] = allMethods[i];
119         }
120 
121         return methods;
122     }
123 }