Search in sources :

Example 1 with EmbeddedDataSource40

use of org.apache.derby.jdbc.EmbeddedDataSource40 in project archaius by Netflix.

the class JDBCConfigurationSourceTest method createDataConfigSource.

/**
	 * Set up code for the test
	 * @param dbname
	 * @return
	 * @throws Throwable
	 */
javax.sql.DataSource createDataConfigSource(String dbname) throws Throwable {
    EmbeddedDataSource40 ds = new EmbeddedDataSource40();
    ds.setDatabaseName(dbname);
    ds.setCreateDatabase(dbname);
    ds.setCreateDatabase("create");
    Connection con = null;
    try {
        con = ds.getConnection();
        // Creating a database table
        Statement sta = null;
        try {
            sta = con.createStatement();
            sta.executeUpdate("DROP TABLE MySiteProperties");
            log.info("Table Dropped");
        } catch (Exception e) {
            log.info("Table did not exist.");
        } finally {
            if (sta != null) {
                sta.close();
            }
        }
        try {
            sta = con.createStatement();
            sta.executeUpdate("CREATE TABLE MySiteProperties (property_key VARCHAR(20)," + " property_value VARCHAR(100))");
            log.info("Table created.");
            sta.close();
        } finally {
            if (sta != null) {
                sta.close();
            }
        }
        try {
            sta = con.createStatement();
            sta.executeUpdate("insert into MySiteProperties values ('prop1','value1')");
            log.info("Properties for testing inserted");
            sta.close();
        } finally {
            if (sta != null) {
                sta.close();
            }
        }
    } catch (Exception e) {
        System.err.println("Exception: " + e.getMessage());
    } finally {
        if (con != null) {
            con.close();
        }
    }
    return ds;
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) EmbeddedDataSource40(org.apache.derby.jdbc.EmbeddedDataSource40)

Example 2 with EmbeddedDataSource40

use of org.apache.derby.jdbc.EmbeddedDataSource40 in project karaf by apache.

the class JdbcLoginModuleTest method setUp.

@Before
public void setUp() throws Exception {
    System.setProperty("derby.stream.error.file", "target/derby.log");
    // Create datasource
    dataSource = new EmbeddedDataSource40();
    dataSource.setDatabaseName("memory:db");
    dataSource.setCreateDatabase("create");
    // Delete tables
    try (Connection connection = dataSource.getConnection()) {
        connection.setAutoCommit(true);
        try {
            try (Statement statement = connection.createStatement()) {
                statement.execute("drop table USERS");
            }
        } catch (SQLException e) {
        // Ignore
        }
        try {
            try (Statement statement = connection.createStatement()) {
                statement.execute("drop table ROLES");
            }
        } catch (SQLException e) {
        // Ignore
        }
        connection.commit();
    }
    // Create tables
    try (Connection connection = dataSource.getConnection()) {
        try (Statement statement = connection.createStatement()) {
            statement.execute("create table USERS (USERNAME VARCHAR(32) PRIMARY KEY, PASSWORD VARCHAR(32))");
        }
        try (Statement statement = connection.createStatement()) {
            statement.execute("create table ROLES (USERNAME VARCHAR(32), ROLE VARCHAR(1024))");
        }
        connection.commit();
    }
    // Mocks
    BundleContext context = EasyMock.createMock(BundleContext.class);
    ServiceReference reference = EasyMock.createMock(ServiceReference.class);
    // Create options
    options = new HashMap<>();
    options.put(JDBCUtils.DATASOURCE, "osgi:" + DataSource.class.getName());
    options.put(BundleContext.class.getName(), context);
    expect(context.getServiceReferences(DataSource.class.getName(), null)).andReturn(new ServiceReference[] { reference });
    expect((DataSource) context.getService(reference)).andReturn(dataSource);
    expect(context.ungetService(reference)).andReturn(true);
    EasyMock.replay(context);
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) EmbeddedDataSource40(org.apache.derby.jdbc.EmbeddedDataSource40) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) DataSource(javax.sql.DataSource) Before(org.junit.Before)

Aggregations

Connection (java.sql.Connection)2 Statement (java.sql.Statement)2 EmbeddedDataSource40 (org.apache.derby.jdbc.EmbeddedDataSource40)2 SQLException (java.sql.SQLException)1 DataSource (javax.sql.DataSource)1 Before (org.junit.Before)1 BundleContext (org.osgi.framework.BundleContext)1 ServiceReference (org.osgi.framework.ServiceReference)1