Search in sources :

Example 1 with C3P0DataSourceServiceAssembler

use of org.qi4j.library.sql.c3p0.C3P0DataSourceServiceAssembler 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 {
            ModuleAssembly configModule = module;
            // Create in-memory store for configurations
            new EntityTestAssembler().assemble(configModule);
            new C3P0DataSourceServiceAssembler().identifiedBy("datasource-service").withConfig(configModule, Visibility.layer).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
            // START SNIPPET: assembly
            new LiquibaseAssembler().withConfig(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");
        }

        @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)

Example 2 with C3P0DataSourceServiceAssembler

use of org.qi4j.library.sql.c3p0.C3P0DataSourceServiceAssembler in project qi4j-sdk by Qi4j.

the class DataSourceConfigurationManagerServiceTest method testDataSources.

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

        @Override
        public void assemble(ModuleAssembly module) throws AssemblyException {
            new JMXAssembler().assemble(module);
            // Create in-memory store for configurations
            new EntityTestAssembler().visibleIn(Visibility.layer).assemble(module);
            // Set up DataSource service that will manage the connection pools
            new C3P0DataSourceServiceAssembler().identifiedBy("datasource-service").visibleIn(Visibility.layer).assemble(module);
            {
                ModuleAssembly testModule = module.layer().module("TestDS");
                // Create a specific DataSource that uses the "datasource" service to do the main work
                new DataSourceAssembler().withDataSourceServiceIdentity("datasource-service").identifiedBy("testds").visibleIn(Visibility.module).withCircuitBreaker(DataSources.newDataSourceCircuitBreaker()).assemble(testModule);
                // Set up Liquibase service that will create the tables
                testModule.services(LiquibaseService.class).identifiedBy("liquibase1").instantiateOnStartup();
                testModule.entities(LiquibaseConfiguration.class);
                testModule.forMixin(LiquibaseConfiguration.class).declareDefaults().enabled().set(true);
                testModule.forMixin(LiquibaseConfiguration.class).declareDefaults().changeLog().set("changelog.xml");
            }
            {
                ModuleAssembly testModule2 = module.layer().module("TestDS2");
                // Create another specific DataSource that uses the "datasource" service to do the main work
                // Use DataSourceAssembler to assemble the DataSource.
                new DataSourceAssembler().withDataSourceServiceIdentity("datasource-service").identifiedBy("testds2").visibleIn(Visibility.module).withCircuitBreaker(DataSources.newDataSourceCircuitBreaker()).assemble(testModule2);
                // Set up Liquibase service that will create the tables
                testModule2.services(LiquibaseService.class).identifiedBy("liquibase2").instantiateOnStartup();
                testModule2.entities(LiquibaseConfiguration.class);
                testModule2.forMixin(LiquibaseConfiguration.class).declareDefaults().enabled().set(true);
                testModule2.forMixin(LiquibaseConfiguration.class).declareDefaults().changeLog().set("changelog.xml");
            }
            // START SNIPPET: jmx
            new DataSourceJMXAssembler().visibleIn(Visibility.module).assemble(module);
        // END SNIPPET: jmx
        }
    };
//        assembler.application().findModule( "Layer 1","Test" ).objectBuilderFactory().newObjectBuilder( DataSourceConfigurationManagerServiceTest.class ).injectTo( this );
}
Also used : ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) C3P0DataSourceServiceAssembler(org.qi4j.library.sql.c3p0.C3P0DataSourceServiceAssembler) DataSourceAssembler(org.qi4j.library.sql.assembly.DataSourceAssembler) DataSourceJMXAssembler(org.qi4j.library.sql.assembly.DataSourceJMXAssembler) JMXAssembler(org.qi4j.library.jmx.JMXAssembler) DataSourceJMXAssembler(org.qi4j.library.sql.assembly.DataSourceJMXAssembler) SingletonAssembler(org.qi4j.bootstrap.SingletonAssembler) EntityTestAssembler(org.qi4j.test.EntityTestAssembler) LiquibaseConfiguration(org.qi4j.library.sql.liquibase.LiquibaseConfiguration) LiquibaseService(org.qi4j.library.sql.liquibase.LiquibaseService) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 ModuleAssembly (org.qi4j.bootstrap.ModuleAssembly)2 SingletonAssembler (org.qi4j.bootstrap.SingletonAssembler)2 DataSourceAssembler (org.qi4j.library.sql.assembly.DataSourceAssembler)2 C3P0DataSourceServiceAssembler (org.qi4j.library.sql.c3p0.C3P0DataSourceServiceAssembler)2 EntityTestAssembler (org.qi4j.test.EntityTestAssembler)2 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 DataSource (javax.sql.DataSource)1 ActivationEvent (org.qi4j.api.activation.ActivationEvent)1 ActivationEventListener (org.qi4j.api.activation.ActivationEventListener)1 Application (org.qi4j.api.structure.Application)1 Module (org.qi4j.api.structure.Module)1 Function (org.qi4j.functional.Function)1 JMXAssembler (org.qi4j.library.jmx.JMXAssembler)1 DataSourceJMXAssembler (org.qi4j.library.sql.assembly.DataSourceJMXAssembler)1 Databases (org.qi4j.library.sql.common.Databases)1 LiquibaseConfiguration (org.qi4j.library.sql.liquibase.LiquibaseConfiguration)1 LiquibaseService (org.qi4j.library.sql.liquibase.LiquibaseService)1