KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > debugger > jpda > EvaluationTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.debugger.jpda;
21
22 import org.netbeans.api.debugger.DebuggerManager;
23 import org.netbeans.junit.NbTestCase;
24
25 /**
26  * Tests evaluation of various expressions.
27  *
28  * @author Maros Sandor, Jan Jancura
29  */

30 public class EvaluationTest extends NbTestCase {
31
32     private JPDASupport support;
33
34
35     public EvaluationTest (String JavaDoc s) {
36         super (s);
37     }
38
39     protected void setUp () throws Exception JavaDoc {
40         super.setUp ();
41         JPDASupport.removeAllBreakpoints ();
42         LineBreakpoint lb = LineBreakpoint.create (
43                 Utils.getURL(System.getProperty ("test.dir.src")+
44                              "org/netbeans/api/debugger/jpda/testapps/EvalApp.java"),
45                 34
46             );
47         DebuggerManager.getDebuggerManager ().addBreakpoint (lb);
48         support = JPDASupport.attach (
49             "org.netbeans.api.debugger.jpda.testapps.EvalApp"
50         );
51         support.waitState (JPDADebugger.STATE_STOPPED);
52     }
53
54     public void testStaticEvaluation () throws Exception JavaDoc {
55         try {
56             checkEval ("1", 1);
57             checkEval ("4.3", 4.3);
58             checkEval ("ix", 74);
59
60             checkEvalFails ("this");
61             checkEvalFails ("NoSuchClass.class");
62         } finally {
63             support.doFinish ();
64         }
65     }
66
67     public void testStaticExpressions () throws Exception JavaDoc {
68         try {
69             checkEval ("ix * fx", 740.0f);
70             checkEval ("sx % 3", 1);
71
72             checkEvalFails ("ix * fx ** fx");
73         } finally {
74             support.doFinish ();
75         }
76     }
77
78     private void checkEval (String JavaDoc expression, int value) {
79         try {
80             Variable var = support.getDebugger ().evaluate (expression);
81             assertEquals (
82                 "Evaluation of expression failed (wrong value): " + expression,
83                 value,
84                 Integer.parseInt (var.getValue ()), 0
85             );
86             assertEquals (
87                 "Evaluation of expression failed (wrong type of result): " +
88                     expression,
89                 "int",
90                 var.getType ()
91             );
92         } catch (InvalidExpressionException e) {
93             fail (
94                 "Evaluation of expression was unsuccessful: " + e
95             );
96         }
97     }
98
99     private void checkEval(String JavaDoc expression, float value) {
100         try {
101             Variable var = support.getDebugger ().evaluate (expression);
102             assertEquals (
103                 "Evaluation of expression failed (wrong value): " + expression,
104                 value,
105                 Float.parseFloat (var.getValue ()),
106                 0
107             );
108             assertEquals (
109                 "Evaluation of expression failed (wrong type of result): " +
110                     expression,
111                 "float",
112                 var.getType ()
113             );
114         } catch (InvalidExpressionException e) {
115             fail (
116                 "Evaluation of expression was unsuccessful: " + e
117             );
118         }
119     }
120
121     private void checkEval (String JavaDoc expression, double value) {
122         try {
123             Variable var = support.getDebugger ().evaluate (expression);
124             assertEquals (
125                 "Evaluation of expression failed (wrong value): " + expression,
126                 value,
127                 Double.parseDouble (var.getValue ()),
128                 0
129             );
130             assertEquals (
131                 "Evaluation of expression failed (wrong type of result): " +
132                     expression,
133                 "double",
134                 var.getType ()
135             );
136         } catch (InvalidExpressionException e) {
137             fail (
138                 "Evaluation of expression was unsuccessful: " + e
139             );
140         }
141     }
142
143     private void checkEval (String JavaDoc expression, String JavaDoc type, String JavaDoc value) {
144         try {
145             Variable var = support.getDebugger ().evaluate (expression);
146             assertEquals (
147                 "Evaluation of expression failed (wrong value): " + expression,
148                 value,
149                 var.getValue ()
150             );
151             assertEquals (
152                 "Evaluation of expression failed (wrong type of result): " +
153                     expression,
154                 type,
155                 var.getType ()
156             );
157         } catch (InvalidExpressionException e) {
158             fail (
159                 "Evaluation of expression was unsuccessful: " + e
160             );
161         }
162     }
163
164     private void checkEvalFails (String JavaDoc expression) {
165         try {
166             Variable var = support.getDebugger ().evaluate (expression);
167             fail (
168                 "Evaluation of expression was unexpectedly successful: " +
169                 expression + " = " + var.getValue ()
170             );
171         } catch (InvalidExpressionException e) {
172             // its ok
173
return;
174         }
175     }
176 }
177
Popular Tags