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
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 }