View Javadoc
1   /*
2    * Copyright (c) 2012-2023, jcabi.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the jcabi.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package com.jcabi.log;
31  
32  import org.apache.commons.text.StringEscapeUtils;
33  import org.apache.log4j.Level;
34  import org.apache.log4j.spi.LoggingEvent;
35  import org.hamcrest.MatcherAssert;
36  import org.hamcrest.Matchers;
37  import org.junit.jupiter.api.Assertions;
38  import org.junit.jupiter.api.Test;
39  import org.mockito.Mockito;
40  
41  /**
42   * Test case for {@link MulticolorLayout}.
43   *
44   * @since 0.1
45   */
46  final class MulticolorLayoutTest {
47  
48      /**
49       * Conversation pattern for test case.
50       */
51      private static final String CONV_PATTERN = "[%color{%p}] %color{%m}";
52  
53      @Test
54      void transformsLoggingEventToText() {
55          final MulticolorLayout layout = new MulticolorLayout();
56          layout.setConversionPattern(MulticolorLayoutTest.CONV_PATTERN);
57          final LoggingEvent event = Mockito.mock(LoggingEvent.class);
58          Mockito.doReturn(Level.DEBUG).when(event).getLevel();
59          Mockito.doReturn("hello").when(event).getRenderedMessage();
60          MatcherAssert.assertThat(
61              StringEscapeUtils.escapeJava(layout.format(event)),
62              Matchers.equalTo(
63                  "[\\u001B[2;37mDEBUG\\u001B[m] \\u001B[2;37mhello\\u001B[m"
64              )
65          );
66      }
67  
68      @Test
69      void overwriteDefaultColor() {
70          final MulticolorLayout layout = new MulticolorLayout();
71          layout.setConversionPattern(MulticolorLayoutTest.CONV_PATTERN);
72          layout.setLevels("INFO:2;10");
73          final LoggingEvent event = Mockito.mock(LoggingEvent.class);
74          Mockito.doReturn(Level.INFO).when(event).getLevel();
75          Mockito.doReturn("change").when(event).getRenderedMessage();
76          MatcherAssert.assertThat(
77              StringEscapeUtils.escapeJava(layout.format(event)),
78              Matchers.equalTo(
79                  "[\\u001B[2;10mINFO\\u001B[m] \\u001B[2;10mchange\\u001B[m"
80              )
81          );
82      }
83  
84      @Test
85      void rendersCustomConstantColor() {
86          final MulticolorLayout layout = new MulticolorLayout();
87          layout.setConversionPattern("%color-red{%p} %m");
88          final LoggingEvent event = Mockito.mock(LoggingEvent.class);
89          Mockito.doReturn(Level.DEBUG).when(event).getLevel();
90          Mockito.doReturn("foo").when(event).getRenderedMessage();
91          MatcherAssert.assertThat(
92              StringEscapeUtils.escapeJava(layout.format(event)),
93              Matchers.equalTo("\\u001B[31mDEBUG\\u001B[m foo")
94          );
95      }
96  
97      @Test
98      void overwriteCustomConstantColor() {
99          final MulticolorLayout layout = new MulticolorLayout();
100         layout.setConversionPattern("%color-white{%p} %m");
101         layout.setColors("white:10");
102         final LoggingEvent event = Mockito.mock(LoggingEvent.class);
103         Mockito.doReturn(Level.DEBUG).when(event).getLevel();
104         Mockito.doReturn("const").when(event).getRenderedMessage();
105         MatcherAssert.assertThat(
106             StringEscapeUtils.escapeJava(layout.format(event)),
107             Matchers.equalTo("\\u001B[10mDEBUG\\u001B[m const")
108         );
109     }
110 
111     @Test
112     void rendersAnsiConstantColor() {
113         final MulticolorLayout layout = new MulticolorLayout();
114         layout.setConversionPattern("%color-0;0;31{%p} %m");
115         final LoggingEvent event = Mockito.mock(LoggingEvent.class);
116         Mockito.doReturn(Level.DEBUG).when(event).getLevel();
117         Mockito.doReturn("bar").when(event).getRenderedMessage();
118         MatcherAssert.assertThat(
119             StringEscapeUtils.escapeJava(layout.format(event)),
120             Matchers.equalTo("\\u001B[0;0;31mDEBUG\\u001B[m bar")
121         );
122     }
123 
124     @Test
125     void throwsOnIllegalColorName() {
126         Assertions.assertThrows(
127             IllegalArgumentException.class,
128             () -> {
129                 final MulticolorLayout layout = new MulticolorLayout();
130                 layout.setConversionPattern("%color-oops{%p} %m");
131                 final LoggingEvent event = Mockito.mock(LoggingEvent.class);
132                 Mockito.doReturn(Level.DEBUG).when(event).getLevel();
133                 Mockito.doReturn("text").when(event).getRenderedMessage();
134                 layout.format(event);
135             }
136         );
137     }
138 
139 }