Search in sources :

Example 6 with SingletonAssembler

use of org.qi4j.bootstrap.SingletonAssembler in project qi4j-sdk by Qi4j.

the class TypeToCompositeLookupTest method objectsAmbiguousDeclaration.

@Test
public void objectsAmbiguousDeclaration() throws ActivationException, AssemblyException {
    Module module = new SingletonAssembler() {

        @Override
        public void assemble(ModuleAssembly module) throws AssemblyException {
            module.objects(SomeOtherFooImpl.class, BasicFooImpl.class);
        }
    }.module();
    assertEquals(CATHEDRAL, module.newObject(SomeOtherFooImpl.class).bar());
    assertEquals(BAZAR, module.newObject(BasicFooImpl.class).bar());
    try {
        module.newObject(Foo.class);
        fail("Ambiguous type exception not detected for Objects");
    } catch (AmbiguousTypeException expected) {
    }
}
Also used : ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) AssemblyException(org.qi4j.bootstrap.AssemblyException) SingletonAssembler(org.qi4j.bootstrap.SingletonAssembler) Module(org.qi4j.api.structure.Module) AmbiguousTypeException(org.qi4j.api.composite.AmbiguousTypeException) Test(org.junit.Test)

Example 7 with SingletonAssembler

use of org.qi4j.bootstrap.SingletonAssembler in project qi4j-sdk by Qi4j.

the class TypeToCompositeLookupTest method transientsAmbiguousDeclaration.

@Test
public void transientsAmbiguousDeclaration() throws ActivationException, AssemblyException {
    Module module = new SingletonAssembler() {

        @Override
        public void assemble(ModuleAssembly module) throws AssemblyException {
            module.transients(SomeOtherFoo.class, BasicFoo.class);
        }
    }.module();
    assertEquals(CATHEDRAL, module.newTransientBuilder(SomeOtherFoo.class).newInstance().bar());
    assertEquals(BAZAR, module.newTransientBuilder(BasicFoo.class).newInstance().bar());
    try {
        module.newTransientBuilder(Foo.class);
        fail("Ambiguous type exception not detected for Transients");
    } catch (AmbiguousTypeException expected) {
    }
}
Also used : ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) AssemblyException(org.qi4j.bootstrap.AssemblyException) SingletonAssembler(org.qi4j.bootstrap.SingletonAssembler) Module(org.qi4j.api.structure.Module) AmbiguousTypeException(org.qi4j.api.composite.AmbiguousTypeException) Test(org.junit.Test)

Example 8 with SingletonAssembler

use of org.qi4j.bootstrap.SingletonAssembler in project qi4j-sdk by Qi4j.

the class AvailableServiceTest method givenEnablableServiceWhenInjectWithAvailableQualifierThenInjectCorrectly.

@Test
public void givenEnablableServiceWhenInjectWithAvailableQualifierThenInjectCorrectly() throws ActivationException, AssemblyException {
    SingletonAssembler assembler = new SingletonAssembler() {

        public void assemble(ModuleAssembly module) throws AssemblyException {
            module.objects(TestObject.class);
            module.services(TestServiceComposite2.class);
            module.entities(TestServiceConfiguration.class);
            new EntityTestAssembler().assemble(module);
        }
    };
    TestObject object = assembler.module().newObject(TestObject.class);
    assertThat("service is unavailable", object.getService(), nullValue());
    ServiceReference<TestServiceComposite2> serviceRef = assembler.module().findService(TestServiceComposite2.class);
    serviceRef.get().get().enabled().set(true);
    serviceRef.get().save();
    object = assembler.module().newObject(TestObject.class);
    assertThat("service is available", object.getService(), notNullValue());
}
Also used : ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) SingletonAssembler(org.qi4j.bootstrap.SingletonAssembler) EntityTestAssembler(org.qi4j.test.EntityTestAssembler) Test(org.junit.Test)

Example 9 with SingletonAssembler

use of org.qi4j.bootstrap.SingletonAssembler in project qi4j-sdk by Qi4j.

the class LazyActivatedServiceTest method testActivatable.

public void testActivatable() throws Exception {
    SingletonAssembler assembly = new SingletonAssembler() {

        public void assemble(ModuleAssembly module) throws AssemblyException {
            module.objects(LazyActivatedServiceTest.class);
            module.services(LazyActivatedServiceTest.MyServiceComposite.class).withActivators(TestActivator.class);
        }
    };
    assertFalse(isActive);
    assembly.module().injectTo(this);
    assertFalse(isActive);
    service.get();
    assertFalse(isActive);
    service.get().doStuff();
    assertTrue(isActive);
    assembly.application().passivate();
    assertFalse(isActive);
}
Also used : ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) SingletonAssembler(org.qi4j.bootstrap.SingletonAssembler)

Example 10 with SingletonAssembler

use of org.qi4j.bootstrap.SingletonAssembler in project qi4j-sdk by Qi4j.

the class PassivationTest method givenMixedSuccessFailurePassivationWhenPassivatingExpectAllPassivateMethodsToBeCalled.

@Test
public void givenMixedSuccessFailurePassivationWhenPassivatingExpectAllPassivateMethodsToBeCalled() throws Exception {
    SingletonAssembler assembly = new SingletonAssembler() {

        public void assemble(ModuleAssembly module) throws AssemblyException {
            module.addServices(DataAccessService.class).withActivators(PassivationSuccessActivator.class);
            module.addServices(DataAccessService.class).withActivators(PassivationSuccessActivator.class);
            module.addServices(DataAccessService.class).withActivators(PassivationSuccessActivator.class);
            module.addServices(DataAccessService.class).withActivators(PassivationFailureActivator.class);
            module.addServices(DataAccessService.class).withActivators(PassivationSuccessActivator.class);
            module.addServices(DataAccessService.class).withActivators(PassivationSuccessActivator.class);
            module.addServices(DataAccessService.class).withActivators(PassivationFailureActivator.class);
            module.addServices(DataAccessService.class).withActivators(PassivationSuccessActivator.class);
            module.addServices(DataAccessService.class).withActivators(PassivationSuccessActivator.class);
            module.addServices(DataAccessService.class).withActivators(PassivationFailureActivator.class);
            module.addServices(DataAccessService.class).withActivators(PassivationSuccessActivator.class);
            module.addServices(DataAccessService.class).withActivators(PassivationSuccessActivator.class);
            module.addServices(DataAccessService.class).withActivators(PassivationFailureActivator.class);
        }
    };
    ArrayList<Data> datas = new ArrayList<Data>();
    Iterable<ServiceReference<DataAccess>> iterable = assembly.module().findServices(DataAccess.class);
    for (ServiceReference<DataAccess> service : iterable) {
        assertTrue("Service should not be Active before accessed", !service.isActive());
        Data data = service.get().data();
        if (DataAccessService.class.isInstance(service.get())) {
            // Collect the expected successes.
            datas.add(data);
        }
        assertTrue("Data should indicate that the service is activated", data.activated);
        assertTrue("Service should be Active after access.", service.isActive());
    }
    try {
        assembly.application().passivate();
        fail("PassivationException should have been thrown.");
    } catch (PassivationException e) {
    // Expected
    }
    // Still ensure that all services have been shutdown.
    for (ServiceReference<DataAccess> service : iterable) {
        assertFalse("All services should have been shutdown", service.isActive());
    }
}
Also used : ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) SingletonAssembler(org.qi4j.bootstrap.SingletonAssembler) PassivationException(org.qi4j.api.activation.PassivationException) ArrayList(java.util.ArrayList) ServiceReference(org.qi4j.api.service.ServiceReference) Test(org.junit.Test)

Aggregations

ModuleAssembly (org.qi4j.bootstrap.ModuleAssembly)87 SingletonAssembler (org.qi4j.bootstrap.SingletonAssembler)87 Test (org.junit.Test)65 Module (org.qi4j.api.structure.Module)15 AssemblyException (org.qi4j.bootstrap.AssemblyException)14 EntityTestAssembler (org.qi4j.test.EntityTestAssembler)13 Before (org.junit.Before)11 Application (org.qi4j.api.structure.Application)10 UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)5 AmbiguousTypeException (org.qi4j.api.composite.AmbiguousTypeException)4 ServiceReference (org.qi4j.api.service.ServiceReference)4 ArrayList (java.util.ArrayList)3 TransientBuilderFactory (org.qi4j.api.composite.TransientBuilderFactory)3 ObjectFactory (org.qi4j.api.object.ObjectFactory)3 ValueBuilder (org.qi4j.api.value.ValueBuilder)3 UnitOfWorkDomainEventsValue (org.qi4j.library.eventsourcing.domain.api.UnitOfWorkDomainEventsValue)3 File (java.io.File)2 BeforeClass (org.junit.BeforeClass)2 Ignore (org.junit.Ignore)2 ServiceDeclaration (org.qi4j.bootstrap.ServiceDeclaration)2