View Javadoc
1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2023, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  
15  package ch.qos.logback.core.encoder;
16  
17  import org.junit.jupiter.api.Test;
18  
19  import static org.junit.jupiter.api.Assertions.*;
20  
21  class JsonEscapeUtilTest {
22  
23      @Test
24      public void smokeTestEscapeCodes() {
25          assertEquals("\\u0001", JsonEscapeUtil.ESCAPE_CODES[1]);
26          assertEquals("\\u0005", JsonEscapeUtil.ESCAPE_CODES[5]);
27          assertEquals("\\b", JsonEscapeUtil.ESCAPE_CODES[8]);
28          assertEquals("\\t", JsonEscapeUtil.ESCAPE_CODES[9]);
29          assertEquals("\\n", JsonEscapeUtil.ESCAPE_CODES[0x0A]);
30          assertEquals("\\u000B", JsonEscapeUtil.ESCAPE_CODES[0x0B]);
31          assertEquals("\\f", JsonEscapeUtil.ESCAPE_CODES[0x0C]);
32          assertEquals("\\r", JsonEscapeUtil.ESCAPE_CODES[0x0D]);
33          assertEquals("\\u000E", JsonEscapeUtil.ESCAPE_CODES[0x0E]);
34  
35          assertEquals("\\u001A", JsonEscapeUtil.ESCAPE_CODES[0x1A]);
36      }
37  
38      @Test
39      public void smokeTestEscapeString() {
40          assertEquals("abc", JsonEscapeUtil.jsonEscapeString("abc"));
41          assertEquals("{world: \\\"world\\\"}", JsonEscapeUtil.jsonEscapeString("{world: \"world\"}"));
42          assertEquals("{world: "+'\\'+'"'+"world\\\"}", JsonEscapeUtil.jsonEscapeString("{world: \"world\"}"));
43      }
44  
45      @Test
46      public void testEscapingLF() {
47          String input = "{\nhello: \"wo\nrld\"}";
48          System.out.println(input);
49          assertEquals("{\\nhello: "+'\\'+'"'+"wo\\nrld\\\"}", JsonEscapeUtil.jsonEscapeString(input));
50      }
51  
52      @Test
53      public void testEscapingTab() {
54          String input = "{hello: \"\tworld\"}";
55          System.out.println(input);
56          assertEquals("{hello: "+'\\'+'"'+"\\tworld\\\"}", JsonEscapeUtil.jsonEscapeString(input));
57      }
58  }