Search in sources :

Example 16 with AssemblyException

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

the class VisitableDetailTest method visit.

@Test
public void visit() throws AssemblyException, ActivationException {
    ApplicationDescriptor application = new Energy4Java().newApplicationModel(new ApplicationAssembler() {

        @Override
        public ApplicationAssembly assemble(ApplicationAssemblyFactory applicationFactory) throws AssemblyException {
            ApplicationAssembly app = applicationFactory.newApplicationAssembly();
            app.setName("UnderTestApp");
            app.withActivators(ApplicationActivator.class);
            LayerAssembly layer = app.layer("LayerName");
            layer.withActivators(LayerActivator.class);
            ModuleAssembly module = layer.module("ModuleName");
            module.withActivators(ModuleActivator.class);
            return app;
        }
    });
    ApplicationDetailDescriptor detail = createApplicationDetailDescriptor(application);
    Visitor visitor = new Visitor();
    detail.accept(visitor);
    assertThat(visitor.events, equalTo(Arrays.asList(// Application
    "visitEnter( UnderTestApp )", "visit( " + ApplicationActivator.class.getName() + " )", // Layer
    "visitEnter( LayerName )", "visit( " + LayerActivator.class.getName() + " )", // Module
    "visitEnter( ModuleName )", "visit( " + ModuleActivator.class.getName() + " )", // Leaving Structure
    "visitLeave( ModuleName )", "visitLeave( LayerName )", "visitLeave( UnderTestApp )")));
}
Also used : ApplicationAssemblyFactory(org.qi4j.bootstrap.ApplicationAssemblyFactory) ApplicationDetailDescriptorBuilder.createApplicationDetailDescriptor(org.qi4j.tools.model.descriptor.ApplicationDetailDescriptorBuilder.createApplicationDetailDescriptor) ApplicationDetailDescriptor(org.qi4j.tools.model.descriptor.ApplicationDetailDescriptor) HierarchicalVisitor(org.qi4j.functional.HierarchicalVisitor) ApplicationAssembly(org.qi4j.bootstrap.ApplicationAssembly) ApplicationDescriptor(org.qi4j.api.structure.ApplicationDescriptor) LayerAssembly(org.qi4j.bootstrap.LayerAssembly) AssemblyException(org.qi4j.bootstrap.AssemblyException) ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) ApplicationAssembler(org.qi4j.bootstrap.ApplicationAssembler) Energy4Java(org.qi4j.bootstrap.Energy4Java) Test(org.junit.Test)

Example 17 with AssemblyException

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

the class PassivationExceptionTest method testPassivationExceptionsAccrossStructure.

@Test
public void testPassivationExceptionsAccrossStructure() throws AssemblyException, ActivationException {
    ApplicationBuilder appBuilder = new ApplicationBuilder("TestApplication");
    appBuilder.withLayer("Layer 1").withModule("Module A").withAssembler(new Assembler() {

        @Override
        public void assemble(ModuleAssembly module) throws AssemblyException {
            module.services(TestService.class).identifiedBy("TestService_Module.A").withActivators(FailBeforePassivationServiceActivator.class).instantiateOnStartup();
        }
    });
    appBuilder.withLayer("Layer 2").withModule("Module B").withAssembler(new Assembler() {

        @Override
        public void assemble(ModuleAssembly module) throws AssemblyException {
            module.services(TestService.class).identifiedBy("TestService_Module.B").withActivators(FailAfterPassivationServiceActivator.class).instantiateOnStartup();
        }
    });
    appBuilder.registerActivationEventListener(new TestActivationEventListener());
    Application app = appBuilder.newApplication();
    try {
        Module moduleA = app.findModule("Layer 1", "Module A");
        TestService service = moduleA.findService(TestService.class).get();
        assertThat(service.hello(), equalTo("Hello Qi4j!"));
    } finally {
        try {
            app.passivate();
            fail("No PassivationException");
        } catch (PassivationException ex) {
            ex.printStackTrace();
            String stack = stack(ex);
            assertThat(ex.getMessage(), containsString("has 12 cause(s)"));
            assertThat(stack, containsString("EVENT: FAIL BEFORE PASSIVATION for TestApplication"));
            assertThat(stack, containsString("EVENT: FAIL BEFORE PASSIVATION for Layer 2"));
            assertThat(stack, containsString("EVENT: FAIL BEFORE PASSIVATION for Module B"));
            assertThat(stack, containsString("ACTIVATOR: FAIL AFTER PASSIVATION for TestService_Module.B(active=false,module='Module B')"));
            assertThat(stack, containsString("EVENT: FAIL AFTER PASSIVATION for Module B"));
            assertThat(stack, containsString("EVENT: FAIL AFTER PASSIVATION for Layer 2"));
            assertThat(stack, containsString("EVENT: FAIL BEFORE PASSIVATION for Layer 1"));
            assertThat(stack, containsString("EVENT: FAIL BEFORE PASSIVATION for Module A"));
            assertThat(stack, containsString("ACTIVATOR: FAIL BEFORE PASSIVATION for TestService_Module.A(active=true,module='Module A')"));
            assertThat(stack, containsString("EVENT: FAIL AFTER PASSIVATION for Module A"));
            assertThat(stack, containsString("EVENT: FAIL AFTER PASSIVATION for Layer 1"));
            assertThat(stack, containsString("EVENT: FAIL AFTER PASSIVATION for TestApplication"));
        }
    }
}
Also used : ApplicationBuilder(org.qi4j.bootstrap.builder.ApplicationBuilder) ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) AssemblyException(org.qi4j.bootstrap.AssemblyException) Assembler(org.qi4j.bootstrap.Assembler) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Module(org.qi4j.api.structure.Module) Application(org.qi4j.api.structure.Application) Test(org.junit.Test)

Example 18 with AssemblyException

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

the class DocumentationSupport method assembledWithValuesModuleSerialization.

// END SNIPPET: io
@Test
public // TODO Include in each ValueSerialization extensions documentation
void assembledWithValuesModuleSerialization() throws Exception {
    Application app = new Energy4Java().newApplication(new ApplicationAssembler() {

        @Override
        public ApplicationAssembly assemble(ApplicationAssemblyFactory applicationFactory) throws AssemblyException {
            Assembler[][][] pancakes = new Assembler[][][] { { { new Assembler() {

                @Override
                public void assemble(ModuleAssembly valuesModule) throws AssemblyException {
                    valuesModule.layer().setName("SINGLE-Layer");
                    valuesModule.setName("VALUES-Module");
                    valuesModule.values(SomeValue.class);
                }
            } }, { new Assembler() {

                @Override
                public void assemble(ModuleAssembly servicesModule) throws AssemblyException {
                    servicesModule.setName("SERVICES-Module");
                    Function<Application, Module> valuesModuleFinder = new Function<Application, Module>() {

                        @Override
                        public Module map(Application app) {
                            return app.findModule("SINGLE-Layer", "VALUES-Module");
                        }
                    };
                    new OrgJsonValueSerializationAssembler().withValuesModuleFinder(valuesModuleFinder).assemble(servicesModule);
                }
            } } } };
            return applicationFactory.newApplicationAssembly(pancakes);
        }
    });
    app.activate();
    try {
        Module valuesModule = app.findModule("SINGLE-Layer", "VALUES-Module");
        SomeValue someValue = someNewValueInstance(valuesModule);
        Module servicesModule = app.findModule("SINGLE-Layer", "SERVICES-Module");
        ValueSerialization valueSerialization = servicesModule.findService(ValueSerialization.class).get();
        String json = valueSerialization.serialize(someValue);
        assertThat(json, equalTo("{\"foo\":\"bar\"}"));
        SomeValue someNewValue = valueSerialization.deserialize(SomeValue.class, json);
        assertThat(someNewValue, equalTo(someValue));
    } finally {
        app.passivate();
    }
}
Also used : ApplicationAssemblyFactory(org.qi4j.bootstrap.ApplicationAssemblyFactory) ApplicationAssembly(org.qi4j.bootstrap.ApplicationAssembly) AssemblyException(org.qi4j.bootstrap.AssemblyException) ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) Function(org.qi4j.functional.Function) ApplicationAssembler(org.qi4j.bootstrap.ApplicationAssembler) Energy4Java(org.qi4j.bootstrap.Energy4Java) Assembler(org.qi4j.bootstrap.Assembler) OrgJsonValueSerializationAssembler(org.qi4j.valueserialization.orgjson.OrgJsonValueSerializationAssembler) ApplicationAssembler(org.qi4j.bootstrap.ApplicationAssembler) Module(org.qi4j.api.structure.Module) Application(org.qi4j.api.structure.Application) OrgJsonValueSerializationAssembler(org.qi4j.valueserialization.orgjson.OrgJsonValueSerializationAssembler) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 19 with AssemblyException

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

the class ModuleAssemblyImpl method assembleModule.

ModuleModel assembleModule(AssemblyHelper helper) throws AssemblyException {
    List<TransientModel> transientModels = new ArrayList<>();
    List<ObjectModel> objectModels = new ArrayList<>();
    List<ValueModel> valueModels = new ArrayList<>();
    List<ServiceModel> serviceModels = new ArrayList<>();
    List<ImportedServiceModel> importedServiceModels = new ArrayList<>();
    if (name == null) {
        throw new AssemblyException("Module must have name set");
    }
    for (TransientAssemblyImpl compositeDeclaration : transientAssemblies.values()) {
        transientModels.add(compositeDeclaration.newTransientModel(metaInfoDeclaration, helper));
    }
    for (ValueAssemblyImpl valueDeclaration : valueAssemblies.values()) {
        valueModels.add(valueDeclaration.newValueModel(metaInfoDeclaration, helper));
    }
    List<EntityModel> entityModels = new ArrayList<>();
    for (EntityAssemblyImpl entityDeclaration : entityAssemblies.values()) {
        entityModels.add(entityDeclaration.newEntityModel(metaInfoDeclaration, metaInfoDeclaration, metaInfoDeclaration, metaInfoDeclaration, helper));
    }
    for (ObjectAssemblyImpl objectDeclaration : objectAssemblies.values()) {
        objectDeclaration.addObjectModel(objectModels);
    }
    for (ServiceAssemblyImpl serviceDeclaration : serviceAssemblies) {
        if (serviceDeclaration.identity == null) {
            serviceDeclaration.identity = generateId(serviceDeclaration.types());
        }
        serviceModels.add(serviceDeclaration.newServiceModel(metaInfoDeclaration, helper));
    }
    for (ImportedServiceAssemblyImpl importedServiceDeclaration : importedServiceAssemblies.values()) {
        importedServiceDeclaration.addImportedServiceModel(importedServiceModels);
    }
    ModuleModel moduleModel = new ModuleModel(name, metaInfo, new ActivatorsModel<>(activators), new TransientsModel(transientModels), new EntitiesModel(entityModels), new ObjectsModel(objectModels), new ValuesModel(valueModels), new ServicesModel(serviceModels), new ImportedServicesModel(importedServiceModels));
    // Check for duplicate service identities
    Set<String> identities = new HashSet<>();
    for (ServiceModel serviceModel : serviceModels) {
        String identity = serviceModel.identity();
        if (identities.contains(identity)) {
            throw new DuplicateServiceIdentityException("Duplicated service identity: " + identity + " in module " + moduleModel.name());
        }
        identities.add(identity);
    }
    for (ImportedServiceModel serviceModel : importedServiceModels) {
        String identity = serviceModel.identity();
        if (identities.contains(identity)) {
            throw new DuplicateServiceIdentityException("Duplicated service identity: " + identity + " in module " + moduleModel.name());
        }
        identities.add(identity);
    }
    for (ImportedServiceModel importedServiceModel : importedServiceModels) {
        boolean found = false;
        for (ObjectModel objectModel : objectModels) {
            if (first(objectModel.types()).equals(importedServiceModel.serviceImporter())) {
                found = true;
                break;
            }
        }
        if (!found) {
            @SuppressWarnings("raw") Class<? extends ServiceImporter> serviceFactoryType = importedServiceModel.serviceImporter();
            ObjectModel objectModel = new ObjectModel(serviceFactoryType, Visibility.module, new MetaInfo());
            objectModels.add(objectModel);
        }
    }
    return moduleModel;
}
Also used : ObjectModel(org.qi4j.runtime.object.ObjectModel) ValueModel(org.qi4j.runtime.value.ValueModel) ImportedServiceModel(org.qi4j.runtime.service.ImportedServiceModel) ArrayList(java.util.ArrayList) ImportedServicesModel(org.qi4j.runtime.service.ImportedServicesModel) MetaInfo(org.qi4j.api.common.MetaInfo) ObjectsModel(org.qi4j.runtime.object.ObjectsModel) ModuleModel(org.qi4j.runtime.structure.ModuleModel) EntitiesModel(org.qi4j.runtime.entity.EntitiesModel) AssemblyException(org.qi4j.bootstrap.AssemblyException) ServiceModel(org.qi4j.runtime.service.ServiceModel) ImportedServiceModel(org.qi4j.runtime.service.ImportedServiceModel) HashSet(java.util.HashSet) ImportedServicesModel(org.qi4j.runtime.service.ImportedServicesModel) ServicesModel(org.qi4j.runtime.service.ServicesModel) EntityModel(org.qi4j.runtime.entity.EntityModel) DuplicateServiceIdentityException(org.qi4j.api.service.DuplicateServiceIdentityException) ValuesModel(org.qi4j.runtime.value.ValuesModel) TransientModel(org.qi4j.runtime.composite.TransientModel) TransientsModel(org.qi4j.runtime.composite.TransientsModel)

Example 20 with AssemblyException

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

the class IssueTest method testLayersCanBeCreatedInOrderDifferentFromTheirDependency.

@Test
public void testLayersCanBeCreatedInOrderDifferentFromTheirDependency() throws AssemblyException {
    Energy4Java qi4j = new Energy4Java();
    Application app = qi4j.newApplication(new ApplicationAssembler() {

        public ApplicationAssembly assemble(ApplicationAssemblyFactory applicationFactory) throws AssemblyException {
            ApplicationAssembly assembly = applicationFactory.newApplicationAssembly();
            LayerAssembly domainLayer = assembly.layer(null);
            domainLayer.setName("Domain");
            LayerAssembly infrastructureLayer = assembly.layer(null);
            infrastructureLayer.setName("Infrastructure");
            domainLayer.uses(infrastructureLayer);
            return assembly;
        }
    });
    ApplicationDescriptor model = app.descriptor();
    model.accept(new HierarchicalVisitorAdapter<Object, Object, RuntimeException>() {

        @Override
        public boolean visitEnter(Object visited) throws RuntimeException {
            return visited instanceof ApplicationDescriptor;
        }

        @Override
        public boolean visitLeave(Object visited) throws RuntimeException {
            return visited instanceof LayerDescriptor;
        }

        @Override
        public boolean visit(Object visited) throws RuntimeException {
            if (visited instanceof LayerDescriptor) {
                Iterable<? extends LayerDescriptor> usedLayers = ((LayerDescriptor) visited).usedLayers().layers();
                for (LayerDescriptor usedLayerModel : usedLayers) {
                    Assert.assertNotNull("Used layer model is null", usedLayerModel);
                }
            }
            return false;
        }
    });
}
Also used : ApplicationAssemblyFactory(org.qi4j.bootstrap.ApplicationAssemblyFactory) ApplicationAssembly(org.qi4j.bootstrap.ApplicationAssembly) LayerAssembly(org.qi4j.bootstrap.LayerAssembly) ApplicationDescriptor(org.qi4j.api.structure.ApplicationDescriptor) AssemblyException(org.qi4j.bootstrap.AssemblyException) ApplicationAssembler(org.qi4j.bootstrap.ApplicationAssembler) Energy4Java(org.qi4j.bootstrap.Energy4Java) Application(org.qi4j.api.structure.Application) LayerDescriptor(org.qi4j.api.structure.LayerDescriptor) Test(org.junit.Test)

Aggregations

AssemblyException (org.qi4j.bootstrap.AssemblyException)38 ModuleAssembly (org.qi4j.bootstrap.ModuleAssembly)27 Test (org.junit.Test)23 Module (org.qi4j.api.structure.Module)14 SingletonAssembler (org.qi4j.bootstrap.SingletonAssembler)14 IOException (java.io.IOException)10 UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)10 ApplicationAssembler (org.qi4j.bootstrap.ApplicationAssembler)10 Assembler (org.qi4j.bootstrap.Assembler)10 ApplicationAssemblyFactory (org.qi4j.bootstrap.ApplicationAssemblyFactory)9 Application (org.qi4j.api.structure.Application)8 ApplicationAssembly (org.qi4j.bootstrap.ApplicationAssembly)8 Energy4Java (org.qi4j.bootstrap.Energy4Java)8 LayerAssembly (org.qi4j.bootstrap.LayerAssembly)5 EntityTestAssembler (org.qi4j.test.EntityTestAssembler)5 AmbiguousTypeException (org.qi4j.api.composite.AmbiguousTypeException)4 ApplicationDescriptor (org.qi4j.api.structure.ApplicationDescriptor)4 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 JettyConfiguration (org.qi4j.library.http.JettyConfiguration)2