|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * Language Server Protocol bridge for SciJava code intelligence. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2026 SciJava developers. |
| 6 | + * %% |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * |
| 10 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer. |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer in the documentation |
| 14 | + * and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 20 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | + * POSSIBILITY OF SUCH DAMAGE. |
| 27 | + * #L% |
| 28 | + */ |
| 29 | + |
| 30 | +package org.scijava.code.lsp.server; |
| 31 | + |
| 32 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 33 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 34 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 35 | +import static org.scijava.code.lsp.server.FakeServers.ENV; |
| 36 | +import static org.scijava.code.lsp.server.FakeServers.complete; |
| 37 | +import static org.scijava.code.lsp.server.FakeServers.open; |
| 38 | +import static org.scijava.code.lsp.server.FakeServers.waitFor; |
| 39 | + |
| 40 | +import java.util.List; |
| 41 | + |
| 42 | +import org.junit.jupiter.api.AfterEach; |
| 43 | +import org.junit.jupiter.api.BeforeEach; |
| 44 | +import org.junit.jupiter.api.Test; |
| 45 | +import org.scijava.Context; |
| 46 | +import org.scijava.log.LogService; |
| 47 | + |
| 48 | +/** |
| 49 | + * Tests {@link AbstractProcessLanguageServerPlugin}. |
| 50 | + * |
| 51 | + * @author Gabriel Selzer |
| 52 | + */ |
| 53 | +public class AbstractProcessLanguageServerPluginTest { |
| 54 | + |
| 55 | + private final FakeServers.Launcher launcher = new FakeServers.Launcher(); |
| 56 | + private final List<FakeServers.Server> started = launcher.started; |
| 57 | + private Context context; |
| 58 | + private AbstractProcessLanguageServerPlugin plugin; |
| 59 | + |
| 60 | + @BeforeEach |
| 61 | + public void setUp() { |
| 62 | + context = new Context(LogService.class); |
| 63 | + // An AbstractProcessLanguageServerPlugin using the fake launcher |
| 64 | + plugin = new AbstractProcessLanguageServerPlugin() { |
| 65 | + |
| 66 | + @Override |
| 67 | + protected ServerLauncher launcher() { |
| 68 | + return launcher; |
| 69 | + } |
| 70 | + }; |
| 71 | + context.inject(plugin); |
| 72 | + } |
| 73 | + |
| 74 | + @AfterEach |
| 75 | + public void tearDown() { |
| 76 | + plugin.dispose(); |
| 77 | + context.dispose(); |
| 78 | + } |
| 79 | + |
| 80 | + /** Tests that the plugin supports its launcher's language, and no other. */ |
| 81 | + @Test |
| 82 | + public void testSupports() { |
| 83 | + assertTrue(plugin.supports(launcher.language())); |
| 84 | + final String unsupportedLanguage = "not" + launcher.language(); |
| 85 | + assertFalse(plugin.supports(unsupportedLanguage)); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Tests that a process is not started when a server is created, only once |
| 90 | + * the server is used. |
| 91 | + */ |
| 92 | + @Test |
| 93 | + public void testLazyStart() throws Exception { |
| 94 | + final ProcessLanguageServer s = (ProcessLanguageServer) plugin.create( |
| 95 | + "python", ENV); |
| 96 | + assertEquals(0, started.size()); |
| 97 | + // Using it starts it |
| 98 | + final String uri = "untitled:/p.py"; |
| 99 | + open(s, uri, "x"); |
| 100 | + assertEquals(3, complete(s, uri).size()); |
| 101 | + assertEquals(1, started.size()); |
| 102 | + } |
| 103 | + |
| 104 | + /** Tests that disposing of the plugin stops its processes. */ |
| 105 | + @Test |
| 106 | + public void testDispose() throws Exception { |
| 107 | + final ProcessLanguageServer s = (ProcessLanguageServer) plugin.create( |
| 108 | + "python", ENV); |
| 109 | + final String uri = "untitled:/p.py"; |
| 110 | + open(s, uri, "x"); |
| 111 | + complete(s, uri); |
| 112 | + plugin.dispose(); |
| 113 | + waitFor(() -> started.get(0).shutdown); |
| 114 | + } |
| 115 | +} |
0 commit comments