use of io.dropwizard.Configuration in project dropwizard by dropwizard.
the class DropwizardTestSupport method startIfRequired.
private void startIfRequired() {
if (jettyServer != null) {
return;
}
try {
application = newApplication();
final Bootstrap<C> bootstrap = new Bootstrap<C>(application) {
@Override
public void run(C configuration, Environment environment) throws Exception {
environment.lifecycle().addServerLifecycleListener(server -> jettyServer = server);
DropwizardTestSupport.this.configuration = configuration;
DropwizardTestSupport.this.environment = environment;
super.run(configuration, environment);
for (ServiceListener<C> listener : listeners) {
try {
listener.onRun(configuration, environment, DropwizardTestSupport.this);
} catch (Exception ex) {
throw new RuntimeException("Error running app rule start listener", ex);
}
}
}
};
if (explicitConfig) {
bootstrap.setConfigurationFactoryFactory((klass, validator, objectMapper, propertyPrefix) -> new POJOConfigurationFactory<>(configuration));
} else if (customPropertyPrefix.isPresent()) {
bootstrap.setConfigurationFactoryFactory((klass, validator, objectMapper, propertyPrefix) -> new YamlConfigurationFactory<>(klass, validator, objectMapper, customPropertyPrefix.get()));
}
application.initialize(bootstrap);
final Command command = commandInstantiator.apply(application);
final ImmutableMap.Builder<String, Object> file = ImmutableMap.builder();
if (!Strings.isNullOrEmpty(configPath)) {
file.put("file", configPath);
}
final Namespace namespace = new Namespace(file.build());
command.run(bootstrap, namespace);
} catch (Exception e) {
Throwables.throwIfUnchecked(e);
throw new RuntimeException(e);
}
}
use of io.dropwizard.Configuration in project dropwizard by dropwizard.
the class InheritedServerCommandTest method usesDefaultConfigPath.
@Test
public void usesDefaultConfigPath() throws Exception {
class SingletonConfigurationFactory implements ConfigurationFactory {
@Override
public Object build(final ConfigurationSourceProvider provider, final String path) throws IOException, ConfigurationException {
return configuration;
}
@Override
public Object build() throws IOException, ConfigurationException {
throw new AssertionError("Didn't use the default config path variable");
}
}
when(configuration.getLoggingFactory()).thenReturn(mock(LoggingFactory.class));
final Bootstrap<Configuration> bootstrap = new Bootstrap<>(application);
bootstrap.setConfigurationFactoryFactory((klass, validator, objectMapper, propertyPrefix) -> new SingletonConfigurationFactory());
bootstrap.addCommand(new ConfiguredCommand<Configuration>("test", "a test command") {
@Override
protected void run(final Bootstrap<Configuration> bootstrap, final Namespace namespace, final Configuration configuration) throws Exception {
assertThat(namespace.getString("file")).isNotEmpty().isEqualTo("yaml/server.yml");
}
@Override
protected Argument addFileArgument(final Subparser subparser) {
return super.addFileArgument(subparser).setDefault("yaml/server.yml");
}
});
final JarLocation location = mock(JarLocation.class);
when(location.toString()).thenReturn("dw-thing.jar");
when(location.getVersion()).thenReturn(Optional.of("1.0.0"));
Cli cli = new Cli(location, bootstrap, System.out, System.err);
cli.run("test");
}
use of io.dropwizard.Configuration in project dropwizard by dropwizard.
the class CommandTest method setUp.
@Before
public void setUp() throws Exception {
final JarLocation location = mock(JarLocation.class);
final Bootstrap<Configuration> bootstrap = new Bootstrap<>(app);
when(location.toString()).thenReturn("dw-thing.jar");
when(location.getVersion()).thenReturn(Optional.of("1.0.0"));
bootstrap.addCommand(command);
cli = new Cli(location, bootstrap, stdOut, stdErr);
}
use of io.dropwizard.Configuration 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");
}
Aggregations