Search in sources :

Example 1 with JarLocation

use of io.dropwizard.util.JarLocation in project dropwizard by dropwizard.

the class InheritedServerCommandTest method usesDefaultConfigPath.

@Test
void usesDefaultConfigPath() throws Exception {
    class SingletonConfigurationFactory implements ConfigurationFactory<Configuration> {

        @Override
        public Configuration build(final ConfigurationSourceProvider provider, final String path) {
            return configuration;
        }

        @Override
        public Configuration build() {
            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) {
            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) ConfigurationSourceProvider(io.dropwizard.configuration.ConfigurationSourceProvider) ConfigurationFactory(io.dropwizard.configuration.ConfigurationFactory) Subparser(net.sourceforge.argparse4j.inf.Subparser) Bootstrap(io.dropwizard.setup.Bootstrap) Test(org.junit.jupiter.api.Test)

Example 2 with JarLocation

use of io.dropwizard.util.JarLocation in project dropwizard by dropwizard.

the class Application method run.

/**
 * Parses command-line arguments and runs the application. Call this method from a {@code public
 * static void main} entry point in your application.
 *
 * @param arguments the command-line arguments
 * @throws Exception if something goes wrong
 */
public void run(String... arguments) throws Exception {
    final Bootstrap<T> bootstrap = new Bootstrap<>(this);
    addDefaultCommands(bootstrap);
    initialize(bootstrap);
    // Should be called after initialize to give an opportunity to set a custom metric registry
    bootstrap.registerMetrics();
    final Cli cli = new Cli(new JarLocation(getClass()), bootstrap, System.out, System.err);
    // only exit if there's an error running the command
    cli.run(arguments).ifPresent(this::onFatalError);
}
Also used : Cli(io.dropwizard.cli.Cli) JarLocation(io.dropwizard.util.JarLocation) Bootstrap(io.dropwizard.setup.Bootstrap)

Example 3 with JarLocation

use of io.dropwizard.util.JarLocation in project dropwizard by dropwizard.

the class CommandTest method setUp.

@BeforeEach
void setUp() {
    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) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

Bootstrap (io.dropwizard.setup.Bootstrap)3 JarLocation (io.dropwizard.util.JarLocation)3 Configuration (io.dropwizard.Configuration)2 Cli (io.dropwizard.cli.Cli)1 ConfigurationFactory (io.dropwizard.configuration.ConfigurationFactory)1 ConfigurationSourceProvider (io.dropwizard.configuration.ConfigurationSourceProvider)1 LoggingFactory (io.dropwizard.logging.LoggingFactory)1 Argument (net.sourceforge.argparse4j.inf.Argument)1 Namespace (net.sourceforge.argparse4j.inf.Namespace)1 Subparser (net.sourceforge.argparse4j.inf.Subparser)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Test (org.junit.jupiter.api.Test)1