@Override public ResultSetMetaData getMetaData() throws SQLException { if (closed) { throw new SQLException("resultSet closed"); } return (MockResultSetMetaData) metaData; }
@Override public boolean isBeforeFirst() throws SQLException { if (closed) { throw new SQLException(); } return false; }
@Override @SuppressWarnings("unchecked") public <T> T unwrap(Class<T> iface) throws SQLException { if (iface.isInstance(this)) { return (T) this; } throw new SQLException("DataSource of type [" + getClass().getName() + "] cannot be unwrapped as [" + iface.getName() + "]"); }
public String decode(ConnectionProxy connection, String s) throws SQLException { try { CharsetConvert charsetConvert = (CharsetConvert) connection.getAttribute(ATTR_CHARSET_CONVERTER); return charsetConvert.decode(s); } catch (UnsupportedEncodingException e) { throw new SQLException(e.getMessage(), e); } }
/** * Specifying a custom username and password doesn't make sense * with a single Connection. Returns the single Connection if given * the same username and password; throws a SQLException else. */ @Override public Connection getConnection(String username, String password) throws SQLException { if (ObjectUtils.nullSafeEquals(username, getUsername()) && ObjectUtils.nullSafeEquals(password, getPassword())) { return getConnection(); } else { throw new SQLException("SingleConnectionDataSource does not support custom username and password"); } }
/** * PostgreSQL can return null. * SAP DB can apparently return empty SQL code. * Bug 729170 */ @Test public void malformedSqlStateCodes() { SQLException sex = new SQLException("Message", null, 1); testMalformedSqlStateCode(sex); sex = new SQLException("Message", "", 1); testMalformedSqlStateCode(sex); // One char's not allowed sex = new SQLException("Message", "I", 1); testMalformedSqlStateCode(sex); }
private void checkTranslation(SQLExceptionTranslator sext, int errorCode, Class<?> exClass) { SQLException sex = new SQLException("", "", errorCode); DataAccessException ex = sext.translate("", "", sex); assertTrue(exClass.isInstance(ex)); assertTrue(ex.getCause() == sex); }
@Test public void badSqlGrammar() { SQLException sex = new SQLException("Message", "42001", 1); try { throw this.trans.translate("task", sql, sex); } catch (BadSqlGrammarException ex) { // OK assertTrue("SQL is correct", sql.equals(ex.getSql())); assertTrue("Exception matches", sex.equals(ex.getSQLException())); } }
@Test public void invalidSqlStateCode() { SQLException sex = new SQLException("Message", "NO SUCH CODE", 1); try { throw this.trans.translate("task", sql, sex); } catch (UncategorizedSQLException ex) { // OK assertTrue("SQL is correct", sql.equals(ex.getSql())); assertTrue("Exception matches", sex.equals(ex.getSQLException())); } }
@Test public void testCouldNotClose() throws Exception { SQLException sqlException = new SQLException("bar"); given(this.connection.createStatement()).willReturn(this.statement); given(this.resultSet.next()).willReturn(false); willThrow(sqlException).given(this.resultSet).close(); willThrow(sqlException).given(this.statement).close(); willThrow(sqlException).given(this.connection).close(); RowCountCallbackHandler rcch = new RowCountCallbackHandler(); this.template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch); verify(this.connection).close(); }
@Test public void testCouldNotGetConnectionForOperationOrExceptionTranslator() throws SQLException { SQLException sqlException = new SQLException("foo", "07xxx"); this.dataSource = mock(DataSource.class); given(this.dataSource.getConnection()).willThrow(sqlException); JdbcTemplate template = new JdbcTemplate(this.dataSource, false); RowCountCallbackHandler rcch = new RowCountCallbackHandler(); this.thrown.expect(CannotGetJdbcConnectionException.class); this.thrown.expect(exceptionCause(sameInstance(sqlException))); template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch); }
@Test public void isCheckedException() { assertTrue(ObjectUtils.isCheckedException(new Exception())); assertTrue(ObjectUtils.isCheckedException(new SQLException())); assertFalse(ObjectUtils.isCheckedException(new RuntimeException())); assertFalse(ObjectUtils.isCheckedException(new IllegalArgumentException(""))); // Any Throwable other than RuntimeException and Error // has to be considered checked according to the JLS. assertTrue(ObjectUtils.isCheckedException(new Throwable())); }
@Test public void testSqlUpdateEncountersSqlException() throws Exception { SQLException sqlException = new SQLException("bad update"); final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = 4"; given(this.statement.executeUpdate(sql)).willThrow(sqlException); given(this.connection.createStatement()).willReturn(this.statement); this.thrown.expect(exceptionCause(sameInstance(sqlException))); try { this.template.update(sql); } finally { verify(this.statement).close(); verify(this.connection, atLeastOnce()).close(); } }
@Test public void testGetFromDataSourceWithSQLException() throws Exception { SQLException expectedSQLException = new SQLException(); DataSource dataSource = mock(DataSource.class); given(dataSource.getConnection()).willThrow(expectedSQLException); SQLErrorCodes sec = SQLErrorCodesFactory.getInstance().getErrorCodes(dataSource); assertIsEmpty(sec); }
@Test public void testCouldNotGetConnectionForOperationWithLazyExceptionTranslator() throws SQLException { SQLException sqlException = new SQLException("foo", "07xxx"); this.dataSource = mock(DataSource.class); given(this.dataSource.getConnection()).willThrow(sqlException); this.template = new JdbcTemplate(); this.template.setDataSource(this.dataSource); this.template.afterPropertiesSet(); RowCountCallbackHandler rcch = new RowCountCallbackHandler(); this.thrown.expect(CannotGetJdbcConnectionException.class); this.thrown.expect(exceptionCause(sameInstance(sqlException))); this.template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch); }
private void doTest(String sqlState, Class<?> dataAccessExceptionType) { SQLException ex = new SQLException(REASON, sqlState); SQLExceptionTranslator translator = new SQLStateSQLExceptionTranslator(); DataAccessException dax = translator.translate(TASK, SQL, ex); assertNotNull("Translation must *never* result in a null DataAccessException being returned.", dax); assertEquals("Wrong DataAccessException type returned as the result of the translation", dataAccessExceptionType, dax.getClass()); assertNotNull("The original SQLException must be preserved in the translated DataAccessException", dax.getCause()); assertSame("The exact same original SQLException must be preserved in the translated DataAccessException", ex, dax.getCause()); }
@Test public void dataTruncationTranslation() { SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES); SQLException dataAccessEx = new SQLException("", "", 5); DataTruncation dataTruncation = new DataTruncation(1, true, true, 1, 1, dataAccessEx); DataAccessResourceFailureException daex = (DataAccessResourceFailureException) sext.translate("task", "SQL", dataTruncation); assertEquals(dataTruncation, daex.getCause()); }
@Test public void testStoredProcedureExceptionTranslator() throws Exception { SQLException sqlException = new SQLException( "Syntax error or access violation exception", "42000"); given(callableStatement.execute()).willThrow(sqlException); given(connection.prepareCall("{call " + StoredProcedureExceptionTranslator.SQL + "()}") ).willReturn(callableStatement); StoredProcedureExceptionTranslator sproc = new StoredProcedureExceptionTranslator(dataSource); thrown.expect(CustomDataException.class); sproc.execute(); }
@Test public void testNoSuchStoredProcedure() throws Exception { SQLException sqlException = new SQLException( "Syntax error or access violation exception", "42000"); given(callableStatement.execute()).willThrow(sqlException); given(connection.prepareCall("{call " + NoSuchStoredProcedure.SQL + "()}")).willReturn( callableStatement); NoSuchStoredProcedure sproc = new NoSuchStoredProcedure(dataSource); thrown.expect(BadSqlGrammarException.class); sproc.execute(); }
@Test public void batchExceptionTranslation() { SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES); SQLException badSqlEx = new SQLException("", "", 1); BatchUpdateException batchUpdateEx = new BatchUpdateException(); batchUpdateEx.setNextException(badSqlEx); BadSqlGrammarException bsgex = (BadSqlGrammarException) sext.translate("task", "SQL", batchUpdateEx); assertEquals("SQL", bsgex.getSql()); assertEquals(badSqlEx, bsgex.getSQLException()); }