Search in sources :

Example 1 with Module

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

the class ApplicationBuilderTest method givenJsonInputStreamWhenBuildingApplicationExpectSuccess.

@Test
public void givenJsonInputStreamWhenBuildingApplicationExpectSuccess() throws IOException, JSONException, ActivationException, AssemblyException {
    InputStream input = new ByteArrayInputStream(APPLICATION.getBytes("UTF-8"));
    ApplicationBuilder builder = ApplicationBuilder.fromJson(input);
    Application application = builder.newApplication();
    Module module = application.findModule("layer3", "test module");
    TestService service = module.findService(TestService.class).get();
    assertThat(service.sayHello(), equalTo("Hello Qi4j!"));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Module(org.qi4j.api.structure.Module) Application(org.qi4j.api.structure.Application) Test(org.junit.Test)

Example 2 with Module

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

the class ApplicationBuilderTest method givenBuilderUseWhenBuildingApplicationExpectSuccess.

@Test
public void givenBuilderUseWhenBuildingApplicationExpectSuccess() throws AssemblyException, ActivationException {
    ApplicationBuilder builder = new ApplicationBuilder("Build from API test.");
    builder.withLayer("layer1").using("layer2").using("layer3");
    builder.withLayer("layer2");
    builder.withLayer("layer3").withModule("test module").withAssemblers(filter(matches(".*ServiceAssembler"), findClasses(getClass())));
    Application application = builder.newApplication();
    Module module = application.findModule("layer3", "test module");
    TestService service = module.findService(TestService.class).get();
    assertThat(service.sayHello(), equalTo("Hello Qi4j!"));
}
Also used : Module(org.qi4j.api.structure.Module) Application(org.qi4j.api.structure.Application) Test(org.junit.Test)

Example 3 with Module

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

the class AbstractEntityStorePerformanceTest method createQi4jRuntime.

private void createQi4jRuntime(Assembler testSetup) throws Exception {
    Energy4Java qi4j = new Energy4Java();
    Assembler[][][] assemblers = new Assembler[][][] { { { infrastructure, testSetup } } };
    application = qi4j.newApplication(new ApplicationAssemblerAdapter(assemblers) {
    });
    application.activate();
    Module moduleInstance = application.findModule("Layer 1", "Module 1");
    module = moduleInstance;
}
Also used : Energy4Java(org.qi4j.bootstrap.Energy4Java) Assembler(org.qi4j.bootstrap.Assembler) Module(org.qi4j.api.structure.Module) ApplicationAssemblerAdapter(org.qi4j.bootstrap.ApplicationAssemblerAdapter)

Example 4 with Module

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

the class ServiceLocator method locateService.

@SuppressWarnings("unchecked")
ServiceReference locateService(Application anApplication) {
    if (layerName != null) {
        Module module = anApplication.findModule(layerName, moduleName);
        Iterable<ServiceReference<Object>> serviceRefs = module.findServices(serviceType);
        for (ServiceReference<Object> serviceRef : serviceRefs) {
            if (serviceId.equals(serviceRef.identity())) {
                return serviceRef;
            }
        }
    }
    return null;
}
Also used : Module(org.qi4j.api.structure.Module) ServiceReference(org.qi4j.api.service.ServiceReference)

Example 5 with Module

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

the class LiquibaseServiceTest method testLiquibase.

@Test
public void testLiquibase() throws SQLException, IOException, ActivationException, AssemblyException {
    final SingletonAssembler assembler = new SingletonAssembler() {

        @Override
        public void assemble(ModuleAssembly module) throws AssemblyException {
            new C3P0DataSourceServiceAssembler().identifiedBy("datasource-service").assemble(module);
            new DataSourceAssembler().withDataSourceServiceIdentity("datasource-service").identifiedBy("testds-liquibase").withCircuitBreaker().assemble(module);
            module.values(SomeValue.class);
            // Set up Liquibase service that will create the tables
            ModuleAssembly configModule = module;
            // START SNIPPET: assembly
            new LiquibaseAssembler(Visibility.module).withConfigIn(configModule, Visibility.layer).assemble(module);
            // END SNIPPET: assembly
            module.forMixin(LiquibaseConfiguration.class).declareDefaults().enabled().set(true);
            module.forMixin(LiquibaseConfiguration.class).declareDefaults().changeLog().set("changelog.xml");
            // Create in-memory store for configurations
            new EntityTestAssembler().assemble(module);
        }

        @Override
        public void beforeActivation(Application application) {
            application.registerActivationEventListener(new ActivationEventListener() {

                @Override
                public void onEvent(ActivationEvent event) {
                    System.out.println(event);
                }
            });
        }
    };
    Module module = assembler.module();
    // START SNIPPET: io
    // Look up the DataSource
    DataSource ds = module.findService(DataSource.class).get();
    // Instanciate Databases helper
    Databases database = new Databases(ds);
    // Assert that insertion works
    assertTrue(database.update("insert into test values ('someid', 'bar')") == 1);
    // END SNIPPET: io
    database.query("select * from test", new Databases.ResultSetVisitor() {

        @Override
        public boolean visit(ResultSet visited) throws SQLException {
            assertThat(visited.getString("id"), equalTo("someid"));
            assertThat(visited.getString("foo"), equalTo("bar"));
            return true;
        }
    });
    Function<ResultSet, SomeValue> toValue = new Function<ResultSet, SomeValue>() {

        @Override
        public SomeValue map(ResultSet resultSet) {
            ValueBuilder<SomeValue> builder = assembler.module().newValueBuilder(SomeValue.class);
            try {
                builder.prototype().id().set(resultSet.getString("id"));
                builder.prototype().foo().set(resultSet.getString("foo"));
            } catch (SQLException e) {
                throw new IllegalArgumentException("Could not convert to SomeValue", e);
            }
            return builder.newInstance();
        }
    };
    // START SNIPPET: io
    // Select rows and load them in a List
    List<SomeValue> rows = new ArrayList<SomeValue>();
    database.query("select * from test").transferTo(map(toValue, collection(rows)));
    // Transfer all rows to System.out
    Inputs.iterable(rows).transferTo(Outputs.systemOut());
// END SNIPPET: io
}
Also used : C3P0DataSourceServiceAssembler(org.qi4j.library.sql.c3p0.C3P0DataSourceServiceAssembler) DataSourceAssembler(org.qi4j.library.sql.assembly.DataSourceAssembler) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) DataSource(javax.sql.DataSource) ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) Function(org.qi4j.functional.Function) Databases(org.qi4j.library.sql.common.Databases) SingletonAssembler(org.qi4j.bootstrap.SingletonAssembler) EntityTestAssembler(org.qi4j.test.EntityTestAssembler) ActivationEventListener(org.qi4j.api.activation.ActivationEventListener) ResultSet(java.sql.ResultSet) ActivationEvent(org.qi4j.api.activation.ActivationEvent) Module(org.qi4j.api.structure.Module) Application(org.qi4j.api.structure.Application) 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