Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Aeschlimann2005-04-13 09:11:47 +0000
committerMartin Aeschlimann2005-04-13 09:11:47 +0000
commitc737b776dac0e11e358ef0aab1fda971baa1dfee (patch)
tree984344dafa2eca7f5eb3e5040ffde7c75767fdbf /org.eclipse.search.tests
parent710f65f0d7faafe3596e453f31789c2878535110 (diff)
downloadeclipse.platform.text-c737b776dac0e11e358ef0aab1fda971baa1dfee.tar.gz
eclipse.platform.text-c737b776dac0e11e358ef0aab1fda971baa1dfee.tar.xz
eclipse.platform.text-c737b776dac0e11e358ef0aab1fda971baa1dfee.zip
90393 3.1 M6: File Global Search and Replace cannot write file
Diffstat (limited to 'org.eclipse.search.tests')
-rw-r--r--org.eclipse.search.tests/src/org/eclipse/search/tests/ResourceHelper.java25
-rw-r--r--org.eclipse.search.tests/src/org/eclipse/search/tests/filesearch/FileCharSequenceTests.java157
-rw-r--r--org.eclipse.search.tests/src/org/eclipse/search/tests/filesearch/FileSearchTests.java86
-rw-r--r--org.eclipse.search.tests/src/org/eclipse/search/tests/filesearch/LineBasedFileSearch.java2
4 files changed, 246 insertions, 24 deletions
diff --git a/org.eclipse.search.tests/src/org/eclipse/search/tests/ResourceHelper.java b/org.eclipse.search.tests/src/org/eclipse/search/tests/ResourceHelper.java
index 523c0341684..ac5c28844a0 100644
--- a/org.eclipse.search.tests/src/org/eclipse/search/tests/ResourceHelper.java
+++ b/org.eclipse.search.tests/src/org/eclipse/search/tests/ResourceHelper.java
@@ -17,6 +17,14 @@ import java.io.InputStream;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.core.runtime.Plugin;
+
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
@@ -27,13 +35,6 @@ import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourcesPlugin;
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.IPath;
-import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.NullProgressMonitor;
-import org.eclipse.core.runtime.Path;
-import org.eclipse.core.runtime.Platform;
-import org.eclipse.core.runtime.Plugin;
/**
* @since 3.0
@@ -108,15 +109,21 @@ public class ResourceHelper {
return folder;
}
- public static IFile createFile(IFolder folder, String name, String contents) throws CoreException {
+ public static IFile createFile(IFolder folder, String name, String contents, String encoding) throws CoreException, IOException {
IFile file= folder.getFile(name);
if (contents == null)
contents= ""; //$NON-NLS-1$
- InputStream inputStream= new ByteArrayInputStream(contents.getBytes());
+ InputStream inputStream= new ByteArrayInputStream(contents.getBytes(encoding));
file.create(inputStream, true, NULL_MONITOR);
+ file.setCharset(encoding, null);
+ inputStream.close();
return file;
}
+ public static IFile createFile(IFolder folder, String name, String contents) throws CoreException, IOException {
+ return createFile(folder, name, contents, "ISO-8859-1");
+ }
+
public static IFile createLinkedFile(IContainer container, IPath linkPath, File linkedFileTarget) throws CoreException {
IFile iFile= container.getFile(linkPath);
iFile.createLink(new Path(linkedFileTarget.getAbsolutePath()), IResource.ALLOW_MISSING_LOCAL, NULL_MONITOR);
diff --git a/org.eclipse.search.tests/src/org/eclipse/search/tests/filesearch/FileCharSequenceTests.java b/org.eclipse.search.tests/src/org/eclipse/search/tests/filesearch/FileCharSequenceTests.java
new file mode 100644
index 00000000000..070fdf72b52
--- /dev/null
+++ b/org.eclipse.search.tests/src/org/eclipse/search/tests/filesearch/FileCharSequenceTests.java
@@ -0,0 +1,157 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2005 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.search.tests.filesearch;
+
+import java.io.IOException;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.eclipse.core.runtime.CoreException;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IFolder;
+import org.eclipse.core.resources.IProject;
+
+import org.eclipse.search.internal.core.text.FileCharSequenceProvider;
+
+import org.eclipse.search.tests.ResourceHelper;
+
+public class FileCharSequenceTests extends TestCase {
+
+ private final String TEST_CONTENT= "ABCDEFGHIJKLMNOPQRSTUVWXYZÜöäüèéùabcdefghijklmnopqrstuvwxyz1234567890@\'\"\n$¢"; //€
+
+ private IProject fProject;
+
+ public FileCharSequenceTests(String name) {
+ super(name);
+ }
+
+ public static Test allTests() {
+ return setUpTest(new TestSuite(FileCharSequenceTests.class));
+ }
+
+ public static Test setUpTest(Test test) {
+ return new JUnitSourceSetup(test);
+ }
+
+ public static Test suite() {
+ return allTests();
+ }
+
+ protected void setUp() throws Exception {
+ fProject= ResourceHelper.createProject("my-project"); //$NON-NLS-1$
+ }
+
+ protected void tearDown() throws Exception {
+ ResourceHelper.deleteProject("my-project"); //$NON-NLS-1$
+ }
+
+
+ public void testFileCharSequence() throws Exception {
+ StringBuffer buf= new StringBuffer();
+ for (int i= 0; i < 500; i++) {
+ buf.append(TEST_CONTENT);
+ }
+ String encoding= "ISO-8859-1";
+ testForEncoding(buf, encoding);
+ }
+
+ public void testFileCharSequence2() throws Exception {
+ StringBuffer buf= new StringBuffer();
+ for (int i= 0; i < 2000; i++) {
+ buf.append(TEST_CONTENT);
+ }
+ String encoding= "UTF-8";
+ testForEncoding(buf, encoding);
+ }
+
+ public void testFileCharSequence3() throws Exception {
+ StringBuffer buf= new StringBuffer();
+ for (int i= 0; i < FileCharSequenceProvider.BUFFER_SIZE * 2; i++) {
+ buf.append(TEST_CONTENT);
+ }
+ String encoding= "UTF-16";
+ testForEncoding(buf, encoding);
+ }
+
+ private void testForEncoding(CharSequence buf, String encoding) throws CoreException, IOException {
+ IFolder folder= ResourceHelper.createFolder(fProject.getFolder("folder1"));
+ IFile file1= ResourceHelper.createFile(folder, "file1", buf.toString(), encoding);
+
+ FileCharSequenceProvider provider= new FileCharSequenceProvider();
+ CharSequence cs= null;
+ try {
+ cs= provider.newCharSequence(file1);
+
+ assertEquals(encoding, cs, buf);
+
+ assertSubSequence(encoding, cs, buf, 0, buf.length() / 6);
+ assertSubSequence(encoding, cs, buf, buf.length() / 6, FileCharSequenceProvider.BUFFER_SIZE);
+
+ int pos= 8 * buf.length() / 9;
+ assertSubSequence(encoding, cs, buf, pos, buf.length() - pos);
+
+ CharSequence seq1a= cs.subSequence(100, 100 + FileCharSequenceProvider.BUFFER_SIZE);
+ CharSequence seq1e= buf.subSequence(100, 100 + FileCharSequenceProvider.BUFFER_SIZE);
+ assertSubSequence(encoding, seq1a, seq1e, 500, 500);
+
+ assertSubSequence(encoding, seq1a, seq1e, FileCharSequenceProvider.BUFFER_SIZE, 0);
+
+ } finally {
+ if (cs != null) {
+ provider.releaseCharSequence(cs);
+ }
+ file1.delete(true, null);
+ }
+ }
+
+ private void assertSubSequence(String message, CharSequence actual, CharSequence expected, int start, int length) {
+ CharSequence actualSub= actual.subSequence(start, start + length);
+ CharSequence expectedSub= expected.subSequence(start, start + length);
+ assertEquals(message + " - subsequence(" + start + ", " + length + ")", actualSub, expectedSub);
+ }
+
+ private void assertEquals(String desc, CharSequence actual, CharSequence expected) {
+ for (int i= 0; i < expected.length(); i++) {
+ assertEquals(desc + " - forward " + i, expected.charAt(i), actual.charAt(i));
+ }
+ assertEquals(desc + " - length", expected.length(), actual.length());
+ for (int i= expected.length() - 1; i >= 0; i--) {
+ assertEquals(desc + " - backward " + i, expected.charAt(i), actual.charAt(i));
+ }
+ for (int i= 0; i < expected.length(); i+= 567) {
+ assertEquals(desc + " - forward - steps" + i, expected.charAt(i), actual.charAt(i));
+ }
+ for (int i= 0; i < expected.length(); i+= FileCharSequenceProvider.BUFFER_SIZE) {
+ assertEquals(desc + " - forward - buffersize" + i, expected.charAt(i), actual.charAt(i));
+ }
+
+ assertOutOfBound(desc + "access at length", actual, expected.length());
+ assertOutOfBound(desc + "access at -1", actual, -1);
+
+ assertEquals(desc + " - length", actual.toString(), expected.toString());
+ }
+
+
+ private void assertOutOfBound(String message, CharSequence cs, int i) {
+ try {
+ cs.charAt(i);
+ } catch (IndexOutOfBoundsException e) {
+ return;
+ }
+ assertFalse(message, true);
+ }
+
+
+
+}
diff --git a/org.eclipse.search.tests/src/org/eclipse/search/tests/filesearch/FileSearchTests.java b/org.eclipse.search.tests/src/org/eclipse/search/tests/filesearch/FileSearchTests.java
index 2f8017ccf01..0dee4edd660 100644
--- a/org.eclipse.search.tests/src/org/eclipse/search/tests/filesearch/FileSearchTests.java
+++ b/org.eclipse.search.tests/src/org/eclipse/search/tests/filesearch/FileSearchTests.java
@@ -17,25 +17,27 @@ import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
-import org.eclipse.search.internal.core.SearchScope;
-import org.eclipse.search.internal.core.text.ITextSearchResultCollector;
-import org.eclipse.search.internal.core.text.MatchLocator;
-import org.eclipse.search.internal.core.text.TextSearchEngine;
-import org.eclipse.search.internal.ui.SearchPlugin;
-import org.eclipse.search.tests.ResourceHelper;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceProxy;
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.ide.IDE;
+import org.eclipse.search.internal.core.SearchScope;
+import org.eclipse.search.internal.core.text.ITextSearchResultCollector;
+import org.eclipse.search.internal.core.text.MatchLocator;
+import org.eclipse.search.internal.core.text.TextSearchEngine;
+import org.eclipse.search.internal.ui.SearchPlugin;
+
+import org.eclipse.search.tests.ResourceHelper;
+
public class FileSearchTests extends TestCase {
private static class TestResult {
@@ -105,6 +107,62 @@ public class FileSearchTests extends TestCase {
ResourceHelper.deleteProject("my-project"); //$NON-NLS-1$
}
+
+ /*
+ private void createFile(File file, String contents) throws IOException {
+ FileWriter fs= new FileWriter(file);
+ try {
+ fs.write(contents);
+ System.out.println(fs.getEncoding());
+ } finally {
+ fs.close();
+ }
+ }
+
+ public void testFileNIOTest1() throws Exception {
+ // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4724038
+ StringBuffer buf= new StringBuffer();
+ buf.append("File1\n");
+ buf.append("hello\n");
+ buf.append("more hello\n");
+ buf.append("world\n");
+
+
+ File file1= File.createTempFile("test", "txt");
+ createFile(file1, buf.toString());
+
+ File file2= File.createTempFile("test", "txt");
+ createFile(file2, buf.toString());
+ assertTrue(file2.delete());
+
+
+ InputStream stream= null;
+ try {
+ stream= new FileInputStream(file1);
+ if (stream instanceof FileInputStream) {
+ FileChannel channel= ((FileInputStream) stream).getChannel();
+ try {
+ MappedByteBuffer mappedBuffer= channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
+
+ Charset charset= Charset.forName("Cp1252");
+ CharsetDecoder decoder = charset.newDecoder();
+
+ CharSequence searchInput= decoder.decode(mappedBuffer);
+
+ int len= Math.min(searchInput.length(), 5);
+ CharSequence sequence= searchInput.subSequence(0, len);
+ System.out.println(sequence.toString());
+ } finally {
+ channel.close();
+ }
+ }
+ } finally {
+ stream.close();
+ }
+ assertTrue(file1.delete());
+ }
+ */
+
public void testSimpleFiles() throws Exception {
StringBuffer buf= new StringBuffer();
buf.append("File1\n");
@@ -120,7 +178,7 @@ public class FileSearchTests extends TestCase {
MatchLocator matchLocator= new MatchLocator("hello", false, true);
SearchScope scope= SearchScope.newSearchScope("test-project", new IResource[] { fProject });
- engine.search(scope, true, collector, matchLocator, true);
+ engine.search(scope, true, collector, matchLocator);
TestResult[] results= collector.getResults();
assertEquals("Number of total results", 4, results.length);
@@ -145,7 +203,7 @@ public class FileSearchTests extends TestCase {
MatchLocator matchLocator= new MatchLocator("mor*", false, false);
SearchScope scope= SearchScope.newSearchScope("test-project", new IResource[] { fProject });
- engine.search(scope, true, collector, matchLocator, true);
+ engine.search(scope, true, collector, matchLocator);
TestResult[] results= collector.getResults();
assertEquals("Number of total results", 6, results.length);
@@ -167,7 +225,7 @@ public class FileSearchTests extends TestCase {
MatchLocator matchLocator= new MatchLocator("mo?e", false, false);
SearchScope scope= SearchScope.newSearchScope("test-project", new IResource[] { fProject });
- engine.search(scope, true, collector, matchLocator, true);
+ engine.search(scope, true, collector, matchLocator);
TestResult[] results= collector.getResults();
assertEquals("Number of total results", 4, results.length);
@@ -196,7 +254,7 @@ public class FileSearchTests extends TestCase {
// search in Junit sources
SearchScope scope= SearchScope.newSearchScope("test-project", new IResource[] {project});
- engine.search(scope, true, collector, matchLocator, true);
+ engine.search(scope, true, collector, matchLocator);
TestResult[] results= collector.getResults();
assertEquals("Number of total results", 748, results.length);
@@ -228,7 +286,7 @@ public class FileSearchTests extends TestCase {
MatchLocator matchLocator= new MatchLocator("hello", false, true);
SearchScope scope= SearchScope.newSearchScope("test-project", new IResource[] {fProject});
- engine.search(scope, true, collector, matchLocator, true);
+ engine.search(scope, true, collector, matchLocator);
TestResult[] results= collector.getResults();
assertEquals("Number of total results", 4, results.length);
diff --git a/org.eclipse.search.tests/src/org/eclipse/search/tests/filesearch/LineBasedFileSearch.java b/org.eclipse.search.tests/src/org/eclipse/search/tests/filesearch/LineBasedFileSearch.java
index 92ede4142e3..2a6dfb585f9 100644
--- a/org.eclipse.search.tests/src/org/eclipse/search/tests/filesearch/LineBasedFileSearch.java
+++ b/org.eclipse.search.tests/src/org/eclipse/search/tests/filesearch/LineBasedFileSearch.java
@@ -126,7 +126,7 @@ public class LineBasedFileSearch extends FileSearchQuery {
final AbstractTextSearchResult textResult= (AbstractTextSearchResult) getSearchResult();
textResult.removeAll();
ITextSearchResultCollector collector= new LineBasedTextSearchResultCollector(textResult, pm);
- return new TextSearchEngine().search(fScope, false, collector, new MatchLocator(getSearchString(), isCaseSensitive(), isRegexSearch()), true);
+ return new TextSearchEngine().search(fScope, false, collector, new MatchLocator(getSearchString(), isCaseSensitive(), isRegexSearch()));
}

Back to the top