|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2026 Eclipse Foundation and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * Arcadiy Ivanov - initial API and implementation |
| 13 | + *******************************************************************************/ |
| 14 | +package org.eclipse.jdt.core.tests.model; |
| 15 | + |
| 16 | +import junit.framework.Test; |
| 17 | +import org.eclipse.core.runtime.CoreException; |
| 18 | +import org.eclipse.jdt.core.IJavaProject; |
| 19 | +import org.eclipse.jdt.core.search.SearchEngine; |
| 20 | +import org.eclipse.jdt.core.search.SearchParticipant; |
| 21 | +import org.eclipse.jdt.internal.core.search.indexing.SearchParticipantRegistry; |
| 22 | + |
| 23 | +/** |
| 24 | + * Tests for the {@code org.eclipse.jdt.core.searchParticipant} extension point |
| 25 | + * and the {@link SearchParticipantRegistry}. |
| 26 | + */ |
| 27 | +public class DerivedSourceSearchParticipantTests extends ModifyingResourceTests { |
| 28 | + |
| 29 | + IJavaProject project; |
| 30 | + |
| 31 | + public DerivedSourceSearchParticipantTests(String name) { |
| 32 | + super(name); |
| 33 | + } |
| 34 | + |
| 35 | + public static Test suite() { |
| 36 | + return buildModelTestSuite(DerivedSourceSearchParticipantTests.class); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + protected void setUp() throws Exception { |
| 41 | + super.setUp(); |
| 42 | + TestDerivedSourceSearchParticipant.reset(); |
| 43 | + SearchParticipantRegistry.reset(); |
| 44 | + this.project = createJavaProject("DSP", new String[] {"src"}, "bin"); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + protected void tearDown() throws Exception { |
| 49 | + deleteProject("DSP"); |
| 50 | + this.project = null; |
| 51 | + super.tearDown(); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Verifies that the registry discovers the {@code .langx} extension |
| 56 | + * from the test plugin's contribution. |
| 57 | + */ |
| 58 | + public void testRegistryHasParticipantForLangx() { |
| 59 | + assertTrue("Registry should have participant for langx", |
| 60 | + SearchParticipantRegistry.hasParticipant("langx")); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Verifies that the registry does not report a participant for |
| 65 | + * an unregistered extension. |
| 66 | + */ |
| 67 | + public void testRegistryNoParticipantForUnknownExtension() { |
| 68 | + assertFalse("Registry should not have participant for unknown_ext", |
| 69 | + SearchParticipantRegistry.hasParticipant("unknown_ext")); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Verifies that {@code getParticipant("langx")} returns a non-null |
| 74 | + * participant of the correct type and that the instance is reused. |
| 75 | + */ |
| 76 | + public void testRegistryGetParticipantSingleton() { |
| 77 | + SearchParticipant p1 = SearchParticipantRegistry.getParticipant("langx"); |
| 78 | + assertNotNull("Should return a participant for langx", p1); |
| 79 | + assertTrue("Should be a TestDerivedSourceSearchParticipant", |
| 80 | + p1 instanceof TestDerivedSourceSearchParticipant); |
| 81 | + SearchParticipant p2 = SearchParticipantRegistry.getParticipant("langx"); |
| 82 | + assertSame("Same instance should be returned on second call", p1, p2); |
| 83 | + assertEquals("Exactly one instance should be created", |
| 84 | + 1, TestDerivedSourceSearchParticipant.instanceCount.get()); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Verifies that {@code getContributedParticipants()} includes the |
| 89 | + * test participant. |
| 90 | + */ |
| 91 | + public void testGetContributedParticipants() { |
| 92 | + SearchParticipant[] contributed = SearchParticipantRegistry.getContributedParticipants(); |
| 93 | + assertTrue("Should have at least one contributed participant", |
| 94 | + contributed.length >= 1); |
| 95 | + boolean found = false; |
| 96 | + for (SearchParticipant p : contributed) { |
| 97 | + if (p instanceof TestDerivedSourceSearchParticipant) { |
| 98 | + found = true; |
| 99 | + break; |
| 100 | + } |
| 101 | + } |
| 102 | + assertTrue("Contributed participants should include TestDerivedSourceSearchParticipant", |
| 103 | + found); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Verifies that {@link SearchEngine#getSearchParticipants()} returns both |
| 108 | + * the default participant and contributed participants. |
| 109 | + */ |
| 110 | + public void testGetSearchParticipants() { |
| 111 | + SearchParticipant[] participants = SearchEngine.getSearchParticipants(); |
| 112 | + assertTrue("Should have at least 2 participants (default + contributed)", |
| 113 | + participants.length >= 2); |
| 114 | + boolean hasDefault = false; |
| 115 | + boolean hasContributed = false; |
| 116 | + SearchParticipant defaultP = SearchEngine.getDefaultSearchParticipant(); |
| 117 | + for (SearchParticipant p : participants) { |
| 118 | + if (p.getClass() == defaultP.getClass()) { |
| 119 | + hasDefault = true; |
| 120 | + } |
| 121 | + if (p instanceof TestDerivedSourceSearchParticipant) { |
| 122 | + hasContributed = true; |
| 123 | + } |
| 124 | + } |
| 125 | + assertTrue("Should include the default Java search participant", hasDefault); |
| 126 | + assertTrue("Should include the contributed test participant", hasContributed); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Verifies that {@code getFileExtension()} correctly extracts extensions. |
| 131 | + */ |
| 132 | + public void testGetFileExtension() { |
| 133 | + assertEquals("kt", SearchParticipantRegistry.getFileExtension("Foo.kt")); |
| 134 | + assertEquals("langx", SearchParticipantRegistry.getFileExtension("Bar.langx")); |
| 135 | + assertEquals("java", SearchParticipantRegistry.getFileExtension("Baz.java")); |
| 136 | + assertNull(SearchParticipantRegistry.getFileExtension("noextension")); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Verifies that adding a {@code .langx} file to a source folder triggers |
| 141 | + * the search participant's {@code indexDocument()} method via the |
| 142 | + * automatic indexing pipeline. |
| 143 | + */ |
| 144 | + public void testIndexingTriggeredForDerivedSourceFile() throws CoreException { |
| 145 | + createFile( |
| 146 | + "/DSP/src/Hello.langx", |
| 147 | + "public class Hello {\n" + |
| 148 | + " public void greet() {}\n" + |
| 149 | + "}" |
| 150 | + ); |
| 151 | + waitUntilIndexesReady(); |
| 152 | + assertTrue("indexDocument should have been called at least once", |
| 153 | + TestDerivedSourceSearchParticipant.indexDocumentCallCount.get() > 0); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Verifies that adding a second {@code .langx} file triggers additional |
| 158 | + * indexing calls. |
| 159 | + */ |
| 160 | + public void testIndexingTriggeredForMultipleDerivedSourceFiles() throws CoreException { |
| 161 | + createFile( |
| 162 | + "/DSP/src/Alpha.langx", |
| 163 | + "public class Alpha {\n" + |
| 164 | + " public int value() { return 1; }\n" + |
| 165 | + "}" |
| 166 | + ); |
| 167 | + createFile( |
| 168 | + "/DSP/src/Beta.langx", |
| 169 | + "public class Beta {\n" + |
| 170 | + " public int value() { return 2; }\n" + |
| 171 | + "}" |
| 172 | + ); |
| 173 | + waitUntilIndexesReady(); |
| 174 | + assertTrue("indexDocument should have been called at least twice", |
| 175 | + TestDerivedSourceSearchParticipant.indexDocumentCallCount.get() >= 2); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Verifies that creating a {@code .langx} file in a subfolder/package |
| 180 | + * triggers indexing via the add-folder-to-index path. |
| 181 | + */ |
| 182 | + public void testIndexingInSubPackage() throws CoreException { |
| 183 | + createFolder("/DSP/src/pkg"); |
| 184 | + createFile( |
| 185 | + "/DSP/src/pkg/InPackage.langx", |
| 186 | + "package pkg;\n" + |
| 187 | + "public class InPackage {}" |
| 188 | + ); |
| 189 | + waitUntilIndexesReady(); |
| 190 | + assertTrue("indexDocument should have been called for file in subpackage", |
| 191 | + TestDerivedSourceSearchParticipant.indexDocumentCallCount.get() > 0); |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * Verifies that deleting a {@code .langx} file does not crash |
| 196 | + * and that the index is updated. |
| 197 | + */ |
| 198 | + public void testDeletionOfDerivedSourceFile() throws CoreException { |
| 199 | + createFile( |
| 200 | + "/DSP/src/ToDelete.langx", |
| 201 | + "public class ToDelete {}" |
| 202 | + ); |
| 203 | + waitUntilIndexesReady(); |
| 204 | + |
| 205 | + // delete the file — should not throw |
| 206 | + deleteFile("/DSP/src/ToDelete.langx"); |
| 207 | + waitUntilIndexesReady(); |
| 208 | + // If we get here without an exception, the delta processor handled removal correctly |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * Verifies that regular {@code .java} files are not routed to the |
| 213 | + * derived source participant. |
| 214 | + */ |
| 215 | + public void testJavaFilesNotRoutedToParticipant() throws CoreException { |
| 216 | + createFile( |
| 217 | + "/DSP/src/Regular.java", |
| 218 | + "public class Regular {}" |
| 219 | + ); |
| 220 | + waitUntilIndexesReady(); |
| 221 | + assertEquals("indexDocument should not be called for .java files", |
| 222 | + 0, TestDerivedSourceSearchParticipant.indexDocumentCallCount.get()); |
| 223 | + } |
| 224 | + |
| 225 | + /** |
| 226 | + * Verifies that the registry reset clears cached participants |
| 227 | + * and forces re-loading on next access. |
| 228 | + */ |
| 229 | + public void testRegistryReset() { |
| 230 | + SearchParticipant before = SearchParticipantRegistry.getParticipant("langx"); |
| 231 | + assertNotNull(before); |
| 232 | + TestDerivedSourceSearchParticipant.reset(); |
| 233 | + SearchParticipantRegistry.reset(); |
| 234 | + |
| 235 | + SearchParticipant after = SearchParticipantRegistry.getParticipant("langx"); |
| 236 | + assertNotNull(after); |
| 237 | + assertNotSame("After reset, a new instance should be created", before, after); |
| 238 | + assertEquals("New instance should have been created after reset", |
| 239 | + 1, TestDerivedSourceSearchParticipant.instanceCount.get()); |
| 240 | + } |
| 241 | +} |
0 commit comments