Search in sources :

Example 26 with Application

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

the class ActivationEventsTest method testSingleModuleSingleLazyService.

@Test
public void testSingleModuleSingleLazyService() throws Exception {
    final List<ActivationEvent> events = new ArrayList<>();
    SingletonAssembler assembler = new SingletonAssembler() {

        @Override
        public void assemble(ModuleAssembly module) throws AssemblyException {
            module.services(TestServiceComposite.class);
        }

        @Override
        protected void beforeActivation(Application application) {
            application.registerActivationEventListener(new EventsRecorder(events));
        }
    };
    Application application = assembler.application();
    application.passivate();
    Iterator<ActivationEvent> it = events.iterator();
    // Activation
    assertEvent(it.next(), ACTIVATING, "Application");
    assertEvent(it.next(), ACTIVATING, "Layer");
    assertEvent(it.next(), ACTIVATING, "Module");
    // Lazy Service NOT activated
    assertEvent(it.next(), ACTIVATED, "Module");
    assertEvent(it.next(), ACTIVATED, "Layer");
    assertEvent(it.next(), ACTIVATED, "Application");
    // Passivation
    assertEvent(it.next(), PASSIVATING, "Application");
    assertEvent(it.next(), PASSIVATING, "Layer");
    assertEvent(it.next(), PASSIVATING, "Module");
    // Lazy Service NOT passivated
    assertEvent(it.next(), PASSIVATED, "Module");
    assertEvent(it.next(), PASSIVATED, "Layer");
    assertEvent(it.next(), PASSIVATED, "Application");
    assertFalse(it.hasNext());
    events.clear();
    application.activate();
    Module module = assembler.module();
    module.findService(TestService.class).get().test();
    application.passivate();
    for (ActivationEvent event : events) {
        System.out.println(event);
    }
    it = events.iterator();
    // Activation
    assertEvent(it.next(), ACTIVATING, "Application");
    assertEvent(it.next(), ACTIVATING, "Layer");
    assertEvent(it.next(), ACTIVATING, "Module");
    assertEvent(it.next(), ACTIVATED, "Module");
    assertEvent(it.next(), ACTIVATED, "Layer");
    assertEvent(it.next(), ACTIVATED, "Application");
    // Lazy Service Activation
    assertEvent(it.next(), ACTIVATING, "TestService");
    assertEvent(it.next(), ACTIVATED, "TestService");
    // Passivation
    assertEvent(it.next(), PASSIVATING, "Application");
    assertEvent(it.next(), PASSIVATING, "Layer");
    assertEvent(it.next(), PASSIVATING, "Module");
    assertEvent(it.next(), PASSIVATING, "TestService");
    assertEvent(it.next(), PASSIVATED, "TestService");
    assertEvent(it.next(), PASSIVATED, "Module");
    assertEvent(it.next(), PASSIVATED, "Layer");
    assertEvent(it.next(), PASSIVATED, "Application");
    assertFalse(it.hasNext());
}
Also used : ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) SingletonAssembler(org.qi4j.bootstrap.SingletonAssembler) ArrayList(java.util.ArrayList) Module(org.qi4j.api.structure.Module) Application(org.qi4j.api.structure.Application) Test(org.junit.Test)

Example 27 with Application

use of org.qi4j.api.structure.Application 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 28 with Application

use of org.qi4j.api.structure.Application 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 29 with Application

use of org.qi4j.api.structure.Application 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)

Example 30 with Application

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

the class ImportedServiceActivationTest method testNewObjectImportedServiceActivators.

@Test
public void testNewObjectImportedServiceActivators() throws Exception {
    SingletonAssembler assembler = new SingletonAssembler() {

        public void assemble(ModuleAssembly module) throws AssemblyException {
            module.importedServices(TestedService.class).importedBy(ImportedServiceDeclaration.NEW_OBJECT).withActivators(TestedActivator.class).importOnStartup();
            module.objects(TestedServiceInstance.class);
        }
    };
    Application application = assembler.application();
    assertEquals("Activation Level", 2, activationLevel);
    application.passivate();
    assertEquals("Passivation Level", 2, passivationLevel);
}
Also used : ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) SingletonAssembler(org.qi4j.bootstrap.SingletonAssembler) Application(org.qi4j.api.structure.Application) Test(org.junit.Test)

Aggregations

Application (org.qi4j.api.structure.Application)50 Test (org.junit.Test)32 ModuleAssembly (org.qi4j.bootstrap.ModuleAssembly)26 Module (org.qi4j.api.structure.Module)18 Energy4Java (org.qi4j.bootstrap.Energy4Java)18 Assembler (org.qi4j.bootstrap.Assembler)13 ApplicationAssemblerAdapter (org.qi4j.bootstrap.ApplicationAssemblerAdapter)10 SingletonAssembler (org.qi4j.bootstrap.SingletonAssembler)10 AssemblyException (org.qi4j.bootstrap.AssemblyException)8 Function (org.qi4j.functional.Function)7 ApplicationAssembler (org.qi4j.bootstrap.ApplicationAssembler)6 ApplicationAssembly (org.qi4j.bootstrap.ApplicationAssembly)6 UnitOfWorkFactory (org.qi4j.api.unitofwork.UnitOfWorkFactory)5 ApplicationAssemblyFactory (org.qi4j.bootstrap.ApplicationAssemblyFactory)5 LayerAssembly (org.qi4j.bootstrap.LayerAssembly)4 MemoryEntityStoreService (org.qi4j.entitystore.memory.MemoryEntityStoreService)4 UuidIdentityGeneratorService (org.qi4j.spi.uuid.UuidIdentityGeneratorService)4 OrgJsonValueSerializationService (org.qi4j.valueserialization.orgjson.OrgJsonValueSerializationService)4 ArrayList (java.util.ArrayList)3 ApplicationDescriptor (org.qi4j.api.structure.ApplicationDescriptor)3