Search in sources :

Example 1 with Environment

use of io.dropwizard.setup.Environment 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 Environment

use of io.dropwizard.setup.Environment in project dropwizard by dropwizard.

the class DropwizardTestSupportTest method returnsEnvironment.

@Test
public void returnsEnvironment() {
    final Environment environment = TEST_SUPPORT.getEnvironment();
    assertThat(environment.getName(), is("TestApplication"));
}
Also used : Environment(io.dropwizard.setup.Environment) Test(org.junit.Test)

Example 3 with Environment

use of io.dropwizard.setup.Environment in project dropwizard by dropwizard.

the class DropwizardAppRuleTest method returnsEnvironment.

@Test
public void returnsEnvironment() {
    final Environment environment = RULE.getEnvironment();
    assertThat(environment.getName(), is("TestApplication"));
}
Also used : Environment(io.dropwizard.setup.Environment) Test(org.junit.Test)

Example 4 with Environment

use of io.dropwizard.setup.Environment in project dropwizard by dropwizard.

the class UnitOfWorkAwareProxyFactoryTest method setUp.

@Before
public void setUp() throws Exception {
    final HibernateBundle<?> bundle = mock(HibernateBundle.class);
    final Environment environment = mock(Environment.class);
    when(environment.lifecycle()).thenReturn(mock(LifecycleEnvironment.class));
    when(environment.metrics()).thenReturn(new MetricRegistry());
    final DataSourceFactory dataSourceFactory = new DataSourceFactory();
    dataSourceFactory.setUrl("jdbc:hsqldb:mem:unit-of-work-" + UUID.randomUUID().toString());
    dataSourceFactory.setUser("sa");
    dataSourceFactory.setDriverClass("org.hsqldb.jdbcDriver");
    dataSourceFactory.setValidationQuery("SELECT 1 FROM INFORMATION_SCHEMA.SYSTEM_USERS");
    dataSourceFactory.setProperties(ImmutableMap.of("hibernate.dialect", "org.hibernate.dialect.HSQLDialect"));
    dataSourceFactory.setInitialSize(1);
    dataSourceFactory.setMinSize(1);
    sessionFactory = new SessionFactoryFactory().build(bundle, environment, dataSourceFactory, ImmutableList.of());
    try (Session session = sessionFactory.openSession()) {
        Transaction transaction = session.beginTransaction();
        session.createNativeQuery("create table user_sessions (token varchar(64) primary key, username varchar(16))").executeUpdate();
        session.createNativeQuery("insert into user_sessions values ('67ab89d', 'jeff_28')").executeUpdate();
        transaction.commit();
    }
}
Also used : DataSourceFactory(io.dropwizard.db.DataSourceFactory) LifecycleEnvironment(io.dropwizard.lifecycle.setup.LifecycleEnvironment) Transaction(org.hibernate.Transaction) MetricRegistry(com.codahale.metrics.MetricRegistry) Environment(io.dropwizard.setup.Environment) LifecycleEnvironment(io.dropwizard.lifecycle.setup.LifecycleEnvironment) Session(org.hibernate.Session) Before(org.junit.Before)

Example 5 with Environment

use of io.dropwizard.setup.Environment in project dropwizard by dropwizard.

the class DropwizardApacheConnectorTest method setup.

@Before
public void setup() throws Exception {
    JerseyClientConfiguration clientConfiguration = new JerseyClientConfiguration();
    clientConfiguration.setConnectionTimeout(Duration.milliseconds(SLEEP_TIME_IN_MILLIS / 2));
    clientConfiguration.setTimeout(Duration.milliseconds(DEFAULT_CONNECT_TIMEOUT_IN_MILLIS));
    environment = new Environment("test-dropwizard-apache-connector", Jackson.newObjectMapper(), Validators.newValidator(), new MetricRegistry(), getClass().getClassLoader());
    client = (JerseyClient) new JerseyClientBuilder(environment).using(clientConfiguration).build("test");
    for (LifeCycle lifeCycle : environment.lifecycle().getManagedObjects()) {
        lifeCycle.start();
    }
}
Also used : LifeCycle(org.eclipse.jetty.util.component.LifeCycle) MetricRegistry(com.codahale.metrics.MetricRegistry) Environment(io.dropwizard.setup.Environment) Before(org.junit.Before)

Aggregations

Environment (io.dropwizard.setup.Environment)14 MetricRegistry (com.codahale.metrics.MetricRegistry)7 Test (org.junit.Test)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 DataSourceFactory (io.dropwizard.db.DataSourceFactory)3 LifecycleEnvironment (io.dropwizard.lifecycle.setup.LifecycleEnvironment)3 Before (org.junit.Before)3 Configuration (io.dropwizard.Configuration)2 YamlConfigurationFactory (io.dropwizard.configuration.YamlConfigurationFactory)2 Managed (io.dropwizard.lifecycle.Managed)2 Bootstrap (io.dropwizard.setup.Bootstrap)2 File (java.io.File)2 Connector (org.eclipse.jetty.server.Connector)2 Server (org.eclipse.jetty.server.Server)2 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 CharStreams (com.google.common.io.CharStreams)1