|
1 | | -/* |
2 | | - * This Java source file was generated by the Gradle 'init' task. |
3 | | - */ |
| 1 | +// Copyright 2022 The NATS Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at: |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
4 | 13 | package nats.io; |
5 | 14 |
|
6 | 15 | import org.junit.jupiter.api.Test; |
7 | 16 | import org.junit.jupiter.params.ParameterizedTest; |
8 | 17 | import org.junit.jupiter.params.provider.Arguments; |
9 | 18 | import org.junit.jupiter.params.provider.MethodSource; |
10 | 19 |
|
11 | | -import java.io.File; |
12 | 20 | import java.io.IOException; |
13 | | -import java.io.InputStream; |
14 | | -import java.net.InetSocketAddress; |
15 | | -import java.net.Socket; |
16 | | -import java.net.SocketAddress; |
17 | | -import java.nio.file.Files; |
18 | 21 | import java.util.Arrays; |
19 | | -import java.util.Collections; |
20 | | -import java.util.List; |
21 | 22 | import java.util.stream.Stream; |
22 | 23 |
|
23 | | -import static java.util.stream.Collectors.toList; |
24 | | -import static nats.io.NatsRunnerUtils.DEBUG_OPTION; |
25 | | -import static nats.io.NatsRunnerUtils.JETSTREAM_OPTION; |
26 | | -import static org.junit.jupiter.api.Assertions.assertEquals; |
27 | | -import static org.junit.jupiter.api.Assertions.assertTrue; |
28 | | -import static org.junit.platform.commons.util.CollectionUtils.toUnmodifiableList; |
29 | | - |
30 | | -class NatsServerRunnerTest { |
31 | | - |
32 | | - public static final String SOURCE_CONFIG_FILE_PATH = "src/test/resources/"; |
| 24 | +class NatsServerRunnerTest extends TestBase { |
33 | 25 |
|
34 | 26 | @Test |
35 | 27 | public void testWithoutConfigDefault() throws IOException, InterruptedException { |
@@ -95,81 +87,4 @@ public void testWithConfig(String configFile, boolean checkConnect) throws IOExc |
95 | 87 | } |
96 | 88 | } |
97 | 89 | } |
98 | | - |
99 | | - private void validateCommandLine(NatsServerRunner runner, boolean debug, boolean jetStream, String... customArgs) { |
100 | | - assertEquals(debug, runner.getCmdLine().contains(" " + DEBUG_OPTION)); |
101 | | - assertEquals(jetStream, runner.getCmdLine().contains(" " + JETSTREAM_OPTION)); |
102 | | - for (String ca : customArgs) { |
103 | | - assertTrue(runner.getCmdLine().contains(" " + ca)); |
104 | | - } |
105 | | - } |
106 | | - |
107 | | - private void validateHostAndPort(NatsServerRunner server) { |
108 | | - assertTrue(server.getPort() > 0); |
109 | | - assertTrue(server.getPort() != 1234); |
110 | | - assertTrue(server.getURI().startsWith("nats://localhost")); |
111 | | - } |
112 | | - |
113 | | - private void validateConfigLines(NatsServerRunner runner) throws IOException { |
114 | | - validateConfigLines(runner, null); |
115 | | - } |
116 | | - |
117 | | - private void validateConfigLines(NatsServerRunner runner, String configFile, String[] configInserts) throws IOException { |
118 | | - List<String> expected = Files.lines(new File(SOURCE_CONFIG_FILE_PATH + configFile).toPath()) |
119 | | - .map(String::trim) |
120 | | - .filter(s -> s.length() > 0) |
121 | | - .filter(s -> !s.contains("port")) |
122 | | - .collect(toList()); |
123 | | - Collections.addAll(expected, configInserts); |
124 | | - validateConfigLines(runner, expected); |
125 | | - } |
126 | | - |
127 | | - private void validateConfigLines(NatsServerRunner runner, List<String> expected) throws IOException { |
128 | | - List<String> lines = Files.lines(new File(runner.getConfigFile()).toPath()) |
129 | | - .map(String::trim) |
130 | | - .filter(s -> s.length() > 0) |
131 | | - .collect(toUnmodifiableList()); |
132 | | - |
133 | | - assertTrue(lines.contains("port: " + runner.getPort())); |
134 | | - if (expected != null) { |
135 | | - for (String ex : expected) { |
136 | | - assertTrue(lines.contains(ex)); |
137 | | - } |
138 | | - } |
139 | | - } |
140 | | - |
141 | | - private static final byte[] CONNECT_BYTES = "CONNECT {\"lang\":\"java\",\"version\":\"2.11.5\",\"protocol\":1,\"verbose\":false,\"pedantic\":false,\"tls_required\":false,\"echo\":true,\"headers\":true,\"no_responders\":true}\r\n".getBytes(); |
142 | | - private void connect(NatsServerRunner runner) throws IOException { |
143 | | - Socket socket = new Socket(); |
144 | | - SocketAddress socketAddress = new InetSocketAddress("127.0.0.1", runner.getPort()); |
145 | | - socket.connect(socketAddress); |
146 | | - assertEquals(runner.getPort(), socket.getPort()); |
147 | | - |
148 | | - socket.getOutputStream().write(CONNECT_BYTES); |
149 | | - socket.getOutputStream().flush(); |
150 | | - |
151 | | - InputStream in = socket.getInputStream(); |
152 | | - // give the server time to respond or this flaps |
153 | | - try { |
154 | | - Thread.sleep(100); |
155 | | - } catch (InterruptedException e) { |
156 | | - // ignore |
157 | | - } |
158 | | - |
159 | | - StringBuilder sb = new StringBuilder(); |
160 | | - int cr = 0; |
161 | | - int i = in.read(); |
162 | | - while (i != -1) { |
163 | | - sb.append((char)i); |
164 | | - if (i == 13) { |
165 | | - cr++; |
166 | | - } |
167 | | - i = (cr > 1) ? -1 : in.read(); |
168 | | - } |
169 | | - in.close(); |
170 | | - |
171 | | - String sbs = sb.toString().trim(); |
172 | | - assertTrue(sbs.startsWith("INFO")); |
173 | | - assertTrue(sbs.contains("\"port\":" + runner.getPort())); |
174 | | - } |
175 | 90 | } |
0 commit comments