Search in sources :

Example 1 with DropwizardTestSupport

use of io.dropwizard.testing.DropwizardTestSupport in project dropwizard by dropwizard.

the class BadLogTest method testSupportShouldResetLogging.

@Test
public void testSupportShouldResetLogging() throws Exception {
    final File logFile = folder.newFile("example.log");
    // Clear out the log file
    Files.write(new byte[] {}, logFile);
    final String configPath = ResourceHelpers.resourceFilePath("badlog/config.yaml");
    final DropwizardTestSupport<Configuration> app = new DropwizardTestSupport<>(BadLogApp.class, configPath, ConfigOverride.config("logging.appenders[0].currentLogFilename", logFile.getAbsolutePath()));
    assertThatThrownBy(app::before).hasMessage("I'm a bad app");
    // Explicitly run the command so that the fatal error function runs
    app.getApplication().run("server", configPath);
    app.after();
    // Since our dropwizard app is only using the file appender, the console
    // appender should not contain our logging statements
    assertThat(new String(out.toByteArray(), UTF_8)).doesNotContain("Mayday we're going down");
    out.reset();
    // and the file should have our logging statements
    final String contents = Files.toString(logFile, UTF_8);
    assertThat(contents).contains("Mayday we're going down");
    // Clear out the log file
    Files.write(new byte[] {}, logFile);
    final DropwizardTestSupport<Configuration> app2 = new DropwizardTestSupport<>(BadLogApp.class, new Configuration());
    assertThatThrownBy(app2::before).hasMessage("I'm a bad app");
    // Explicitly run the command so that the fatal error function runs
    app2.getApplication().run("server");
    app2.after();
    // Now the console appender will see the fatal error
    assertThat(new String(out.toByteArray(), UTF_8)).contains("Mayday we're going down");
    out.reset();
    // and the old log file shouldn't
    final String contents2 = Files.toString(logFile, UTF_8);
    assertThat(contents2).doesNotContain("Mayday we're going down");
    // And for the final set of assertions will make sure that going back to the app
    // that logs to a file is still behaviorally correct.
    // Clear out the log file
    Files.write(new byte[] {}, logFile);
    final DropwizardTestSupport<Configuration> app3 = new DropwizardTestSupport<>(BadLogApp.class, configPath, ConfigOverride.config("logging.appenders[0].currentLogFilename", logFile.getAbsolutePath()));
    assertThatThrownBy(app3::before).hasMessage("I'm a bad app");
    // Explicitly run the command so that the fatal error function runs
    app3.getApplication().run("server", configPath);
    app3.after();
    // Since our dropwizard app is only using the file appender, the console
    // appender should not contain our logging statements
    assertThat(new String(out.toByteArray(), UTF_8)).doesNotContain("Mayday we're going down");
    // and the file should have our logging statements
    final String contents3 = Files.toString(logFile, UTF_8);
    assertThat(contents3).contains("Mayday we're going down");
}
Also used : Configuration(io.dropwizard.Configuration) File(java.io.File) DropwizardTestSupport(io.dropwizard.testing.DropwizardTestSupport) Test(org.junit.Test)

Aggregations

Configuration (io.dropwizard.Configuration)1 DropwizardTestSupport (io.dropwizard.testing.DropwizardTestSupport)1 File (java.io.File)1 Test (org.junit.Test)1