Search in sources :

Example 6 with Env

use of se.jbee.inject.Env in project silk by jbee.

the class TestExampleAnnotatedActionsBinds method annotatedMethodIsBoundAsActionWithGlobalSelector.

@Test
void annotatedMethodIsBoundAsActionWithGlobalSelector() {
    Env env = Bootstrap.DEFAULT_ENV.with(CONNECT_QUALIFIER, ProducesBy.class, OPTIMISTIC.annotatedWith(Marker.class));
    annotatedMethodIsBoundAsAction(Bootstrap.injector(env, TestExampleAnnotatedActionsBindsModule2.class));
}
Also used : Env(se.jbee.inject.Env) Test(org.junit.jupiter.api.Test)

Example 7 with Env

use of se.jbee.inject.Env in project silk by jbee.

the class TestExamplePubSubBinds method assertSubscribedToPublisher.

private static void assertSubscribedToPublisher(Class<? extends Bundle> bundle, int initiallyExpectedSubscribers) {
    PublisherImpl pub = new PublisherImpl();
    Env env = Bootstrap.DEFAULT_ENV.with(Publisher.class, pub).with(PublishesBy.class, PublishesBy.OPTIMISTIC);
    Injector context = Bootstrap.injector(env, bundle);
    // at this point the context is created by not necessarily any instances
    // this is where the difference between eager and lazy shows
    assertEquals(initiallyExpectedSubscribers, pub.subs.size());
    assertSame(pub, context.resolve(PublisherImpl.class));
    assertEquals(3, pub.subs.size(), "lazy did not cause subscription");
    Service1 sub1 = context.resolve(Service1.class);
    Service2 sub2 = context.resolve(Service2.class);
    Service3 sub3 = context.resolve(Service3.class);
    assertEquals(0, sub1.events);
    assertEquals(0, sub2.events);
    assertEquals(0, sub3.events);
    pub.publish();
    assertEquals(1, sub1.events);
    assertEquals(1, sub2.events);
    assertEquals(1, sub3.events);
}
Also used : Injector(se.jbee.inject.Injector) Env(se.jbee.inject.Env)

Example 8 with Env

use of se.jbee.inject.Env in project silk by jbee.

the class TestExampleEnvConfiguredHintsBinds method passingPropertiesConfigurationToConfigureHints.

@Test
void passingPropertiesConfigurationToConfigureHints() {
    Properties props = new Properties();
    props.put("x", "abc");
    props.put("y", 12);
    Env env = Bootstrap.DEFAULT_ENV.with(Properties.class, props);
    Injector injector = Bootstrap.injector(env, Example1Module1.class);
    MyClass obj = injector.resolve(MyClass.class);
    assertEquals(12, obj.twelve);
    assertEquals("abc", obj.abc);
}
Also used : Injector(se.jbee.inject.Injector) Properties(java.util.Properties) Env(se.jbee.inject.Env) Test(org.junit.jupiter.api.Test)

Example 9 with Env

use of se.jbee.inject.Env in project silk by jbee.

the class TestFeatureDiskScopeSync method schedulerCallsSyncInConfiguredInterval.

/**
 * When disk is asked to store the entry at least 4 times this means
 * that in general scheduling has picked up the annotated method as well
 * as that the configuration change did have an effect on the interval.
 */
@Test
void schedulerCallsSyncInConfiguredInterval() {
    List<DiskScope.DiskEntry> recorded = new CopyOnWriteArrayList<>();
    Consumer<DiskScope.DiskEntry> recorder = recorded::add;
    RecordingScheduledExecutor executor = new RecordingScheduledExecutor();
    Env env = Bootstrap.DEFAULT_ENV.with(consumerTypeOf(DiskScope.DiskEntry.class), recorder).with(SchedulerModule.ScheduledExecutor.class, executor);
    Injector context = Bootstrap.injector(env, TestFeatureDiskScopeSyncModule.class);
    assertEquals("Hello", context.resolve(String.class));
    // after we now have created the scheduled "bean" and by that the scheduler
    // the executor should have gotten a job
    assertEquals(1, executor.recorded.size());
    // verify the details of that job
    Job last = executor.lastRecorded();
    assertNotNull(last);
    assertNotNull(last.task);
    assertEquals(20L, last.unit.toMillis(last.period));
    assertEquals(0L, last.initialDelay);
    // now we simulate the scheduler work
    assertEquals(1, recorded.size(), "loading does not update to disk");
    last.task.run();
    assertEquals(2, recorded.size());
    last.task.run();
    assertEquals(3, recorded.size());
    last.task.run();
    assertEquals(4, recorded.size());
    // make sure all recorded entries are indeed the same
    assertAllSame(recorded);
}
Also used : Injector(se.jbee.inject.Injector) DiskScope(se.jbee.inject.disk.DiskScope) SchedulerModule(se.jbee.inject.schedule.SchedulerModule) Env(se.jbee.inject.Env) Job(test.integration.event.RecordingScheduledExecutor.Job) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.jupiter.api.Test)

Example 10 with Env

use of se.jbee.inject.Env in project silk by jbee.

the class TestCustomAnnotationBinds method customAnnotationsAddedViaServiceLoader.

/**
 * This verifies that the {@link Support} annotation's effect is loaded via
 * {@link ServiceLoader}. It is defined in the com.example.app test
 * dependency jar file. If the {@link MySupportService} can be resolved it
 * was bound which meant the annotation had done its effect. Otherwise no
 * binding would exist for the class.
 */
@Test
void customAnnotationsAddedViaServiceLoader() {
    Env env = Example.EXAMPLE_1.env();
    Injector injector = Bootstrap.injector(env, TestCustomAnnotationBindsModule.class);
    MySupportService service = injector.resolve(MySupportService.class);
    assertNotNull(service);
}
Also used : Injector(se.jbee.inject.Injector) Env(se.jbee.inject.Env) Test(org.junit.jupiter.api.Test)

Aggregations

Env (se.jbee.inject.Env)13 Injector (se.jbee.inject.Injector)11 Test (org.junit.jupiter.api.Test)10 METHOD (java.lang.annotation.ElementType.METHOD)1 Retention (java.lang.annotation.Retention)1 RUNTIME (java.lang.annotation.RetentionPolicy.RUNTIME)1 Target (java.lang.annotation.Target)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1 Properties (java.util.Properties)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Assertions (org.junit.jupiter.api.Assertions)1 Hint (se.jbee.inject.Hint)1 Name.named (se.jbee.inject.Name.named)1 UnresolvableDependency (se.jbee.inject.UnresolvableDependency)1 BinderModuleWith (se.jbee.inject.binder.BinderModuleWith)1 Bootstrap (se.jbee.inject.bootstrap.Bootstrap)1 Connector (se.jbee.inject.config.Connector)1