KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > editor > suites > abbrevs > AbbreviationsAddRemovePerformer


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.test.editor.suites.abbrevs;
21
22 import java.awt.event.KeyEvent JavaDoc;
23 import java.io.File JavaDoc;
24 import org.netbeans.jellytools.*;
25 import org.netbeans.jellytools.EditorOperator;
26 import org.netbeans.jellytools.actions.OpenAction;
27 import org.netbeans.jellytools.modules.editor.Abbreviations;
28 import org.netbeans.jellytools.nodes.Node;
29 import org.netbeans.jemmy.EventTool;
30 import org.netbeans.test.editor.LineDiff;
31
32 /**
33  * Test adding/removing of abbreviation via advanced options panel
34  * @author Jan Lahoda
35  * @author Max Sauer
36  */

37 public class AbbreviationsAddRemovePerformer extends JellyTestCase {
38     
39     /** 'Source Packages' string from j2se project bundle */
40     public static final String JavaDoc SRC_PACKAGES_PATH =
41             Bundle.getString("org.netbeans.modules.java.j2seproject.Bundle",
42             "NAME_src.dir");
43     
44     private static final String JavaDoc editor = "Java Editor";
45     private boolean isInFramework;
46     
47     /** Creates a new instance of AbbreviationsAddRemove */
48     public AbbreviationsAddRemovePerformer(String JavaDoc name) {
49         super(name);
50         isInFramework = false;
51     }
52     
53     public EditorOperator openFile() {
54         Node pn = new ProjectsTabOperator().getProjectRootNode(
55                 "editor_test");
56         pn.select();
57         //Open Test.java from editor_test project
58
Node n = new Node(pn, SRC_PACKAGES_PATH + "|" + "abbrev" + "|" + "Test");
59         n.select();
60         new OpenAction().perform();
61         new EventTool().waitNoEvent(500);
62         
63         return new EditorOperator("Test");
64     }
65     
66     private void checkAbbreviation(String JavaDoc abbreviation) throws Exception JavaDoc {
67         //Open an editor:
68
System.out.println("### Checking abbreviation \"" + abbreviation + "\"");
69         EditorOperator editor = openFile();
70         
71         //This line is reserved for testing. All previous content is destroyed
72
//(and fails test!).
73
editor.setCaretPosition(24, 1);
74         
75         //Write abbreviation:
76
editor.txtEditorPane().typeText(abbreviation);
77         //Expand abbreviation:
78
editor.pushTabKey();
79         //Flush current line to output (ref output!)
80
ref(editor.getText(editor.getLineNumber()));
81         //Delete what we have written:
82
editor.select(editor.getLineNumber());
83         editor.pushKey(KeyEvent.VK_DELETE, 0);
84         editor.pushKey(KeyEvent.VK_S, KeyEvent.CTRL_MASK);
85         editor.closeDiscard();
86     }
87     
88     /**
89      * @param args the command line arguments
90      */

91     public void doTest() throws Exception JavaDoc {
92         log("doTest start");
93         Object JavaDoc backup = Utilities.saveAbbreviationsState();
94         
95         try {
96             //For test testing, remove two testing abbreviations. Remove in final version.
97
Abbreviations.addAbbreviation(editor, "ts", "Thread.dumpStack();");
98             Abbreviations.addAbbreviation(editor, "tst", "Thread.sleep(1000);");
99             
100             checkAbbreviation("ts");
101             checkAbbreviation("tst");
102             
103             //remove "ts"
104
boolean remove0 = Abbreviations.removeAbbreviation(editor, "ts");
105             System.out.println("remove0: " + remove0);
106             assertTrue("Previously added \"ts\" abbrev could not be removed.", remove0);
107             
108             //try to remove "ts" again -- should not be possible
109
boolean remove1 = Abbreviations.removeAbbreviation(editor, "ts");
110             System.out.println("remove1: "+ remove1);
111             assertFalse("Previously removed \"ts\" abbrev should not be aviable.", remove1);
112             
113             checkAbbreviation("ts");
114             checkAbbreviation("tst");
115             System.out.println("remove0: "+Abbreviations.removeAbbreviation(editor, "tst"));
116             System.out.println("remove1: "+Abbreviations.removeAbbreviation(editor, "tst"));
117             checkAbbreviation("ts");
118             checkAbbreviation("tst");
119             
120         } catch (Exception JavaDoc ex) {
121             log("Bug in test: "+ex.getMessage()+" by "+ex.getClass());
122             ex.printStackTrace(getLog());
123         } finally {
124             Utilities.restoreAbbreviationsState(backup);
125             log("doTest finished");
126         }
127     }
128     
129 /* public void ref(String ref) {
130         if (isInFramework) {
131             getRef().println(ref);
132             getRef().flush();
133         } else {
134             System.out.println("TEST_OUTPUT:" + ref);
135         }
136     }
137  
138     public void log(String log) {
139         if (isInFramework) {
140             getLog().println(log);
141             getLog().flush();
142         } else {
143             System.err.println(log);
144         }
145     }*/

146     
147     public void setUp() {
148         isInFramework = true;
149         log("Starting abbreviations add/remove test.");
150         log("Test name=" + getName());
151         try {
152             EditorOperator.closeDiscardAll();
153             log("Closed Welcome screen.");
154         } catch (Exception JavaDoc ex) {
155         }
156     }
157     
158     public void tearDown() throws Exception JavaDoc {
159         log("Finishing abbreviations add/remove test.");
160         assertFile("Output does not match golden file.", getGoldenFile(), new File JavaDoc(getWorkDir(), this.getName() + ".ref"),
161         new File JavaDoc(getWorkDir(), this.getName() + ".diff"), new LineDiff(false));
162         isInFramework = false;
163     }
164     
165     public void testAddRemove() throws Exception JavaDoc {
166         doTest();
167     }
168     
169     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
170         new AbbreviationsAddRemovePerformer("testAddRemove").doTest();
171     }
172     
173 }
174
Popular Tags