mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Valery Kharseko
yesterday 6356883aae48bd09070693eb0e324e2765022983
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions copyright [year] [name of copyright owner]".
 *
 * Copyright 2026 3A Systems, LLC.
 */
package org.opends.server.loggers;
 
import static org.assertj.core.api.Assertions.assertThat;
 
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
 
import org.opends.server.DirectoryServerTestCase;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
 
/** Tests the decoration of the arguments of a debug log message. */
@SuppressWarnings("javadoc")
public class DebugMessageFormatterTest extends DirectoryServerTestCase
{
  @DataProvider
  public Object[][] arrayArguments()
  {
    return new Object[][] {
      { new boolean[] { true, false },    "[ true, false ]" },
      { new byte[] { 1, -2 },             "[ 1, -2 ]" },
      { new char[] { 'a', 'b' },          "[ a, b ]" },
      { new double[] { 1.5, 2.0 },        "[ 1.5, 2.0 ]" },
      { new float[] { 1.5f, 2.0f },       "[ 1.5, 2.0 ]" },
      { new int[] { 1, 2 },               "[ 1, 2 ]" },
      { new long[] { 1L, 2L },            "[ 1, 2 ]" },
      { new short[] { 1, 2 },             "[ 1, 2 ]" },
      { new String[] { "a", "b" },        "[ a, b ]" },
      { new boolean[0],                   "[  ]" },
    };
  }
 
  /** Arrays are formatted element by element, whatever their component type. */
  @Test(dataProvider = "arrayArguments")
  public void testArrayArgumentIsDecorated(Object array, String expected)
  {
    assertThat(DebugMessageFormatter.format("%s", new Object[] { array })).isEqualTo(expected);
  }
 
  /** Lists, maps and object arrays decorate their elements in turn. */
  @Test
  public void testNestedArgumentsAreDecorated()
  {
    final Map<String, Object> map = new LinkedHashMap<>();
    map.put("key", new int[] { 1, 2 });
 
    assertThat(DebugMessageFormatter.format("%s", new Object[] { map })).isEqualTo("{ key=[ 1, 2 ] }");
    assertThat(DebugMessageFormatter.format("%s", new Object[] { Arrays.asList("a", new int[] { 3 }) }))
        .isEqualTo("[ a, [ 3 ] ]");
    assertThat(DebugMessageFormatter.format("%s", new Object[] { new Object[] { new char[] { 'x' } } }))
        .isEqualTo("[ [ x ] ]");
  }
 
  @Test
  public void testNonArrayArgumentsAreLeftAlone()
  {
    assertThat(DebugMessageFormatter.format("%s and %s", new Object[] { "text", 42 })).isEqualTo("text and 42");
    assertThat(DebugMessageFormatter.format("%s", new Object[] { null })).isEqualTo("null");
  }
 
  /** A format string which does not match its arguments falls back to concatenation. */
  @Test
  public void testInvalidFormatFallsBackToConcatenation()
  {
    assertThat(DebugMessageFormatter.format("%d", new Object[] { new int[] { 1 } })).isEqualTo("%d [ 1 ]");
    assertThat(DebugMessageFormatter.format(null, new Object[] { new int[] { 1 } })).isEqualTo(" [ 1 ]");
  }
}