Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
If an error occurred while setting up logback, the error message is n…
…ot now printed to the console
- Loading branch information
Christoffer Eide
committed
Feb 17, 2015
1 parent
dbe1fe4
commit 515a719
Showing
2 changed files
with
115 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
...ard-logging/src/test/java/io/dropwizard/logging/LoggingFactoryPrintErrorMessagesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,93 @@ | |||
package io.dropwizard.logging; | |||
|
|||
import ch.qos.logback.classic.LoggerContext; | |||
import ch.qos.logback.core.util.StatusPrinter; | |||
import com.codahale.metrics.MetricRegistry; | |||
import org.junit.After; | |||
import org.junit.Before; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
import org.junit.rules.TemporaryFolder; | |||
|
|||
import java.io.ByteArrayOutputStream; | |||
import java.io.File; | |||
import java.io.PrintStream; | |||
import java.io.UnsupportedEncodingException; | |||
import java.nio.charset.StandardCharsets; | |||
|
|||
import static java.util.Collections.singletonList; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
|
|||
public class LoggingFactoryPrintErrorMessagesTest { | |||
|
|||
@Rule | |||
public final TemporaryFolder tempDir = new TemporaryFolder(); | |||
|
|||
File folderWithoutWritePermission; | |||
File folderWithWritePermission; | |||
|
|||
LoggingFactory factory; | |||
LoggerContext loggerContext; | |||
|
|||
@Before | |||
public void setUp() throws Exception { | |||
folderWithoutWritePermission = tempDir.newFolder("folder-without-write-permission"); | |||
folderWithoutWritePermission.setWritable(false); | |||
|
|||
folderWithWritePermission = tempDir.newFolder("folder-with-write-permission"); | |||
|
|||
loggerContext = new LoggerContext(); | |||
factory = new LoggingFactory(loggerContext); | |||
} | |||
|
|||
@After | |||
public void tearDown() throws Exception { | |||
loggerContext.stop(); | |||
} | |||
|
|||
private void configureLoggingFactoryWithFileAppender(File file){ | |||
factory.setAppenders(singletonList(newFileAppenderFactory(file))); | |||
} | |||
|
|||
private AppenderFactory newFileAppenderFactory(File file){ | |||
FileAppenderFactory fileAppenderFactory = new FileAppenderFactory(); | |||
|
|||
fileAppenderFactory.setCurrentLogFilename(file.toString() + File.separator + "my-log-file.log"); | |||
fileAppenderFactory.setArchive(false); | |||
|
|||
return fileAppenderFactory; | |||
} | |||
|
|||
private String configureAndCaptureSystemOut() throws UnsupportedEncodingException { | |||
ByteArrayOutputStream out = new ByteArrayOutputStream(); | |||
|
|||
try { | |||
StatusPrinter.setPrintStream(new PrintStream(out)); | |||
factory.configure(new MetricRegistry(), "logger-test"); | |||
}finally { | |||
StatusPrinter.setPrintStream(System.out); | |||
} | |||
|
|||
return out.toString(StandardCharsets.UTF_8.name()); | |||
} | |||
|
|||
@Test | |||
public void testWhenFileAppenderDoesNotHaveWritePermissionToFolder_PrintsErrorMessageToConsole() throws Exception { | |||
File file = folderWithoutWritePermission; | |||
|
|||
configureLoggingFactoryWithFileAppender(file); | |||
|
|||
assertThat(file.canWrite()).isFalse(); | |||
assertThat(configureAndCaptureSystemOut()).contains(file.toString()); | |||
} | |||
|
|||
@Test | |||
public void testWhenSettingUpLoggingWithValidConfiguration_NoErrorMessageIsPrintedToConsole() throws Exception { | |||
File file = folderWithWritePermission; | |||
|
|||
configureLoggingFactoryWithFileAppender(file); | |||
|
|||
assertThat(file.canWrite()).isTrue(); | |||
assertThat(configureAndCaptureSystemOut()).isEmpty(); | |||
} | |||
} |