Search in sources :

Example 1 with Configuration

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);
    }
}
Also used : Bootstrap(io.dropwizard.setup.Bootstrap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) Configuration(io.dropwizard.Configuration) Namespace(net.sourceforge.argparse4j.inf.Namespace) Command(io.dropwizard.cli.Command) YamlConfigurationFactory(io.dropwizard.configuration.YamlConfigurationFactory) Server(org.eclipse.jetty.server.Server) Nullable(javax.annotation.Nullable) Environment(io.dropwizard.setup.Environment) Application(io.dropwizard.Application) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Throwables(com.google.common.base.Throwables) Set(java.util.Set) ServerCommand(io.dropwizard.cli.ServerCommand) InvocationTargetException(java.lang.reflect.InvocationTargetException) ServerConnector(org.eclipse.jetty.server.ServerConnector) List(java.util.List) Managed(io.dropwizard.lifecycle.Managed) Optional(java.util.Optional) MoreObjects.firstNonNull(com.google.common.base.MoreObjects.firstNonNull) Connector(org.eclipse.jetty.server.Connector) YamlConfigurationFactory(io.dropwizard.configuration.YamlConfigurationFactory) InvocationTargetException(java.lang.reflect.InvocationTargetException) ImmutableMap(com.google.common.collect.ImmutableMap) Namespace(net.sourceforge.argparse4j.inf.Namespace) Command(io.dropwizard.cli.Command) ServerCommand(io.dropwizard.cli.ServerCommand) Bootstrap(io.dropwizard.setup.Bootstrap) Environment(io.dropwizard.setup.Environment)

Example 2 with Configuration

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");
}
Also used : LoggingFactory(io.dropwizard.logging.LoggingFactory) JarLocation(io.dropwizard.util.JarLocation) Configuration(io.dropwizard.Configuration) Argument(net.sourceforge.argparse4j.inf.Argument) Namespace(net.sourceforge.argparse4j.inf.Namespace) IOException(java.io.IOException) ConfigurationException(io.dropwizard.configuration.ConfigurationException) ConfigurationSourceProvider(io.dropwizard.configuration.ConfigurationSourceProvider) ConfigurationFactory(io.dropwizard.configuration.ConfigurationFactory) Subparser(net.sourceforge.argparse4j.inf.Subparser) Bootstrap(io.dropwizard.setup.Bootstrap) Test(org.junit.Test)

Example 3 with Configuration

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);
}
Also used : JarLocation(io.dropwizard.util.JarLocation) Configuration(io.dropwizard.Configuration) Bootstrap(io.dropwizard.setup.Bootstrap) Before(org.junit.Before)

Example 4 with Configuration

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");
}
Also used : Configuration(io.dropwizard.Configuration) File(java.io.File) DropwizardTestSupport(io.dropwizard.testing.DropwizardTestSupport) Test(org.junit.Test)

Aggregations

Configuration (io.dropwizard.Configuration)4 Bootstrap (io.dropwizard.setup.Bootstrap)3 JarLocation (io.dropwizard.util.JarLocation)2 Namespace (net.sourceforge.argparse4j.inf.Namespace)2 Test (org.junit.Test)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 MoreObjects.firstNonNull (com.google.common.base.MoreObjects.firstNonNull)1 Strings (com.google.common.base.Strings)1 Throwables (com.google.common.base.Throwables)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Application (io.dropwizard.Application)1 Command (io.dropwizard.cli.Command)1 ServerCommand (io.dropwizard.cli.ServerCommand)1 ConfigurationException (io.dropwizard.configuration.ConfigurationException)1 ConfigurationFactory (io.dropwizard.configuration.ConfigurationFactory)1 ConfigurationSourceProvider (io.dropwizard.configuration.ConfigurationSourceProvider)1 YamlConfigurationFactory (io.dropwizard.configuration.YamlConfigurationFactory)1 Managed (io.dropwizard.lifecycle.Managed)1 LoggingFactory (io.dropwizard.logging.LoggingFactory)1