Use NIO and final.
@@ -19,9 +19,10 @@ package org.apache.log4j.config;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
-import java.io.File;
-import java.io.FileInputStream;
import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
@@ -45,49 +46,48 @@ public class AsyncAppenderTest {
@Test
public void testAsyncXml() throws Exception {
- LoggerContext loggerContext = configure("target/test-classes/log4j1-async.xml");
- Logger logger = LogManager.getLogger("test");
+ final LoggerContext loggerContext = configure("target/test-classes/log4j1-async.xml");
+ final Logger logger = LogManager.getLogger("test");
logger.debug("This is a test of the root logger");
Thread.sleep(50);
- Configuration configuration = loggerContext.getConfiguration();
- Map<String, Appender> appenders = configuration.getAppenders();
+ final Configuration configuration = loggerContext.getConfiguration();
+ final Map<String, Appender> appenders = configuration.getAppenders();
ListAppender messageAppender = null;
- for (Map.Entry<String, Appender> entry : appenders.entrySet()) {
+ for (final Map.Entry<String, Appender> entry : appenders.entrySet()) {
if (entry.getKey().equals("list")) {
messageAppender = (ListAppender) ((AppenderAdapter.Adapter) entry.getValue()).getAppender();
}
}
assertNotNull("No Message Appender", messageAppender);
- List<String> messages = messageAppender.getMessages();
+ final List<String> messages = messageAppender.getMessages();
assertTrue("No messages", messages != null && messages.size() > 0);
}
@Test
public void testAsyncProperties() throws Exception {
- LoggerContext loggerContext = configure("target/test-classes/log4j1-async.properties");
- Logger logger = LogManager.getLogger("test");
+ final LoggerContext loggerContext = configure("target/test-classes/log4j1-async.properties");
+ final Logger logger = LogManager.getLogger("test");
logger.debug("This is a test of the root logger");
Thread.sleep(50);
- Configuration configuration = loggerContext.getConfiguration();
- Map<String, Appender> appenders = configuration.getAppenders();
+ final Configuration configuration = loggerContext.getConfiguration();
+ final Map<String, Appender> appenders = configuration.getAppenders();
ListAppender messageAppender = null;
- for (Map.Entry<String, Appender> entry : appenders.entrySet()) {
+ for (final Map.Entry<String, Appender> entry : appenders.entrySet()) {
if (entry.getKey().equals("list")) {
messageAppender = (ListAppender) ((AppenderAdapter.Adapter) entry.getValue()).getAppender();
}
}
assertNotNull("No Message Appender", messageAppender);
- List<String> messages = messageAppender.getMessages();
+ final List<String> messages = messageAppender.getMessages();
assertTrue("No messages", messages != null && messages.size() > 0);
}
- private LoggerContext configure(String configLocation) throws Exception {
- File file = new File(configLocation);
- InputStream is = new FileInputStream(file);
- ConfigurationSource source = new ConfigurationSource(is, file);
- LoggerContextFactory factory = org.apache.logging.log4j.LogManager.getFactory();
- LoggerContext context = (LoggerContext) org.apache.logging.log4j.LogManager.getContext(false);
+ private LoggerContext configure(final String configLocation) throws Exception {
+ final Path path = Paths.get(configLocation);
+ final InputStream is = Files.newInputStream(path);
+ final ConfigurationSource source = new ConfigurationSource(is, path.toFile());
+ final LoggerContext context = (LoggerContext) org.apache.logging.log4j.LogManager.getContext(false);
Configuration configuration;
if (configLocation.endsWith(".xml")) {
configuration = new XmlConfigurationFactory().getConfiguration(context, source);
