KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > csdl > jblanket > modifier > TestMethodModifier


1 package csdl.jblanket.modifier;
2
3 import java.io.File JavaDoc;
4
5 import junit.framework.TestCase;
6
7 import org.apache.bcel.Constants;
8 import org.apache.bcel.classfile.ClassParser;
9 import org.apache.bcel.classfile.JavaClass;
10 import org.apache.bcel.classfile.LineNumberTable;
11 import org.apache.bcel.classfile.Method;
12 import org.apache.bcel.generic.ClassGen;
13 import org.apache.bcel.generic.ConstantPoolGen;
14 import org.apache.bcel.generic.MethodGen;
15
16 import csdl.jblanket.methodset.MethodInfo;
17 import csdl.jblanket.methodset.MethodSet;
18 import csdl.jblanket.methodset.MethodSetManager;
19 import csdl.jblanket.util.MethodCategories;
20
21 /**
22  * Tests the MethodModifier class.
23  *
24  * @author Joy M. Agustin
25  * @version $Id: TestMethodModifier.java,v 1.5 2005/02/21 20:28:43 timshadel Exp $
26  */

27 public class TestMethodModifier extends TestCase {
28
29     /** MethodCounter object */
30     private MethodCounter counter;
31
32     /** Stack.class */
33     private JavaClass clazz;
34
35     /** Array of methods in Stack.class */
36     private Method[] methods;
37
38     /** Directory separator */
39     private static final String JavaDoc SLASH = File.separator;
40
41     /**
42      * Constructor required by JUnit.
43      *
44      * @param name
45      * the name of this test class.
46      */

47     public TestMethodModifier(String JavaDoc name) {
48         super(name);
49     }
50
51     /**
52      * Sets up the instance variables for each test case.
53      *
54      * @throws Exception
55      * when unable to complete a setup.
56      */

57     public void setUp() throws Exception JavaDoc {
58
59         String JavaDoc testDir = System.getProperty("jblanket.data.dir");
60         File JavaDoc dir = new File JavaDoc(testDir, "unjar");
61         File JavaDoc classDir = new File JavaDoc(dir, "edu" + SLASH + "hawaii" + SLASH
62                 + "stack");
63         File JavaDoc inFile = new File JavaDoc(classDir, "Debug.class");
64         System.setProperty("jblanket.dir", testDir + SLASH + "xml");
65
66         this.counter = new MethodCounter();
67
68         // retrieve class byte code -- throws IOException
69
this.clazz = (new ClassParser(inFile.getAbsolutePath())).parse();
70         this.methods = clazz.getMethods();
71     }
72
73     /**
74      * Tests the modifyMethods, hasLineNumbers, isConstructor methods.
75      */

76     public void testModifyMethods() {
77
78         String JavaDoc userDir = System.getProperty("jblanket.testdir") + SLASH
79                 + "testmethodmodifier";
80         System.setProperty("jblanket.dir", userDir);
81
82         ConstantPoolGen pool = new ConstantPoolGen(clazz.getConstantPool());
83         MethodCategories categories = MethodCategories.getInstance();
84         MethodSetManager manager = MethodSetManager.getInstance();
85         MethodSet constructorSet = manager.getMethodSet(categories
86                 .getFileName("constructorFile"));
87         MethodSet oneLineSet = manager.getMethodSet(categories
88                 .getFileName("oneLineFile"));
89
90         int oldConstructorSetSize = constructorSet.size();
91         int oldOneLineSetSize = oneLineSet.size();
92
93         // test the constructor '<init>'
94
MethodGen method = new MethodGen(methods[0], clazz.getClassName(), pool);
95         MethodModifier modifier = new MethodModifier(false, "Test*.class",
96                 true, false, true, method);
97         Method processedMethod = modifier.processMethod(pool, true);
98         assertEquals("Checking if has line numbers", true, modifier
99                 .hasLineNumbers());
100         assertEquals("Checking if one-line method", false, modifier.isOneLine());
101         assertEquals("Checking if constructor", true, modifier.isConstructor());
102         assertEquals("Checking size of constructor MethodSet",
103                 ++oldConstructorSetSize, constructorSet.size());
104         assertEquals("Checking size of one-line MethodSet", oldOneLineSetSize,
105                 oneLineSet.size());
106
107         // test the 'getRelease' method
108
method = new MethodGen(methods[1], clazz.getClassName(), pool);
109         modifier = new MethodModifier(false, "Test*.class", true, false, true, method);
110         processedMethod = modifier.processMethod(pool, true);
111         assertEquals("Checking if has line numbers", true, modifier
112                 .hasLineNumbers());
113         assertEquals("Checking if one-line method", true, modifier.isOneLine());
114         assertEquals("Checking if constructor", false, modifier.isConstructor());
115         assertEquals("Checking size of constructor MethodSet",
116                 oldConstructorSetSize, constructorSet.size());
117         assertEquals("Checking size of one-line MethodSet",
118                 ++oldOneLineSetSize, oneLineSet.size());
119     }
120
121
122     /**
123      * Tests the modifyMethods, hasLineNumbers, isConstructor methods.
124      */

125     public void testUntestableConstructorMethods() {
126
127         String JavaDoc userDir = System.getProperty("jblanket.testdir") + SLASH
128                 + "testmethodmodifier";
129         System.setProperty("jblanket.dir", userDir);
130
131         ConstantPoolGen pool = new ConstantPoolGen(clazz.getConstantPool());
132         MethodCategories categories = MethodCategories.getInstance();
133         MethodSetManager manager = MethodSetManager.getInstance();
134         MethodSet constructorSet = manager.getMethodSet(categories
135                 .getFileName("constructorFile"));
136         MethodSet untestableSet = manager.getMethodSet(categories
137                 .getFileName("untestableFile"));
138         MethodSet oneLineSet = manager.getMethodSet(categories
139                 .getFileName("oneLineFile"));
140
141         int oldConstructorSetSize = constructorSet.size();
142         int oldUntestableSetSize = untestableSet.size();
143         int oldOneLineSetSize = oneLineSet.size();
144
145         // test the constructor '<init>'
146
MethodGen method = new MethodGen(methods[0], clazz.getClassName(), pool);
147         MethodModifier modifier = new MethodModifier(false, "Test*.class",
148                 true, false, true, method);
149         Method processedMethod = modifier.processMethod(pool, true);
150         assertEquals("Checking if has line numbers", true, modifier
151                 .hasLineNumbers());
152         assertEquals("Checking if one-line method", false, modifier.isOneLine());
153         assertEquals("Checking if constructor", true, modifier.isConstructor());
154         assertEquals("Checking size of constructor MethodSet",
155                 oldConstructorSetSize, constructorSet.size());
156         assertEquals("Checking size of untestable MethodSet",
157                 oldUntestableSetSize+1, untestableSet.size());
158         assertEquals("Checking size of one-line MethodSet", oldOneLineSetSize,
159                 oneLineSet.size());
160
161         // test the 'getRelease' method
162
method = new MethodGen(methods[1], clazz.getClassName(), pool);
163         modifier = new MethodModifier(false, "Test*.class", true, false, true, method);
164         processedMethod = modifier.processMethod(pool, true);
165         assertEquals("Checking if has line numbers", true, modifier
166                 .hasLineNumbers());
167         assertEquals("Checking if one-line method", true, modifier.isOneLine());
168         assertEquals("Checking if constructor", false, modifier.isConstructor());
169         assertEquals("Checking size of constructor MethodSet",
170                 oldConstructorSetSize, constructorSet.size());
171         assertEquals("Checking size of one-line MethodSet",
172                 ++oldOneLineSetSize, oneLineSet.size());
173     }
174
175     /**
176      * Tests the excludeMethod method.
177      */

178     public void testExcludeMethod() {
179
180         MethodSet excludeSet = new MethodSet();
181         ConstantPoolGen pool = new ConstantPoolGen(clazz.getConstantPool());
182
183         // get the constructor '<init>'
184
MethodGen method = new MethodGen(methods[0], clazz.getClassName(), pool);
185         MethodModifier modifier = new MethodModifier(false, "Test*.class",
186                 true, false, true, method);
187         modifier.excludeMethod(excludeSet);
188         assertEquals("Checking size of exclude MethodSet", 1, excludeSet.size());
189         MethodInfo methodInfo = (MethodInfo) excludeSet.iterator().next();
190         assertEquals("Checking the name of constructor",
191                 "edu.hawaii.stack.Debug", methodInfo.getClassName());
192     }
193
194     /**
195      * Test methods created by a process like AspectJ or BCEL that don't have
196      * line numbers. See JBL-5.
197      */

198     public void testGeneratedMethods() {
199         // Remove default constructor from regular test class
200
ClassGen classGen = new ClassGen(clazz);
201         classGen.removeMethod(methods[0]);
202         classGen.addEmptyConstructor(Constants.ACC_PUBLIC);
203
204         // Use our modified test class to test against
205
clazz = classGen.getJavaClass();
206         methods = clazz.getMethods();
207         for (int i = 0; i < methods.length; i++) {
208             Method method = methods[i];
209             System.out.println(method.getName() + ": "
210                     + getLineNumberCount(method));
211         }
212
213         String JavaDoc dataDir = System.getProperty("jblanket.data.dir") + SLASH
214                 + "xml";
215         assertNotNull("Checking for presence of jblanket.data.dir.", dataDir);
216         System.setProperty("jblanket.dir", dataDir);
217
218         ConstantPoolGen pool = new ConstantPoolGen(clazz.getConstantPool());
219         MethodCategories categories = MethodCategories.getInstance();
220         MethodSetManager manager = MethodSetManager.getInstance();
221         MethodSet constructorSet = manager.getMethodSet(categories
222                 .getFileName("constructorFile"));
223         MethodSet oneLineSet = manager.getMethodSet(categories
224                 .getFileName("oneLineFile"));
225
226         int oldConstructorSetSize = constructorSet.size();
227         int oldOneLineSetSize = oneLineSet.size();
228
229         // test the constructor '<init>'
230
MethodGen constructor = new MethodGen(methods[methods.length-1], clazz.getClassName(),
231                 pool);
232         MethodModifier modifier = new MethodModifier(false, "Test*.class",
233                 true, true, true, constructor);
234         Method processedMethod = modifier.processMethod(pool, true);
235         assertEquals("Checking if has line numbers", false, modifier
236                 .hasLineNumbers());
237         assertEquals("Checking if one-line method", false, modifier.isOneLine());
238         assertEquals("Checking if constructor", true, modifier.isConstructor());
239         assertEquals("Checking size of constructor MethodSet",
240                 oldConstructorSetSize, constructorSet.size());
241         assertEquals("Checking size of one-line MethodSet", oldOneLineSetSize,
242                 oneLineSet.size());
243     }
244
245     /**
246      * @param method
247      * @return
248      */

249     private int getLineNumberCount(Method method) {
250         LineNumberTable lineNumberTable = method.getLineNumberTable();
251         return lineNumberTable == null ? 0 : lineNumberTable.getLength();
252     }
253 }
254
Popular Tags