Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Maeder2004-05-24 08:10:41 +0000
committerThomas Maeder2004-05-24 08:10:41 +0000
commitd55343e1bacf412a4b899262de7bb73531e14be6 (patch)
treed9d178695ecc2c336799d3ffbeed530ddf760dce /org.eclipse.search.tests/src
parent6e18de7760dcbfb2b92483d03f1f23b3f1562ef7 (diff)
downloadeclipse.platform.text-d55343e1bacf412a4b899262de7bb73531e14be6.tar.gz
eclipse.platform.text-d55343e1bacf412a4b899262de7bb73531e14be6.tar.xz
eclipse.platform.text-d55343e1bacf412a4b899262de7bb73531e14be6.zip
fix for bug 62959
Diffstat (limited to 'org.eclipse.search.tests/src')
-rw-r--r--org.eclipse.search.tests/src/org/eclipse/search/tests/filesearch/PositionTrackerTest.java128
1 files changed, 128 insertions, 0 deletions
diff --git a/org.eclipse.search.tests/src/org/eclipse/search/tests/filesearch/PositionTrackerTest.java b/org.eclipse.search.tests/src/org/eclipse/search/tests/filesearch/PositionTrackerTest.java
new file mode 100644
index 00000000000..c735186c5e2
--- /dev/null
+++ b/org.eclipse.search.tests/src/org/eclipse/search/tests/filesearch/PositionTrackerTest.java
@@ -0,0 +1,128 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2004 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Common Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/cpl-v10.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.search.tests.filesearch;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.eclipse.core.filebuffers.FileBuffers;
+import org.eclipse.core.filebuffers.ITextFileBuffer;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.Position;
+import org.eclipse.search.internal.core.text.TextSearchScope;
+import org.eclipse.search.internal.ui.SearchPlugin;
+import org.eclipse.search.internal.ui.text.FileSearchQuery;
+import org.eclipse.search.internal.ui.text.FileSearchResult;
+import org.eclipse.search.ui.NewSearchUI;
+import org.eclipse.search.ui.text.AbstractTextSearchResult;
+import org.eclipse.search.ui.text.Match;
+import org.eclipse.search2.internal.ui.InternalSearchUI;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.ide.IDE;
+
+public class PositionTrackerTest extends TestCase {
+ FileSearchQuery fQuery1;
+
+ public PositionTrackerTest(String name) {
+ super(name);
+ }
+
+ public static Test allTests() {
+ return new JUnitSetup(new TestSuite(PositionTrackerTest.class));
+ }
+
+ public static Test suite() {
+ return allTests();
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ TextSearchScope scope= TextSearchScope.newWorkspaceScope();
+ scope.addExtension("*.java");
+ fQuery1= new FileSearchQuery(scope, "", "Test");
+ }
+
+ public void testInsertAt0() throws Exception {
+ NewSearchUI.activateSearchResultView();
+ NewSearchUI.runQueryInForeground(null, fQuery1);
+ AbstractTextSearchResult result= (AbstractTextSearchResult) fQuery1.getSearchResult();
+ Object[] elements= result.getElements();
+ try {
+ for (int i = 0; i < elements.length; i++) {
+ checkInsertAtZero(result, (IFile) elements[i]);
+ }
+ } finally {
+ SearchPlugin.getActivePage().closeAllEditors(false);
+ }
+ }
+
+ public void testInsertInsideMatch() throws Exception {
+ NewSearchUI.activateSearchResultView();
+ NewSearchUI.runQueryInForeground(null, fQuery1);
+ FileSearchResult result= (FileSearchResult) fQuery1.getSearchResult();
+ Object[] elements= result.getElements();
+ try {
+ for (int i = 0; i < elements.length; i++) {
+ checkInsertInsideMatch(result, (IFile) elements[i]);
+ }
+ } finally {
+ SearchPlugin.getActivePage().closeAllEditors(false);
+ }
+ }
+
+
+ private void checkInsertInsideMatch(FileSearchResult result, IFile file) throws PartInitException, BadLocationException {
+ Match[] matches= result.getMatches(file);
+ IDE.openEditor(SearchPlugin.getActivePage(), file);
+ ITextFileBuffer fb= FileBuffers.getTextFileBufferManager().getTextFileBuffer(file.getLocation());
+ IDocument doc= fb.getDocument();
+
+ for (int i = 0; i < matches.length; i++) {
+ Position currentPosition= InternalSearchUI.getInstance().getPositionTracker().getCurrentPosition(matches[i]);
+ assertNotNull(currentPosition);
+ doc.replace(currentPosition.offset+1, 0, "Test");
+ }
+
+ for (int i = 0; i < matches.length; i++) {
+ Position currentPosition= InternalSearchUI.getInstance().getPositionTracker().getCurrentPosition(matches[i]);
+ assertNotNull(currentPosition);
+ String text= doc.get(currentPosition.offset, currentPosition.length);
+ StringBuffer buf= new StringBuffer();
+ buf.append(text.charAt(0));
+ buf.append(text.substring(5));
+ assertEquals(buf.toString(), ((FileSearchQuery)result.getQuery()).getSearchString());
+ }
+}
+
+ private void checkInsertAtZero(AbstractTextSearchResult result, IFile file) throws PartInitException, BadLocationException {
+ Match[] matches= result.getMatches(file);
+ int[] originalStarts= new int[matches.length];
+ for (int i = 0; i < originalStarts.length; i++) {
+ originalStarts[i]= matches[i].getOffset();
+ }
+ IDE.openEditor(SearchPlugin.getActivePage(), file);
+ ITextFileBuffer fb= FileBuffers.getTextFileBufferManager().getTextFileBuffer(file.getLocation());
+ IDocument doc= fb.getDocument();
+ doc.replace(0, 0, "Test");
+
+ for (int i = 0; i < originalStarts.length; i++) {
+ Position currentPosition= InternalSearchUI.getInstance().getPositionTracker().getCurrentPosition(matches[i]);
+ assertNotNull(currentPosition);
+ assertEquals(originalStarts[i]+"Test".length(), currentPosition.getOffset());
+
+ }
+ }
+
+}

Back to the top