KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hanseltest > TestStackBugs


1 package org.hanseltest;
2
3
4 import java.io.InputStream JavaDoc;
5
6 import junit.framework.TestCase;
7
8 import org.hansel.HanselFrame;
9 import org.hansel.HanselInterpreter;
10 import org.objectweb.asm.ClassReader;
11 import org.objectweb.asm.tree.ClassNode;
12 import org.objectweb.asm.tree.MethodNode;
13 import org.objectweb.asm.tree.analysis.Analyzer;
14 import org.objectweb.asm.tree.analysis.AnalyzerException;
15 import org.objectweb.asm.tree.analysis.Frame;
16
17
18 /**
19  * This is a combined testcase for bugs 755796, 755797, 755768, 755798
20  * and RFE 744114.
21  */

22 public class TestStackBugs extends TestCase {
23
24     private ClassNode classNode;
25     
26     public TestStackBugs(String JavaDoc name) {
27         super(name);
28     }
29
30     public void setUp() throws Exception JavaDoc {
31         InputStream JavaDoc resourceAsStream = getClass().getResourceAsStream("TestStackBugs$Example.class");
32         ClassReader cr = new ClassReader(resourceAsStream);
33         
34         classNode = new ClassNode();
35         cr.accept(classNode, 0);
36     }
37
38     private String JavaDoc getResultToString(String JavaDoc methodName,
39                                      int relIndex) throws AnalyzerException {
40         for (Object JavaDoc method : classNode.methods) {
41             MethodNode methodNode = (MethodNode) method;
42             
43             if (methodNode.name.equals(methodName)) {
44                 Analyzer analyzer =
45                     new Analyzer(new HanselInterpreter(methodNode.localVariables)) {
46                     protected Frame newFrame (final int nLocals, final int nStack) {
47                         return new HanselFrame(nLocals, nStack);
48                     }
49
50                     protected Frame newFrame (final Frame src) {
51                         return new HanselFrame((HanselFrame) src);
52                     }
53                 };
54
55                 Frame[] frames = analyzer.analyze(classNode.name, methodNode);
56                 HanselFrame[] hanselFrames = new HanselFrame[frames.length];
57                 for (int i=0; i<frames.length; i++) {
58                     hanselFrames[i] = (HanselFrame) frames[i];
59                 }
60               
61                 for (int i=hanselFrames.length - 2; i>0; i--) {
62                     if (hanselFrames[i].getStackSize() > 0) {
63                         return hanselFrames[i].pop().toString();
64                     }
65                     
66                 }
67                 throw new IllegalStateException JavaDoc("Didn't find result: " + methodName);
68             }
69         }
70
71         throw new IllegalArgumentException JavaDoc("Unknown method: " + methodName);
72     }
73
74     public void testBug755796() throws AnalyzerException {
75         assertEquals("\"testString\"", getResultToString("bug755796", 0));
76     }
77
78     public void testBug755797() throws AnalyzerException {
79         assertEquals("!(a | b)", getResultToString("bug755797", 1));
80         assertEquals("!a", getResultToString("bug755797a", 1));
81         assertEquals("!(a & b)", getResultToString("bug755797b", 1));
82         assertEquals("!(a && b)", getResultToString("bug755797c", 1));
83         assertEquals("!(a || b)", getResultToString("bug755797d", 1));
84         assertEquals("a | b", getResultToString("bug755797e", 1));
85         assertEquals("a", getResultToString("bug755797f", 1));
86         assertEquals("a & b", getResultToString("bug755797g", 1));
87         assertEquals("a && b", getResultToString("bug755797h", 1));
88         assertEquals("a || b", getResultToString("bug755797i", 1));
89     }
90     
91     public void testBug755768() throws AnalyzerException {
92         assertEquals("obj1 != obj2", getResultToString("bug755768", 1));
93         assertEquals("obj1 == obj2", getResultToString("bug755768a", 1));
94         assertEquals("a >= b", getResultToString("bug755768b", 1));
95         assertEquals("a <= b", getResultToString("bug755768c", 1));
96         assertEquals("a > b", getResultToString("bug755768d", 1));
97         assertEquals("a < b", getResultToString("bug755768e", 1));
98     }
99
100     public void testBug755798() throws AnalyzerException {
101        assertEquals("c1.getName().length() == 123 == c2.getName().length() == 234",
102                      getResultToString("bug755798", 1));
103     }
104
105     public void testRFE744114() throws AnalyzerException {
106         assertEquals("this.testMethod(true)",
107                      getResultToString("rfe744114", 1));
108         assertEquals("this.testMethod(false)",
109                      getResultToString("rfe744114a", 1));
110     }
111
112     public static class Example {
113         private boolean boolVar;
114
115         public String JavaDoc bug755796() {
116             /* Code:
117              * 0: ldc #2; //String testString
118              * 2: areturn
119              */

120             return "testString";
121         }
122
123         public void bug755797(boolean a, boolean b) {
124             boolVar = !(a | b);
125         }
126
127         public void bug755797a(boolean a) {
128             boolVar = !a;
129         }
130
131         public void bug755797b(boolean a, boolean b) {
132             boolVar = !(a & b);
133         }
134
135         public void bug755797c(boolean a, boolean b) {
136             boolVar = !(a && b);
137         }
138
139         public void bug755797c1(boolean a, boolean b) {
140             boolVar = !a || (a && !b);
141         }
142
143         public void bug755797d(boolean a, boolean b) {
144             boolVar = !(a || b);
145         }
146
147         public void bug755797e(boolean a, boolean b) {
148             boolVar = (a | b);
149         }
150
151         public void bug755797f(boolean a) {
152             boolVar = a;
153         }
154
155         public void bug755797g(boolean a, boolean b) {
156             boolVar = (a & b);
157         }
158
159         public void bug755797h(boolean a, boolean b) {
160             boolVar = (a && b);
161         }
162
163         public void bug755797i(boolean a, boolean b) {
164             boolVar = (a || b);
165         }
166
167         public void bug755768(Object JavaDoc obj1, Object JavaDoc obj2) {
168             boolVar = obj1 != obj2;
169         }
170
171         public void bug755768a(Object JavaDoc obj1, Object JavaDoc obj2) {
172             boolVar = obj1 == obj2;
173         }
174
175         public void bug755768b(int a, int b) {
176             boolVar = a >= b;
177         }
178
179         public void bug755768c(int a, int b) {
180             boolVar = a <= b;
181         }
182
183         public void bug755768d(int a, int b) {
184             boolVar = a > b;
185         }
186
187         public void bug755768e(int a, int b) {
188             boolVar = a < b;
189         }
190
191         public void bug755798(Class JavaDoc c1, Class JavaDoc c2) {
192             boolVar = (c1.getName().length() == 123) == (c2.getName ().length() == 234);
193         }
194
195         private boolean testMethod(boolean param) {
196             return param;
197         }
198
199         public void rfe744114() {
200             boolVar = testMethod(true);
201         }
202
203         public void rfe744114a() {
204             boolVar = testMethod(false);
205         }
206         
207         public boolean getResult() {
208             return boolVar;
209         }
210     }
211  
212 }
213
Popular Tags