DatabaseAccessObject { public void dropColumn(Connection conn, String tableName, String columnName) { try (PreparedStatement pstmt = conn.prepareStatement("ALTER TABLE " + tableName + " DROP COLUMN " + columnName);){ pstmt.executeUpdate(); s_logger.debug("Column " + columnName + " is dropped successfully from the table " + tableName); } catch (SQLException e) { s_logger.warn("Unable to drop column " + columnName + " due to exception", e); } }  void dropKey(Connection conn, String tableName, String key, boolean isForeignKey); void dropPrimaryKey(Connection conn, String tableName); void dropColumn(Connection conn, String tableName, String columnName); boolean columnExists(Connection conn, String tableName, String columnName);  }
@Test public void testDropColumn() throws Exception { when(connectionMock.prepareStatement(anyString())).thenReturn(preparedStatementMock); Connection conn = connectionMock; String tableName = "tableName"; String columnName = "columnName"; dao.dropColumn(conn, tableName, columnName); verify(connectionMock, times(1)).prepareStatement(anyString()); verify(preparedStatementMock, times(0)).executeQuery(); verify(preparedStatementMock, times(1)).executeUpdate(); verify(preparedStatementMock, times(1)).close(); verify(loggerMock, times(1)).debug(anyString()); verify(loggerMock, times(0)).warn(anyString(), any(Throwable.class)); }
@Test(expected = NullPointerException.class) public void testDropColumnWhenConnectionIsNull() throws Exception { Connection conn = null; String tableName = "tableName"; String columnName = "columnName"; dao.dropColumn(conn, tableName, columnName); }
@Test public void testDropColumnWhenTableNameIsNull() throws Exception { SQLException sqlException = new SQLException(); when(connectionMock.prepareStatement(contains("ALTER TABLE null"))).thenReturn(preparedStatementMock); when(preparedStatementMock.executeUpdate()).thenThrow(sqlException); Connection conn = connectionMock; String tableName = null; String columnName = "columnName"; dao.dropColumn(conn, tableName, columnName); verify(connectionMock, times(1)).prepareStatement(anyString()); verify(preparedStatementMock, times(1)).executeUpdate(); verify(preparedStatementMock, times(1)).close(); verify(loggerMock, times(0)).debug(anyString()); verify(loggerMock, times(1)).warn(anyString(), eq(sqlException)); }
@Test public void testDropColumnWhenColumnNameIsNull() throws Exception { SQLException sqlException = new SQLException(); when(connectionMock.prepareStatement(contains("DROP COLUMN null"))).thenReturn(preparedStatementMock); when(preparedStatementMock.executeUpdate()).thenThrow(sqlException); Connection conn = connectionMock; String tableName = "tableName"; String columnName = null; dao.dropColumn(conn, tableName, columnName); verify(connectionMock, times(1)).prepareStatement(anyString()); verify(preparedStatementMock, times(1)).executeUpdate(); verify(preparedStatementMock, times(1)).close(); verify(loggerMock, times(0)).debug(anyString()); verify(loggerMock, times(1)).warn(anyString(), eq(sqlException)); }
@Test public void testDropColumnWhenPrepareStatementResultsInException() throws Exception { SQLException sqlException = new SQLException(); when(connectionMock.prepareStatement(anyString())).thenThrow(sqlException); Connection conn = connectionMock; String tableName = "tableName"; String columnName = "columnName"; dao.dropColumn(conn, tableName, columnName); verify(connectionMock, times(1)).prepareStatement(anyString()); verify(preparedStatementMock, times(0)).executeUpdate(); verify(preparedStatementMock, times(0)).close(); verify(loggerMock, times(0)).debug(anyString()); verify(loggerMock, times(1)).warn(anyString(), eq(sqlException)); }
@Test public void testDropColumnWhenexecuteUpdateResultsInException() throws Exception { SQLException sqlException = new SQLException(); when(connectionMock.prepareStatement(anyString())).thenReturn(preparedStatementMock); when(preparedStatementMock.executeUpdate()).thenThrow(sqlException); Connection conn = connectionMock; String tableName = "tableName"; String columnName = "columnName"; dao.dropColumn(conn, tableName, columnName); verify(connectionMock, times(1)).prepareStatement(anyString()); verify(preparedStatementMock, times(1)).executeUpdate(); verify(preparedStatementMock, times(1)).close(); verify(loggerMock, times(0)).debug(anyString()); verify(loggerMock, times(1)).warn(anyString(), eq(sqlException)); }