-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
WriterAppenderTest.java
104 lines (90 loc) · 3.84 KB
/
WriterAppenderTest.java
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache license, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the license for the specific language governing permissions and
* limitations under the license.
*/
package org.apache.logging.log4j.core.appender;
import java.io.ByteArrayOutputStream;
import java.io.CharArrayWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.sql.SQLException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
/**
* Tests {@link WriterAppender}.
*/
public class WriterAppenderTest {
private static final String TEST_MSG = "FOO ERROR";
@Rule
public TestName testName = new TestName();
private String getName(final Writer writer) {
return writer.getClass().getSimpleName() + "." + testName.getMethodName();
}
private void test(final ByteArrayOutputStream out, final Writer writer) throws SQLException {
final String name = getName(writer);
addAppender(writer, name);
final Logger logger = LogManager.getLogger(name);
logger.error(TEST_MSG);
final String actual = out.toString();
Assert.assertTrue(actual, actual.contains(TEST_MSG));
}
private void test(final Writer writer) throws SQLException {
final String name = getName(writer);
addAppender(writer, name);
final Logger logger = LogManager.getLogger(name);
logger.error(TEST_MSG);
final String actual = writer.toString();
Assert.assertTrue(actual, actual.contains(TEST_MSG));
}
private void addAppender(final Writer writer, final String writerName) {
final LoggerContext context = LoggerContext.getContext(false);
final Configuration config = context.getConfiguration();
final PatternLayout layout = PatternLayout.createDefaultLayout(config);
final Appender appender = WriterAppender.createAppender(layout, null, writer, writerName, false, true);
appender.start();
config.addAppender(appender);
ConfigurationTestUtils.updateLoggers(appender, config);
}
@Test
public void testWriterAppenderToCharArrayWriter() throws SQLException {
test(new CharArrayWriter());
}
@Test
public void testWriterAppenderToOutputStreamWriter() throws SQLException {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final Writer writer = new OutputStreamWriter(out);
test(out, writer);
}
@Test
public void testWriterAppenderToPrintWriter() throws SQLException {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final Writer writer = new PrintWriter(out);
test(out, writer);
}
@Test
public void testWriterAppenderToStringWriter() throws SQLException {
test(new StringWriter());
}
}