Skip to content

Commit 782eb0a

Browse files
authored
Fix line separators in JSON logging tests (elastic#38771)
The hardcoded '\n' in string will not work in Windows where there is a different line separator. A System.lineSeparator should be used to make it work on all platforms closes elastic#38705
1 parent 90e6fb0 commit 782eb0a

File tree

2 files changed

+31
-29
lines changed

2 files changed

+31
-29
lines changed

qa/logging-config/src/test/java/org/elasticsearch/common/logging/JsonLoggerTests.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
* It has to be in a <code>org.elasticsearch.common.logging</code> package to use <code>PrefixLogger</code>
4747
*/
4848
public class JsonLoggerTests extends ESTestCase {
49+
private static final String LINE_SEPARATOR = System.lineSeparator();
4950

5051
@BeforeClass
5152
public static void initNodeName() {
@@ -109,15 +110,15 @@ public void testPrefixLoggerInJson() throws IOException {
109110

110111
public void testJsonInMessage() throws IOException {
111112
final Logger testLogger = LogManager.getLogger("test");
112-
String json = "{\n" +
113-
" \"terms\" : {\n" +
114-
" \"user\" : [\n" +
115-
" \"u1\",\n" +
116-
" \"u2\",\n" +
117-
" \"u3\"\n" +
118-
" ],\n" +
119-
" \"boost\" : 1.0\n" +
120-
" }\n" +
113+
String json = "{" + LINE_SEPARATOR +
114+
" \"terms\" : {" + LINE_SEPARATOR +
115+
" \"user\" : [" + LINE_SEPARATOR +
116+
" \"u1\"," + LINE_SEPARATOR +
117+
" \"u2\"," + LINE_SEPARATOR +
118+
" \"u3\"" + LINE_SEPARATOR +
119+
" ]," + LINE_SEPARATOR +
120+
" \"boost\" : 1.0" + LINE_SEPARATOR +
121+
" }" + LINE_SEPARATOR +
121122
"}";
122123

123124
testLogger.info(json);
@@ -151,15 +152,15 @@ public void testStacktrace() throws IOException {
151152
public void testJsonInStacktraceMessageIsSplitted() throws IOException {
152153
final Logger testLogger = LogManager.getLogger("test");
153154

154-
String json = "{\n" +
155-
" \"terms\" : {\n" +
156-
" \"user\" : [\n" +
157-
" \"u1\",\n" +
158-
" \"u2\",\n" +
159-
" \"u3\"\n" +
160-
" ],\n" +
161-
" \"boost\" : 1.0\n" +
162-
" }\n" +
155+
String json = "{" + LINE_SEPARATOR +
156+
" \"terms\" : {" + LINE_SEPARATOR +
157+
" \"user\" : [" + LINE_SEPARATOR +
158+
" \"u1\"," + LINE_SEPARATOR +
159+
" \"u2\"," + LINE_SEPARATOR +
160+
" \"u3\"" + LINE_SEPARATOR +
161+
" ]," + LINE_SEPARATOR +
162+
" \"boost\" : 1.0" + LINE_SEPARATOR +
163+
" }" + LINE_SEPARATOR +
163164
"}";
164165
testLogger.error("error message " + json, new Exception(json));
165166

server/src/test/java/org/elasticsearch/common/logging/JsonThrowablePatternConverterTests.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
import static org.hamcrest.Matchers.equalTo;
3333

3434
public class JsonThrowablePatternConverterTests extends ESTestCase {
35-
JsonThrowablePatternConverter converter = JsonThrowablePatternConverter.newInstance(null, null);
35+
private static final String LINE_SEPARATOR = System.lineSeparator();
36+
private JsonThrowablePatternConverter converter = JsonThrowablePatternConverter.newInstance(null, null);
3637

3738
public void testNoStacktrace() throws IOException {
3839
LogEvent event = Log4jLogEvent.newBuilder()
@@ -48,15 +49,15 @@ public void testNoStacktrace() throws IOException {
4849

4950
public void testStacktraceWithJson() throws IOException {
5051

51-
String json = "{\n" +
52-
" \"terms\" : {\n" +
53-
" \"user\" : [\n" +
54-
" \"u1\",\n" +
55-
" \"u2\",\n" +
56-
" \"u3\"\n" +
57-
" ],\n" +
58-
" \"boost\" : 1.0\n" +
59-
" }\n" +
52+
String json = "{" + LINE_SEPARATOR +
53+
" \"terms\" : {" + LINE_SEPARATOR +
54+
" \"user\" : [" + LINE_SEPARATOR +
55+
" \"u1\"," + LINE_SEPARATOR +
56+
" \"u2\"," + LINE_SEPARATOR +
57+
" \"u3\"" + LINE_SEPARATOR +
58+
" ]," + LINE_SEPARATOR +
59+
" \"boost\" : 1.0" + LINE_SEPARATOR +
60+
" }" + LINE_SEPARATOR +
6061
"}";
6162
Exception thrown = new Exception(json);
6263
LogEvent event = Log4jLogEvent.newBuilder()
@@ -73,7 +74,7 @@ public void testStacktraceWithJson() throws IOException {
7374
.findFirst()
7475
.orElseThrow(() -> new AssertionError("no logs parsed"));
7576

76-
int jsonLength = json.split("\n").length;
77+
int jsonLength = json.split(LINE_SEPARATOR).length;
7778
int stacktraceLength = thrown.getStackTrace().length;
7879
assertThat("stacktrace should formatted in multiple lines. JsonLogLine= " + jsonLogLine+" result= "+result,
7980
jsonLogLine.stacktrace().size(), equalTo(jsonLength + stacktraceLength));

0 commit comments

Comments
 (0)