KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gui > debuggercore > LocalVariables


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  * The Original Software is NetBeans.
15  * The Initial Developer of the Original Software is Sun Microsystems, Inc.
16  * Portions created by Sun Microsystems, Inc. are Copyright (C) 2003
17  * All Rights Reserved.
18  *
19  * Contributor(s): Sun Microsystems, Inc.
20  */

21
22 package gui.debuggercore;
23
24 import java.io.File JavaDoc;
25 import junit.textui.TestRunner;
26 import org.netbeans.jellytools.*;
27 import org.netbeans.jellytools.actions.Action;
28 import org.netbeans.jellytools.actions.OpenAction;
29 import org.netbeans.jellytools.modules.debugger.actions.RunToCursorAction;
30 import org.netbeans.jellytools.nodes.Node;
31 import org.netbeans.jellytools.nodes.SourcePackagesNode;
32 import org.netbeans.jemmy.EventTool;
33 import org.netbeans.jemmy.JemmyProperties;
34 import org.netbeans.jemmy.operators.JTableOperator;
35 import org.netbeans.jemmy.util.PNGEncoder;
36 import org.netbeans.junit.NbTestSuite;
37
38 /**
39  *
40  * @author ehucka
41  */

42 public class LocalVariables extends JellyTestCase {
43     
44     static int consoleLineNumber = 0;
45     
46     String JavaDoc projectPropertiesTitle;
47     
48     /**
49      *
50      * @param name
51      */

52     public LocalVariables(String JavaDoc name) {
53         super(name);
54     }
55     
56     /**
57      *
58      * @param args
59      */

60     public static void main(String JavaDoc[] args) {
61         TestRunner.run(suite());
62     }
63     
64     /**
65      *
66      * @return
67      */

68     public static NbTestSuite suite() {
69         NbTestSuite suite = new NbTestSuite();
70         suite.addTest(new LocalVariables("testLocalVariablesThisNode"));
71         suite.addTest(new LocalVariables("testLocalVariablesStaticNode"));
72         suite.addTest(new LocalVariables("testLocalVariablesStaticInherited"));
73         suite.addTest(new LocalVariables("testLocalVariablesInheritedNode"));
74         suite.addTest(new LocalVariables("testLocalVariablesExtended"));
75         suite.addTest(new LocalVariables("testLocalVariablesValues"));
76         suite.addTest(new LocalVariables("testLocalVariablesSubExpressions"));
77         return suite;
78     }
79     
80     /**
81      *
82      */

83     public void setUp() {
84         System.out.println("######## " + getName() + " #######");
85         if (getName().equals("testLocalVariablesThisNode")) {
86             //open source
87
Node beanNode = new Node(new SourcePackagesNode(Utilities.testProjectName), "examples.advanced|MemoryView.java"); //NOI18N
88
new OpenAction().performAPI(beanNode);
89             EditorOperator eo = new EditorOperator("MemoryView.java");
90             Utilities.setCaret(eo, 52);
91             new RunToCursorAction().perform();
92             Utilities.getDebugToolbar().waitComponentVisible(true);
93             consoleLineNumber = Utilities.waitDebuggerConsole("Thread main stopped at MemoryView.java:52.", 0);
94             new EventTool().waitNoEvent(1000);
95             expandNodes();
96         }
97     }
98     
99     /**
100      *
101      */

102     public void tearDown() {
103         JemmyProperties.getCurrentOutput().printTrace("\nteardown\n");
104         if (getName().equals("testLocalVariablesSubExpressions")) {//last
105
Utilities.endAllSessions();
106         }
107     }
108     
109     /**
110      *
111      */

112     protected void expandNodes() {
113         Utilities.showDebuggerView(Utilities.localVarsViewTitle);
114         JTableOperator jTableOperator = new JTableOperator(new TopComponentOperator(Utilities.localVarsViewTitle));
115         TreeTableOperator treeTableOperator = new TreeTableOperator((javax.swing.JTable JavaDoc) jTableOperator.getSource());
116         new org.netbeans.jellytools.nodes.Node(treeTableOperator.tree(), "this").expand();
117         //Utilities.sleep(500);
118
new org.netbeans.jellytools.nodes.Node(treeTableOperator.tree(), "this|Static").expand();
119         //Utilities.sleep(500);
120
new org.netbeans.jellytools.nodes.Node(treeTableOperator.tree(), "this|Inherited").expand();
121         //Utilities.sleep(500);
122
}
123     
124     /**
125      *
126      */

127     public void testLocalVariablesThisNode() throws Throwable JavaDoc {
128         try {
129             JTableOperator jTableOperator = new JTableOperator(new TopComponentOperator(Utilities.localVarsViewTitle));
130             checkTreeTableLine(jTableOperator, 2, "Vpublic", "String", "\"Public Variable\"");
131             checkTreeTableLine(jTableOperator, 3, "Vprotected", "String", "\"Protected Variable\"");
132             checkTreeTableLine(jTableOperator, 4, "Vprivate", "String", "\"Private Variable\"");
133             checkTreeTableLine(jTableOperator, 5, "VpackagePrivate", "String", "\"Package-private Variable\"");
134         } catch (Throwable JavaDoc th) {
135             try {
136                 // capture screen before cleanup in finally clause is completed
137
PNGEncoder.captureScreen(getWorkDir().getAbsolutePath()+File.separator+"screenBeforeCleanup.png");
138             } catch (Exception JavaDoc e1) {
139                 // ignore it
140
}
141             throw th;
142         }
143     }
144     
145     /**
146      *
147      */

148     public void testLocalVariablesStaticNode() throws Throwable JavaDoc {
149         try {
150             JTableOperator jTableOperator = new JTableOperator(new TopComponentOperator(Utilities.localVarsViewTitle));
151             checkTreeTableLine(jTableOperator, 10, "Spublic", "String", "\"Public Variable\"");
152             checkTreeTableLine(jTableOperator, 11, "Sprotected", "String", "\"Protected Variable\"");
153             checkTreeTableLine(jTableOperator, 12, "Sprivate", "String", "\"Private Variable\"");
154             checkTreeTableLine(jTableOperator, 13, "SpackagePrivate", "String", "\"Package-private Variable\"");
155         } catch (Throwable JavaDoc th) {
156             try {
157                 // capture screen before cleanup in finally clause is completed
158
PNGEncoder.captureScreen(getWorkDir().getAbsolutePath()+File.separator+"screenBeforeCleanup.png");
159             } catch (Exception JavaDoc e1) {
160                 // ignore it
161
}
162             throw th;
163         }
164     }
165     
166     /**
167      *
168      */

169     public void testLocalVariablesStaticInherited() throws Throwable JavaDoc {
170         try {
171             JTableOperator jTableOperator = new JTableOperator(new TopComponentOperator(Utilities.localVarsViewTitle));
172             checkTreeTableLine(jTableOperator, 15, "inheritedSpublic", "String", "\"Inherited Public Variable\"");
173             checkTreeTableLine(jTableOperator, 16, "inheritedSprotected", "String", "\"Inherited Protected Variable\"");
174             checkTreeTableLine(jTableOperator, 17, "inheritedSprivate", "String", "\"Inherited Private Variable\"");
175             checkTreeTableLine(jTableOperator, 18, "inheritedSpackagePrivate", "String", "\"Inherited Package-private Variable\"");
176         } catch (Throwable JavaDoc th) {
177             try {
178                 // capture screen before cleanup in finally clause is completed
179
PNGEncoder.captureScreen(getWorkDir().getAbsolutePath()+File.separator+"screenBeforeCleanup.png");
180             } catch (Exception JavaDoc e1) {
181                 // ignore it
182
}
183             throw th;
184         }
185     }
186     
187     /**
188      *
189      */

190     public void testLocalVariablesInheritedNode() throws Throwable JavaDoc {
191         try {
192             JTableOperator jTableOperator = new JTableOperator(new TopComponentOperator(Utilities.localVarsViewTitle));
193             checkTreeTableLine(jTableOperator, 20, "inheritedVpublic", "String", "\"Inherited Public Variable\"");
194             checkTreeTableLine(jTableOperator, 21, "inheritedVprotected", "String", "\"Inherited Protected Variable\"");
195             checkTreeTableLine(jTableOperator, 22, "inheritedVprivate", "String", "\"Inherited Private Variable\"");
196             checkTreeTableLine(jTableOperator, 23, "inheritedVpackagePrivate", "String", "\"Inherited Package-private Variable\"");
197         } catch (Throwable JavaDoc th) {
198             try {
199                 // capture screen before cleanup in finally clause is completed
200
PNGEncoder.captureScreen(getWorkDir().getAbsolutePath()+File.separator+"screenBeforeCleanup.png");
201             } catch (Exception JavaDoc e1) {
202                 // ignore it
203
}
204             throw th;
205         }
206     }
207     
208     /**
209      *
210      */

211     public void testLocalVariablesExtended() throws Throwable JavaDoc {
212         try {
213             EditorOperator eo = new EditorOperator("MemoryView.java");
214             Utilities.setCaret(eo, 76);
215             new RunToCursorAction().perform();
216             consoleLineNumber = Utilities.waitDebuggerConsole("Thread main stopped at MemoryView.java:76.", consoleLineNumber+1);
217             new EventTool().waitNoEvent(1000);
218             
219             Utilities.showDebuggerView(Utilities.localVarsViewTitle);
220             JTableOperator jTableOperator = new JTableOperator(new TopComponentOperator(Utilities.localVarsViewTitle));
221             TreeTableOperator treeTableOperator = new TreeTableOperator((javax.swing.JTable JavaDoc) jTableOperator.getSource());
222             int count = 0;
223             checkTreeTableLine(jTableOperator, count++, "this", "MemoryView", null);
224             checkTreeTableLine(jTableOperator, count++, "timer", null, "null");
225             checkTreeTableLine(jTableOperator, count++, "Vpublic", "String", "\"Public Variable\"");
226             checkTreeTableLine(jTableOperator, count++, "Vprotected", "String", "\"Protected Variable\"");
227             checkTreeTableLine(jTableOperator, count++, "Vprivate", "String", "\"Private Variable\"");
228             checkTreeTableLine(jTableOperator, count++, "VpackagePrivate", "String", "\"Package-private Variable\"");
229             checkTreeTableLine(jTableOperator, count++, "Static", null, null);
230             checkTreeTableLine(jTableOperator, count++, "bundle", "PropertyResourceBundle", null);
231             assertTrue("Node bundle has no child nodes", hasChildNodes("this|Static|bundle", treeTableOperator));
232             checkTreeTableLine(jTableOperator, count++, "msgMemory", "MessageFormat", null);
233             assertTrue("Node msgMemory has no child nodes", hasChildNodes("this|Static|msgMemory", treeTableOperator));
234             checkTreeTableLine(jTableOperator, count++, "UPDATE_TIME", "int", "1000");
235             checkTreeTableLine(jTableOperator, count++, "Spublic", "String", "\"Public Variable\"");
236             checkTreeTableLine(jTableOperator, count++, "Sprotected", "String", "\"Protected Variable\"");
237             checkTreeTableLine(jTableOperator, count++, "Sprivate", "String", "\"Private Variable\"");
238             checkTreeTableLine(jTableOperator, count++, "SpackagePrivate", "String", "\"Package-private Variable\"");
239             checkTreeTableLine(jTableOperator, count++, "class$java$lang$Runtime", "Class", "class java.lang.Runtime");
240             assertTrue("Node class$java$lang$Runtime has no child nodes", hasChildNodes("this|Static|class$java$lang$Runtime", treeTableOperator));
241             checkTreeTableLine(jTableOperator, count++, "inheritedSpublic", "String", "\"Inherited Public Variable\"");
242             checkTreeTableLine(jTableOperator, count++, "inheritedSprotected", "String", "\"Inherited Protected Variable\"");
243             checkTreeTableLine(jTableOperator, count++, "inheritedSprivate", "String", "\"Inherited Private Variable\"");
244             checkTreeTableLine(jTableOperator, count++, "inheritedSpackagePrivate", "String", "\"Inherited Package-private Variable\"");
245             checkTreeTableLine(jTableOperator, count++, "Inherited", null, null);
246             checkTreeTableLine(jTableOperator, count++, "inheritedVpublic", "String", "\"Inherited Public Variable\"");
247             checkTreeTableLine(jTableOperator, count++, "inheritedVprotected", "String", "\"Inherited Protected Variable\"");
248             checkTreeTableLine(jTableOperator, count++, "inheritedVprivate", "String", "\"Inherited Private Variable\"");
249             checkTreeTableLine(jTableOperator, count++, "inheritedVpackagePrivate", "String", "\"Inherited Package-private Variable\"");
250             checkTreeTableLine(jTableOperator, count++, "clazz", "Class", "class java.lang.Runtime");
251             assertTrue("Node clazz has no child nodes", hasChildNodes("clazz", treeTableOperator));
252             checkTreeTableLine(jTableOperator, count++, "string", "String", "\"Hi!\"");
253             checkTreeTableLine(jTableOperator, count++, "n", "int", "50");
254             checkTreeTableLine(jTableOperator, count++, "llist", "LinkedList", null);
255             assertTrue("Node llist has no child nodes", hasChildNodes("llist", treeTableOperator));
256             checkTreeTableLine(jTableOperator, count++, "alist", "ArrayList", null);
257             assertTrue("Node alist has no child nodes", hasChildNodes("alist", treeTableOperator));
258             checkTreeTableLine(jTableOperator, count++, "vec", "Vector", null);
259             assertTrue("Node vec has no child nodes", hasChildNodes("vec", treeTableOperator));
260             checkTreeTableLine(jTableOperator, count++, "hmap", "HashMap", null);
261             assertTrue("Node hmap has no child nodes", hasChildNodes("hmap", treeTableOperator));
262             checkTreeTableLine(jTableOperator, count++, "htab", "Hashtable", null);
263             assertTrue("Node htab has no child nodes", hasChildNodes("htab", treeTableOperator));
264             checkTreeTableLine(jTableOperator, count++, "tmap", "TreeMap", null);
265             assertTrue("Node tmap has no child nodes", hasChildNodes("tmap", treeTableOperator));
266             checkTreeTableLine(jTableOperator, count++, "hset", "HashSet", null);
267             assertTrue("Node hset has no child nodes", hasChildNodes("hset", treeTableOperator));
268             checkTreeTableLine(jTableOperator, count++, "tset", "TreeSet", null);
269             assertTrue("Node tset has no child nodes", hasChildNodes("tset", treeTableOperator));
270             checkTreeTableLine(jTableOperator, count++, "policko", "int[]", null);
271             assertTrue("Node policko has no child nodes", hasChildNodes("policko", treeTableOperator));
272             checkTreeTableLine(jTableOperator, count++, "pole", "int[]", null);
273             assertTrue("Node pole has no child nodes", hasChildNodes("pole", treeTableOperator));
274             checkTreeTableLine(jTableOperator, count++, "d2", "int[][]", null);
275             assertTrue("Node d2 has no child nodes", hasChildNodes("d2", treeTableOperator));
276         } catch (Throwable JavaDoc th) {
277             try {
278                 // capture screen before cleanup in finally clause is completed
279
PNGEncoder.captureScreen(getWorkDir().getAbsolutePath()+File.separator+"screenBeforeCleanup.png");
280             } catch (Exception JavaDoc e1) {
281                 // ignore it
282
}
283             throw th;
284         }
285     }
286     
287     /**
288      *
289      */

290     public void testLocalVariablesValues() throws Throwable JavaDoc {
291         try {
292             Node beanNode = new Node(new SourcePackagesNode(Utilities.testProjectName), "examples.advanced|MemoryView.java"); //NOI18N
293
new OpenAction().performAPI(beanNode);
294             EditorOperator eo = new EditorOperator("MemoryView.java");
295             Utilities.setCaret(eo, 104);
296             new EventTool().waitNoEvent(500);
297             new RunToCursorAction().performMenu();
298             new EventTool().waitNoEvent(500);
299             consoleLineNumber = Utilities.waitDebuggerConsole("Thread main stopped at MemoryView.java:104.", consoleLineNumber+1);
300             
301             Utilities.showDebuggerView(Utilities.localVarsViewTitle);
302             JTableOperator jTableOperator = new JTableOperator(new TopComponentOperator(Utilities.localVarsViewTitle));
303             try {
304                 org.openide.nodes.Node.Property property;
305                 property = (org.openide.nodes.Node.Property)jTableOperator.getValueAt(25, 2);
306                 long free = Long.parseLong(property.getValue().toString());
307                 property = (org.openide.nodes.Node.Property)jTableOperator.getValueAt(26, 2);
308                 long total = Long.parseLong(property.getValue().toString());
309                 property = (org.openide.nodes.Node.Property)jTableOperator.getValueAt(27, 2);
310                 long taken = Long.parseLong(property.getValue().toString());
311                 assertTrue("Local varaibles values does not seem to be correct (total != free + taken) - "+total+" != "+free+" + "+taken, (total == free + taken));
312                 
313             } catch (java.lang.IllegalAccessException JavaDoc e1) {
314                 assertTrue(e1.getMessage(), false);
315             } catch (java.lang.reflect.InvocationTargetException JavaDoc e2) {
316                 assertTrue(e2.getMessage(), false);
317             }
318         } catch (Throwable JavaDoc th) {
319             try {
320                 // capture screen before cleanup in finally clause is completed
321
PNGEncoder.captureScreen(getWorkDir().getAbsolutePath()+File.separator+"screenBeforeCleanup.png");
322             } catch (Exception JavaDoc e1) {
323                 // ignore it
324
}
325             throw th;
326         }
327     }
328     
329     /**
330      *
331      */

332     public void testLocalVariablesSubExpressions() throws Throwable JavaDoc {
333         try {
334             /*Node beanNode = new Node(new SourcePackagesNode(Utilities.testProjectName), "examples.advanced|MemoryView.java"); //NOI18N
335             new OpenAction().performAPI(beanNode);
336             EditorOperator eo = new EditorOperator("MemoryView.java");
337             Utilities.setCaret(eo, 104);
338             new EventTool().waitNoEvent(500);
339             new RunToCursorAction().performMenu();
340             new EventTool().waitNoEvent(500);
341             consoleLineNumber = Utilities.waitDebuggerConsole("Thread main stopped at MemoryView.java:104.", consoleLineNumber+1);
342             */

343             new Action(Utilities.runMenu+"|"+Utilities.stepOverExpresItem, null).perform();
344             new Action(Utilities.runMenu+"|"+Utilities.stepOverExpresItem, null).perform();
345             new Action(Utilities.runMenu+"|"+Utilities.stepOverExpresItem, null).perform();
346             new Action(Utilities.runMenu+"|"+Utilities.stepOverExpresItem, null).perform();
347             new Action(Utilities.runMenu+"|"+Utilities.stepOverExpresItem, null).perform();
348             new Action(Utilities.runMenu+"|"+Utilities.stepOverExpresItem, null).perform();
349             
350             Utilities.showDebuggerView(Utilities.localVarsViewTitle);
351             JTableOperator jTableOperator = new JTableOperator(new TopComponentOperator(Utilities.localVarsViewTitle));
352                 TreeTableOperator treeTableOperator = new TreeTableOperator((javax.swing.JTable JavaDoc) jTableOperator.getSource());
353                 String JavaDoc name = Bundle.getString("org.netbeans.modules.debugger.jpda.ui.models.Bundle", "lastOperationsNode");
354                 org.netbeans.jellytools.nodes.Node nd = new org.netbeans.jellytools.nodes.Node(treeTableOperator.tree(), name);
355                 nd.expand();
356                 String JavaDoc[] children = nd.getChildren();
357                 assertEquals(5, children.length);
358                 assertEquals("Wrong sub-expression node", "return <init>()", children[0]);
359                 assertEquals("Wrong sub-expression node", "return <init>()", children[1]);
360                 assertEquals("Wrong sub-expression node", "return <init>()", children[2]);
361                 assertEquals("Wrong sub-expression node", "return format()", children[3]);
362                 assertEquals("Wrong sub-expression node", "return println()", children[4]);
363
364         } catch (Throwable JavaDoc th) {
365             try {
366                 // capture screen before cleanup in finally clause is completed
367
PNGEncoder.captureScreen(getWorkDir().getAbsolutePath()+File.separator+"screenBeforeCleanup.png");
368             } catch (Exception JavaDoc e1) {
369                 // ignore it
370
}
371             throw th;
372         }
373     }
374     
375     /**
376      * check values in TreeTable line
377      * @param table
378      * @param lineNumber
379      * @param name
380      * @param type
381      * @param value
382      */

383     protected void checkTreeTableLine(JTableOperator table, int lineNumber, String JavaDoc name, String JavaDoc type, String JavaDoc value) {
384         try {
385             table.scrollToCell(lineNumber, 0);
386             org.openide.nodes.Node.Property property;
387             String JavaDoc string = null;
388             assertTrue("Node " + name + " not displayed in Local Variables view", name.equals(table.getValueAt(lineNumber, 0).toString()));
389             property = (org.openide.nodes.Node.Property)table.getValueAt(lineNumber, 1);
390             string = property.getValue().toString();
391             int maxWait = 100;
392             while (string.equals(Utilities.evaluatingPropertyText) && maxWait > 0) {
393                 new EventTool().waitNoEvent(300);
394                 maxWait--;
395             }
396             assertTrue("Node " + name + " has wrong type in Local Variables view (displayed: " + string + ", expected: " + type + ")",
397                     (type == null) || type.equals(string));
398             property = (org.openide.nodes.Node.Property)table.getValueAt(lineNumber, 2);
399             string = property.getValue().toString();
400             maxWait = 100;
401             while (string.equals(Utilities.evaluatingPropertyText) && maxWait > 0) {
402                 new EventTool().waitNoEvent(300);
403                 maxWait--;
404             }
405             assertTrue("Node " + name + " has wrong value in Local Variables view (displayed: " + string + ", expected: " + value + ")",
406                     (type == null) || !type.equals(string));
407         } catch (java.lang.IllegalAccessException JavaDoc e1) {
408             assertTrue(e1.getMessage(), false);
409         } catch (java.lang.reflect.InvocationTargetException JavaDoc e2) {
410             assertTrue(e2.getMessage(), false);
411         }
412     }
413     
414     protected boolean hasChildNodes(String JavaDoc nodePath, TreeTableOperator jTableOperator) {
415         org.netbeans.jellytools.nodes.Node node = new org.netbeans.jellytools.nodes.Node(jTableOperator.tree(), nodePath);
416         node.select();
417         return !node.isLeaf();
418     }
419 }
420
Popular Tags