Search in sources :

Example 31 with Module

use of org.qi4j.api.structure.Module in project qi4j-sdk by Qi4j.

the class CompositeModelResolverTest method testWhenDependentMixinsThenOrderMixins.

@Test
public void testWhenDependentMixinsThenOrderMixins() throws Exception {
    Module module = new SingletonAssembler() {

        public void assemble(ModuleAssembly module) throws AssemblyException {
            module.transients(TestComposite1.class);
        }
    }.module();
    Assert.assertEquals("ok", module.newTransient(TestComposite1.class).testB());
}
Also used : ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) AssemblyException(org.qi4j.bootstrap.AssemblyException) SingletonAssembler(org.qi4j.bootstrap.SingletonAssembler) Module(org.qi4j.api.structure.Module) Test(org.junit.Test)

Example 32 with Module

use of org.qi4j.api.structure.Module in project qi4j-sdk by Qi4j.

the class PrivateEntityUnitOfWorkTest method givenAppWithPrivateEntityWhenUnitOfWorkCanSeeItThenCanCommit.

@Test
public void givenAppWithPrivateEntityWhenUnitOfWorkCanSeeItThenCanCommit() throws Exception {
    System.setProperty("qi4j.compacttrace", "off");
    Energy4Java is = new Energy4Java();
    Application app = is.newApplication(new ApplicationAssembler() {

        public ApplicationAssembly assemble(ApplicationAssemblyFactory applicationFactory) throws AssemblyException {
            return applicationFactory.newApplicationAssembly(new Assembler[][][] { { { new Assembler() {

                public void assemble(ModuleAssembly module) throws AssemblyException {
                    module.objects(PrivateEntityUnitOfWorkTest.class);
                }
            } } }, { { new Assembler() {

                public void assemble(ModuleAssembly module) throws AssemblyException {
                    module.entities(ProductEntity.class);
                    module.entities(ProductCatalogEntity.class).visibleIn(application);
                    module.values(ProductInfo.class);
                    new EntityTestAssembler().assemble(module);
                }
            } } } });
        }
    });
    app.activate();
    Module module = app.findModule("Layer 1", "Module 1");
    module.injectTo(this);
    UnitOfWork unitOfWork = uowf.newUnitOfWork();
    try {
        unitOfWork.newEntity(ProductEntity.class);
        fail("Should not be able to create product here");
    } catch (EntityTypeNotFoundException e) {
        // Ok
        ProductCatalog catalog = unitOfWork.newEntity(ProductCatalog.class, "1");
        unitOfWork.complete();
    }
    unitOfWork = uowf.newUnitOfWork();
    String id;
    try {
        ProductCatalog catalog = unitOfWork.get(ProductCatalog.class, "1");
        id = ((Identity) catalog.newProduct()).identity().get();
        unitOfWork.complete();
    } finally {
        unitOfWork.discard();
    }
    unitOfWork = module.newUnitOfWork();
    try {
        ProductCatalog catalog = unitOfWork.get(ProductCatalog.class, "1");
        Product product = catalog.findProduct(id);
        product.price().set(100);
        unitOfWork.complete();
    } finally {
        unitOfWork.discard();
    }
}
Also used : ApplicationAssemblyFactory(org.qi4j.bootstrap.ApplicationAssemblyFactory) UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) EntityTypeNotFoundException(org.qi4j.api.unitofwork.EntityTypeNotFoundException) ApplicationAssembly(org.qi4j.bootstrap.ApplicationAssembly) AssemblyException(org.qi4j.bootstrap.AssemblyException) ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) ApplicationAssembler(org.qi4j.bootstrap.ApplicationAssembler) EntityTestAssembler(org.qi4j.test.EntityTestAssembler) Energy4Java(org.qi4j.bootstrap.Energy4Java) Assembler(org.qi4j.bootstrap.Assembler) ApplicationAssembler(org.qi4j.bootstrap.ApplicationAssembler) EntityTestAssembler(org.qi4j.test.EntityTestAssembler) Module(org.qi4j.api.structure.Module) Identity(org.qi4j.api.entity.Identity) Application(org.qi4j.api.structure.Application) Test(org.junit.Test)

Example 33 with Module

use of org.qi4j.api.structure.Module in project qi4j-sdk by Qi4j.

the class TypeToCompositeLookupTest method servicesPluralDeclaration.

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

        @Override
        public void assemble(ModuleAssembly module) throws AssemblyException {
            module.services(SomeOtherFoo.class, BasicFoo.class);
        }
    }.module();
    assertEquals(1, Iterables.count(module.findServices(SomeOtherFoo.class)));
    assertEquals(2, Iterables.count(module.findServices(BasicFoo.class)));
    assertEquals(2, Iterables.count(module.findServices(Foo.class)));
    assertEquals(CATHEDRAL, module.findService(SomeOtherFoo.class).get().bar());
    // Exact type match first even if it is assembled _after_ an assignable, the assignable comes after
    Iterator<ServiceReference<BasicFoo>> basicFoos = module.findServices(BasicFoo.class).iterator();
    assertEquals(BAZAR, basicFoos.next().get().bar());
    assertEquals(CATHEDRAL, basicFoos.next().get().bar());
    assertFalse(basicFoos.hasNext());
    // No exact type match, all assembled are assignable, follows assembly Type order
    Iterator<ServiceReference<Foo>> foos = module.findServices(Foo.class).iterator();
    assertEquals(CATHEDRAL, foos.next().get().bar());
    assertEquals(BAZAR, foos.next().get().bar());
    assertFalse(foos.hasNext());
}
Also used : ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) AssemblyException(org.qi4j.bootstrap.AssemblyException) SingletonAssembler(org.qi4j.bootstrap.SingletonAssembler) Module(org.qi4j.api.structure.Module) ServiceReference(org.qi4j.api.service.ServiceReference) Test(org.junit.Test)

Example 34 with Module

use of org.qi4j.api.structure.Module in project qi4j-sdk by Qi4j.

the class TypeToCompositeLookupTest method entities.

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

        @Override
        public void assemble(ModuleAssembly module) throws AssemblyException {
            new EntityTestAssembler().assemble(module);
            module.entities(SomeOtherFoo.class);
        }
    }.module();
    UnitOfWork uow = module.newUnitOfWork();
    SomeOtherFoo someOtherFoo = uow.newEntityBuilder(SomeOtherFoo.class).newInstance();
    BasicFoo basicFoo = uow.newEntityBuilder(BasicFoo.class).newInstance();
    Foo foo = uow.newEntityBuilder(Foo.class).newInstance();
    assertEquals(CATHEDRAL, someOtherFoo.bar());
    assertEquals(CATHEDRAL, basicFoo.bar());
    assertEquals(CATHEDRAL, foo.bar());
    String someOtherFooIdentity = ((Identity) someOtherFoo).identity().get();
    String basicFooIdentity = ((Identity) basicFoo).identity().get();
    String fooIdentity = ((Identity) foo).identity().get();
    uow.complete();
    uow = module.newUnitOfWork();
    uow.get(SomeOtherFoo.class, someOtherFooIdentity);
    uow.get(BasicFoo.class, basicFooIdentity);
    uow.get(Foo.class, fooIdentity);
    uow.discard();
}
Also used : ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) AssemblyException(org.qi4j.bootstrap.AssemblyException) UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) SingletonAssembler(org.qi4j.bootstrap.SingletonAssembler) EntityTestAssembler(org.qi4j.test.EntityTestAssembler) Module(org.qi4j.api.structure.Module) Test(org.junit.Test)

Example 35 with Module

use of org.qi4j.api.structure.Module in project qi4j-sdk by Qi4j.

the class TypeToCompositeLookupTest method valuesAmbiguousDeclaration.

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

        @Override
        public void assemble(ModuleAssembly module) throws AssemblyException {
            module.values(SomeOtherFoo.class, BasicFoo.class);
        }
    }.module();
    assertEquals(CATHEDRAL, module.newValueBuilder(SomeOtherFoo.class).newInstance().bar());
    assertEquals(BAZAR, module.newValueBuilder(BasicFoo.class).newInstance().bar());
    try {
        module.newValueBuilder(Foo.class);
        fail("Ambiguous type exception not detected for Values");
    } 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)

Aggregations

Module (org.qi4j.api.structure.Module)41 Test (org.junit.Test)28 ModuleAssembly (org.qi4j.bootstrap.ModuleAssembly)18 Application (org.qi4j.api.structure.Application)17 AssemblyException (org.qi4j.bootstrap.AssemblyException)16 SingletonAssembler (org.qi4j.bootstrap.SingletonAssembler)15 ArrayList (java.util.ArrayList)6 Energy4Java (org.qi4j.bootstrap.Energy4Java)6 UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)5 UnitOfWorkFactory (org.qi4j.api.unitofwork.UnitOfWorkFactory)5 AmbiguousTypeException (org.qi4j.api.composite.AmbiguousTypeException)4 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)4 EntityTypeNotFoundException (org.qi4j.api.unitofwork.EntityTypeNotFoundException)4 Assembler (org.qi4j.bootstrap.Assembler)4 HashMap (java.util.HashMap)3 List (java.util.List)3 JSONException (org.json.JSONException)3 JSONObject (org.json.JSONObject)3 JSONTokener (org.json.JSONTokener)3 AssociationDescriptor (org.qi4j.api.association.AssociationDescriptor)3