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 java.io.OutputStream;
33  import java.io.OutputStreamWriter;
34  import java.io.PrintWriter;
35  import java.util.concurrent.TimeUnit;
36  import java.util.logging.Level;
37  import org.apache.log4j.LogManager;
38  import org.hamcrest.MatcherAssert;
39  import org.hamcrest.Matchers;
40  import org.junit.jupiter.api.Assertions;
41  import org.junit.jupiter.api.Test;
42  
43  /**
44   * Test case for {@link Logger}.
45   * @since 0.1
46   */
47  final class LoggerTest {
48  
49      @Test
50      void detectsLoggerNameCorrectly() {
51          // not implemented yet
52      }
53  
54      @Test
55      void detectsNameOfStaticSource() {
56          // not implemented yet
57      }
58  
59      @Test
60      void setsLoggingLevel() {
61          // not implemented yet
62      }
63  
64      @Test
65      void doesntFormatArraysSinceTheyAreVarArgs() {
66          Assertions.assertThrows(
67              IllegalArgumentException.class,
68              () -> Logger.format("array: %[list]s", new Object[] {"hi", 1})
69          );
70      }
71  
72      @Test
73      void interpretsArraysAsVarArgs() {
74          MatcherAssert.assertThat(
75              Logger.format("array: %s : %d", new Object[] {"hello", 2}),
76              Matchers.is("array: hello : 2")
77          );
78      }
79  
80      @Test
81      void providesOutputStream() throws Exception {
82          final OutputStream stream = Logger.stream(Level.INFO, this);
83          final PrintWriter writer = new PrintWriter(
84              new OutputStreamWriter(stream, "UTF-8")
85          );
86          // @checkstyle LineLength (1 line)
87          writer.print("hello, \u20ac, how're\u040a?\nI'm fine, \u0000\u0007!\n");
88          writer.flush();
89          writer.close();
90      }
91  
92      @Test
93      void throwsWhenParamsLessThanFormatArgs() {
94          Assertions.assertThrows(
95              ArrayIndexOutOfBoundsException.class,
96              () -> Logger.format("String %s Char %c Number %d", "howdy", 'x')
97          );
98      }
99  
100     @Test
101     void throwsWhenParamsMoreThanFormatArgs() {
102         Assertions.assertThrows(
103             IllegalArgumentException.class,
104             () -> Logger.format("String %s Number %d Char %c", "hey", 1, 'x', 2)
105         );
106     }
107 
108     @Test
109     void checksLogLevel() throws Exception {
110         LogManager.getRootLogger().setLevel(org.apache.log4j.Level.INFO);
111         TimeUnit.MILLISECONDS.sleep(1L);
112         MatcherAssert.assertThat(
113             Logger.isEnabled(Level.INFO, LogManager.getRootLogger()),
114             Matchers.is(true)
115         );
116         MatcherAssert.assertThat(
117             Logger.isEnabled(Level.FINEST, LogManager.getRootLogger()),
118             Matchers.is(false)
119         );
120     }
121 
122     @Test
123     void usesStringAsLoggerName() {
124         Logger.info("com.jcabi.log...why.not", "hello, %s!", "world!");
125     }
126 
127     @Test
128     void findsArgsByPositions() {
129         final String first = "xyz";
130         final String second = "ddd";
131         MatcherAssert.assertThat(
132             Logger.format("first: %s, first again: %1$s %s", first, second),
133             Matchers.endsWith(
134                 String.format(": %s, first again: %1$s %s", first, second)
135             )
136         );
137     }
138 
139 }