Search in sources :

Example 1 with BasicSetup

use of com.peterphi.std.guice.apploader.BasicSetup in project stdlib by petergeneric.

the class GuiceRegistryBuilder method createRegistry.

private static GuiceRegistry createRegistry(GuiceConfig config, TestClass clazz) {
    GuiceRegistry registry = new GuiceRegistry();
    ClassScannerFactory scanner = null;
    GuiceRole[] roles = null;
    if (config != null) {
        if (config.packages().length > 0 || config.classPackages().length > 0) {
            Set<String> packages = new HashSet<>();
            packages.addAll(Arrays.asList(config.packages()));
            for (Class c : config.classPackages()) packages.add(c.getPackage().getName());
            scanner = new ClassScannerFactory(packages.toArray(new String[packages.size()]));
        }
        if (config.role().length > 0) {
            List<GuiceRole> instances = new ArrayList<>();
            for (Class<? extends GuiceRole> role : config.role()) {
                try {
                    instances.add(role.newInstance());
                } catch (Exception e) {
                    throw new IllegalArgumentException("Error instantiating GuiceRole " + role, e);
                }
            }
            roles = instances.toArray(new GuiceRole[instances.size()]);
        }
    }
    GuiceBuilder builder = registry.getBuilder();
    if (scanner != null)
        builder.withScannerFactory(scanner);
    else
        builder.withNoScannerFactory();
    if (config != null && config.config().length > 0)
        builder.withConfig(config.config());
    if (config != null)
        builder.withAutoLoadRoles(config.autoLoadRoles());
    if (roles != null)
        builder.withRole(roles);
    // Add local method config sources
    {
        validateGuiceTestConfigMethods(clazz);
        for (Object src : clazz.getAnnotatedMethodValues(null, TestConfig.class, Object.class)) {
            if (src instanceof Properties)
                builder.withConfig((Properties) src);
            else if (src instanceof PropertyFile)
                builder.withConfig((PropertyFile) src);
        }
    }
    // Add local method module sources
    {
        validateGuiceTestModuleMethods(clazz);
        builder.withRole(new ModuleAddingGuiceRole(clazz.getAnnotatedMethodValues(null, TestModule.class, Module.class)));
    }
    // Auto-detect @Automock annotated fields in the test and create mocks for them
    {
        List<FrameworkField> fields = clazz.getAnnotatedFields(Automock.class);
        if (fields.size() > 0)
            builder.withRole(new ModuleAddingGuiceRole(new AutomockAnnotatedMockModule(clazz.getJavaClass(), fields)));
    }
    // Make sure we set the unit test property so roles are aware they're running in a unit test (e.g. so they don't auto-register REST services)
    {
        PropertyFile props = new PropertyFile();
        props.set(GuiceProperties.UNIT_TEST, "true");
        builder.withConfig(props);
    }
    // Add the Setup class, or if none is specified then add local modules:
    if (config != null && config.setup().length > 0) {
        builder.withSetup(config.setup()[0]);
    } else {
        builder.withSetup(new BasicSetup());
    }
    return registry;
}
Also used : ArrayList(java.util.ArrayList) Properties(java.util.Properties) GuiceProperties(com.peterphi.std.guice.apploader.GuiceProperties) ArrayList(java.util.ArrayList) List(java.util.List) ClassScannerFactory(com.peterphi.std.guice.common.ClassScannerFactory) HashSet(java.util.HashSet) TestConfig(com.peterphi.std.guice.testing.com.peterphi.std.guice.testing.annotations.TestConfig) GuiceBuilder(com.peterphi.std.guice.apploader.impl.GuiceBuilder) GuiceRegistry(com.peterphi.std.guice.apploader.impl.GuiceRegistry) GuiceRole(com.peterphi.std.guice.apploader.GuiceRole) PropertyFile(com.peterphi.std.io.PropertyFile) BasicSetup(com.peterphi.std.guice.apploader.BasicSetup) TestClass(org.junit.runners.model.TestClass) Module(com.google.inject.Module) TestModule(com.peterphi.std.guice.testing.com.peterphi.std.guice.testing.annotations.TestModule) TestModule(com.peterphi.std.guice.testing.com.peterphi.std.guice.testing.annotations.TestModule) Automock(com.peterphi.std.guice.testing.com.peterphi.std.guice.testing.annotations.Automock)

Example 2 with BasicSetup

use of com.peterphi.std.guice.apploader.BasicSetup in project stdlib by petergeneric.

the class CacheMethodInterceptorTest method testCache.

@Test
public void testCache() throws Exception {
    Injector injector = new GuiceBuilder().withSetup(new BasicSetup()).withNoScannerFactory().withAutoLoadRoles(false).build();
    CacheTest test = injector.getInstance(CacheTest.class);
    // no calls to the test class have been made yet
    assertEquals(0, test.getRealCalls());
    // make a call
    test.getValue();
    // one real call has been made
    assertEquals(1, test.getRealCalls());
    // make a call, a cached value should be returned
    test.getValue();
    // so the number of real calls is still 1
    assertEquals(1, test.getRealCalls());
    // wait a while (but not long enough for the result to go stale)
    Thread.sleep(500l);
    // make a call, a cached value should be returned
    test.getValue();
    // so the number of real calls is still 1
    assertEquals(1, test.getRealCalls());
    // wait a while longer (long enough for the result to go stale)
    Thread.sleep(2000l);
    // make a call, a fresh value should be returned
    test.getValue();
    // so now the number of real calls to the method is 2
    assertEquals("number of real calls to method after longer sleep", 2, test.getRealCalls());
}
Also used : Injector(com.google.inject.Injector) GuiceBuilder(com.peterphi.std.guice.apploader.impl.GuiceBuilder) BasicSetup(com.peterphi.std.guice.apploader.BasicSetup) Test(org.junit.Test)

Example 3 with BasicSetup

use of com.peterphi.std.guice.apploader.BasicSetup in project stdlib by petergeneric.

the class RetryMethodInterceptorTest method testRetry.

@Test
public void testRetry() throws Exception {
    AtomicInteger var = new AtomicInteger();
    Injector injector = new GuiceBuilder().withSetup(new BasicSetup()).withNoScannerFactory().withAutoLoadRoles(false).build();
    RetryTest test = injector.getInstance(RetryTest.class);
    assertEquals(0, var.get());
    test.increment(var);
    assertEquals(1, var.get());
    try {
        test.fail();
        fail("fail method should not have succeeded!");
    } catch (Exception e) {
    // expected
    }
    test.incrementAndFailUnlessSeven(var);
    assertEquals(7, var.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Injector(com.google.inject.Injector) GuiceBuilder(com.peterphi.std.guice.apploader.impl.GuiceBuilder) BasicSetup(com.peterphi.std.guice.apploader.BasicSetup) Test(org.junit.Test)

Aggregations

BasicSetup (com.peterphi.std.guice.apploader.BasicSetup)3 GuiceBuilder (com.peterphi.std.guice.apploader.impl.GuiceBuilder)3 Injector (com.google.inject.Injector)2 Test (org.junit.Test)2 Module (com.google.inject.Module)1 GuiceProperties (com.peterphi.std.guice.apploader.GuiceProperties)1 GuiceRole (com.peterphi.std.guice.apploader.GuiceRole)1 GuiceRegistry (com.peterphi.std.guice.apploader.impl.GuiceRegistry)1 ClassScannerFactory (com.peterphi.std.guice.common.ClassScannerFactory)1 Automock (com.peterphi.std.guice.testing.com.peterphi.std.guice.testing.annotations.Automock)1 TestConfig (com.peterphi.std.guice.testing.com.peterphi.std.guice.testing.annotations.TestConfig)1 TestModule (com.peterphi.std.guice.testing.com.peterphi.std.guice.testing.annotations.TestModule)1 PropertyFile (com.peterphi.std.io.PropertyFile)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Properties (java.util.Properties)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 TestClass (org.junit.runners.model.TestClass)1