001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.model;
038:
039: import edu.rice.cs.drjava.DrJavaTestCase;
040: import edu.rice.cs.plt.io.IOUtil;
041: import edu.rice.cs.util.StringOps;
042: import edu.rice.cs.util.UnexpectedException;
043: import edu.rice.cs.util.swing.Utilities;
044: import edu.rice.cs.util.text.AbstractDocumentInterface;
045:
046: import javax.swing.text.BadLocationException;
047: import java.io.File;
048: import java.io.IOException;
049:
050: /** Tests the FindReplaceMachine.
051: * @version $Id: FindReplaceMachineTest.java 4255 2007-08-28 19:17:37Z mgricken $
052: */
053: public class FindReplaceMachineTest extends DrJavaTestCase {
054: private volatile OpenDefinitionsDocument _doc; // working document accessible across threads
055: private volatile OpenDefinitionsDocument _docPrev;
056: private volatile OpenDefinitionsDocument _docNext;
057: private volatile FindResult _result; // working result variable accessible across threads
058: private volatile FindReplaceMachine _frm;
059: private volatile File _tempDir;
060: private volatile int _offset;
061: private static final AbstractGlobalModel _model = new AbstractGlobalModel();
062:
063: private static final String EVIL_TEXT = "Hear no evil, see no evil, speak no evil.";
064: private static final String EVIL_TEXT_PREV = "Hear no evilprev, see no evilprev, speak no evilprev.";
065: private static final String EVIL_TEXT_NEXT = "Hear no evilnext, see no evilnext, speak no evilnext.";
066: private static final String FIND_WHOLE_WORD_TEST_1 = "public class Foo\n"
067: + "{\n"
068: + " /**\n"
069: + " * Barry Good!\n"
070: + " * (what I really mean is bar)\n"
071: + " */\n"
072: + " public void bar() \n"
073: + " {\n"
074: + " this.bar();\n"
075: + " }\n" + "}";
076:
077: private static final String FIND_MULTI_LINE_SEARCH_STR = "{"
078: + StringOps.EOL;
079:
080: private static final String IGNORE_TEXT =
081: //edge cases (each require special code to take care of it)
082: "/* \" */ plt \n"
083: + //double quotes inside block comment
084: "\" /* \" plt \n"
085: + //opening block comment inside string
086: "/* // */ plt \n"
087: + //opening line comment inside block comment
088: "\" // \" plt \n"
089: + //opening line comment inside string
090: "\" \\\" \" plt \n"
091: + //double quotes inside a string
092: "\'\"\' plt \n"
093: + //double quotes inside char delimiters
094: "\'//\' plt \n"
095: + //opening line comment inside char delimiters (syntax error irrelevant in find)
096: "\'/*\' plt \n"
097: + //opening block comment inside char delmimites (syntax error irrelevant in find)
098:
099: //non-edge cases
100: "/*This is a block comment*/ This is not a block comment\n"
101: + "//This is a line comment \n This is not a line comment\n"
102: + "\"This is a string\" This is not a string\n"
103: + "\'@\' That was a character, but this is not: @\n"
104: + "/*This is a two-lined \n commment*/ This is not a two-lined comment";
105:
106: /** Initializes the document for the tests. */
107: public void setUp() throws Exception {
108: super .setUp();
109: String user = System.getProperty("user.name");
110: _tempDir = IOUtil.createAndMarkTempDirectory("DrJava-test-"
111: + user, "");
112: _docPrev = _model.newFile(_tempDir);
113: ;
114: _doc = _model.newFile(_tempDir);
115: _docNext = _model.newFile(_tempDir);
116:
117: _frm = new FindReplaceMachine(_model, _model
118: .getDocumentIterator());
119: _frm.setDocument(_doc);
120: }
121:
122: public void tearDown() throws Exception {
123: _frm.cleanUp();
124: _frm = null;
125: _model.closeAllFiles();
126: _tempDir = null;
127: super .tearDown();
128: }
129:
130: private void _initFrm(int pos) {
131: _frm.setPosition(pos);
132: }
133:
134: public void testCreateMachineSuccess() throws BadLocationException {
135: _doc.insertString(0, EVIL_TEXT, null);
136: _initFrm(4);
137: // System.err.println("testCreateMachineSuccess completed completed");
138: }
139:
140: // public void testCreateMachineFail() {
141: // // before 0
142: // try {
143: // _initFrm(-2);
144: // System.err.println(_frm.getStartOffset() + " " +
145: // _frm.getCurrentOffset());
146: // fail("creating invalid position in constructor");
147: // }
148: // catch (UnexpectedException e) {
149: // // expected: -2 is not a valid offset.
150: // }
151: //
152: // // after doc.getLength()
153: // try {
154: // _initFrm(5);
155: // System.out.println(_frm.getStartOffset() + " " +
156: // _frm.getCurrentOffset());
157: // fail("creating invalid position in constructor");
158: // }
159: // catch (UnexpectedException e) {
160: // // expected: 5 is larger than document
161: // }
162: // }
163:
164: public void testFindNextUpdatesCurrent()
165: throws BadLocationException {
166: _doc.insertString(0, EVIL_TEXT, null);
167: _initFrm(0);
168: _assertOffsets(_frm, 0, 0);
169: _frm.setFindWord("evil");
170:
171: _testFindNextSucceeds(_frm, 0, 12);
172: // System.err.println("testFindNextUpdatesCurrent completed completed");
173: }
174:
175: public void testFindNextAndFailIsOnMatch()
176: throws BadLocationException {
177: _doc.insertString(0, EVIL_TEXT, null);
178: _initFrm(0);
179: _assertOffsets(_frm, 0, 0);
180: _frm.setFindWord("evil");
181: _testFindNextSucceeds(_frm, 0, 12);
182: _doc.insertString(9, "-", null);
183: assertTrue("no longer on find text", !_frm.onMatch());
184: // System.err.println("testFindNextAndFailIsOnMatch completed");
185: }
186:
187: public void testMultipleCallsToFindNext()
188: throws BadLocationException {
189: _doc.insertString(0, EVIL_TEXT, null);
190: _initFrm(0);
191: _assertOffsets(_frm, 0, 0);
192: _frm.setFindWord("evil");
193: _testFindNextSucceeds(_frm, 0, 12);
194: _testFindNextSucceeds(_frm, 0, 25);
195: _testFindNextSucceeds(_frm, 0, 40);
196: // System.err.println("testMultipleCallsToFindNext completed");
197: }
198:
199: public void testStartFromTopContinue() throws BadLocationException {
200: _doc.insertString(0, EVIL_TEXT, null);
201: _initFrm(5);
202: _assertOffsets(_frm, 5, 5);
203: _frm.setFindWord("Hear");
204: _testFindNextSucceeds(_frm, 5, 4);
205: // System.err.println("testStartFromTopContinue completed");
206: }
207:
208: public void testNotInDocument() throws BadLocationException {
209: _doc.insertString(0, EVIL_TEXT, null);
210: _initFrm(5);
211: _assertOffsets(_frm, 5, 5);
212: _frm.setFindWord("monkey");
213: _testFindNextFails(_frm, 5, 5);
214: // System.err.println("testNotInDocument completed");
215: }
216:
217: public void testSimpleReplace() throws BadLocationException {
218: _doc.insertString(0, EVIL_TEXT, null);
219: _initFrm(0);
220: _assertOffsets(_frm, 0, 0);
221: _frm.setFindWord("evil");
222: _frm.setReplaceWord("monkey");
223: _testFindNextSucceeds(_frm, 0, 12);
224: Utilities.invokeAndWait(new Runnable() {
225: public void run() {
226: _frm.replaceCurrent();
227: }
228: });
229: assertEquals("new replaced text",
230: "Hear no monkey, see no evil, speak no evil.", _doc
231: .getText());
232: // System.err.println("testSimpleReplace completed");
233: }
234:
235: /* Test replacing all occurrence of word in a single document. */
236: public void testReplaceAllContinue() throws BadLocationException {
237: _doc.insertString(0, EVIL_TEXT, null);
238: _initFrm(15);
239: _assertOffsets(_frm, 15, 15);
240: _frm.setFindWord("evil");
241: _frm.setReplaceWord("monkey");
242: replaceAll();
243: assertEquals("revised text",
244: "Hear no monkey, see no monkey, speak no monkey.", _doc
245: .getText());
246: // System.err.println("testReplaceAllContinue completed");
247: }
248:
249: public void testFindNoMatchCase() throws BadLocationException {
250: _doc.insertString(0, EVIL_TEXT, null);
251: _initFrm(0);
252: _assertOffsets(_frm, 0, 0);
253: _frm.setMatchCase(false);
254: _frm.setFindWord("eViL");
255: _testFindNextSucceeds(_frm, 0, 12);
256: // System.err.println("testFindNoMatchCase");
257: }
258:
259: public void testReplaceAllContinueNoMatchCase()
260: throws BadLocationException {
261: _doc.insertString(0, EVIL_TEXT, null);
262: _initFrm(15);
263: _assertOffsets(_frm, 15, 15);
264: _frm.setFindWord("eViL");
265: _frm.setReplaceWord("monkey");
266: _frm.setMatchCase(false);
267: replaceAll();
268: assertEquals("revised text",
269: "Hear no monkey, see no monkey, speak no monkey.", _doc
270: .getText());
271: // System.err.println("testReplaceAllContinueNoMatchCase completed");
272: }
273:
274: public void testReplaceAllBackwards() throws BadLocationException {
275: _doc.insertString(0, "hElo helO", null);
276: _initFrm(3);
277: _frm.setFindWord("heLo");
278: _frm.setReplaceWord("cool");
279: _frm.setMatchCase(false);
280: _frm.setSearchBackwards(true);
281: replaceAll();
282: assertEquals("backwards replace", "cool cool", _doc.getText());
283: // System.err.println("testReplaceAllBackwards completed");
284: }
285:
286: public void testFindMatchWithCaretInMiddle()
287: throws BadLocationException {
288: _doc.insertString(0, "hello hello", null);
289: _initFrm(3);
290: _frm.setFindWord("hello");
291: _frm.setMatchCase(false);
292: _frm.setSearchBackwards(false);
293: _testFindNextSucceeds(_frm, 3, 11);
294: _testFindNextSucceeds(_frm, 3, 5);
295: // System.err.println("testFindMatchWithCaretInMiddle completed");
296: }
297:
298: public void testFindMatchWithCaretInMiddleBackwards()
299: throws BadLocationException {
300: _doc.insertString(0, "hello hello", null);
301: _initFrm(8);
302: _frm.setFindWord("helLo");
303: _frm.setMatchCase(false);
304: _frm.setSearchBackwards(true);
305: _testFindNextSucceeds(_frm, 8, 0);
306: _testFindNextSucceeds(_frm, 8, 6);
307: // System.err.println("testFindMatchWithCaretInMiddleBackwards completed");
308: }
309:
310: /** This tests that a replace all where the replacement action creates a new match
311: * does not replace this new match
312: */
313: public void testReplaceCreatesMatch() throws BadLocationException {
314: _doc.insertString(0, "hhelloello", null);
315: _initFrm(1);
316: _frm.setFindWord("hello");
317: _frm.setMatchCase(false);
318: _frm.setSearchBackwards(false);
319: _frm.setReplaceWord("");
320: replaceAll();
321: assertEquals("replace creates new match", "hello", _doc
322: .getText());
323: // System.err.println("testReplaceCreatesMatch completed");
324: }
325:
326: /** This tests that a replace all backwards where the replacement action creates a new match
327: * does not replace this new match
328: */
329: public void testReplaceCreatesMatchBackwards()
330: throws BadLocationException {
331: _doc.insertString(0, "hhelloello", null);
332: _initFrm(1);
333: _frm.setFindWord("hello");
334: _frm.setMatchCase(false);
335: _frm.setSearchBackwards(true);
336: _frm.setReplaceWord("");
337: replaceAll();
338: assertEquals("replace creates new match", "hello", _doc
339: .getText());
340: // System.err.println("testReplaceCreatesMatchBackwards completed");
341: }
342:
343: /** This test checks that replacing a word with itself will halt on replace all. */
344: public void testReplaceAllSameWord() throws BadLocationException {
345: _doc.insertString(0, "cool cool", null);
346: _initFrm(3);
347: _frm.setFindWord("cool");
348: _frm.setMatchCase(false);
349: _frm.setSearchBackwards(false);
350: _frm.setReplaceWord("cool");
351: replaceAll();
352: assertEquals("replace all with the same word", "cool cool",
353: _doc.getText());
354: // System.err.println("Forward part of testReplaceAllSameWord completed");
355: _frm.setSearchBackwards(true);
356: replaceAll();
357: assertEquals("replace all backward with the same word",
358: "cool cool", _doc.getText());
359: // System.err.println("testReplaceAllSameWord completed");
360: }
361:
362: /** This test checks that a findNext won't find two matches that partially overlap.
363: * This is the current behavior of the FindReplaceMachine, though at some time
364: * in the future someone may want to change it.
365: */
366: public void testFindPartialSubstrings() throws BadLocationException {
367: _doc.insertString(0, "ooAooAoo", null);
368: _initFrm(0);
369: _frm.setFindWord("ooAo");
370: _frm.setMatchCase(false);
371: _frm.setSearchBackwards(false);
372: _testFindNextSucceeds(_frm, 0, 4);
373: _testFindNextSucceeds(_frm, 0, 4);
374:
375: _initFrm(8);
376: _frm.setSearchBackwards(true);
377: _testFindNextSucceeds(_frm, 8, 3);
378: _testFindNextSucceeds(_frm, 8, 3);
379: // System.err.println("testFindPartialSubstrings completed");
380: }
381:
382: /** This test addresses bug #745714 Searches Repeat When Changing Direction.
383: * The word that was just found should not be found again after toggling
384: * the search backwards flag.
385: */
386: public void testSearchesDoNotRepeatWhenChangingDirection()
387: throws BadLocationException {
388: _doc.insertString(0, "int int int", null);
389: _initFrm(0);
390: _frm.setFindWord("int");
391: _frm.setMatchCase(false);
392: _frm.setSearchBackwards(false);
393: _testFindNextSucceeds(_frm, 0, 3);
394: _testFindNextSucceeds(_frm, 0, 7);
395:
396: _frm.setLastFindWord();
397: _frm.setSearchBackwards(true);
398: _testFindNextSucceeds(_frm, 0, 0);
399:
400: _frm.setLastFindWord();
401: _frm.setSearchBackwards(false);
402: _testFindNextSucceeds(_frm, 0, 7);
403:
404: _frm.setLastFindWord();
405: _frm.positionChanged();
406: _frm.setSearchBackwards(true);
407: _testFindNextSucceeds(_frm, 0, 4);
408: // System.err.println("testSearchesDoNotRepeatWhenChangingDirection completed");
409: }
410:
411: /** This test addresses feature request #784514 Find/Replace in all Open Files. */
412: public void testFindReplaceInAllOpenFiles()
413: throws BadLocationException {
414: _doc.insertString(0, EVIL_TEXT, null);
415: _docPrev.insertString(0, EVIL_TEXT_PREV, null);
416: _docNext.insertString(0, EVIL_TEXT_NEXT, null);
417: // put the caret after the last instance of the findWord in doc
418: _initFrm(40);
419: _frm.setFindWord("evil");
420: _frm.setMatchCase(false);
421: _frm.setSearchBackwards(false);
422: _frm.setSearchAllDocuments(true);
423: _testFindNextSucceeds(_frm, 12, 12, _docNext);
424: _testFindNextSucceeds(_frm, 12, 29, _docNext);
425: _testFindNextSucceeds(_frm, 12, 48, _docNext);
426: _testFindNextSucceeds(_frm, 12, 12, _docPrev);
427: _testFindNextSucceeds(_frm, 12, 29, _docPrev);
428: _testFindNextSucceeds(_frm, 12, 48, _docPrev);
429: _testFindNextSucceeds(_frm, 12, 12, _doc);
430: _testFindNextSucceeds(_frm, 12, 25, _doc);
431: _testFindNextSucceeds(_frm, 12, 40, _doc);
432: _testFindNextSucceeds(_frm, 12, 12, _docNext);
433: // System.err.println("First sequence of global search tests complete");
434: _frm.setLastFindWord();
435: // System.err.println("_lastFindWord set to " + _frm.getFindWord());
436: _frm.setSearchBackwards(true);
437: _testFindNextSucceeds(_frm, 36, 36, _doc);
438: _testFindNextSucceeds(_frm, 36, 21, _doc);
439: _testFindNextSucceeds(_frm, 36, 8, _doc);
440: _testFindNextSucceeds(_frm, 44, 44, _docPrev);
441: _frm.setReplaceWord("monkey");
442: replaceAll();
443: assertEquals("revised text",
444: "Hear no monkey, see no monkey, speak no monkey.", _doc
445: .getText());
446: assertEquals(
447: "revised text",
448: "Hear no monkeyprev, see no monkeyprev, speak no monkeyprev.",
449: _docPrev.getText());
450: assertEquals(
451: "revised text",
452: "Hear no monkeynext, see no monkeynext, speak no monkeynext.",
453: _docNext.getText());
454: // System.err.println("testFindReplaceInAllOpenFiles completed");
455: }
456:
457: public void testFindReplaceInAllOpenFilesWholeWord()
458: throws BadLocationException {
459: _doc.insertString(0, EVIL_TEXT, null);
460: _docPrev.insertString(0, EVIL_TEXT_PREV, null);
461: _docNext.insertString(0, EVIL_TEXT_NEXT, null);
462: // put the caret after the last instance of the findWord in doc
463: _initFrm(40);
464: _frm.setFindWord("no");
465: _frm.setMatchWholeWord();
466: _frm.setMatchCase(false);
467: _frm.setSearchBackwards(false);
468: _frm.setSearchAllDocuments(true);
469: _testFindNextSucceeds(_frm, 7, 7, _docNext);
470: _testFindNextSucceeds(_frm, 7, 24, _docNext);
471: _testFindNextSucceeds(_frm, 7, 43, _docNext);
472: _testFindNextSucceeds(_frm, 7, 7, _docPrev);
473: _testFindNextSucceeds(_frm, 7, 24, _docPrev);
474: _testFindNextSucceeds(_frm, 7, 43, _docPrev);
475: _testFindNextSucceeds(_frm, 7, 7, _doc);
476: _testFindNextSucceeds(_frm, 7, 20, _doc);
477: _testFindNextSucceeds(_frm, 7, 35, _doc);
478: _testFindNextSucceeds(_frm, 7, 7, _docNext);
479: _frm.setLastFindWord();
480: _frm.setSearchBackwards(true);
481: _testFindNextSucceeds(_frm, 33, 33, _doc);
482: _testFindNextSucceeds(_frm, 33, 18, _doc);
483: _testFindNextSucceeds(_frm, 33, 5, _doc);
484: _testFindNextSucceeds(_frm, 41, 41, _docPrev);
485: _frm.setReplaceWord("monkey");
486: replaceAll();
487: assertEquals(
488: "revised text",
489: "Hear monkey evil, see monkey evil, speak monkey evil.",
490: _doc.getText(0, _doc.getLength()));
491: assertEquals(
492: "revised text",
493: "Hear monkey evilprev, see monkey evilprev, speak monkey evilprev.",
494: _docPrev.getText(0, _docPrev.getLength()));
495: assertEquals(
496: "revised text",
497: "Hear monkey evilnext, see monkey evilnext, speak monkey evilnext.",
498: _docNext.getText(0, _docNext.getLength()));
499: // System.err.println("testFindReplaceInAllOpenFilesWholeWord completed");
500: }
501:
502: public void testFindMultiLine() throws BadLocationException {
503: // System.err.println("testFindMultiLine");
504: _doc.insertString(0, FIND_WHOLE_WORD_TEST_1, null);
505: // System.err.println(FIND_WHOLE_WORD_TEST_1);
506: _initFrm(0);
507: _frm.setFindWord(FIND_MULTI_LINE_SEARCH_STR);
508: _frm.setSearchBackwards(false);
509:
510: _testFindNextSucceeds(_frm, 0, 19);
511: // System.err.println("testFindMultiLine completed");
512: }
513:
514: public void testWholeWordSearchOnTestString1()
515: throws BadLocationException {
516: // System.err.println("Running testWholeWordSearchOnTestString1");
517: _doc.insertString(0, FIND_WHOLE_WORD_TEST_1, null);
518: // System.err.println(FIND_WHOLE_WORD_TEST_1);
519: _initFrm(0);
520: _frm.setFindWord("bar");
521: _frm.setMatchWholeWord();
522: _frm.setSearchBackwards(false);
523:
524: _testFindNextSucceeds(_frm, 0, 91);
525: _testFindNextSucceeds(_frm, 0, 128);
526: _testFindNextSucceeds(_frm, 0, 166);
527: _frm.setLastFindWord();
528: _frm.setSearchBackwards(true);
529: _testFindNextSucceeds(_frm, 0, 125);
530: _testFindNextSucceeds(_frm, 0, 88);
531: _testFindNextSucceeds(_frm, 0, 163);
532:
533: _frm.setFindWord("ubl");
534: _testFindNextFails(_frm, 0, 163);
535:
536: _frm.setSearchBackwards(false);
537: _frm.setFindWord("pub");
538: _testFindNextFails(_frm, 0, 163);
539:
540: _frm.setSearchBackwards(true);
541: _frm.setFindWord("pub");
542: _testFindNextFails(_frm, 0, 163);
543: // System.err.println("testWholeWordSearchOnTestString1 completed");
544: }
545:
546: public void testWholeWordSearchIgnore() throws BadLocationException {
547: _doc.insertString(0, IGNORE_TEXT, null);
548: // System.err.println(IGNORE_TEXT);
549: _initFrm(0);
550: _frm.setFindWord("plt");
551: _frm.setMatchWholeWord();
552: _frm.setIgnoreCommentsAndStrings(true);
553: _frm.setSearchBackwards(false);
554:
555: _testFindNextSucceeds(_frm, 0, 12);
556: _testFindNextSucceeds(_frm, 0, 25);
557: _testFindNextSucceeds(_frm, 0, 40);
558: _testFindNextSucceeds(_frm, 0, 53);
559: _testFindNextSucceeds(_frm, 0, 66);
560: _testFindNextSucceeds(_frm, 0, 75);
561: _testFindNextSucceeds(_frm, 0, 85);
562: _testFindNextSucceeds(_frm, 0, 95);
563: _frm.setLastFindWord();
564: _frm.setSearchBackwards(true);
565: _testFindNextSucceeds(_frm, 0, 82);
566: _testFindNextSucceeds(_frm, 0, 72);
567: _testFindNextSucceeds(_frm, 0, 63);
568: _testFindNextSucceeds(_frm, 0, 50);
569: _testFindNextSucceeds(_frm, 0, 37);
570: _testFindNextSucceeds(_frm, 0, 22);
571: _testFindNextSucceeds(_frm, 0, 9);
572: _testFindNextSucceeds(_frm, 0, 92);
573:
574: _frm.setSearchBackwards(false);
575: _frm.setFindWord("comment");
576: _testFindNextSucceeds(_frm, 0, 152);
577: _testFindNextSucceeds(_frm, 0, 206);
578: _testFindNextSucceeds(_frm, 0, 358);
579:
580: _frm.setLastFindWord();
581: _frm.setSearchBackwards(true);
582: _testFindNextSucceeds(_frm, 0, 199);
583: _testFindNextSucceeds(_frm, 0, 145);
584: _testFindNextSucceeds(_frm, 0, 351);
585:
586: _frm.setSearchBackwards(false);
587: _frm.setFindWord("@");
588: _testFindNextSucceeds(_frm, 0, 291);
589:
590: _frm.setLastFindWord();
591: _frm.setSearchBackwards(true);
592: _testFindNextSucceeds(_frm, 0, 290);
593:
594: _frm.setSearchBackwards(false);
595: _frm.setFindWord("string");
596: _testFindNextSucceeds(_frm, 0, 246);
597:
598: _frm.setLastFindWord();
599: _frm.setSearchBackwards(true);
600: _testFindNextSucceeds(_frm, 0, 240);
601: // System.err.println("testWholeWordSearchIgnore completed");
602: }
603:
604: public void testAnyOccurrenceSearchIgnore()
605: throws BadLocationException {
606: _doc.insertString(0, IGNORE_TEXT, null);
607: // System.err.println(IGNORE_TEXT);
608: _initFrm(0);
609: _frm.setFindWord("lt");
610: _frm.setIgnoreCommentsAndStrings(true);
611: _frm.setSearchBackwards(false);
612:
613: _testFindNextSucceeds(_frm, 0, 12);
614: _testFindNextSucceeds(_frm, 0, 25);
615: _testFindNextSucceeds(_frm, 0, 40);
616: _testFindNextSucceeds(_frm, 0, 53);
617: _testFindNextSucceeds(_frm, 0, 66);
618: _testFindNextSucceeds(_frm, 0, 75);
619: _testFindNextSucceeds(_frm, 0, 85);
620: _testFindNextSucceeds(_frm, 0, 95);
621: _frm.setLastFindWord();
622: _frm.setSearchBackwards(true);
623: _testFindNextSucceeds(_frm, 0, 83);
624: _testFindNextSucceeds(_frm, 0, 73);
625: _testFindNextSucceeds(_frm, 0, 64);
626: _testFindNextSucceeds(_frm, 0, 51);
627: _testFindNextSucceeds(_frm, 0, 38);
628: _testFindNextSucceeds(_frm, 0, 23);
629: _testFindNextSucceeds(_frm, 0, 10);
630: _testFindNextSucceeds(_frm, 0, 93);
631:
632: _frm.setSearchBackwards(false);
633: _frm.setFindWord("ment");
634: _testFindNextSucceeds(_frm, 0, 152);
635: _testFindNextSucceeds(_frm, 0, 206);
636: _testFindNextSucceeds(_frm, 0, 358);
637:
638: _frm.setLastFindWord();
639: _frm.setSearchBackwards(true);
640: _testFindNextSucceeds(_frm, 0, 202);
641: _testFindNextSucceeds(_frm, 0, 148);
642: _testFindNextSucceeds(_frm, 0, 354);
643:
644: _frm.setSearchBackwards(false);
645: _frm.setFindWord("@");
646: _testFindNextSucceeds(_frm, 0, 291);
647:
648: _frm.setLastFindWord();
649: _frm.setSearchBackwards(true);
650: _testFindNextSucceeds(_frm, 0, 290);
651:
652: _frm.setSearchBackwards(false);
653: _frm.setFindWord("ring");
654: _testFindNextSucceeds(_frm, 0, 246);
655:
656: _frm.setLastFindWord();
657: _frm.setSearchBackwards(true);
658: _testFindNextSucceeds(_frm, 0, 242);
659: // System.err.println("testAnyOccurrenceSearchIgnore completed");
660: }
661:
662: private void _testFindNextSucceeds(final FindReplaceMachine frm,
663: int start, final int found, OpenDefinitionsDocument doc) {
664: Utilities.invokeAndWait(new Runnable() {
665: public void run() {
666: try {
667: _result = frm.findNext();
668: OpenDefinitionsDocument newDoc = _result
669: .getDocument();
670: if (frm.getDocument() != newDoc) {
671: // do FindReplacePanel's _updateMachine
672: // Utilities.show("return doc = " + d + " distinct from current machine doc = " + frm.getDocument());
673: frm.setDocument(newDoc);
674: frm.setPosition(found);
675: }
676: } catch (Exception e) {
677: throw new UnexpectedException(e);
678: }
679: }
680: });
681:
682: Utilities.clearEventQueue();
683: assertEquals("documents should equal", doc.toString(), frm
684: .getDocument().toString());
685: assertEquals("findNext return value", found, _result
686: .getFoundOffset());
687: _assertOffsets(frm, start, found);
688: assertTrue("on find text", frm.onMatch());
689: }
690:
691: private void _testFindNextSucceeds(final FindReplaceMachine frm,
692: int start, int found) {
693: Utilities.invokeAndWait(new Runnable() {
694: public void run() {
695: try {
696: _offset = frm.findNext().getFoundOffset();
697: } catch (Exception e) {
698: throw new UnexpectedException(e);
699: }
700: }
701: });
702: assertEquals("findNext return value", found, _offset);
703: _assertOffsets(frm, start, found);
704: assertTrue("on find text", frm.onMatch());
705: }
706:
707: private void _testFindNextFails(final FindReplaceMachine frm,
708: int start, int current) {
709: Utilities.invokeAndWait(new Runnable() {
710: public void run() {
711: try {
712: _offset = frm.findNext().getFoundOffset();
713: } catch (Exception e) {
714: throw new UnexpectedException(e);
715: }
716: }
717: });
718: assertEquals("findNext return value", -1, _offset);
719: _assertOffsets(frm, start, current);
720: }
721:
722: private void _assertOffsets(FindReplaceMachine frm, int start,
723: int current) {
724: // Utilities.show("_assertOffsets(" + start + ", " + current + ")");
725: // assertEquals("start offset", start, frm.getStartOffset());
726: assertEquals("current offset", current, frm.getCurrentOffset());
727: }
728:
729: private void replaceAll() {
730: Utilities.invokeAndWait(new Runnable() {
731: public void run() {
732: _frm.replaceAll();
733: }
734: });
735: }
736:
737: // /** A thunk returning boolean. */
738: // private interface ContinueCommand {
739: // public boolean shouldContinue();
740: // }
741:
742: // private static ContinueCommand CONTINUE = new ContinueCommand() {
743: // public boolean shouldContinue() {
744: // return true;
745: // }
746: // };
747: }
|