Search in sources :

Example 96 with Injector

use of com.google.inject.Injector in project acceptance-test-harness by jenkinsci.

the class TSRJenkinsAcceptanceTestRule method apply.

@Override
public Statement apply(final Statement base, final FrameworkMethod method, final Object target) {
    final Description description = Description.createTestDescription(method.getMethod().getDeclaringClass(), method.getName(), method.getAnnotations());
    return new Statement() {

        @Inject
        JenkinsController controller;

        @Inject
        Injector injector;

        @Override
        public void evaluate() throws Throwable {
            World world = World.get();
            Injector injector = world.getInjector();
            world.startTestScope(description.getDisplayName());
            injector.injectMembers(target);
            injector.injectMembers(this);
            System.out.println("=== Starting " + description.getDisplayName());
            try {
                decorateWithRules(base).evaluate();
            } catch (AssumptionViolatedException e) {
                throw e;
            } catch (Exception | AssertionError e) {
                // Errors and failures
                controller.diagnose(e);
                throw e;
            } finally {
                world.endTestScope();
            }
        }

        /**
         * Look for annotations on a test and honor {@link RuleAnnotation}s in them.
         */
        private Statement decorateWithRules(Statement body) {
            Set<Class<? extends Annotation>> annotations = new HashSet<>();
            collectAnnotationTypes(method.getMethod(), annotations);
            collectAnnotationTypes(target.getClass(), annotations);
            Description testDescription = Description.createTestDescription(target.getClass(), method.getName(), method.getAnnotations());
            for (Class<? extends Annotation> a : annotations) {
                RuleAnnotation r = a.getAnnotation(RuleAnnotation.class);
                if (r != null) {
                    TestRule tr = injector.getInstance(r.value());
                    body = tr.apply(body, testDescription);
                }
            }
            return body;
        }

        private void collectAnnotationTypes(AnnotatedElement e, Collection<Class<? extends Annotation>> types) {
            for (Annotation a : e.getAnnotations()) {
                types.add(a.annotationType());
            }
        }
    };
}
Also used : Description(org.junit.runner.Description) AssumptionViolatedException(org.junit.internal.AssumptionViolatedException) Statement(org.junit.runners.model.Statement) AnnotatedElement(java.lang.reflect.AnnotatedElement) World(org.jenkinsci.test.acceptance.guice.World) AssumptionViolatedException(org.junit.internal.AssumptionViolatedException) Annotation(java.lang.annotation.Annotation) TestRule(org.junit.rules.TestRule) JenkinsController(org.jenkinsci.test.acceptance.controller.JenkinsController) Injector(com.google.inject.Injector) Collection(java.util.Collection) HashSet(java.util.HashSet)

Example 97 with Injector

use of com.google.inject.Injector in project open-kilda by telstra.

the class AbstractTopologyRunner method startup.

public static void startup(Class<? extends AbstractTopologyRunner> klass, Module module) {
    String overridesFile = null;
    if (System.getProperty("storm.topology.config.overrides.file") != null) {
        overridesFile = System.getProperty("storm.topology.config.overrides.file");
    } else {
        overridesFile = klass.getResource("kilda-overrides.yml").getPath();
    }
    Injector injector = Guice.createInjector(new YamlConfigModule(overridesFile), module);
    AbstractTopologyRunner runner = injector.getInstance(klass);
    runner.run();
}
Also used : YamlConfigModule(org.bitbucket.kilda.storm.topology.guice.module.YamlConfigModule) Injector(com.google.inject.Injector)

Example 98 with Injector

use of com.google.inject.Injector in project open-kilda by telstra.

the class Main method main.

public static void main(String[] args) {
    try {
        Main.Initialize(args);
        Injector injector = Guice.createInjector(new YamlConfigModule(System.getProperty("controller.config.overrides.file")));
        Main main = injector.getInstance(Main.class);
        main.Startup();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : YamlConfigModule(org.bitbucket.kilda.controller.guice.module.YamlConfigModule) Injector(com.google.inject.Injector)

Example 99 with Injector

use of com.google.inject.Injector in project airlift by airlift.

the class ConfigurationFactoryTest method testConfigurationThroughLegacyConfig.

@Test
public void testConfigurationThroughLegacyConfig() {
    Map<String, String> properties = new TreeMap<>();
    properties.put("string-value", "this is a");
    properties.put("string-b", "this is b");
    TestMonitor monitor = new TestMonitor();
    Injector injector = createInjector(properties, monitor, binder -> configBinder(binder).bindConfig(LegacyConfigPresent.class));
    LegacyConfigPresent legacyConfigPresent = injector.getInstance(LegacyConfigPresent.class);
    monitor.assertNumberOfErrors(0);
    monitor.assertNumberOfWarnings(1);
    monitor.assertMatchingWarningRecorded("string-value", "replaced", "Use 'string-a'");
    assertNotNull(legacyConfigPresent);
    assertEquals(legacyConfigPresent.getStringA(), "this is a");
    assertEquals(legacyConfigPresent.getStringB(), "this is b");
}
Also used : Injector(com.google.inject.Injector) TreeMap(java.util.TreeMap) Test(org.testng.annotations.Test)

Example 100 with Injector

use of com.google.inject.Injector in project airlift by airlift.

the class ConfigurationFactoryTest method testConfigurationThroughDeprecatedConfig.

@Test
public void testConfigurationThroughDeprecatedConfig() {
    Map<String, String> properties = new TreeMap<>();
    properties.put("string-a", "this is a");
    properties.put("string-b", "this is b");
    TestMonitor monitor = new TestMonitor();
    Injector injector = createInjector(properties, monitor, binder -> configBinder(binder).bindConfig(DeprecatedConfigPresent.class));
    DeprecatedConfigPresent deprecatedConfigPresent = injector.getInstance(DeprecatedConfigPresent.class);
    monitor.assertNumberOfErrors(0);
    monitor.assertNumberOfWarnings(1);
    monitor.assertMatchingWarningRecorded("string-a", "deprecated and should not be used");
    assertNotNull(deprecatedConfigPresent);
    assertEquals(deprecatedConfigPresent.getStringA(), "this is a");
    assertEquals(deprecatedConfigPresent.getStringB(), "this is b");
}
Also used : Injector(com.google.inject.Injector) TreeMap(java.util.TreeMap) Test(org.testng.annotations.Test)

Aggregations

Injector (com.google.inject.Injector)2159 AbstractModule (com.google.inject.AbstractModule)623 Test (org.junit.Test)513 Module (com.google.inject.Module)364 Test (org.testng.annotations.Test)131 Before (org.junit.Before)116 Binder (com.google.inject.Binder)114 Properties (java.util.Properties)110 Key (com.google.inject.Key)84 Map (java.util.Map)78 HttpServletRequest (javax.servlet.http.HttpServletRequest)78 Provider (com.google.inject.Provider)74 IOException (java.io.IOException)71 TypeLiteral (com.google.inject.TypeLiteral)70 Set (java.util.Set)64 BeforeClass (org.junit.BeforeClass)61 File (java.io.File)60 CConfiguration (co.cask.cdap.common.conf.CConfiguration)55 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)55 PrivateModule (com.google.inject.PrivateModule)55