protected void closeConnectionSource() throws Exception { if (connectionSource != null) { connectionSource.close(); connectionSource = null; } }
@Test public void testStuff() throws Exception { File path = new File("/tmp/x"); JdbcConnectionSource connection = new JdbcConnectionSource("jdbc:h2:file:" + path.getAbsolutePath() + ".h2.db"); // TableUtils.createTable(connection, SomeClass.class); Dao<SomeClass, Integer> dao = DaoManager.createDao(connection, SomeClass.class); int id = 131233; SomeClass sc = new SomeClass(id, "fopewjfew"); dao.create(sc); SomeClass retrieved = dao.queryForId(id); System.out.println("" + retrieved); connection.close(); }
@Test(expected = IOException.class) public void testCloseBeforeInitialize() throws Exception { JdbcConnectionSource sds = new JdbcConnectionSource(); sds.close(); }
private void doMain(String[] args) throws Exception { JdbcConnectionSource connectionSource = null; try { // create our data-source for the database connectionSource = new JdbcConnectionSource(DATABASE_URL); // setup our database and DAOs setupDatabase(connectionSource); // read and write some data readWriteData(); System.out.println("\n\nIt seems to have worked\n\n"); } finally { // destroy the data source which should close underlying connections if (connectionSource != null) { connectionSource.close(); } } }
private void doMain(String[] args) throws Exception { JdbcConnectionSource connectionSource = null; try { // create our data source connectionSource = new JdbcConnectionSource(DATABASE_URL); // setup our database and DAOs setupDatabase(connectionSource); // read and write some data readWriteData(); System.out.println("\n\nIt seems to have worked\n\n"); } finally { // destroy the data source which should close underlying connections if (connectionSource != null) { connectionSource.close(); } } }
private void doMain(String[] args) throws Exception { JdbcConnectionSource connectionSource = null; try { // create our data-source for the database connectionSource = new JdbcConnectionSource(DATABASE_URL); // setup our database and DAOs setupDatabase(connectionSource); // read and write some data readWriteData(); System.out.println("\n\nIt seems to have worked\n\n"); } finally { // destroy the data source which should close underlying connections if (connectionSource != null) { connectionSource.close(); } } }
@Test(expected = SQLException.class) public void testReleaseConnectionBeforeInitialize() throws Exception { JdbcConnectionSource sds = new JdbcConnectionSource(); sds.releaseConnection(null); sds.close(); }
@Test(expected = SQLException.class) public void testInitNoUrl() throws Exception { JdbcConnectionSource cs = new JdbcConnectionSource(); cs.initialize(); cs.close(); }
@Test(expected = SQLException.class) public void testGetReadOnlyConnectionBeforeInitialize() throws Exception { JdbcConnectionSource sds = new JdbcConnectionSource(); sds.getReadOnlyConnection(null); sds.close(); }
@Test(expected = SQLException.class) public void testGetReadWriteConnectionBeforeInitialize() throws Exception { JdbcConnectionSource sds = new JdbcConnectionSource(); sds.getReadWriteConnection(null); sds.close(); }
@Test(expected = IllegalStateException.class) public void testGetDatabaseTypeBeforeInitialize() throws Exception { JdbcConnectionSource sds = new JdbcConnectionSource(); sds.getDatabaseType(); sds.close(); }
@Test public void testDoubleInit() throws Exception { JdbcConnectionSource sds = new JdbcConnectionSource("jdbc:h2:mem:baz"); sds.initialize(); sds.close(); }
@Test public void testSpringWiring() throws Exception { String url = "jdbc:h2:mem:baz"; JdbcConnectionSource sds = new JdbcConnectionSource(); sds.setUrl(url); sds.initialize(); sds.close(); }
@Test public void testSimpleDataSourceStringStringString() throws Exception { String username = "user"; String password = "_secret"; String url = "jdbc:h2:mem:ormlite-up;USER=" + username + ";PASSWORD=" + password; JdbcConnectionSource sds = new JdbcConnectionSource(url, username, password); assertNotNull(sds.getReadOnlyConnection(null)); sds.close(); }
@Test public void testSimpleDataSourceString() throws Exception { String url = "jdbc:h2:mem:baz"; JdbcConnectionSource sds = new JdbcConnectionSource(url); assertEquals(url, sds.getUrl()); sds.close(); }
@Test public void testGetConnectionUserPassSetters() throws Exception { String username = "user"; String password = "_secret"; String url = "jdbc:h2:mem:ormlite-up;USER=" + username + ";PASSWORD=" + password; JdbcConnectionSource sds = new JdbcConnectionSource(url); sds.setUsername(username); sds.setPassword(password); assertNotNull(sds.getReadOnlyConnection(null)); sds.close(); }
@Test public void testSimpleDataSource() throws Exception { JdbcConnectionSource sds = new JdbcConnectionSource(); String url = "jdbc:h2:mem:baz"; sds.setUrl(url); assertEquals(url, sds.getUrl()); sds.initialize(); sds.close(); }
@Test public void testSaveAndClear() throws Exception { JdbcConnectionSource sds = new JdbcConnectionSource("jdbc:h2:mem:baz"); DatabaseConnection conn1 = sds.getReadOnlyConnection(null); DatabaseConnection conn2 = sds.getReadOnlyConnection(null); assertSame(conn1, conn2); sds.saveSpecialConnection(conn1); sds.clearSpecialConnection(conn1); sds.releaseConnection(conn1); sds.close(); }
@Test public void testSetDatabaseType() throws Exception { JdbcConnectionSource sds = new JdbcConnectionSource(); sds.setUrl("jdbc:h2:mem:baz"); sds.setDatabaseType(new H2DatabaseType()); sds.initialize(); assertTrue(sds.getDatabaseType() instanceof H2DatabaseType); sds.close(); }
@Test public void testIsOpen() throws Exception { JdbcConnectionSource sds = new JdbcConnectionSource("jdbc:h2:mem:baz"); // no get connection yet assertFalse(sds.isOpen(null)); sds.releaseConnection(sds.getReadOnlyConnection(null)); assertTrue(sds.isOpen(null)); sds.close(); assertFalse(sds.isOpen(null)); } }