KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > FindReplaceMachineTest


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.model;
35
36 import edu.rice.cs.drjava.DrJavaTestCase;
37 import edu.rice.cs.plt.io.IOUtil;
38 import edu.rice.cs.util.UnexpectedException;
39 import edu.rice.cs.util.swing.Utilities;
40 import edu.rice.cs.util.text.AbstractDocumentInterface;
41
42 import javax.swing.text.BadLocationException JavaDoc;
43 import java.io.File JavaDoc;
44 import java.io.IOException JavaDoc;
45
46 /** Tests the FindReplaceMachine.
47   * @version $Id: FindReplaceMachineTest.java 4075 2007-01-19 21:35:50Z dlsmith $
48   */

49 public class FindReplaceMachineTest extends DrJavaTestCase {
50   private volatile OpenDefinitionsDocument _doc; // working document accessible across threads
51
private volatile OpenDefinitionsDocument _docPrev;
52   private volatile OpenDefinitionsDocument _docNext;
53   private volatile FindResult _result; // working result variable accessible across threads
54
private volatile FindReplaceMachine _frm;
55   private volatile File JavaDoc _tempDir;
56   private volatile int _offset;
57   private static final AbstractGlobalModel _model = new AbstractGlobalModel();
58
59   private static final String JavaDoc EVIL_TEXT =
60       "Hear no evil, see no evil, speak no evil.";
61   private static final String JavaDoc EVIL_TEXT_PREV =
62       "Hear no evilprev, see no evilprev, speak no evilprev.";
63   private static final String JavaDoc EVIL_TEXT_NEXT =
64       "Hear no evilnext, see no evilnext, speak no evilnext.";
65   private static final String JavaDoc FIND_WHOLE_WORD_TEST_1 =
66       "public class Foo\n" +
67       "{\n" +
68       " /**\n" +
69       " * Barry Good!\n" +
70       " * (what I really mean is bar)\n" +
71       " */\n" +
72       " public void bar() \n" +
73       " {\n" +
74       " this.bar();\n" +
75       " }\n" +
76       "}";
77
78   private static final String JavaDoc FIND_MULTI_LINE_SEARCH_STR =
79       "{" + System.getProperty("line.separator");
80   
81   private static final String JavaDoc IGNORE_TEXT =
82     //edge cases (each require special code to take care of it)
83
"/* \" */ plt \n" + //double quotes inside block comment
84
"\" /* \" plt \n" + //opening block comment inside string
85
"/* // */ plt \n" + //opening line comment inside block comment
86
"\" // \" plt \n" + //opening line comment inside string
87
"\" \\\" \" plt \n" + //double quotes inside a string
88
"\'\"\' plt \n" + //double quotes inside char delimiters
89
"\'//\' plt \n" + //opening line comment inside char delimiters (syntax error irrelevant in find)
90
"\'/*\' plt \n" + //opening block comment inside char delmimites (syntax error irrelevant in find)
91

92     //non-edge cases
93
"/*This is a block comment*/ This is not a block comment\n" +
94     "//This is a line comment \n This is not a line comment\n" +
95     "\"This is a string\" This is not a string\n" +
96     "\'@\' That was a character, but this is not: @\n" +
97     "/*This is a two-lined \n commment*/ This is not a two-lined comment";
98
99   /** Initializes the document for the tests. */
100   public void setUp() throws Exception JavaDoc {
101     super.setUp();
102     String JavaDoc user = System.getProperty("user.name");
103     _tempDir = IOUtil.createAndMarkTempDirectory("DrJava-test-" + user, "");
104     _docPrev = _model.newFile(_tempDir);;
105     _doc = _model.newFile(_tempDir);
106     _docNext = _model.newFile(_tempDir);
107     
108     _frm = new FindReplaceMachine(_model, _model.getDocumentIterator());
109     _frm.setDocument(_doc);
110   }
111   
112   public void tearDown() throws Exception JavaDoc {
113     _frm.cleanUp();
114     _frm = null;
115     _model.closeAllFiles();
116     _tempDir = null;
117     super.tearDown();
118   }
119
120   private void _initFrm(int pos) { _frm.setPosition(pos); }
121   
122   public void testCreateMachineSuccess() throws BadLocationException JavaDoc {
123     _doc.insertString(0, EVIL_TEXT, null);
124     _initFrm(4);
125 // System.err.println("testCreateMachineSuccess completed completed");
126
}
127
128 // public void testCreateMachineFail() {
129
// // before 0
130
// try {
131
// _initFrm(-2);
132
// System.err.println(_frm.getStartOffset() + " " +
133
// _frm.getCurrentOffset());
134
// fail("creating invalid position in constructor");
135
// }
136
// catch (UnexpectedException e) {
137
// // expected: -2 is not a valid offset.
138
// }
139
//
140
// // after doc.getLength()
141
// try {
142
// _initFrm(5);
143
// System.out.println(_frm.getStartOffset() + " " +
144
// _frm.getCurrentOffset());
145
// fail("creating invalid position in constructor");
146
// }
147
// catch (UnexpectedException e) {
148
// // expected: 5 is larger than document
149
// }
150
// }
151

152   public void testFindNextUpdatesCurrent() throws BadLocationException JavaDoc {
153     _doc.insertString(0, EVIL_TEXT, null);
154     _initFrm(0);
155     _assertOffsets(_frm, 0, 0);
156     _frm.setFindWord("evil");
157
158     _testFindNextSucceeds(_frm, 0, 12);
159 // System.err.println("testFindNextUpdatesCurrent completed completed");
160
}
161
162   public void testFindNextAndFailIsOnMatch() throws BadLocationException JavaDoc {
163     _doc.insertString(0, EVIL_TEXT, null);
164     _initFrm(0);
165     _assertOffsets(_frm, 0, 0);
166     _frm.setFindWord("evil");
167     _testFindNextSucceeds(_frm, 0, 12);
168     _doc.insertString(9, "-", null);
169     assertTrue("no longer on find text", !_frm.onMatch());
170 // System.err.println("testFindNextAndFailIsOnMatch completed");
171
}
172
173   public void testMultipleCallsToFindNext() throws BadLocationException JavaDoc {
174     _doc.insertString(0, EVIL_TEXT, null);
175     _initFrm(0);
176     _assertOffsets(_frm, 0, 0);
177     _frm.setFindWord("evil");
178     _testFindNextSucceeds(_frm, 0, 12);
179     _testFindNextSucceeds(_frm, 0, 25);
180     _testFindNextSucceeds(_frm, 0, 40);
181 // System.err.println("testMultipleCallsToFindNext completed");
182
}
183
184   public void testStartFromTopContinue() throws BadLocationException JavaDoc {
185     _doc.insertString(0, EVIL_TEXT, null);
186     _initFrm(5);
187     _assertOffsets(_frm, 5, 5);
188     _frm.setFindWord("Hear");
189     _testFindNextSucceeds(_frm, 5, 4);
190 // System.err.println("testStartFromTopContinue completed");
191
}
192
193   public void testNotInDocument() throws BadLocationException JavaDoc {
194     _doc.insertString(0, EVIL_TEXT, null);
195     _initFrm(5);
196     _assertOffsets(_frm, 5, 5);
197     _frm.setFindWord("monkey");
198     _testFindNextFails(_frm, 5, 5);
199 // System.err.println("testNotInDocument completed");
200
}
201
202   public void testSimpleReplace() throws BadLocationException JavaDoc {
203     _doc.insertString(0, EVIL_TEXT, null);
204     _initFrm(0);
205     _assertOffsets(_frm, 0, 0);
206     _frm.setFindWord("evil");
207     _frm.setReplaceWord("monkey");
208     _testFindNextSucceeds(_frm, 0, 12);
209     Utilities.invokeAndWait(new Runnable JavaDoc() { public void run() { _frm.replaceCurrent(); } });
210     assertEquals("new replaced text", "Hear no monkey, see no evil, speak no evil.", _doc.getText());
211 // System.err.println("testSimpleReplace completed");
212
}
213
214   /* Test replacing all occurrence of word in a single document. */
215   public void testReplaceAllContinue() throws BadLocationException JavaDoc {
216     _doc.insertString(0, EVIL_TEXT, null);
217     _initFrm(15);
218     _assertOffsets(_frm, 15, 15);
219     _frm.setFindWord("evil");
220     _frm.setReplaceWord("monkey");
221     replaceAll();
222     assertEquals("revised text", "Hear no monkey, see no monkey, speak no monkey.", _doc.getText());
223 // System.err.println("testReplaceAllContinue completed");
224
}
225
226   public void testFindNoMatchCase() throws BadLocationException JavaDoc {
227     _doc.insertString(0, EVIL_TEXT, null);
228     _initFrm(0);
229     _assertOffsets(_frm, 0, 0);
230     _frm.setMatchCase(false);
231     _frm.setFindWord("eViL");
232     _testFindNextSucceeds(_frm, 0, 12);
233 // System.err.println("testFindNoMatchCase");
234
}
235
236   public void testReplaceAllContinueNoMatchCase() throws BadLocationException JavaDoc {
237     _doc.insertString(0, EVIL_TEXT, null);
238     _initFrm(15);
239     _assertOffsets(_frm, 15, 15);
240     _frm.setFindWord("eViL");
241     _frm.setReplaceWord("monkey");
242     _frm.setMatchCase(false);
243     replaceAll();
244     assertEquals("revised text", "Hear no monkey, see no monkey, speak no monkey.", _doc.getText());
245 // System.err.println("testReplaceAllContinueNoMatchCase completed");
246
}
247
248   public void testReplaceAllBackwards() throws BadLocationException JavaDoc {
249     _doc.insertString(0, "hElo helO", null);
250     _initFrm(3);
251     _frm.setFindWord("heLo");
252     _frm.setReplaceWord("cool");
253     _frm.setMatchCase(false);
254     _frm.setSearchBackwards(true);
255     replaceAll();
256     assertEquals("backwards replace", "cool cool", _doc.getText());
257 // System.err.println("testReplaceAllBackwards completed");
258
}
259
260   public void testFindMatchWithCaretInMiddle() throws BadLocationException JavaDoc {
261     _doc.insertString(0, "hello hello", null);
262     _initFrm(3);
263     _frm.setFindWord("hello");
264     _frm.setMatchCase(false);
265     _frm.setSearchBackwards(false);
266     _testFindNextSucceeds(_frm, 3, 11);
267     _testFindNextSucceeds(_frm, 3, 5);
268 // System.err.println("testFindMatchWithCaretInMiddle completed");
269
}
270
271   public void testFindMatchWithCaretInMiddleBackwards() throws BadLocationException JavaDoc {
272     _doc.insertString(0, "hello hello", null);
273     _initFrm(8);
274     _frm.setFindWord("helLo");
275     _frm.setMatchCase(false);
276     _frm.setSearchBackwards(true);
277     _testFindNextSucceeds(_frm, 8, 0);
278     _testFindNextSucceeds(_frm, 8, 6);
279 // System.err.println("testFindMatchWithCaretInMiddleBackwards completed");
280
}
281
282   /** This tests that a replace all where the replacement action creates a new match
283    * does not replace this new match
284    */

285   public void testReplaceCreatesMatch() throws BadLocationException JavaDoc {
286     _doc.insertString(0, "hhelloello", null);
287     _initFrm(1);
288     _frm.setFindWord("hello");
289     _frm.setMatchCase(false);
290     _frm.setSearchBackwards(false);
291     _frm.setReplaceWord("");
292     replaceAll();
293     assertEquals("replace creates new match", "hello", _doc.getText());
294 // System.err.println("testReplaceCreatesMatch completed");
295
}
296
297   /** This tests that a replace all backwards where the replacement action creates a new match
298    * does not replace this new match
299    */

300   public void testReplaceCreatesMatchBackwards() throws BadLocationException JavaDoc {
301     _doc.insertString(0, "hhelloello", null);
302     _initFrm(1);
303     _frm.setFindWord("hello");
304     _frm.setMatchCase(false);
305     _frm.setSearchBackwards(true);
306     _frm.setReplaceWord("");
307     replaceAll();
308     assertEquals("replace creates new match", "hello", _doc.getText());
309 // System.err.println("testReplaceCreatesMatchBackwards completed");
310
}
311
312   /** This test checks that replacing a word with itself will halt on replace all. */
313   public void testReplaceAllSameWord() throws BadLocationException JavaDoc {
314     _doc.insertString(0, "cool cool", null);
315     _initFrm(3);
316     _frm.setFindWord("cool");
317     _frm.setMatchCase(false);
318     _frm.setSearchBackwards(false);
319     _frm.setReplaceWord("cool");
320     replaceAll();
321     assertEquals("replace all with the same word", "cool cool", _doc.getText());
322 // System.err.println("Forward part of testReplaceAllSameWord completed");
323
_frm.setSearchBackwards(true);
324     replaceAll();
325     assertEquals("replace all backward with the same word", "cool cool", _doc.getText());
326 // System.err.println("testReplaceAllSameWord completed");
327
}
328
329   /** This test checks that a findNext won't find two matches that partially overlap.
330    * This is the current behavior of the FindReplaceMachine, though at some time
331    * in the future someone may want to change it.
332    */

333   public void testFindPartialSubstrings() throws BadLocationException JavaDoc {
334     _doc.insertString(0, "ooAooAoo", null);
335     _initFrm(0);
336     _frm.setFindWord("ooAo");
337     _frm.setMatchCase(false);
338     _frm.setSearchBackwards(false);
339     _testFindNextSucceeds(_frm, 0, 4);
340     _testFindNextSucceeds(_frm, 0, 4);
341
342     _initFrm(8);
343     _frm.setSearchBackwards(true);
344     _testFindNextSucceeds(_frm, 8, 3);
345     _testFindNextSucceeds(_frm, 8, 3);
346 // System.err.println("testFindPartialSubstrings completed");
347
}
348
349   /** This test addresses bug #745714 Searches Repeat When Changing Direction.
350    * The word that was just found should not be found again after toggling
351    * the search backwards flag.
352    */

353   public void testSearchesDoNotRepeatWhenChangingDirection() throws BadLocationException JavaDoc {
354     _doc.insertString(0, "int int int", null);
355     _initFrm(0);
356     _frm.setFindWord("int");
357     _frm.setMatchCase(false);
358     _frm.setSearchBackwards(false);
359     _testFindNextSucceeds(_frm, 0, 3);
360     _testFindNextSucceeds(_frm, 0, 7);
361
362     _frm.setLastFindWord();
363     _frm.setSearchBackwards(true);
364     _testFindNextSucceeds(_frm, 0, 0);
365
366     _frm.setLastFindWord();
367     _frm.setSearchBackwards(false);
368     _testFindNextSucceeds(_frm, 0, 7);
369
370     _frm.setLastFindWord();
371     _frm.positionChanged();
372     _frm.setSearchBackwards(true);
373     _testFindNextSucceeds(_frm, 0, 4);
374 // System.err.println("testSearchesDoNotRepeatWhenChangingDirection completed");
375
}
376
377   /** This test addresses feature request #784514 Find/Replace in all Open Files. */
378   public void testFindReplaceInAllOpenFiles() throws BadLocationException JavaDoc {
379     _doc.insertString(0, EVIL_TEXT, null);
380     _docPrev.insertString(0, EVIL_TEXT_PREV, null);
381     _docNext.insertString(0, EVIL_TEXT_NEXT, null);
382     // put the caret after the last instance of the findWord in doc
383
_initFrm(40);
384     _frm.setFindWord("evil");
385     _frm.setMatchCase(false);
386     _frm.setSearchBackwards(false);
387     _frm.setSearchAllDocuments(true);
388     _testFindNextSucceeds(_frm, 12, 12, _docNext);
389     _testFindNextSucceeds(_frm, 12, 29, _docNext);
390     _testFindNextSucceeds(_frm, 12, 48, _docNext);
391     _testFindNextSucceeds(_frm, 12, 12, _docPrev);
392     _testFindNextSucceeds(_frm, 12, 29, _docPrev);
393     _testFindNextSucceeds(_frm, 12, 48, _docPrev);
394     _testFindNextSucceeds(_frm, 12, 12, _doc);
395     _testFindNextSucceeds(_frm, 12, 25, _doc);
396     _testFindNextSucceeds(_frm, 12, 40, _doc);
397     _testFindNextSucceeds(_frm, 12, 12, _docNext);
398 // System.err.println("First sequence of global search tests complete");
399
_frm.setLastFindWord();
400 // System.err.println("_lastFindWord set to " + _frm.getFindWord());
401
_frm.setSearchBackwards(true);
402     _testFindNextSucceeds(_frm, 36, 36, _doc);
403     _testFindNextSucceeds(_frm, 36, 21, _doc);
404     _testFindNextSucceeds(_frm, 36, 8, _doc);
405     _testFindNextSucceeds(_frm, 44, 44, _docPrev);
406     _frm.setReplaceWord("monkey");
407     replaceAll();
408     assertEquals("revised text", "Hear no monkey, see no monkey, speak no monkey.", _doc.getText());
409     assertEquals("revised text", "Hear no monkeyprev, see no monkeyprev, speak no monkeyprev.", _docPrev.getText());
410     assertEquals("revised text", "Hear no monkeynext, see no monkeynext, speak no monkeynext.", _docNext.getText());
411 // System.err.println("testFindReplaceInAllOpenFiles completed");
412
}
413
414   public void testFindReplaceInAllOpenFilesWholeWord() throws BadLocationException JavaDoc {
415     _doc.insertString(0, EVIL_TEXT, null);
416     _docPrev.insertString(0, EVIL_TEXT_PREV, null);
417     _docNext.insertString(0, EVIL_TEXT_NEXT, null);
418     // put the caret after the last instance of the findWord in doc
419
_initFrm(40);
420     _frm.setFindWord("no");
421     _frm.setMatchWholeWord();
422     _frm.setMatchCase(false);
423     _frm.setSearchBackwards(false);
424     _frm.setSearchAllDocuments(true);
425     _testFindNextSucceeds(_frm, 7, 7, _docNext);
426     _testFindNextSucceeds(_frm, 7, 24, _docNext);
427     _testFindNextSucceeds(_frm, 7, 43, _docNext);
428     _testFindNextSucceeds(_frm, 7, 7, _docPrev);
429     _testFindNextSucceeds(_frm, 7, 24, _docPrev);
430     _testFindNextSucceeds(_frm, 7, 43, _docPrev);
431     _testFindNextSucceeds(_frm, 7, 7, _doc);
432     _testFindNextSucceeds(_frm, 7, 20, _doc);
433     _testFindNextSucceeds(_frm, 7, 35, _doc);
434     _testFindNextSucceeds(_frm, 7, 7, _docNext);
435     _frm.setLastFindWord();
436     _frm.setSearchBackwards(true);
437     _testFindNextSucceeds(_frm, 33, 33, _doc);
438     _testFindNextSucceeds(_frm, 33, 18, _doc);
439     _testFindNextSucceeds(_frm, 33, 5, _doc);
440     _testFindNextSucceeds(_frm, 41, 41, _docPrev);
441     _frm.setReplaceWord("monkey");
442     replaceAll();
443     assertEquals("revised text",
444                  "Hear monkey evil, see monkey evil, speak monkey evil.",
445                  _doc.getText(0, _doc.getLength()));
446     assertEquals("revised text",
447                  "Hear monkey evilprev, see monkey evilprev, speak monkey evilprev.",
448                  _docPrev.getText(0, _docPrev.getLength()));
449     assertEquals("revised text",
450                  "Hear monkey evilnext, see monkey evilnext, speak monkey evilnext.",
451                  _docNext.getText(0, _docNext.getLength()));
452 // System.err.println("testFindReplaceInAllOpenFilesWholeWord completed");
453
}
454
455   public void testFindMultiLine() throws BadLocationException JavaDoc {
456 // System.err.println("testFindMultiLine");
457
_doc.insertString(0, FIND_WHOLE_WORD_TEST_1, null);
458 // System.err.println(FIND_WHOLE_WORD_TEST_1);
459
_initFrm(0);
460     _frm.setFindWord(FIND_MULTI_LINE_SEARCH_STR);
461     _frm.setSearchBackwards(false);
462
463     _testFindNextSucceeds(_frm, 0, 19);
464 // System.err.println("testFindMultiLine completed");
465
}
466
467   public void testWholeWordSearchOnTestString1() throws BadLocationException JavaDoc {
468 // System.err.println("Running testWholeWordSearchOnTestString1");
469
_doc.insertString(0, FIND_WHOLE_WORD_TEST_1, null);
470 // System.err.println(FIND_WHOLE_WORD_TEST_1);
471
_initFrm(0);
472     _frm.setFindWord("bar");
473     _frm.setMatchWholeWord();
474     _frm.setSearchBackwards(false);
475
476     _testFindNextSucceeds(_frm, 0, 91);
477     _testFindNextSucceeds(_frm, 0, 128);
478     _testFindNextSucceeds(_frm, 0, 166);
479     _frm.setLastFindWord();
480     _frm.setSearchBackwards(true);
481     _testFindNextSucceeds(_frm, 0, 125);
482     _testFindNextSucceeds(_frm, 0, 88);
483     _testFindNextSucceeds(_frm, 0, 163);
484
485     _frm.setFindWord("ubl");
486     _testFindNextFails(_frm, 0, 163);
487
488     _frm.setSearchBackwards(false);
489     _frm.setFindWord("pub");
490     _testFindNextFails(_frm, 0, 163);
491
492     _frm.setSearchBackwards(true);
493     _frm.setFindWord("pub");
494     _testFindNextFails(_frm, 0, 163);
495 // System.err.println("testWholeWordSearchOnTestString1 completed");
496
}
497   
498   public void testWholeWordSearchIgnore() throws BadLocationException JavaDoc {
499     _doc.insertString(0, IGNORE_TEXT, null);
500 // System.err.println(IGNORE_TEXT);
501
_initFrm(0);
502     _frm.setFindWord("plt");
503     _frm.setMatchWholeWord();
504     _frm.setIgnoreCommentsAndStrings(true);
505     _frm.setSearchBackwards(false);
506
507     _testFindNextSucceeds(_frm, 0, 12);
508     _testFindNextSucceeds(_frm, 0, 25);
509     _testFindNextSucceeds(_frm, 0, 40);
510     _testFindNextSucceeds(_frm, 0, 53);
511     _testFindNextSucceeds(_frm, 0, 66);
512     _testFindNextSucceeds(_frm, 0, 75);
513     _testFindNextSucceeds(_frm, 0, 85);
514     _testFindNextSucceeds(_frm, 0, 95);
515     _frm.setLastFindWord();
516     _frm.setSearchBackwards(true);
517     _testFindNextSucceeds(_frm, 0, 82);
518     _testFindNextSucceeds(_frm, 0, 72);
519     _testFindNextSucceeds(_frm, 0, 63);
520     _testFindNextSucceeds(_frm, 0, 50);
521     _testFindNextSucceeds(_frm, 0, 37);
522     _testFindNextSucceeds(_frm, 0, 22);
523     _testFindNextSucceeds(_frm, 0, 9);
524     _testFindNextSucceeds(_frm, 0, 92);
525
526      _frm.setSearchBackwards(false);
527     _frm.setFindWord("comment");
528     _testFindNextSucceeds(_frm, 0, 152);
529     _testFindNextSucceeds(_frm, 0, 206);
530     _testFindNextSucceeds(_frm, 0, 358);
531     
532     _frm.setLastFindWord();
533     _frm.setSearchBackwards(true);
534     _testFindNextSucceeds(_frm, 0, 199);
535     _testFindNextSucceeds(_frm, 0, 145);
536     _testFindNextSucceeds(_frm, 0, 351);
537
538     _frm.setSearchBackwards(false);
539     _frm.setFindWord("@");
540     _testFindNextSucceeds(_frm, 0, 291);
541     
542     _frm.setLastFindWord();
543     _frm.setSearchBackwards(true);
544     _testFindNextSucceeds(_frm, 0, 290);
545     
546     _frm.setSearchBackwards(false);
547     _frm.setFindWord("string");
548     _testFindNextSucceeds(_frm, 0, 246);
549     
550     _frm.setLastFindWord();
551     _frm.setSearchBackwards(true);
552     _testFindNextSucceeds(_frm, 0, 240);
553 // System.err.println("testWholeWordSearchIgnore completed");
554
}
555   
556   public void testAnyOccurrenceSearchIgnore() throws BadLocationException JavaDoc {
557     _doc.insertString(0, IGNORE_TEXT, null);
558 // System.err.println(IGNORE_TEXT);
559
_initFrm(0);
560     _frm.setFindWord("lt");
561     _frm.setIgnoreCommentsAndStrings(true);
562     _frm.setSearchBackwards(false);
563
564     _testFindNextSucceeds(_frm, 0, 12);
565     _testFindNextSucceeds(_frm, 0, 25);
566     _testFindNextSucceeds(_frm, 0, 40);
567     _testFindNextSucceeds(_frm, 0, 53);
568     _testFindNextSucceeds(_frm, 0, 66);
569     _testFindNextSucceeds(_frm, 0, 75);
570     _testFindNextSucceeds(_frm, 0, 85);
571     _testFindNextSucceeds(_frm, 0, 95);
572     _frm.setLastFindWord();
573     _frm.setSearchBackwards(true);
574     _testFindNextSucceeds(_frm, 0, 83);
575     _testFindNextSucceeds(_frm, 0, 73);
576     _testFindNextSucceeds(_frm, 0, 64);
577     _testFindNextSucceeds(_frm, 0, 51);
578     _testFindNextSucceeds(_frm, 0, 38);
579     _testFindNextSucceeds(_frm, 0, 23);
580     _testFindNextSucceeds(_frm, 0, 10);
581     _testFindNextSucceeds(_frm, 0, 93);
582
583     _frm.setSearchBackwards(false);
584     _frm.setFindWord("ment");
585     _testFindNextSucceeds(_frm, 0, 152);
586     _testFindNextSucceeds(_frm, 0, 206);
587     _testFindNextSucceeds(_frm, 0, 358);
588     
589     _frm.setLastFindWord();
590     _frm.setSearchBackwards(true);
591     _testFindNextSucceeds(_frm, 0, 202);
592     _testFindNextSucceeds(_frm, 0, 148);
593     _testFindNextSucceeds(_frm, 0, 354);
594
595     _frm.setSearchBackwards(false);
596     _frm.setFindWord("@");
597     _testFindNextSucceeds(_frm, 0, 291);
598     
599     _frm.setLastFindWord();
600     _frm.setSearchBackwards(true);
601     _testFindNextSucceeds(_frm, 0, 290);
602     
603     _frm.setSearchBackwards(false);
604     _frm.setFindWord("ring");
605     _testFindNextSucceeds(_frm, 0, 246);
606     
607     _frm.setLastFindWord();
608     _frm.setSearchBackwards(true);
609     _testFindNextSucceeds(_frm, 0, 242);
610 // System.err.println("testAnyOccurrenceSearchIgnore completed");
611
}
612   
613   private void _testFindNextSucceeds(final FindReplaceMachine frm, int start, final int found,
614                                      OpenDefinitionsDocument doc) {
615     Utilities.invokeAndWait(new Runnable JavaDoc() {
616       public void run() {
617         try {
618           _result = frm.findNext();
619           OpenDefinitionsDocument newDoc = _result.getDocument();
620           if (frm.getDocument() != newDoc) {
621             // do FindReplacePanel's _updateMachine
622
// Utilities.show("return doc = " + d + " distinct from current machine doc = " + frm.getDocument());
623
frm.setDocument(newDoc);
624             frm.setPosition(found);
625           }
626         }
627         catch (Exception JavaDoc e) { throw new UnexpectedException(e); }
628       }
629     });
630
631     Utilities.clearEventQueue();
632     assertEquals("documents should equal", doc.toString(), frm.getDocument().toString());
633     assertEquals("findNext return value", found, _result.getFoundOffset());
634     _assertOffsets(frm, start, found);
635     assertTrue("on find text", frm.onMatch());
636   }
637
638   private void _testFindNextSucceeds(final FindReplaceMachine frm, int start, int found) {
639     Utilities.invokeAndWait(new Runnable JavaDoc() {
640       public void run() {
641         try { _offset = frm.findNext().getFoundOffset(); }
642         catch(Exception JavaDoc e) { throw new UnexpectedException(e); }
643       }
644     });
645     assertEquals("findNext return value", found, _offset);
646     _assertOffsets(frm, start, found);
647     assertTrue("on find text", frm.onMatch());
648   }
649   
650   private void _testFindNextFails(final FindReplaceMachine frm, int start, int current) {
651     Utilities.invokeAndWait(new Runnable JavaDoc() {
652       public void run() {
653         try { _offset = frm.findNext().getFoundOffset(); }
654         catch(Exception JavaDoc e) { throw new UnexpectedException(e); }
655       }
656     });
657     assertEquals("findNext return value", -1, _offset);
658     _assertOffsets(frm, start, current);
659   }
660
661   private void _assertOffsets(FindReplaceMachine frm, int start, int current) {
662 // Utilities.show("_assertOffsets(" + start + ", " + current + ")");
663
// assertEquals("start offset", start, frm.getStartOffset());
664
assertEquals("current offset", current, frm.getCurrentOffset());
665   }
666   
667   private void replaceAll() {
668     Utilities.invokeAndWait(new Runnable JavaDoc() { public void run() { _frm.replaceAll(); } });
669   }
670   
671 // /** A thunk returning boolean. */
672
// private interface ContinueCommand {
673
// public boolean shouldContinue();
674
// }
675

676 // private static ContinueCommand CONTINUE = new ContinueCommand() {
677
// public boolean shouldContinue() {
678
// return true;
679
// }
680
// };
681
}
682
Popular Tags