KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
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.2 $ $Date: 2004/04/04 09:59:22 $
42  */

43 public class InstructionTest extends TestCase {
44     private JiapiClass clazz1;
45
46     public String JavaDoc getName() {
47         return "InstructionTest: " + super.getName();
48     }
49
50     public InstructionTest(String JavaDoc name) {
51         super(name);
52     }
53
54     protected void setUp() {
55         try {
56             Loader loader = new Loader();
57             clazz1 = loader.loadClass("alt.jiapi.reflect.InstructionTest");
58         }
59         catch (Exception JavaDoc e) {
60             e.printStackTrace();
61         }
62     }
63
64     public void testStackUsage() {
65         JiapiMethod jm = null;
66
67         try {
68             jm = clazz1.getDeclaredMethod("t1", new String JavaDoc[0]);
69         }
70         catch(Exception JavaDoc e) {
71             fail("Could not get JiapiMethod for testing");
72         }
73
74         InstructionList il = jm.getInstructionList();
75
76         int idx = il.indexOf(Opcodes.INVOKESTATIC);
77         assertTrue(idx != -1);
78
79         Invocation ins = (Invocation)il.get(idx);
80         assertTrue(ins.stackUsage() == 0);
81
82         // ----------------------------------------------------
83

84         idx = il.indexOf(Opcodes.INVOKEVIRTUAL);
85         assertTrue(idx != -1);
86
87         ins = (Invocation)il.get(idx);
88         assertTrue(ins.stackUsage() == -1);
89     }
90
91
92     public void t1() {
93         InstructionTest.bar1();
94         foo1();
95     }
96
97     public static void bar1() {
98     }
99
100     public void foo1() {
101     }
102
103     public int foo2() {
104         return 1;
105     }
106 }
107
108
Popular Tags