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 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
45
46
47 final class LoggerTest {
48
49 @Test
50 void detectsLoggerNameCorrectly() {
51
52 }
53
54 @Test
55 void detectsNameOfStaticSource() {
56
57 }
58
59 @Test
60 void setsLoggingLevel() {
61
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 writer.print("hello, \u20ac, how're\u040a?\nI'm fine, \u0000\u0007!\n");
87 writer.flush();
88 writer.close();
89 }
90
91 @Test
92 void throwsWhenParamsLessThanFormatArgs() {
93 Assertions.assertThrows(
94 ArrayIndexOutOfBoundsException.class,
95 () -> Logger.format("String %s Char %c Number %d", "howdy", 'x')
96 );
97 }
98
99 @Test
100 void throwsWhenParamsMoreThanFormatArgs() {
101 Assertions.assertThrows(
102 IllegalArgumentException.class,
103 () -> Logger.format("String %s Number %d Char %c", "hey", 1, 'x', 2)
104 );
105 }
106
107 @Test
108 void checksLogLevel() throws Exception {
109 LogManager.getRootLogger().setLevel(org.apache.log4j.Level.INFO);
110 TimeUnit.MILLISECONDS.sleep(1L);
111 MatcherAssert.assertThat(
112 Logger.isEnabled(Level.INFO, LogManager.getRootLogger()),
113 Matchers.is(true)
114 );
115 MatcherAssert.assertThat(
116 Logger.isEnabled(Level.FINEST, LogManager.getRootLogger()),
117 Matchers.is(false)
118 );
119 }
120
121 @Test
122 void usesStringAsLoggerName() {
123 Logger.info("com.jcabi.log...why.not", "hello, %s!", "world!");
124 }
125
126 @Test
127 void findsArgsByPositions() {
128 final String first = "xyz";
129 final String second = "ddd";
130 MatcherAssert.assertThat(
131 Logger.format("first: %s, first again: %1$s %s", first, second),
132 Matchers.endsWith(
133 String.format(": %s, first again: %1$s %s", first, second)
134 )
135 );
136 }
137
138 }