KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > alt > jiapi > reflect > InstructionListTest


1 /*
2  * Copyright(C) 2002 Mika Riekkinen, Joni Suominen
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or(at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package alt.jiapi.reflect;
20
21 import java.util.Iterator JavaDoc;
22 import junit.framework.TestCase;
23
24 import alt.jiapi.reflect.instruction.Opcodes;
25
26 /**
27  * A JUnit test case for InstructionList.
28  * NOTE! By default this tests BCEL provider. If another provider
29  * needs to be tested a system property alt.jiapi.provider.factory
30  * can be used.
31  * <p>
32  * The test first creates two object representations for class
33  * <code>java.net.URLClassLoader</code>. NOTE! Almost any class
34  * would be feasible to be used for testing. The reason this is chosen
35  * is that it is reasonable complicated and is certainly in a CLASSPATH.
36  * <p>
37  * The test then creates a few test cases.
38  *
39  * @author Mika Riekkinen
40  * @author Joni Suominen
41  * @version $Revision: 1.3 $ $Date: 2004/04/04 09:59:22 $
42  */

43 public class InstructionListTest extends TestCase {
44     private JiapiClass clazz1;
45     private JiapiClass clazz2;
46
47     public String JavaDoc getName() {
48         return "InstructionListTest: " + super.getName();
49     }
50
51     public InstructionListTest(String JavaDoc name) {
52         super(name);
53     }
54
55     protected void setUp() {
56         try {
57             Loader loader = new Loader();
58             clazz1 = loader.loadClass("java.net.URLClassLoader");
59             clazz2 = loader.loadClass("java.net.URLClassLoader");
60         } catch (Exception JavaDoc e) {
61             e.printStackTrace();
62         }
63     }
64
65     /**
66      */

67     public void testEquals() {
68         JiapiMethod[] methods = clazz1.getDeclaredMethods();
69         for (int i = 0; i < methods.length; i++) {
70             InstructionList l1 = methods[i].getInstructionList();
71             InstructionList l2 = methods[i].getInstructionList();
72
73             assertEquals(l1, l2);
74         }
75     }
76
77 // public void testCopy() throws Exception {
78
// JiapiMethod[] methods = clazz1.getDeclaredMethods();
79
// for (int i = 0; i < methods.length; i++) {
80
// InstructionList orig = methods[i].getInstructionList();
81
// JiapiMethod copyMethod =
82
// clazz1.addMethod(1, "hslfhsfhsjfh" + i,
83
// new Signature("void", new String[0]));
84
// InstructionList copy = copyMethod.getInstructionList();
85
// copy.add(orig);
86

87 // assertEquals(orig, copy);
88
// }
89
// }
90

91     public void testAdd() {
92         JiapiMethod[] methods1 = clazz1.getDeclaredMethods();
93         InstructionList il1 = methods1[0].getInstructionList();
94         JiapiMethod[] methods2 = clazz2.getDeclaredMethods();
95         InstructionList il2 = methods2[0].getInstructionList();
96         //assertEquals(il1, il2);
97
il1.add(methods1[0].getInstructionFactory().pushConstant("sdgdfh"));
98         assertTrue(il1.size() > il2.size());
99     }
100
101     /**
102      * NOTE! This test depends on existence of ClassLoader.findClass method.
103      */

104     public void testView() throws Exception JavaDoc {
105         JiapiMethod m =
106             clazz1.getDeclaredMethod("findClass",
107                                      new String JavaDoc[] { "java.lang.String" });
108         assertNotNull(m);
109         InstructionList il = m.getInstructionList();
110
111         int size = il.size();
112
113         // Then make two views which divide the original:
114
// original = view1 + view2
115
int div = size / 2;
116         InstructionList view1 = il.createView(0, div);
117         InstructionList view2 = il.createView(div, size);
118         
119         // Then make a third view which overlaps with view1 and view2.
120
InstructionList view3 = il.createView(div - 3, div + 3);
121         
122         // Then create two views from the existing views. Note that
123
// these views should equal.
124
InstructionList view4 = view1.createView(div - 3, view1.size());
125         InstructionList view5 = view3.createView(0, 3);
126
127
128         // Test exceptions.
129
try {
130             InstructionList foo = il.createView(0, size + 1);
131             fail("should throw an IndexOutOfBoundsException: " + (size + 1) +
132                  " > " + size);
133         } catch (IndexOutOfBoundsException JavaDoc ioobe) {
134         }
135
136         // Test size() method.
137
assertEquals(div, view1.size());
138         assertEquals(size - div, view2.size());
139         assertEquals(6, view3.size());
140         assertEquals(3, view5.size());
141         assertEquals(view4.size(), view5.size());
142
143         // Test equals.
144
// assertEquals(view4, view5);
145
assertTrue(!(il.equals(view1)));
146         assertTrue(!(view1.equals(view3)));
147
148         // Create a new empty InstructionList and verify
149
// view1 + view2 = original
150
// JiapiMethod copyMethod =
151
// clazz1.addMethod(1, "ksdfksdfm",
152
// new Signature("void", new String[0]));
153
// InstructionList copy = copyMethod.getInstructionList();
154
// copy.add(view1);
155
// copy.add(view2);
156
// assertEquals(il, copy);
157

158         // Test additions.
159
InstructionFactory factory = m.getInstructionFactory();
160         int size1 = il.size();
161         int size2 = view2.size();
162         // Test when adding to the start of a view:
163
view2.insert(0, factory.pushNull());
164         assertEquals(size1 + 1, il.size());
165         assertEquals(size2 + 1, view2.size());
166         // Test when adding to the end of a view:
167
view2.add(factory.pushNull());
168         assertEquals(size1 + 2, il.size());
169         assertEquals(size2 + 2, view2.size());
170
171         // Test removes.
172
// Test remove from the start of a list:
173
// view2.remove(0);
174
// assertEquals(size1 + 1, il.size());
175
// assertEquals(size2 + 1, view2.size());
176
// // Test remove from the end of a list:
177
// view2.remove(view2.size() - 1);
178
// assertEquals(size1, il.size());
179
// assertEquals(size2, view2.size());
180
}
181
182     // Compares search result of original list and its view.
183
public void testOpcodeSearch() {
184         int indexTestCount = 0;
185         int lastIndexTestCount = 0;
186
187         JiapiMethod[] methods = clazz1.getDeclaredMethods();
188         for (int i = 0; i < methods.length; i++) {
189             InstructionList il = methods[i].getInstructionList();
190             InstructionList view0 = il.createView(0); // same list
191
InstructionList view1 = il.createView(1); // offset + 1
192
InstructionList view2 = il.createView(0, il.size() - 1); //size - 1
193

194             int index = il.indexOf(Opcodes.INVOKEVIRTUAL);
195             int index0 = view0.indexOf(Opcodes.INVOKEVIRTUAL);
196             assertEquals(index, index0);
197
198             int index1 = view1.indexOf(Opcodes.INVOKEVIRTUAL);
199             int index2 = view2.indexOf(Opcodes.INVOKEVIRTUAL);
200
201             if (index > 0) {
202                 // Test, if Instructions at index is correct
203
assertEquals(Opcodes.INVOKEVIRTUAL,
204                              il.get(index).getOpcode());
205                 assertEquals(Opcodes.INVOKEVIRTUAL,
206                              view1.get(index1).getOpcode());
207
208                 // Test, if view produces correct index
209
assertEquals(index, index1);
210                 indexTestCount++;
211             }
212         }
213
214         // Make sure at least one assertion is done above
215
assertTrue(indexTestCount > 0);
216     }
217
218
219     // Test replacement of instructions
220
public void testReplace() {
221         JiapiMethod[] methods = clazz1.getDeclaredMethods();
222         JiapiMethod m = methods[0];
223
224         InstructionList il = m.getInstructionList();
225         InstructionList r = il.createEmptyList();
226         InstructionFactory f = il.getInstructionFactory();
227
228         if (il.stackUsage() == 0) {
229             r.add(f.pushNull()); // Make sure stack usages differ
230
}
231
232         try {
233             // Stack usages must match on replace
234
il.replace(0, il.size(), r);
235             fail("Should have thrown IllegalArgumentException");
236         }
237         catch(IllegalArgumentException JavaDoc iae) {
238         }
239
240         // Replace r with r. Stack usages are same.
241
r.replace(0, r.size(), r);
242     }
243
244
245     public void testStackUsage() {
246         JiapiMethod[] methods = clazz1.getDeclaredMethods();
247         for (int i = 0; i < methods.length; i++) {
248             InstructionList il = methods[i].getInstructionList();
249             if (il != null) {
250                 System.out.println(il.stackUsage());
251                 //assertEquals(0, il.stackUsage());
252
}
253         }
254     }
255 }
256
257
Popular Tags