001: package org.hanseltest;
002:
003: import java.io.InputStream;
004:
005: import junit.framework.TestCase;
006:
007: import org.hansel.HanselFrame;
008: import org.hansel.HanselInterpreter;
009: import org.objectweb.asm.ClassReader;
010: import org.objectweb.asm.tree.ClassNode;
011: import org.objectweb.asm.tree.MethodNode;
012: import org.objectweb.asm.tree.analysis.Analyzer;
013: import org.objectweb.asm.tree.analysis.AnalyzerException;
014: import org.objectweb.asm.tree.analysis.Frame;
015:
016: /**
017: * This is a combined testcase for bugs 755796, 755797, 755768, 755798
018: * and RFE 744114.
019: */
020: public class TestStackBugs extends TestCase {
021:
022: private ClassNode classNode;
023:
024: public TestStackBugs(String name) {
025: super (name);
026: }
027:
028: public void setUp() throws Exception {
029: InputStream resourceAsStream = getClass().getResourceAsStream(
030: "TestStackBugs$Example.class");
031: ClassReader cr = new ClassReader(resourceAsStream);
032:
033: classNode = new ClassNode();
034: cr.accept(classNode, 0);
035: }
036:
037: private String getResultToString(String methodName, int relIndex)
038: throws AnalyzerException {
039: for (Object method : classNode.methods) {
040: MethodNode methodNode = (MethodNode) method;
041:
042: if (methodNode.name.equals(methodName)) {
043: Analyzer analyzer = new Analyzer(new HanselInterpreter(
044: methodNode.localVariables)) {
045: protected Frame newFrame(final int nLocals,
046: final int nStack) {
047: return new HanselFrame(nLocals, nStack);
048: }
049:
050: protected Frame newFrame(final Frame src) {
051: return new HanselFrame((HanselFrame) src);
052: }
053: };
054:
055: Frame[] frames = analyzer.analyze(classNode.name,
056: methodNode);
057: HanselFrame[] hanselFrames = new HanselFrame[frames.length];
058: for (int i = 0; i < frames.length; i++) {
059: hanselFrames[i] = (HanselFrame) frames[i];
060: }
061:
062: for (int i = hanselFrames.length - 2; i > 0; i--) {
063: if (hanselFrames[i].getStackSize() > 0) {
064: return hanselFrames[i].pop().toString();
065: }
066:
067: }
068: throw new IllegalStateException("Didn't find result: "
069: + methodName);
070: }
071: }
072:
073: throw new IllegalArgumentException("Unknown method: "
074: + methodName);
075: }
076:
077: public void testBug755796() throws AnalyzerException {
078: assertEquals("\"testString\"",
079: getResultToString("bug755796", 0));
080: }
081:
082: public void testBug755797() throws AnalyzerException {
083: assertEquals("!(a | b)", getResultToString("bug755797", 1));
084: assertEquals("!a", getResultToString("bug755797a", 1));
085: assertEquals("!(a & b)", getResultToString("bug755797b", 1));
086: assertEquals("!(a && b)", getResultToString("bug755797c", 1));
087: assertEquals("!(a || b)", getResultToString("bug755797d", 1));
088: assertEquals("a | b", getResultToString("bug755797e", 1));
089: assertEquals("a", getResultToString("bug755797f", 1));
090: assertEquals("a & b", getResultToString("bug755797g", 1));
091: assertEquals("a && b", getResultToString("bug755797h", 1));
092: assertEquals("a || b", getResultToString("bug755797i", 1));
093: }
094:
095: public void testBug755768() throws AnalyzerException {
096: assertEquals("obj1 != obj2", getResultToString("bug755768", 1));
097: assertEquals("obj1 == obj2", getResultToString("bug755768a", 1));
098: assertEquals("a >= b", getResultToString("bug755768b", 1));
099: assertEquals("a <= b", getResultToString("bug755768c", 1));
100: assertEquals("a > b", getResultToString("bug755768d", 1));
101: assertEquals("a < b", getResultToString("bug755768e", 1));
102: }
103:
104: public void testBug755798() throws AnalyzerException {
105: assertEquals(
106: "c1.getName().length() == 123 == c2.getName().length() == 234",
107: getResultToString("bug755798", 1));
108: }
109:
110: public void testRFE744114() throws AnalyzerException {
111: assertEquals("this.testMethod(true)", getResultToString(
112: "rfe744114", 1));
113: assertEquals("this.testMethod(false)", getResultToString(
114: "rfe744114a", 1));
115: }
116:
117: public static class Example {
118: private boolean boolVar;
119:
120: public String bug755796() {
121: /* Code:
122: * 0: ldc #2; //String testString
123: * 2: areturn
124: */
125: return "testString";
126: }
127:
128: public void bug755797(boolean a, boolean b) {
129: boolVar = !(a | b);
130: }
131:
132: public void bug755797a(boolean a) {
133: boolVar = !a;
134: }
135:
136: public void bug755797b(boolean a, boolean b) {
137: boolVar = !(a & b);
138: }
139:
140: public void bug755797c(boolean a, boolean b) {
141: boolVar = !(a && b);
142: }
143:
144: public void bug755797c1(boolean a, boolean b) {
145: boolVar = !a || (a && !b);
146: }
147:
148: public void bug755797d(boolean a, boolean b) {
149: boolVar = !(a || b);
150: }
151:
152: public void bug755797e(boolean a, boolean b) {
153: boolVar = (a | b);
154: }
155:
156: public void bug755797f(boolean a) {
157: boolVar = a;
158: }
159:
160: public void bug755797g(boolean a, boolean b) {
161: boolVar = (a & b);
162: }
163:
164: public void bug755797h(boolean a, boolean b) {
165: boolVar = (a && b);
166: }
167:
168: public void bug755797i(boolean a, boolean b) {
169: boolVar = (a || b);
170: }
171:
172: public void bug755768(Object obj1, Object obj2) {
173: boolVar = obj1 != obj2;
174: }
175:
176: public void bug755768a(Object obj1, Object obj2) {
177: boolVar = obj1 == obj2;
178: }
179:
180: public void bug755768b(int a, int b) {
181: boolVar = a >= b;
182: }
183:
184: public void bug755768c(int a, int b) {
185: boolVar = a <= b;
186: }
187:
188: public void bug755768d(int a, int b) {
189: boolVar = a > b;
190: }
191:
192: public void bug755768e(int a, int b) {
193: boolVar = a < b;
194: }
195:
196: public void bug755798(Class c1, Class c2) {
197: boolVar = (c1.getName().length() == 123) == (c2.getName()
198: .length() == 234);
199: }
200:
201: private boolean testMethod(boolean param) {
202: return param;
203: }
204:
205: public void rfe744114() {
206: boolVar = testMethod(true);
207: }
208:
209: public void rfe744114a() {
210: boolVar = testMethod(false);
211: }
212:
213: public boolean getResult() {
214: return boolVar;
215: }
216: }
217:
218: }
|