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