@Test public void testTestConnection() throws Exception { Connection c = mock(Connection.class); Statement s = mock(Statement.class); ResultSet rs = mock(ResultSet.class); ResultSetMetaData rsm = mock(ResultSetMetaData.class); when(c.createStatement()).thenReturn(s); when(s.executeQuery(anyString())).thenReturn(rs); when(rs.next()).thenReturn(false, true); when(rs.getObject(anyInt())).thenReturn("columnData"); when(rs.getMetaData()).thenReturn(rsm); when(rsm.getColumnName(anyInt())).thenReturn("columnName"); when(rsm.getColumnCount()).thenReturn(1); testConnection(c, null, false); testConnection(c, "hello", true); testConnection(c, "hello", true); testConnection(c, "hello", false); }
public static Connection testConnection(Connection sqlConnection, String testStatement, boolean debugMode) throws SQLException { Statement stmt = sqlConnection.createStatement(); ResultSet rs = null; try { if (isEmpty(testStatement)) { return sqlConnection; } if (debugMode) { rs = stmt.executeQuery(testStatement); if (rs.next()) { StringBuffer sb = new StringBuffer("TestStatement Results - "); ResultSetMetaData rsm = rs.getMetaData(); for (int i = 1; i <= rsm.getColumnCount(); i++) { sb.append("["); sb.append(rsm.getColumnName(i)); sb.append("="); sb.append(rs.getObject(i)); sb.append("] "); } log.trace(sb.toString()); } } else { stmt.execute(testStatement); } } finally { JdbcUtil.closeQuietly(rs); JdbcUtil.closeQuietly(stmt); } return sqlConnection; }
JdbcUtil { public static Connection testConnection(Connection sqlConnection, String testStatement, boolean debugMode) throws SQLException { Statement stmt = sqlConnection.createStatement(); ResultSet rs = null; try { if (isEmpty(testStatement)) { return sqlConnection; } if (debugMode) { rs = stmt.executeQuery(testStatement); if (rs.next()) { StringBuffer sb = new StringBuffer("TestStatement Results - "); ResultSetMetaData rsm = rs.getMetaData(); for (int i = 1; i <= rsm.getColumnCount(); i++) { sb.append("["); sb.append(rsm.getColumnName(i)); sb.append("="); sb.append(rs.getObject(i)); sb.append("] "); } log.trace(sb.toString()); } } else { stmt.execute(testStatement); } } finally { JdbcUtil.closeQuietly(rs); JdbcUtil.closeQuietly(stmt); } return sqlConnection; } }
JdbcUtil { public static Connection testConnection(Connection sqlConnection, String testStatement, boolean debugMode) throws SQLException { Statement stmt = sqlConnection.createStatement(); ResultSet rs = null; try { if (isEmpty(testStatement)) { return sqlConnection; } if (debugMode) { rs = stmt.executeQuery(testStatement); if (rs.next()) { StringBuffer sb = new StringBuffer("TestStatement Results - "); ResultSetMetaData rsm = rs.getMetaData(); for (int i = 1; i <= rsm.getColumnCount(); i++) { sb.append("["); sb.append(rsm.getColumnName(i)); sb.append("="); sb.append(rs.getObject(i)); sb.append("] "); } log.trace(sb.toString()); } } else { stmt.execute(testStatement); } } finally { JdbcUtil.closeQuietly(rs); JdbcUtil.closeQuietly(stmt); } return sqlConnection; }  }
JdbcUtil { public static Connection testConnection(Connection sqlConnection, String testStatement, boolean debugMode) throws SQLException { Statement stmt = sqlConnection.createStatement(); ResultSet rs = null; try { if (isEmpty(testStatement)) { return sqlConnection; } if (debugMode) { rs = stmt.executeQuery(testStatement); if (rs.next()) { StringBuffer sb = new StringBuffer("TestStatement Results - "); ResultSetMetaData rsm = rs.getMetaData(); for (int i = 1; i <= rsm.getColumnCount(); i++) { sb.append("["); sb.append(rsm.getColumnName(i)); sb.append("="); sb.append(rs.getObject(i)); sb.append("] "); } log.trace(sb.toString()); } } else { stmt.execute(testStatement); } } finally { JdbcUtil.closeQuietly(rs); JdbcUtil.closeQuietly(stmt); } return sqlConnection; }  static void closeQuietly(AutoCloseable... closeables); static void rollback(Savepoint svp, Connection sqlConnection); static void rollback(Connection sqlConnection); static void commit(Connection sqlConnection); static Savepoint createSavepoint(Connection sqlConnection); static Connection testConnection(Connection sqlConnection, String testStatement, boolean debugMode); static Properties mergeConnectionProperties(Properties p, String username, String password); static Connection getConnection(AdaptrisMessage msg, AdaptrisConnection adpCon); static void rollback(Connection sqlConnection, AdaptrisMessage msg); static void commit(Connection sqlConnection, AdaptrisMessage msg); }
JdbcUtil { public static Connection testConnection(Connection sqlConnection, String testStatement, boolean debugMode) throws SQLException { Statement stmt = sqlConnection.createStatement(); ResultSet rs = null; try { if (isEmpty(testStatement)) { return sqlConnection; } if (debugMode) { rs = stmt.executeQuery(testStatement); if (rs.next()) { StringBuffer sb = new StringBuffer("TestStatement Results - "); ResultSetMetaData rsm = rs.getMetaData(); for (int i = 1; i <= rsm.getColumnCount(); i++) { sb.append("["); sb.append(rsm.getColumnName(i)); sb.append("="); sb.append(rs.getObject(i)); sb.append("] "); } log.trace(sb.toString()); } } else { stmt.execute(testStatement); } } finally { JdbcUtil.closeQuietly(rs); JdbcUtil.closeQuietly(stmt); } return sqlConnection; }  static void closeQuietly(AutoCloseable... closeables); static void rollback(Savepoint svp, Connection sqlConnection); static void rollback(Connection sqlConnection); static void commit(Connection sqlConnection); static Savepoint createSavepoint(Connection sqlConnection); static Connection testConnection(Connection sqlConnection, String testStatement, boolean debugMode); static Properties mergeConnectionProperties(Properties p, String username, String password); static Connection getConnection(AdaptrisMessage msg, AdaptrisConnection adpCon); static void rollback(Connection sqlConnection, AdaptrisMessage msg); static void commit(Connection sqlConnection, AdaptrisMessage msg);  }