|
17 | 17 |
|
18 | 18 | import static org.junit.Assert.assertEquals;
|
19 | 19 |
|
| 20 | +import java.io.ByteArrayInputStream; |
| 21 | +import java.lang.reflect.Field; |
20 | 22 | import java.util.ArrayList;
|
| 23 | +import java.util.Arrays; |
21 | 24 | import java.util.List;
|
22 | 25 | import java.util.regex.Pattern;
|
23 | 26 |
|
|
26 | 29 | import org.junit.ClassRule;
|
27 | 30 | import org.junit.Test;
|
28 | 31 |
|
| 32 | +import org.eclipse.core.runtime.ContributorFactorySimple; |
29 | 33 | import org.eclipse.core.runtime.CoreException;
|
| 34 | +import org.eclipse.core.runtime.IContributor; |
| 35 | +import org.eclipse.core.runtime.IExtensionRegistry; |
| 36 | +import org.eclipse.core.runtime.Platform; |
30 | 37 |
|
31 | 38 | import org.eclipse.core.resources.IFile;
|
32 | 39 | import org.eclipse.core.resources.IFolder;
|
@@ -547,6 +554,70 @@ private TestResult[] performSearch(TestResultCollector collector, String[] fileN
|
547 | 554 |
|
548 | 555 | }
|
549 | 556 |
|
| 557 | + @Test |
| 558 | + public void testBinaryContentTypeWithDescriberSerial() throws Exception { |
| 559 | + testBinaryContentTypeWithDescriber(new SerialTestResultCollector()); |
| 560 | + } |
| 561 | + |
| 562 | + @Test |
| 563 | + public void testBinaryContentTypeWithDescriberParallel() throws Exception { |
| 564 | + testBinaryContentTypeWithDescriber(new ParallelTestResultCollector()); |
| 565 | + } |
| 566 | + |
| 567 | + private void testBinaryContentTypeWithDescriber(TestResultCollector collector) throws Exception { |
| 568 | + IExtensionRegistry registry= Platform.getExtensionRegistry(); |
| 569 | + |
| 570 | + Field field= org.eclipse.core.internal.registry.ExtensionRegistry.class |
| 571 | + .getDeclaredField("masterToken"); |
| 572 | + field.setAccessible(true); |
| 573 | + Object masterToken= field.get(registry); |
| 574 | + |
| 575 | + IContributor contributor= ContributorFactorySimple.createContributor(this); |
| 576 | + |
| 577 | + try (java.io.InputStream is= new ByteArrayInputStream(""" |
| 578 | + <?xml version="1.0"?> |
| 579 | + <plugin> |
| 580 | + <extension point="org.eclipse.core.contenttype.contentTypes"> |
| 581 | + <content-type |
| 582 | + id="org.eclipse.search.tests.binaryFile" |
| 583 | + name="Search Test Binary File" |
| 584 | + priority="low"> |
| 585 | + <describer |
| 586 | + class="org.eclipse.core.runtime.content.BinarySignatureDescriber" |
| 587 | + plugin="org.eclipse.core.contenttype"> |
| 588 | + <!-- "binary" in ASCII encoding --> |
| 589 | + <parameter name="signature" value="62 69 6E 61 72 79"/> |
| 590 | + </describer> |
| 591 | + </content-type> |
| 592 | + <file-association |
| 593 | + content-type="org.eclipse.search.tests.binaryFile" |
| 594 | + file-patterns="[^.]+"/> <!-- no file extension --> |
| 595 | + </extension> |
| 596 | + </plugin>""".getBytes())) { |
| 597 | + registry.addContribution(is, contributor, false, null, null, masterToken); |
| 598 | + try (AutoCloseable c= () -> { |
| 599 | + Arrays.stream(registry.getExtensions(contributor)) |
| 600 | + .forEach(extension -> registry.removeExtension(extension, masterToken)); |
| 601 | + }) { |
| 602 | + |
| 603 | + IFolder folder= ResourceHelper.createFolder(fProject.getFolder("folder1")); |
| 604 | + IFile textfile= ResourceHelper.createFile(folder, "textfile", "text hello"); |
| 605 | + IFile binaryfile= ResourceHelper.createFile(folder, "binaryfile", "binary hello"); |
| 606 | + |
| 607 | + Pattern searchPattern= PatternConstructor.createPattern("hello", true, false); |
| 608 | + |
| 609 | + FileTextSearchScope scope= FileTextSearchScope.newSearchScope(new IResource[] { fProject }, (String[]) null, false); |
| 610 | + TextSearchEngine.create().search(scope, collector, searchPattern, null); |
| 611 | + |
| 612 | + TestResult[] results= collector.getResults(); |
| 613 | + assertEquals("Number of total results", 1, results.length); |
| 614 | + |
| 615 | + assertMatches(results, 1, textfile, "text hello", "hello"); |
| 616 | + assertMatches(results, 0, binaryfile, "binary hello", "hello"); |
| 617 | + } |
| 618 | + } |
| 619 | + } |
| 620 | + |
550 | 621 |
|
551 | 622 |
|
552 | 623 | private void assertMatches(TestResult[] results, int expectedCount, IFile file, String fileContent, String string) {
|
|
0 commit comments