KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > search_replace > ReplaceTest


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 search_replace;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Window JavaDoc;
24 import java.awt.event.KeyEvent JavaDoc;
25 import lib.EditorTestCase;
26 import org.netbeans.jellytools.EditorOperator;
27 import org.netbeans.jellytools.HelpOperator;
28 import org.netbeans.jellytools.actions.ReplaceAction;
29 import org.netbeans.jellytools.modules.editor.Replace;
30 import org.netbeans.jemmy.ComponentChooser;
31 import org.netbeans.jemmy.EventTool;
32 import org.netbeans.jemmy.TestOut;
33 import org.netbeans.jemmy.operators.JEditorPaneOperator;
34 import org.netbeans.jemmy.operators.WindowOperator;
35
36 /**
37  *
38  * @author Roman Strobl
39  */

40 public class ReplaceTest extends EditorTestCase {
41     
42     private static int REPLACE_TIMEOUT = 1000;
43     
44     /** Creates a new instance of ReplaceTest */
45     public ReplaceTest(String JavaDoc testMethodName) {
46         super(testMethodName);
47     }
48     
49     @Override JavaDoc
50     protected void setUp() throws Exception JavaDoc {
51         super.setUp();
52         closeReplaceDialogIfOpened();
53     }
54
55     
56     
57     /**
58      * TC1 - open and close replace dialog
59      */

60     public void testReplaceDialogOpenClose() {
61         openDefaultProject();
62         openDefaultSampleFile();
63         try {
64             EditorOperator editor = getDefaultSampleEditorOperator();
65             JEditorPaneOperator txtOper = editor.txtEditorPane();
66             
67             // open replace and close
68
new ReplaceAction().perform();
69             txtOper.pushKey(KeyEvent.VK_ESCAPE);
70             
71             // open replace and open help
72
txtOper.pushKey(KeyEvent.VK_H, KeyEvent.CTRL_MASK);
73             Replace replace = new Replace();
74             replace.btHelp().doClick();
75             
76             // close help
77
HelpOperator help = new HelpOperator();
78             help.close();
79             
80             // close replace
81
replace.btClose().doClick();
82             
83         } finally {
84             closeReplaceDialogIfOpened();
85             closeFileWithDiscard();
86         }
87     }
88     
89     /**
90      * TC2 - Replace Dialog Open - Selection
91      */

92     public void testReplaceSelectionRepeated() {
93         openDefaultProject();
94         openDefaultSampleFile();
95         try {
96             EditorOperator editor = getDefaultSampleEditorOperator();
97             
98             // choose the "testReplaceSelectionRepeated" word
99
editor.select(13, 14, 41);
100             new EventTool().waitNoEvent(REPLACE_TIMEOUT);
101             new ReplaceAction().perform();
102             Replace replace = new Replace();
103             
104             // check only selected checkboxes
105
uncheckAll();
106             replace.cbHighlightSearch().doClick();
107             replace.cbIncrementalSearch().doClick();
108             replace.cbWrapSearch().doClick();
109             
110             String JavaDoc text = replace.cboFindWhat().getTextField().getText();
111             // compare
112
assertEquals(text, "testReplaceSelectionRepeated");
113             replace.cboReplaceWith().clearText();
114             replace.cboReplaceWith().typeText("testReplaceSelectionRepeated2");
115             replace.replace();
116             new EventTool().waitNoEvent(REPLACE_TIMEOUT);
117             replace.close();
118             // check status bar
119
log(editor.lblStatusBar().getText());
120             waitForLabel("'testReplaceSelectionRepeated' found at 16:12");
121             
122             // choose the "testReplaceSelectionRepeated" word
123
editor.select(15, 35, 62);
124             new ReplaceAction().perform();
125             Replace replace2 = new Replace();
126             text = replace2.cboFindWhat().getTextField().getText();
127             // compare
128
assertEquals(text, "testReplaceSelectionRepeated");
129             replace2.replace();
130             new EventTool().waitNoEvent(REPLACE_TIMEOUT);
131             replace2.close();
132             // check status bar
133
waitForLabel("'testReplaceSelectionRepeated' found at 2:4; End of document reached. "
134                     +"Continuing search from beginning.");
135             
136             ref(editor.getText());
137             compareReferenceFiles();
138             
139         } finally {
140             closeReplaceDialogIfOpened();
141             closeFileWithDiscard();
142         }
143     }
144     
145     /**
146      * TC3 - Replace Dialog Combo Box
147      */

148     public void testReplaceDialogComboBox() {
149         openDefaultProject();
150         openDefaultSampleFile();
151         try {
152             EditorOperator editor = getDefaultSampleEditorOperator();
153             
154             editor.setCaretPosition(1,1);
155             editor.txtEditorPane().pushKey(KeyEvent.VK_H, KeyEvent.CTRL_MASK);
156             Replace replace = new Replace();
157             replace.cboFindWhat().removeAllItems();
158             replace.cboReplaceWith().removeAllItems();
159             
160             // check only selected checkboxes
161
uncheckAll();
162             replace.cbHighlightSearch().doClick();
163             replace.cbIncrementalSearch().doClick();
164             replace.cbWrapSearch().doClick();
165             
166             replace.cboFindWhat().clearText();
167             replace.cboFindWhat().typeText("package");
168             replace.cboReplaceWith().clearText();
169             replace.cboReplaceWith().typeText("pakaz");
170             replace.replace();
171             // check status bar
172
waitForLabel("'package' not found");
173             
174             replace.cboFindWhat().clearText();
175             replace.cboFindWhat().typeText("class");
176             replace.cboReplaceWith().clearText();
177             replace.cboReplaceWith().typeText("klasa");
178             replace.replace();
179             // check status bar
180
waitForLabel("'class' not found");
181             
182             replace.cboFindWhat().clearText();
183             replace.cboFindWhat().typeText("testReplaceDialogComboBox");
184             replace.cboReplaceWith().clearText();
185             replace.cboReplaceWith().typeText("testReplaceDialogComboBox2");
186             replace.replace();
187             // check status bar
188
waitForLabel("'testReplaceDialogComboBox' found at 13:35");
189             
190             boolean found1 = false;
191             boolean found2 = false;
192             boolean found3 = false;
193             for (int i = 0; i<replace.cboFindWhat().getItemCount(); i++) {
194                 if (((String JavaDoc)replace.cboFindWhat().getItemAt(i)).equals(
195                         "testReplaceDialogComboBox")) found1 = true;
196                 if (((String JavaDoc)replace.cboFindWhat().getItemAt(i)).equals(
197                         "class")) found2 = true;
198                 if (((String JavaDoc)replace.cboFindWhat().getItemAt(i)).equals(
199                         "package")) found3 = true;
200             }
201             assertEquals(found1, true);
202             assertEquals(found2, true);
203             assertEquals(found3, true);
204             
205             found1 = false;
206             found2 = false;
207             found3 = false;
208             for (int i = 0; i<replace.cboReplaceWith().getItemCount(); i++) {
209                 if (((String JavaDoc)replace.cboReplaceWith().getItemAt(i)).equals(
210                         "testReplaceDialogComboBox2")) found1 = true;
211                 if (((String JavaDoc)replace.cboReplaceWith().getItemAt(i)).equals(
212                         "klasa")) found2 = true;
213                 if (((String JavaDoc)replace.cboReplaceWith().getItemAt(i)).equals(
214                         "pakaz")) found3 = true;
215             }
216             assertEquals(found1, true);
217             assertEquals(found2, true);
218             assertEquals(found3, true);
219             
220             new EventTool().waitNoEvent(REPLACE_TIMEOUT);
221             replace.close();
222             
223             ref(editor.getText());
224             compareReferenceFiles();
225             
226         } finally {
227             closeReplaceDialogIfOpened();
228             closeFileWithDiscard();
229         }
230     }
231     
232     /**
233      * TC4 - Replace Match Case
234      */

235     public void testReplaceMatchCase() {
236         openDefaultProject();
237         openDefaultSampleFile();
238         try {
239             EditorOperator editor = getDefaultSampleEditorOperator();
240             
241             editor.setCaretPosition(1,1);
242             new ReplaceAction().perform();
243             Replace replace = new Replace();
244             
245             // check only selected checkboxes
246
uncheckAll();
247             replace.cbMatchCase().doClick();
248             
249             replace.cboFindWhat().clearText();
250             replace.cboFindWhat().typeText("testCase");
251             replace.cboReplaceWith().clearText();
252             replace.cboReplaceWith().typeText("xxxxXxxx");
253             replace.replace();
254             
255             replace.close();
256             
257             ref(editor.getText());
258             compareReferenceFiles();
259             
260         } finally {
261             closeReplaceDialogIfOpened();
262             closeFileWithDiscard();
263         }
264     }
265     
266     /**
267      * TC5 - Replace All
268      */

269     public void testReplaceAll() {
270         openDefaultProject();
271         openDefaultSampleFile();
272         try {
273             EditorOperator editor = getDefaultSampleEditorOperator();
274             
275             editor.setCaretPosition(1,1);
276             new ReplaceAction().perform();
277             Replace replace = new Replace();
278             
279             // check only selected checkboxes
280
uncheckAll();
281             
282             replace.cboFindWhat().clearText();
283             replace.cboFindWhat().typeText("testWord");
284             replace.cboReplaceWith().clearText();
285             replace.cboReplaceWith().typeText("xxxxXxxx");
286             replace.replaceAll();
287             // check status bar
288
waitForLabel("14 of 14 items replaced");
289             
290             replace.close();
291             
292             ref(editor.getText());
293             compareReferenceFiles();
294             
295         } finally {
296             closeReplaceDialogIfOpened();
297             closeFileWithDiscard();
298         }
299     }
300     
301     /**
302      * TC6 - Replace in Selection Only
303      */

304     public void testReplaceInSelectionOnly() {
305         openDefaultProject();
306         openDefaultSampleFile();
307         try {
308             EditorOperator editor = getDefaultSampleEditorOperator();
309             
310             editor.select(20, 24);
311             new ReplaceAction().perform();
312             Replace replace = new Replace();
313             
314             // check only selected checkboxes
315
uncheckAll();
316             replace.cbSearchSelection().doClick();
317             replace.cboFindWhat().clearText();
318             replace.cboFindWhat().typeText("testWord");
319             replace.cboReplaceWith().clearText();
320             replace.cboReplaceWith().typeText("xxxxXxxx");
321             replace.replaceAll();
322             // check status bar
323
waitForLabel("5 of 5 items replaced");
324             
325             replace.close();
326             
327             ref(editor.getText());
328             compareReferenceFiles();
329             
330         } finally {
331             closeReplaceDialogIfOpened();
332             closeFileWithDiscard();
333         }
334     }
335     
336     /**
337      * Unchecks all checkboxes in find dialog.
338      */

339     public void uncheckAll() {
340         Replace replace = new Replace();
341         replace.cbBackwardSearch().setSelected(false);
342         replace.cbSearchSelection().setSelected(false);
343         replace.cbHighlightSearch().setSelected(false);
344         replace.cbIncrementalSearch().setSelected(false);
345         replace.cbMatchCase().setSelected(false);
346         replace.cbMatchWholeWordsOnly().setSelected(false);
347         replace.cbRegularExpressions().setSelected(false);
348         //replace.cbSmartCase().setSelected(false);
349
replace.cbWrapSearch().setSelected(false);
350     }
351     
352     /**
353      * Waits for label to appear on Status Bar, checks it 10 times before
354      * failing.
355      * @param label label which should be displayed on status bar
356      */

357     public void waitForLabel(String JavaDoc label) {
358         EditorOperator editor = getDefaultSampleEditorOperator();
359         for (int i = 0; i<10; i++) {
360             if (editor.lblStatusBar().getText().equals(label)) break;
361             new EventTool().waitNoEvent(REPLACE_TIMEOUT);
362         }
363         assertEquals(editor.lblStatusBar().getText(), label);
364     }
365     
366     /**
367      * Checks if a replace dialog is opened and if yes it closes it.
368      */

369     public void closeReplaceDialogIfOpened() {
370         Window JavaDoc replaceWindow = WindowOperator.findWindow(
371                 new ComponentChooser() {
372             public boolean checkComponent(Component JavaDoc comp) {
373                 WindowOperator winOper = new WindowOperator((Window JavaDoc)comp);
374                 winOper.setOutput(TestOut.getNullOutput());
375                 return null != winOper.findSubComponent(
376                         new ComponentChooser() {
377                     public boolean checkComponent(Component JavaDoc comp) {
378                         return comp.getClass().getName().startsWith(
379                                 "org.netbeans.editor.ext.Find"); //NOI18N
380
}
381                     public String JavaDoc getDescription() {
382                         return("any replace dialog"); //NOI18N
383
}
384                 });
385             }
386             public String JavaDoc getDescription() {
387                 return "containing any replace dialog"; //NOI18N
388
}
389         });
390         if(replaceWindow != null) {
391             new Replace().close();
392         }
393     }
394     
395 }
396
Popular Tags