Search in sources :

Example 1 with ConfigurationSourceProvider

use of io.dropwizard.configuration.ConfigurationSourceProvider 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)

Aggregations

Configuration (io.dropwizard.Configuration)1 ConfigurationException (io.dropwizard.configuration.ConfigurationException)1 ConfigurationFactory (io.dropwizard.configuration.ConfigurationFactory)1 ConfigurationSourceProvider (io.dropwizard.configuration.ConfigurationSourceProvider)1 LoggingFactory (io.dropwizard.logging.LoggingFactory)1 Bootstrap (io.dropwizard.setup.Bootstrap)1 JarLocation (io.dropwizard.util.JarLocation)1 IOException (java.io.IOException)1 Argument (net.sourceforge.argparse4j.inf.Argument)1 Namespace (net.sourceforge.argparse4j.inf.Namespace)1 Subparser (net.sourceforge.argparse4j.inf.Subparser)1 Test (org.junit.Test)1