diff --git a/bundles/org.eclipse.search.core/META-INF/MANIFEST.MF b/bundles/org.eclipse.search.core/META-INF/MANIFEST.MF index b7ae62f251e..7e060fe5eae 100644 --- a/bundles/org.eclipse.search.core/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.search.core/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %pluginName Bundle-SymbolicName: org.eclipse.search.core;singleton:=true -Bundle-Version: 3.16.400.qualifier +Bundle-Version: 3.16.500.qualifier Bundle-Activator: org.eclipse.search.internal.core.SearchCorePlugin Bundle-ActivationPolicy: lazy Bundle-Vendor: %providerName diff --git a/bundles/org.eclipse.search.core/search/org/eclipse/search/internal/core/text/TextSearchVisitor.java b/bundles/org.eclipse.search.core/search/org/eclipse/search/internal/core/text/TextSearchVisitor.java index 8a0354bf93f..0118aa4ea11 100644 --- a/bundles/org.eclipse.search.core/search/org/eclipse/search/internal/core/text/TextSearchVisitor.java +++ b/bundles/org.eclipse.search.core/search/org/eclipse/search/internal/core/text/TextSearchVisitor.java @@ -442,7 +442,13 @@ public IStatus search(TextSearchScope scope, IProgressMonitor monitor) { private final IContentType TEXT_TYPE = Platform.getContentTypeManager().getContentType(IContentTypeManager.CT_TEXT); private boolean hasBinaryContentType(IFile file) { - IContentType[] contentTypes = Platform.getContentTypeManager().findContentTypesFor(file.getName()); + IContentType[] contentTypes; + try (java.io.InputStream contents = file.getContents()) { + contentTypes = Platform.getContentTypeManager().findContentTypesFor(contents, file.getName()); + } catch (IOException | CoreException e) { + SearchCorePlugin.log(e); + contentTypes = Platform.getContentTypeManager().findContentTypesFor(file.getName()); + } for (IContentType contentType : contentTypes) { if (contentType.isKindOf(TEXT_TYPE)) { return false; // is text diff --git a/tests/org.eclipse.search.tests/META-INF/MANIFEST.MF b/tests/org.eclipse.search.tests/META-INF/MANIFEST.MF index ddd2d496922..204847946af 100644 --- a/tests/org.eclipse.search.tests/META-INF/MANIFEST.MF +++ b/tests/org.eclipse.search.tests/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %pluginName Bundle-SymbolicName: org.eclipse.search.tests;singleton:=true -Bundle-Version: 3.11.700.qualifier +Bundle-Version: 3.11.800.qualifier Bundle-Vendor: %providerName Bundle-Localization: plugin Export-Package: org.eclipse.search.core.tests;x-internal:=true, diff --git a/tests/org.eclipse.search.tests/src/org/eclipse/search/tests/filesearch/FileSearchTests.java b/tests/org.eclipse.search.tests/src/org/eclipse/search/tests/filesearch/FileSearchTests.java index 38791d8fb41..11ecc1f4a7c 100644 --- a/tests/org.eclipse.search.tests/src/org/eclipse/search/tests/filesearch/FileSearchTests.java +++ b/tests/org.eclipse.search.tests/src/org/eclipse/search/tests/filesearch/FileSearchTests.java @@ -17,7 +17,10 @@ import static org.junit.Assert.assertEquals; +import java.io.ByteArrayInputStream; +import java.lang.reflect.Field; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.regex.Pattern; @@ -26,7 +29,11 @@ import org.junit.ClassRule; import org.junit.Test; +import org.eclipse.core.runtime.ContributorFactorySimple; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IContributor; +import org.eclipse.core.runtime.IExtensionRegistry; +import org.eclipse.core.runtime.Platform; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; @@ -547,6 +554,70 @@ private TestResult[] performSearch(TestResultCollector collector, String[] fileN } + @Test + public void testBinaryContentTypeWithDescriberSerial() throws Exception { + testBinaryContentTypeWithDescriber(new SerialTestResultCollector()); + } + + @Test + public void testBinaryContentTypeWithDescriberParallel() throws Exception { + testBinaryContentTypeWithDescriber(new ParallelTestResultCollector()); + } + + private void testBinaryContentTypeWithDescriber(TestResultCollector collector) throws Exception { + IExtensionRegistry registry= Platform.getExtensionRegistry(); + + Field field= org.eclipse.core.internal.registry.ExtensionRegistry.class + .getDeclaredField("masterToken"); + field.setAccessible(true); + Object masterToken= field.get(registry); + + IContributor contributor= ContributorFactorySimple.createContributor(this); + + try (java.io.InputStream is= new ByteArrayInputStream(""" + + + + + + + + + + + + """.getBytes())) { + registry.addContribution(is, contributor, false, null, null, masterToken); + try (AutoCloseable c= () -> { + Arrays.stream(registry.getExtensions(contributor)) + .forEach(extension -> registry.removeExtension(extension, masterToken)); + }) { + + IFolder folder= ResourceHelper.createFolder(fProject.getFolder("folder1")); + IFile textfile= ResourceHelper.createFile(folder, "textfile", "text hello"); + IFile binaryfile= ResourceHelper.createFile(folder, "binaryfile", "binary hello"); + + Pattern searchPattern= PatternConstructor.createPattern("hello", true, false); + + FileTextSearchScope scope= FileTextSearchScope.newSearchScope(new IResource[] { fProject }, (String[]) null, false); + TextSearchEngine.create().search(scope, collector, searchPattern, null); + + TestResult[] results= collector.getResults(); + assertEquals("Number of total results", 1, results.length); + + assertMatches(results, 1, textfile, "text hello", "hello"); + assertMatches(results, 0, binaryfile, "binary hello", "hello"); + } + } + } + private void assertMatches(TestResult[] results, int expectedCount, IFile file, String fileContent, String string) {