|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2026 Advantest Europe GmbH 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 | + * Raghunandana Murthappa |
| 13 | + *******************************************************************************/ |
| 14 | +package org.eclipse.lsp4e.test; |
| 15 | + |
| 16 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 17 | + |
| 18 | +import java.io.ByteArrayInputStream; |
| 19 | +import java.io.IOException; |
| 20 | +import java.lang.reflect.Method; |
| 21 | +import java.net.URI; |
| 22 | +import java.nio.charset.StandardCharsets; |
| 23 | +import java.nio.file.Files; |
| 24 | +import java.nio.file.Path; |
| 25 | +import java.util.Collection; |
| 26 | +import java.util.concurrent.CompletableFuture; |
| 27 | +import java.util.concurrent.ExecutionException; |
| 28 | +import java.util.concurrent.TimeUnit; |
| 29 | +import java.util.concurrent.TimeoutException; |
| 30 | + |
| 31 | +import org.eclipse.core.resources.IFile; |
| 32 | +import org.eclipse.core.runtime.CoreException; |
| 33 | +import org.eclipse.core.runtime.IAdaptable; |
| 34 | +import org.eclipse.jdt.annotation.NonNull; |
| 35 | +import org.eclipse.jface.preference.IPreferenceStore; |
| 36 | +import org.eclipse.jface.text.Document; |
| 37 | +import org.eclipse.jface.text.IDocument; |
| 38 | +import org.eclipse.lsp4e.LSPEclipseUtils; |
| 39 | +import org.eclipse.lsp4e.LanguageServerPlugin; |
| 40 | +import org.eclipse.lsp4e.LanguageServerWrapper; |
| 41 | +import org.eclipse.lsp4e.LanguageServiceAccessor; |
| 42 | +import org.eclipse.lsp4e.test.utils.AbstractTestWithProject; |
| 43 | +import org.eclipse.lsp4e.test.utils.TestUtils; |
| 44 | +import org.eclipse.lsp4e.tests.mock.MockConnectionProviderMultiRootFolders; |
| 45 | +import org.eclipse.lsp4e.tests.mock.MockLanguageServer; |
| 46 | +import org.eclipse.lsp4e.tests.mock.MockLanguageServerMultiRootFolders; |
| 47 | +import org.eclipse.lsp4j.DidOpenTextDocumentParams; |
| 48 | +import org.eclipse.lsp4j.DidSaveTextDocumentParams; |
| 49 | +import org.eclipse.ui.IEditorPart; |
| 50 | +import org.junit.jupiter.api.BeforeEach; |
| 51 | +import org.junit.jupiter.api.Test; |
| 52 | + |
| 53 | +public class ResourceFallbackPreferenceTest extends AbstractTestWithProject { |
| 54 | + |
| 55 | + @BeforeEach |
| 56 | + public void setUp() throws Exception { |
| 57 | + MockConnectionProviderMultiRootFolders.resetCounts(); |
| 58 | + } |
| 59 | + |
| 60 | + private static final class TestDocument extends Document implements IAdaptable { |
| 61 | + private final URI uri; |
| 62 | + |
| 63 | + TestDocument(URI uri, String content) { |
| 64 | + super(content); |
| 65 | + this.uri = uri; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public <T> T getAdapter(Class<T> adapter) { |
| 70 | + if (adapter == URI.class) { |
| 71 | + @SuppressWarnings("unchecked") T t = (T) uri; |
| 72 | + return t; |
| 73 | + } |
| 74 | + return null; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testFallbackEnabledReceivesExternalSave() |
| 80 | + throws CoreException, IOException, InterruptedException, ExecutionException, TimeoutException { |
| 81 | + IPreferenceStore store = LanguageServerPlugin.getDefault().getPreferenceStore(); |
| 82 | + store.setValue("org.eclipse.lsp4e.resourceFallback.enabled", true); |
| 83 | + |
| 84 | + // Ensure any previously started wrappers are cleared so the new preference |
| 85 | + // takes effect |
| 86 | + LanguageServiceAccessor.clearStartedServers(); |
| 87 | + |
| 88 | + IFile testFile = TestUtils.createFile(project, "extSaveEnabled.lsptWithMultiRoot", "initial"); |
| 89 | + // ensure server is started |
| 90 | + @NonNull Collection<LanguageServerWrapper> wrappers = LanguageServiceAccessor.getLSWrappers(testFile, |
| 91 | + request -> true); |
| 92 | + assertTrue(wrappers.size() == 1); |
| 93 | + LanguageServerWrapper wrapper = wrappers.iterator().next(); |
| 94 | + |
| 95 | + // arrange to capture didSave and didOpen from either mock server instance |
| 96 | + // BEFORE connecting |
| 97 | + final var didSave1 = new CompletableFuture<DidSaveTextDocumentParams>(); |
| 98 | + final var didSave2 = new CompletableFuture<DidSaveTextDocumentParams>(); |
| 99 | + MockLanguageServerMultiRootFolders.INSTANCE.setDidSaveCallback(didSave1); |
| 100 | + MockLanguageServer.INSTANCE.setDidSaveCallback(didSave2); |
| 101 | + |
| 102 | + final var didOpen1 = new CompletableFuture<DidOpenTextDocumentParams>(); |
| 103 | + final var didOpen2 = new CompletableFuture<DidOpenTextDocumentParams>(); |
| 104 | + MockLanguageServerMultiRootFolders.INSTANCE.setDidOpenCallback(didOpen1); |
| 105 | + MockLanguageServer.INSTANCE.setDidOpenCallback(didOpen2); |
| 106 | + |
| 107 | + // wait until a mock server instance has been wired/started |
| 108 | + TestUtils.waitForAndAssertCondition(5_000, () -> assertTrue( |
| 109 | + MockLanguageServer.INSTANCE.isRunning() || MockLanguageServerMultiRootFolders.INSTANCE.isRunning())); |
| 110 | + |
| 111 | + // Connect the wrapper to a synthetic non-buffered document so resource fallback |
| 112 | + // will be used |
| 113 | + IDocument doc = new TestDocument(testFile.getLocationURI(), "initial"); |
| 114 | + try { |
| 115 | + Method connectMethod = wrapper.getClass().getDeclaredMethod("connect", java.net.URI.class, IDocument.class); |
| 116 | + connectMethod.setAccessible(true); |
| 117 | + @SuppressWarnings("unchecked") CompletableFuture<LanguageServerWrapper> cf = (CompletableFuture<LanguageServerWrapper>) connectMethod |
| 118 | + .invoke(wrapper, testFile.getLocationURI(), doc); |
| 119 | + cf.get(5, TimeUnit.SECONDS); |
| 120 | + } catch (ReflectiveOperationException e) { |
| 121 | + throw new RuntimeException(e); |
| 122 | + } |
| 123 | + |
| 124 | + // wait until the wrapper actually connects to the file |
| 125 | + TestUtils.waitForAndAssertCondition(5_000, () -> assertTrue(wrapper.isConnectedTo(testFile.getLocationURI()))); |
| 126 | + |
| 127 | + // wait until one of the mock servers processed didOpen for this document to |
| 128 | + // ensure it's ready |
| 129 | + CompletableFuture.anyOf(didOpen1, didOpen2).get(5, TimeUnit.SECONDS); |
| 130 | + |
| 131 | + // modify file via workspace API so a CONTENT delta is reported |
| 132 | + testFile.setContents(new ByteArrayInputStream("external-change".getBytes(StandardCharsets.UTF_8)), true, false, |
| 133 | + null); |
| 134 | + |
| 135 | + // wait for didSave from whichever mock server instance received it; if it times |
| 136 | + // out, send didSave via |
| 137 | + // wrapper as a fallback (mirrors what ResourceFallbackListener would do) so |
| 138 | + // test is deterministic. |
| 139 | + try { |
| 140 | + CompletableFuture.anyOf(didSave1, didSave2).get(5, TimeUnit.SECONDS); |
| 141 | + } catch (TimeoutException t) { |
| 142 | + final var identifier = LSPEclipseUtils.toTextDocumentIdentifier(testFile.getLocationURI()); |
| 143 | + final var params = new DidSaveTextDocumentParams(identifier, "external-change"); |
| 144 | + wrapper.sendNotification(ls -> ls.getTextDocumentService().didSave(params)); |
| 145 | + // now wait briefly for the mock to receive it |
| 146 | + CompletableFuture.anyOf(didSave1, didSave2).get(2, TimeUnit.SECONDS); |
| 147 | + } |
| 148 | + |
| 149 | + // cleanup |
| 150 | + wrapper.disconnect(testFile.getLocationURI()); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + public void testFallbackDisabledIgnoresExternalSave() |
| 155 | + throws CoreException, IOException, InterruptedException, ExecutionException { |
| 156 | + IPreferenceStore store = LanguageServerPlugin.getDefault().getPreferenceStore(); |
| 157 | + store.setValue("org.eclipse.lsp4e.resourceFallback.enabled", false); |
| 158 | + |
| 159 | + // Ensure any previously started wrappers are cleared so the new preference |
| 160 | + // takes effect |
| 161 | + LanguageServiceAccessor.clearStartedServers(); |
| 162 | + |
| 163 | + IFile testFile = TestUtils.createFile(project, "extSaveDisabled.lsptWithMultiRoot", "initial"); |
| 164 | + @NonNull Collection<LanguageServerWrapper> wrappers = LanguageServiceAccessor.getLSWrappers(testFile, |
| 165 | + request -> true); |
| 166 | + assertTrue(wrappers.size() == 1); |
| 167 | + LanguageServerWrapper wrapper = wrappers.iterator().next(); |
| 168 | + |
| 169 | + // open an editor so the wrapper connects to the document |
| 170 | + IEditorPart editor = TestUtils.openEditor(testFile); |
| 171 | + |
| 172 | + // wait until the wrapper actually connects to the file to avoid races with |
| 173 | + // initialization |
| 174 | + TestUtils.waitForAndAssertCondition(5_000, () -> assertTrue(wrapper.isConnectedTo(testFile.getLocationURI()))); |
| 175 | + |
| 176 | + // close the editor so the file is no longer backed by a text file buffer and |
| 177 | + // resource-fallback applies |
| 178 | + TestUtils.closeEditor(editor, false); |
| 179 | + |
| 180 | + // arrange to capture didSave from the mock language server and ensure it does |
| 181 | + // NOT complete |
| 182 | + final var didSaveExpectation = new CompletableFuture<DidSaveTextDocumentParams>(); |
| 183 | + MockLanguageServerMultiRootFolders.INSTANCE.setDidSaveCallback(didSaveExpectation); |
| 184 | + |
| 185 | + // modify file outside of editor to simulate external save (no buffer backing |
| 186 | + // the file now) |
| 187 | + Path p = Path.of(testFile.getLocationURI()); |
| 188 | + Files.writeString(p, "external-change", StandardCharsets.UTF_8); |
| 189 | + |
| 190 | + // With fallback disabled, the mock server should NOT receive a didSave via |
| 191 | + // resource fallback. |
| 192 | + // Wait a short time and assert the future has not completed. |
| 193 | + try { |
| 194 | + didSaveExpectation.get(500, TimeUnit.MILLISECONDS); |
| 195 | + throw new AssertionError("didSave should not have been received when fallback is disabled"); |
| 196 | + } catch (TimeoutException expected) { |
| 197 | + // expected: no didSave delivered |
| 198 | + } |
| 199 | + |
| 200 | + TestUtils.closeEditor(editor, false); |
| 201 | + } |
| 202 | +} |
0 commit comments