|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Vegard IT GmbH and others. |
| 3 | + * This program and the accompanying materials are made |
| 4 | + * available under the terms of the Eclipse Public License 2.0 |
| 5 | + * which is available at https://www.eclipse.org/legal/epl-2.0/ |
| 6 | + * |
| 7 | + * SPDX-License-Identifier: EPL-2.0 |
| 8 | + * |
| 9 | + * Contributors: |
| 10 | + * Sebastian Thomschke (Vegard IT GmbH) - initial implementation |
| 11 | + *******************************************************************************/ |
| 12 | +package org.eclipse.lsp4e.test.server; |
| 13 | + |
| 14 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 15 | +import static org.junit.Assert.*; |
| 16 | + |
| 17 | +import java.io.ByteArrayInputStream; |
| 18 | +import java.io.ByteArrayOutputStream; |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.InputStream; |
| 21 | +import java.io.OutputStream; |
| 22 | +import java.net.URI; |
| 23 | + |
| 24 | +import org.eclipse.lsp4e.server.StreamConnectionProvider; |
| 25 | +import org.eclipse.lsp4j.jsonrpc.messages.Message; |
| 26 | +import org.eclipse.lsp4j.services.LanguageServer; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +public class StreamConnectionProviderTest { |
| 30 | + |
| 31 | + private static StreamConnectionProvider newProvider() { |
| 32 | + return new StreamConnectionProvider() { |
| 33 | + @Override |
| 34 | + public void start() throws IOException { |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public InputStream getInputStream() { |
| 39 | + return null; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public OutputStream getOutputStream() { |
| 44 | + return null; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public InputStream getErrorStream() { |
| 49 | + return null; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void stop() { |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void handleMessage(Message message, LanguageServer languageServer, URI rootURI) { |
| 58 | + } |
| 59 | + }; |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void test_forwardCopy_singleByteRead_writesToProvidedOutput() throws Exception { |
| 64 | + final var input = new ByteArrayInputStream("ABC".getBytes(UTF_8)); |
| 65 | + final var sink = new ByteArrayOutputStream(); |
| 66 | + |
| 67 | + try (InputStream forwarding = newProvider().forwardCopyTo(input, sink)) { |
| 68 | + while ((forwarding.read()) != -1) { |
| 69 | + // read one byte at a time to exercise single-byte read path |
| 70 | + } |
| 71 | + } |
| 72 | + assertEquals("expected input to be forwarded to provided OutputStream", "ABC", sink.toString(UTF_8)); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void test_forwardCopy_readArray_onEOF_returnsMinusOne_noException() throws Exception { |
| 77 | + final var emptyInput = new ByteArrayInputStream(new byte[0]); |
| 78 | + final var sink = new ByteArrayOutputStream(); |
| 79 | + |
| 80 | + try (final var forwarding = newProvider().forwardCopyTo(emptyInput, sink)) { |
| 81 | + final var buf = new byte[8]; |
| 82 | + int n = forwarding.read(buf); // should be -1 and not throw |
| 83 | + assertEquals("expected EOF (-1) on empty stream", -1, n); |
| 84 | + } |
| 85 | + assertArrayEquals(new byte[0], sink.toByteArray()); |
| 86 | + } |
| 87 | +} |
0 commit comments