KickJava   Java API By Example, From Geeks To Geeks.

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


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 /*
21  * AbbreviationsTest.java
22  *
23  * Created on August 28, 2002, 11:15 AM
24  */

25
26 package org.netbeans.test.editor.suites.abbrevs;
27
28 import java.awt.event.InputEvent JavaDoc;
29 import java.awt.event.KeyEvent JavaDoc;
30 import java.io.File JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Map JavaDoc;
35 import org.netbeans.jellytools.EditorOperator;
36 import org.netbeans.jellytools.JellyTestCase;
37 import org.netbeans.jellytools.modules.editor.Abbreviations;
38 import org.netbeans.modules.editor.options.BaseOptions;
39 import org.netbeans.test.editor.LineDiff;
40 import org.openide.options.SystemOption;
41
42 /**
43  *
44  * @author Jan Lahoda
45  */

46 public abstract class AbbreviationsTest extends JellyTestCase {
47     
48     /** Creates a new instance of AbbreviationsTest */
49     public AbbreviationsTest(String JavaDoc name) {
50         super(name);
51     }
52     
53     public static class Abbreviation {
54         private String JavaDoc name;
55         private String JavaDoc expansion;
56         private String JavaDoc oldName;
57         private String JavaDoc oldExpansion;
58         
59         public Abbreviation(String JavaDoc name, String JavaDoc expansion, String JavaDoc oldName, String JavaDoc oldExpansion) {
60             this.name = name;
61             this.expansion = expansion;
62             this.oldName = oldName;
63             this.oldExpansion = oldExpansion;
64         }
65         
66         public String JavaDoc getName() {
67             return name;
68         }
69         
70         public String JavaDoc getExpansion() {
71             return expansion;
72         }
73         
74         public String JavaDoc getOldName() {
75             return oldName;
76         }
77         
78         public String JavaDoc getOldExpansion() {
79             return expansion;
80         }
81     }
82     
83     public abstract Abbreviation[] getDefaultAbbreviations();
84     public abstract EditorOperator getTestEditor();
85     public abstract void prepareEditor();
86     public abstract void moveCaretIntoComment();
87     public abstract void moveCaretIntoCode();
88     public abstract Abbreviation[] getAbbreviationsToAdd();
89     public abstract Abbreviation[] getAbbreviationsToRemove();
90     public abstract Abbreviation[] getAbbreviationsToModify();
91     public abstract String JavaDoc getEditorName();
92     public abstract void finishEditor();
93     
94     protected void flushResult() {
95         getRef().print(getTestEditor().getText());
96         getRef().flush();
97         try {
98             assertFile("Output does not match golden file.", getGoldenFile(), new File JavaDoc(getWorkDir(), this.getName() + ".ref"),
99             new File JavaDoc(getWorkDir(), this.getName() + ".diff"), new LineDiff(false));
100         } catch (IOException JavaDoc e) {
101             assertTrue("IOException: " + e.getMessage(), false);
102         }
103     }
104     
105     private void useAbbreviation(String JavaDoc abbreviation, boolean expand) {
106         EditorOperator editor = getTestEditor();
107         
108         /*This seemed like a good idea. It works when I run the tests manually
109          *using main method of JavaAbbreviationsTestPerformer, but does not
110          *when I run them automaticaly.
111          */

112         // editor.clickMouse();
113

114         editor.txtEditorPane().typeText(abbreviation);
115         if (expand)
116             editor.typeKey(' ');
117         else
118             editor.typeKey(' ', KeyEvent.SHIFT_MASK);
119         try {
120             Thread.currentThread().sleep(100); //wait for abbrevition
121
} catch (InterruptedException JavaDoc ex) {
122         }
123         editor.txtEditorPane().typeText("CARET_POSITION");
124         editor.pushKey(KeyEvent.VK_END);
125         editor.pushKey(KeyEvent.VK_ENTER);
126     }
127     
128     public void testAbbreviationTest() {
129         log("testAbbreviationTest start");
130         try {
131             Abbreviation[] abbs = getDefaultAbbreviations();
132             
133             prepareEditor();
134             
135             for (int cntr = 0; cntr < abbs.length; cntr++) {
136                 moveCaretIntoCode();
137                 useAbbreviation(abbs[cntr].getName(), true);
138             }
139             
140             log("testAbbreviationTest flush results:");
141             
142             flushResult();
143         } finally {
144             
145             log("testAbbreviationTest closing editor:");
146             
147             finishEditor();
148             
149             log("testAbbreviationTest finished");
150         }
151     }
152     
153     public void testAbbreviationInsideComment() {
154         log("testAbbreviationInsideComment start");
155         try {
156             Abbreviation[] abbs = getDefaultAbbreviations();
157             
158             prepareEditor();
159             
160             for (int cntr = 0; cntr < abbs.length; cntr++) {
161                 moveCaretIntoComment();
162                 useAbbreviation(abbs[cntr].getName(), true);
163             }
164             
165             log("testAbbreviationInsideComment flush results:");
166             
167             flushResult();
168         } finally {
169             log("testAbbreviationInsideComment closing editor:");
170             
171             finishEditor();
172             
173             log("testAbbreviationInsideComment finished");
174         }
175     }
176     
177     public void testAbbreviationWithoutExpansion() {
178         log("testAbbreviationWithoutExpansion start");
179         try {
180             Abbreviation[] abbs = getDefaultAbbreviations();
181             
182             prepareEditor();
183             
184             for (int cntr = 0; cntr < abbs.length; cntr++) {
185                 moveCaretIntoCode();
186                 useAbbreviation(abbs[cntr].getName(), false);
187             }
188             
189             log("testAbbreviationWithoutExpansion flush results:");
190             
191             flushResult();
192         } finally {
193             log("testAbbreviationWithoutExpansion closing editor:");
194             
195             finishEditor();
196             
197             log("testAbbreviationWithoutExpansion finished");
198         }
199     }
200     
201     public void testAbbreviationAdd() {
202         log("testAbbreviationAdd start");
203         Object JavaDoc backup = Utilities.saveAbbreviationsState();
204         
205         try {
206             Abbreviation[] toAdd = getAbbreviationsToAdd();
207             
208             prepareEditor();
209             
210             for (int cntr = 0; cntr < toAdd.length; cntr++) {
211                 Abbreviations.addAbbreviation(getEditorName(), toAdd[cntr].getName(), toAdd[cntr].getExpansion());
212             }
213             
214             for (int cntr = 0; cntr < toAdd.length; cntr++) {
215                 moveCaretIntoCode();
216                 useAbbreviation(toAdd[cntr].getName(), true);
217             }
218             
219             log("testAbbreviationAdd flush results:");
220             
221             flushResult();
222         } finally {
223             log("testAbbreviationAdd closing editor:");
224             
225             finishEditor();
226             
227             log("testAbbreviationAdd restoring abbreviations map:");
228             
229             Utilities.restoreAbbreviationsState(backup);
230             
231             log("testAbbreviationAdd finished");
232         }
233     }
234     
235     public void testAbbreviationChange() {
236         log("testAbbreviationChange start");
237         Object JavaDoc backup = Utilities.saveAbbreviationsState();
238         
239         try {
240             Abbreviation[] toChange = getAbbreviationsToModify();
241             
242             prepareEditor();
243             
244             for (int cntr = 0; cntr < toChange.length; cntr++) {
245                 assertTrue("Editing of abbreviation with original name=\"" + toChange[cntr].getOldName() + "\" failed.",
246                 Abbreviations.editAbbreviation(getEditorName(),
247                 toChange[cntr].getOldName(),
248                 toChange[cntr].getName(),
249                 toChange[cntr].getExpansion()));
250             }
251             
252             for (int cntr = 0; cntr < toChange.length; cntr++) {
253                 moveCaretIntoCode();
254                 //Test whether old abbreviation does NOT work:
255
useAbbreviation(toChange[cntr].getOldName(), true);
256                 //Test whether new abbreviation works:
257
useAbbreviation(toChange[cntr].getName(), true);
258             }
259             
260             log("testAbbreviationChange flush results:");
261             
262             flushResult();
263         } finally {
264             log("testAbbreviationChange closing editor:");
265             
266             finishEditor();
267             
268             log("testAbbreviationChange results abbreviations map:");
269             
270             Utilities.restoreAbbreviationsState(backup);
271             
272             log("testAbbreviationChange finished");
273         }
274     }
275     
276     public void testAbbreviationRemoveCancel() {
277         log("testAbbreviationRemoveCancel start");
278         Object JavaDoc backup = Utilities.saveAbbreviationsState();
279         
280         prepareEditor();
281         
282         try {
283             Abbreviations dialog = Abbreviations.invoke(getEditorName());
284             
285             Object JavaDoc[] keys=dialog.listAbbreviations().keySet().toArray();
286             
287             for (int cntr = 0; cntr < keys.length; cntr++) {
288                 if (!dialog.removeAbbreviation((String JavaDoc)keys[cntr]))
289                     log("Couldn't remove abbreviation: "+keys[cntr]);
290             }
291             
292             if (dialog.listAbbreviations().size() > 0) {
293                 Object JavaDoc[] lst=dialog.listAbbreviations().values().toArray();
294                 for (int i=0;i < lst.length;i++) {
295                     log("Remained abbreviation: "+lst[i]);
296                 }
297                 assertTrue("After removing all known abbreviations, some of them remained. Probably bug of test.", false);
298             }
299             dialog.cancel();
300             
301             for (int cntr = 0; cntr < keys.length; cntr++) {
302                 moveCaretIntoCode();
303                 //Test whether the old abbreviation does NOT work:
304
useAbbreviation((String JavaDoc)keys[cntr], true);
305             }
306             
307             log("testAbbreviationRemoveCancel flush results:");
308             
309             flushResult();
310         } catch (Exception JavaDoc ex) {
311             ex.printStackTrace(getLog());
312         } finally {
313             log("testAbbreviationRemoveCancel closing editor:");
314             
315             finishEditor();
316             
317             log("testAbbreviationRemoveCancel restoring abbreviations map:");
318             
319             Utilities.restoreAbbreviationsState(backup);
320             
321             log("testAbbreviationRemoveCancel finished");
322         }
323     }
324     
325     public void testAbbreviationRemove() {
326         log("testAbbreviationRemove start");
327         Object JavaDoc backup = Utilities.saveAbbreviationsState();
328         
329         prepareEditor();
330         
331         try {
332             Abbreviations dialog = null;
333             dialog = Abbreviations.invoke(getEditorName());
334             
335             Object JavaDoc[] keysold;
336             Object JavaDoc[] keys=dialog.listAbbreviations().keySet().toArray();
337             
338             for (int cntr = 0; cntr < keys.length; cntr++) {
339                 if (!dialog.removeAbbreviation((String JavaDoc)keys[cntr]))
340                     log("Couldn't remove abbreviation: "+keys[cntr]);
341             }
342             
343             if (dialog.listAbbreviations().size() > 0) {
344                 keysold=keys;
345                 keys=dialog.listAbbreviations().keySet().toArray();
346                 for (int i=0;i < keys.length;i++) {
347                     log("Remained abbreviation: "+keys[i]);
348                     for (int j=0;j < keysold.length;j++) {
349                         if (((String JavaDoc)keysold[j]).compareTo((String JavaDoc)keys[i]) == 0) {
350                             log("is in default list.");
351                             break;
352                         }
353                     }
354                 }
355                 //assertTrue("After removing all known abbreviations, some of them remained. Probably bug of test.", false);
356
}
357             dialog.oK();
358             log("Abbreviations removing confirmed.");
359             for (int cntr = 0; cntr < keys.length; cntr++) {
360                 moveCaretIntoCode();
361                 //Test whether the old abbreviation does NOT work:
362
useAbbreviation((String JavaDoc)keys[cntr], true);
363             }
364             
365             log("testAbbreviationRemove flush results:");
366             
367             flushResult();
368         } catch (Exception JavaDoc ex) {
369             ex.printStackTrace(getLog());
370         } finally {
371             log("testAbbreviationRemove closing editor:");
372             
373             finishEditor();
374             
375             log("testAbbreviationRemove restoring abbreviations map:");
376             
377             Utilities.restoreAbbreviationsState(backup);
378             
379             log("testAbbreviationRemove finished");
380         }
381     }
382     
383     public void setUp() {
384         log("Starting abbreviations test. Test class=" + getClass());
385         log("Test name=" + getName());
386         System.setOut(getLog());
387     }
388     
389     public void tearDown() throws Exception JavaDoc {
390         log("Finishing abbreviations test. Test class=" + getClass());
391     }
392 }
393
Popular Tags