@Test public void testDscsDoubleInit() throws Exception { DataSource dataSource = createMock(DataSource.class); expect(dataSource.getConnection()).andReturn(null); expect(dataSource.getConnection()).andReturn(null); replay(dataSource); DataSourceConnectionSource dcs = new DataSourceConnectionSource(dataSource, connectionSource.getDatabaseType()); dcs.initialize(); assertEquals(connectionSource.getDatabaseType(), dcs.getDatabaseType()); dcs.releaseConnection(createMock(DatabaseConnection.class)); dcs.close(); verify(dataSource); }
@Test(expected = IllegalStateException.class) public void testGetDatabaseTypeBeforeInitialize() throws Exception { JdbcConnectionSource sds = new JdbcConnectionSource(); sds.getDatabaseType(); sds.close(); }
private void doOpenConnectionSource() throws Exception { if (connectionSource == null) { isConnectionExpected = isConnectionExpected(); if (isConnectionExpected) { connectionSource = new JdbcConnectionSource(databaseUrl, userName, password); } } if (databaseType == null) { if (connectionSource != null) { databaseType = connectionSource.getDatabaseType(); } } else { if (connectionSource != null) { connectionSource.setDatabaseType(databaseType); } } }
@Test public void testStringWidthTooLong() throws Exception { if (connectionSource == null) { return; } if (!connectionSource.getDatabaseType().isVarcharFieldWidthSupported()) { return; } Dao<StringWidth, Object> stringWidthDao = createDao(StringWidth.class, true); StringWidth stringWidth = new StringWidth(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < ALL_TYPES_STRING_WIDTH + 1; i++) { sb.append('c'); } String string = sb.toString(); assertTrue(string.length() > ALL_TYPES_STRING_WIDTH); stringWidth.stringField = string; try { stringWidthDao.create(stringWidth); fail("expected exception"); } catch (SQLException e) { // expected } }
@Test public void testEscapeCharInField() throws Exception { if (connectionSource == null) { return; } StringBuilder sb = new StringBuilder(); String word = "foo"; connectionSource.getDatabaseType().appendEscapedWord(sb, word); String escaped = sb.toString(); int index = escaped.indexOf(word); String escapeString = escaped.substring(0, index); Dao<Basic, String> fooDao = createDao(Basic.class, true); Basic foo1 = new Basic(); String id = word + escapeString + word; foo1.id = id; assertEquals(1, fooDao.create(foo1)); Basic foo2 = fooDao.queryForId(id); assertNotNull(foo2); assertEquals(id, foo2.id); }
@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(); }