KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > alt > jiapi > InstrumentorTest


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;
20
21 import junit.framework.TestCase;
22
23 import alt.jiapi.instrumentor.CombInstrumentor;
24 import alt.jiapi.reflect.instruction.InstructionList;
25 import alt.jiapi.reflect.JiapiClass;
26 import alt.jiapi.reflect.JiapiMethod;
27 import alt.jiapi.reflect.Loader;
28
29 /**
30  * A JUnit test case for Instrumentors, that do not modify bytecode,
31  * but selects parts of it forwards them. This strategy cannot be
32  * applied to all such Instrumentors.
33  *
34  * @author Mika Riekkinen
35  * @author Joni Suominen
36  * @version $Revision: 1.4 $ $Date: 2002/05/03 16:02:45 $
37  */

38 public class InstrumentorTest extends TestCase {
39     JiapiMethod[] methods;
40
41     public InstrumentorTest(String JavaDoc name) {
42         super(name);
43     }
44
45     protected void setUp() {
46         try {
47             Loader loader = new Loader();
48             JiapiClass clazz = loader.loadClass("alt.jiapi.InstrumentorTest");
49             methods = clazz.getDeclaredMethods();
50         } catch (Exception JavaDoc e) {
51             e.printStackTrace();
52         }
53
54     }
55
56     public void testInstrumentors() {
57         combTest0(); // 0x1 : forwards all
58
combTest1(); // 0x2 : block, forward, block, forward, ...
59
}
60
61
62     private void combTest0() {
63         TestCombInstrumentor combInstrumentor =
64             new TestCombInstrumentor(0x1); // pattern 1; forwards all.
65

66         for(int i = 0; i < methods.length; i++) {
67             combInstrumentor.instrument(methods[i].getInstructionList());
68             assertTrue(!combInstrumentor.isBlocked()); // Should forward
69
}
70     }
71     private void combTest1() {
72         TestCombInstrumentor combInstrumentor =
73             new TestCombInstrumentor(0x2); // pattern 10
74

75         for(int i = 0; i < methods.length; i++) {
76             combInstrumentor.instrument(methods[i].getInstructionList());
77             assertTrue(combInstrumentor.isBlocked()); // Should block
78

79             if (i < methods.length - 1) {
80                 combInstrumentor.instrument(methods[i+1].getInstructionList());
81                 assertTrue(!combInstrumentor.isBlocked()); // Should forward
82
}
83         }
84     }
85
86
87     // Used in testing. Grep'n'Split.
88
private void someMethod() {
89         foo();
90         bar();
91     }
92
93     // Couple of empty methods for testing. Comb'n'Null.
94
private void foo() {}
95     private void bar() {}
96     private void baz() {}
97     private void foobar() {}
98     private void foobaz() {}
99
100
101     //
102
// Inner classes to be used by this test
103
//
104
class TestCombInstrumentor extends CombInstrumentor {
105         public TestCombInstrumentor(int i) {
106             super(i);
107         }
108         
109         private boolean forwardCalled = false;
110         
111         public void forward(InstructionList il) {
112             forwardCalled = true;
113         }
114         
115         public boolean isBlocked() {
116             boolean retVal = true;
117             
118             if (forwardCalled) {
119                 retVal = false;
120             }
121             
122             forwardCalled = false;
123             
124             return retVal;
125         }
126     }
127     
128 }
129
130
Popular Tags