Java Code Examples for org.hsqldb.jdbc.JDBCDataSource

The following examples show how to use org.hsqldb.jdbc.JDBCDataSource. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: sis   Source File: TestDatabase.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates a in-memory database on HSQLDB. The database can optionally use a connection pool.
 * The test method can set {@code pooled} to {@code true} if it needs the data to survive when
 * the connection is closed and re-opened.
 *
 * @param  name    the database name (without {@code "jdbc:hsqldb:mem:"} prefix).
 * @param  pooled  whether the database should use a connection pool.
 * @return connection to the test database.
 * @throws SQLException if an error occurred while creating the database.
 *
 * @see <a href="http://hsqldb.org/doc/apidocs/org/hsqldb/jdbc/JDBCDataSource.html">JDBC data source for HSQL</a>
 *
 * @since 1.0
 */
public static TestDatabase createOnHSQLDB(final String name, final boolean pooled) throws SQLException {
    final DataSource ds;
    final JDBCPool pool;
    final String url = "jdbc:hsqldb:mem:".concat(name);
    if (pooled) {
        pool = new JDBCPool();
        pool.setURL(url);
        ds = pool;
    } else {
        final JDBCDataSource simple = new JDBCDataSource();
        simple.setURL(url);
        ds = simple;
        pool = null;
    }
    return new TestDatabase(ds) {
        @Override public void close() throws SQLException {
            try (Connection c = ds.getConnection(); Statement s = c.createStatement()) {
                s.execute("SHUTDOWN");
            }
            if (pool != null) {
                pool.close(2);
            }
        }
    };
}
 
Example 2
Source Project: piranha   Source File: JPATest.java    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Test /index.html.
 *
 * @throws Exception
 */
@Test
public void testIndexHtml() throws Exception {
    System.getProperties().put("java.naming.factory.initial", 
            "cloud.piranha.jndi.memory.DefaultInitialContextFactory");
    InitialContext initialContext = new InitialContext();
    JDBCDataSource ds = new JDBCDataSource();
    ds.setUrl("jdbc:hsqldb:mem:demo");
    ds.setUser("sa");
    initialContext.bind("jdbc/demo", ds);
    EmbeddedPiranha piranha = new EmbeddedPiranhaBuilder()
            .directoryResource("src/main/webapp")
            .aliasedDirectoryResource("target/classes", "/WEB-INF/classes")
            .initializer(WeldInitializer.class.getName())
            .initializer(MojarraInitializer.class.getName())
            .build()
            .start();
    EmbeddedRequest request = new EmbeddedRequestBuilder()
            .contextPath("")
            .servletPath("/index.html")
            .build();
    EmbeddedResponse response = new EmbeddedResponse();
    piranha.service(request, response);
    assertEquals(response.getStatus(), 200);
    assertTrue(response.getResponseAsString().contains("Count: 0"));
    piranha.stop()
            .destroy();
}
 
Example 3
Source Project: micrometer   Source File: DatabaseTableMetricsTest.java    License: Apache License 2.0 5 votes vote down vote up
@BeforeEach
void setup() throws SQLException {
    registry = new SimpleMeterRegistry();

    ds = new JDBCDataSource();
    ds.setURL("jdbc:hsqldb:mem:test");

    try (Connection conn = ds.getConnection()) {
        conn.prepareStatement("CREATE TABLE foo (id int)").execute();
        conn.prepareStatement("INSERT INTO foo VALUES (1)").executeUpdate();
    }
}
 
Example 4
protected ProxyDataSource newDataSource() {
    JDBCDataSource actualDataSource = new JDBCDataSource();
    actualDataSource.setUrl("jdbc:hsqldb:mem:test");
    actualDataSource.setUser("sa");
    actualDataSource.setPassword("");
    ProxyDataSource proxyDataSource = new ProxyDataSource();
    proxyDataSource.setDataSource(actualDataSource);
    proxyDataSource.setListener(new SLF4JQueryLoggingListener());
    return proxyDataSource;
}
 
Example 5
protected ProxyDataSource newDataSource() {
    JDBCDataSource actualDataSource = new JDBCDataSource();
    actualDataSource.setUrl("jdbc:hsqldb:mem:test");
    actualDataSource.setUser("sa");
    actualDataSource.setPassword("");
    Properties properties = new Properties();
    properties.setProperty("hsqldb.tx_level", "SERIALIZABLE");
    actualDataSource.setProperties(properties);
    ProxyDataSource proxyDataSource = new ProxyDataSource();
    proxyDataSource.setDataSource(actualDataSource);
    proxyDataSource.setListener(new SLF4JQueryLoggingListener());
    return proxyDataSource;
}
 
Example 6
Source Project: hibernate-master-class   Source File: AbstractJPATest.java    License: Apache License 2.0 5 votes vote down vote up
private ProxyDataSource newDataSource() {
    JDBCDataSource actualDataSource = new JDBCDataSource();
    actualDataSource.setUrl("jdbc:hsqldb:mem:test");
    actualDataSource.setUser("sa");
    actualDataSource.setPassword("");
    ProxyDataSource proxyDataSource = new ProxyDataSource();
    proxyDataSource.setDataSource(actualDataSource);
    proxyDataSource.setListener(new SLF4JQueryLoggingListener());
    return proxyDataSource;
}
 
Example 7
Source Project: jqm   Source File: JqmAsyncTester.java    License: Apache License 2.0 5 votes vote down vote up
public JqmAsyncTester()
{
    // Ext dir
    File extDir = new File("./ext");
    if (!extDir.exists() && !extDir.mkdir())
    {
        throw new RuntimeException(new IOException("./ext directory does not exist and cannot create it"));
    }

    s = Common.createHsqlServer();
    s.start();

    JDBCDataSource ds = new JDBCDataSource();
    ds.setDatabase("jdbc:hsqldb:mem:" + s.getDatabaseName(0, true));
    db = new Db(ds, true);
    cnx = db.getConn();

    Properties p2 = Common.dbProperties(s);
    p2.put("com.enioka.jqm.jdbc.contextobject", db);
    JqmClientFactory.setProperties(p2);
    JqmEngineFactory.setDatasource(db);
    JqmEngineFactory.initializeMetadata();
    cnx.runUpdate("dp_delete_all");
    cnx.runUpdate("q_delete_all");
    cnx.commit();

    // Needed parameters
    addGlobalParameter("defaultConnection", "");
    addGlobalParameter("disableWsApi", "true");
    addGlobalParameter("logFilePerLaunch", "false");

    // Prepare DB

}
 
Example 8
Source Project: tomee   Source File: OpenEjbBrokerFactoryTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testDirectDataSource() throws Exception {

        final Properties properties = new Properties();

        final JDBCDataSource dataSource = new JDBCDataSource();
        dataSource.setDatabase("jdbc:hsqldb:mem:testdb" + System.currentTimeMillis());
        dataSource.setUser("sa");
        dataSource.setPassword("");
        dataSource.getConnection().close();

        properties.put("DataSource", dataSource);
        properties.put("UseDatabaseLock", "false");
        properties.put("StartupTimeout", "10000");

        ActiveMQFactory.setThreadProperties(properties);
        BrokerService broker = null;
        try {
            broker = BrokerFactory.createBroker(new URI(getBrokerUri(
                "broker:(tcp://localhost:" + brokerPort + ")?useJmx=false")));
            assertNotNull("broker is null", broker);

            final PersistenceAdapter persistenceAdapter = broker.getPersistenceAdapter();
            assertNotNull("persistenceAdapter is null", persistenceAdapter);

            assertTrue("persistenceAdapter should be an instance of JDBCPersistenceAdapter",
                persistenceAdapter instanceof JDBCPersistenceAdapter);
            final JDBCPersistenceAdapter jdbcPersistenceAdapter = (JDBCPersistenceAdapter) persistenceAdapter;

            assertSame(dataSource, jdbcPersistenceAdapter.getDataSource());
        } finally {
            stopBroker(broker);
            ActiveMQFactory.setThreadProperties(null);
        }
    }
 
Example 9
Source Project: tomee   Source File: DataSourceDefinitionPlaceHolderTest.java    License: Apache License 2.0 5 votes vote down vote up
private void check(final DataSource ds) throws NoSuchFieldException, IllegalAccessException {
    assertNotNull(ds);
    assertThat(ds, instanceOf(BasicDataSource.class));

    final BasicDataSource bds = (BasicDataSource) ds;
    assertEquals("sa", bds.getUsername());
    assertEquals("", bds.getPassword());

    final Field fieldDs = bds.getClass().getDeclaredField("ds");
    fieldDs.setAccessible(true);
    final JDBCDataSource realDs = (JDBCDataSource) fieldDs.get(bds);
    assertEquals("jdbc:hsqldb:mem:superDS", realDs.getUrl());
    assertEquals("sa", realDs.getUser());
}
 
Example 10
@Override
public Class<? extends DataSource> dataSourceClassName() {
    return JDBCDataSource.class;
}
 
Example 11
Source Project: hibernate-types   Source File: HSQLDBDataSourceProvider.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Class<? extends DataSource> dataSourceClassName() {
    return JDBCDataSource.class;
}
 
Example 12
Source Project: hibernate-types   Source File: HSQLDBDataSourceProvider.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Class<? extends DataSource> dataSourceClassName() {
    return JDBCDataSource.class;
}
 
Example 13
Source Project: hibernate-types   Source File: HSQLDBDataSourceProvider.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Class<? extends DataSource> dataSourceClassName() {
    return JDBCDataSource.class;
}
 
Example 14
Source Project: hibernate-types   Source File: HSQLDBDataSourceProvider.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Class<? extends DataSource> dataSourceClassName() {
    return JDBCDataSource.class;
}
 
Example 15
@Override
public Class<? extends DataSource> dataSourceClassName() {
    return JDBCDataSource.class;
}
 
Example 16
Source Project: hibernate-master-class   Source File: AbstractTest.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Class<? extends DataSource> dataSourceClassName() {
    return JDBCDataSource.class;
}
 
Example 17
Source Project: jqm   Source File: JqmTester.java    License: Apache License 2.0 4 votes vote down vote up
private JqmTester(String className)
{
    s = Common.createHsqlServer();
    s.start();

    JDBCDataSource ds = new JDBCDataSource();
    ds.setDatabase("jdbc:hsqldb:mem:" + s.getDatabaseName(0, true));
    db = new Db(ds, true);
    cnx = db.getConn();

    JqmSingleRunner.setConnection(db);

    Properties p2 = new Properties();
    p2.put("com.enioka.jqm.jdbc.contextobject", db);
    JqmClientFactory.setProperties(p2);

    // Needed parameters
    GlobalParameter.setParameter(cnx, "defaultConnection", "");
    cnx.commit();

    // Ext dir
    File extDir = new File("./ext");
    if (!extDir.exists() && !extDir.mkdir())
    {
        throw new RuntimeException(new IOException("./ext directory does not exist and cannot create it"));
    }

    // Create node
    resDirectoryPath = Common.createTempDirectory();
    node = Node.create(cnx, "testtempnode", 12, resDirectoryPath.getAbsolutePath(), resDirectoryPath.getAbsolutePath(),
            resDirectoryPath.getAbsolutePath(), "test", "INFO");

    q = Queue.create(cnx, "default", "default test queue", true); // Only useful because JobDef.queue is non-null

    jd = JobDef.create(cnx, "test application", className, null, "/dev/null", q, 0, "TestApplication", null, null, null, null, null,
            false, null, PathType.MEMORY);

    ji = JobInstance.enqueue(cnx, State.SUBMITTED, q, jd, null, null, null, null, null, null, null, null, null, false, false, null, 0,
            Instruction.RUN, null);
    cnx.runUpdate("ji_update_status_by_id", node.getId(), ji);

    cnx.commit();
}
 
Example 18
Source Project: tomee   Source File: OpenEjbBrokerFactoryTest.java    License: Apache License 2.0 4 votes vote down vote up
public void testLookupDataSource() throws Exception {

        final Properties properties = new Properties();

        final JDBCDataSource dataSource = new JDBCDataSource();
        dataSource.setDatabase("jdbc:hsqldb:mem:testdb" + System.currentTimeMillis());
        dataSource.setUser("sa");
        dataSource.setPassword("");
        dataSource.getConnection().close();

        MockInitialContextFactory.install(Collections.singletonMap("openejb/Resource/TestDs", dataSource));
        assertSame(dataSource, new InitialContext().lookup("openejb/Resource/TestDs"));

        final CoreContainerSystem containerSystem = new CoreContainerSystem(new IvmJndiFactory());
        containerSystem.getJNDIContext().bind("openejb/Resource/TestDs", dataSource);
        SystemInstance.get().setComponent(ContainerSystem.class, containerSystem);

        properties.put("DataSource", "TestDs");
        properties.put("UseDatabaseLock", "false");
        properties.put("StartupTimeout", "10000");

        ActiveMQFactory.setThreadProperties(properties);
        BrokerService broker = null;
        try {
            broker = BrokerFactory.createBroker(new URI(getBrokerUri(
                "broker:(tcp://localhost:" + brokerPort + ")?useJmx=false")));
            assertNotNull("broker is null", broker);

            final PersistenceAdapter persistenceAdapter = broker.getPersistenceAdapter();
            assertNotNull("persistenceAdapter is null", persistenceAdapter);

            assertTrue("persistenceAdapter should be an instance of JDBCPersistenceAdapter",
                persistenceAdapter instanceof JDBCPersistenceAdapter);
            final JDBCPersistenceAdapter jdbcPersistenceAdapter = (JDBCPersistenceAdapter) persistenceAdapter;

            assertSame(dataSource, jdbcPersistenceAdapter.getDataSource());
        } finally {
            stopBroker(broker);
            ActiveMQFactory.setThreadProperties(null);
        }
    }