DatabaseAccessObject { public void dropKey(Connection conn, String tableName, String key, boolean isForeignKey) { String alter_sql_str; if (isForeignKey) { alter_sql_str = "ALTER TABLE " + tableName + " DROP FOREIGN KEY " + key; } else { alter_sql_str = "ALTER TABLE " + tableName + " DROP KEY " + key; } try(PreparedStatement pstmt = conn.prepareStatement(alter_sql_str);) { pstmt.executeUpdate(); s_logger.debug("Key " + key + " is dropped successfully from the table " + tableName); } catch (SQLException e) { s_logger.debug("Ignored SQL Exception when trying to drop " + (isForeignKey ? "foreign " : "") + "key " + key + " on table " + tableName + " exception: " + e.getMessage()); } }  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 testDropKey() throws Exception { when(connectionMock.prepareStatement(contains("DROP KEY"))).thenReturn(preparedStatementMock); Connection conn = connectionMock; String tableName = "tableName"; String key = "key"; boolean isForeignKey = false; dao.dropKey(conn, tableName, key, isForeignKey); verify(connectionMock, times(1)).prepareStatement(anyString()); verify(preparedStatementMock, times(1)).executeUpdate(); verify(preparedStatementMock, times(1)).close(); verify(loggerMock, times(1)).debug(contains("successfully")); }
@Test(expected = NullPointerException.class) public void testDropKeyWhenConnectionIsNull() throws Exception { Connection conn = null; String tableName = "tableName"; String key = "key"; boolean isForeignKey = false; dao.dropKey(conn, tableName, key, isForeignKey); }
@Test public void testDropKeyWhenTableNameIsNull() throws Exception { SQLException sqlException = new SQLException(); when(connectionMock.prepareStatement(contains("null DROP KEY"))).thenReturn(preparedStatementMock); when(preparedStatementMock.executeUpdate()).thenThrow(sqlException); Connection conn = connectionMock; String tableName = null; String key = "key"; boolean isForeignKey = false; dao.dropKey(conn, tableName, key, isForeignKey); verify(connectionMock, times(1)).prepareStatement(anyString()); verify(preparedStatementMock, times(1)).executeUpdate(); verify(preparedStatementMock, times(1)).close(); verify(loggerMock, times(1)).debug(contains("Exception")); }
@Test public void testDropKeyWhenKeyIsNull() throws Exception { SQLException sqlException = new SQLException(); when(connectionMock.prepareStatement(contains("DROP KEY null"))).thenReturn(preparedStatementMock); when(preparedStatementMock.executeUpdate()).thenThrow(sqlException); Connection conn = connectionMock; String tableName = "tableName"; String key = null; boolean isForeignKey = false; dao.dropKey(conn, tableName, key, isForeignKey); verify(connectionMock, times(1)).prepareStatement(anyString()); verify(preparedStatementMock, times(1)).executeUpdate(); verify(preparedStatementMock, times(1)).close(); verify(loggerMock, times(1)).debug(contains("Exception")); }
@Test public void testDropKeyWhenKeysAreForeignKeys() throws Exception { when(connectionMock.prepareStatement(contains("DROP FOREIGN KEY"))).thenReturn(preparedStatementMock); Connection conn = connectionMock; String tableName = "tableName"; String key = "key"; boolean isForeignKey = true; dao.dropKey(conn, tableName, key, isForeignKey); verify(connectionMock, times(1)).prepareStatement(anyString()); verify(preparedStatementMock, times(1)).executeUpdate(); verify(preparedStatementMock, times(1)).close(); verify(loggerMock, times(1)).debug(contains("successfully")); }
@Test public void testDropKeyWhenPrepareStatementResultsInException() throws Exception { SQLException sqlException = new SQLException(); when(connectionMock.prepareStatement(any(String.class))).thenThrow(sqlException); Connection conn = connectionMock; String tableName = "tableName"; String key = "key"; boolean isForeignKey = false; dao.dropKey(conn, tableName, key, isForeignKey); verify(connectionMock, times(1)).prepareStatement(anyString()); verify(preparedStatementMock, times(0)).executeUpdate(); verify(preparedStatementMock, times(0)).close(); verify(loggerMock, times(1)).debug(contains("Exception")); }
@Test public void testDropKeyWhenExecuteUpdateResultsInException() throws Exception { SQLException sqlException = new SQLException(); when(connectionMock.prepareStatement(contains("DROP KEY"))).thenReturn(preparedStatementMock); when(preparedStatementMock.executeUpdate()).thenThrow(sqlException); Connection conn = connectionMock; String tableName = "tableName"; String key = "key"; boolean isForeignKey = false; dao.dropKey(conn, tableName, key, isForeignKey); verify(connectionMock, times(1)).prepareStatement(anyString()); verify(preparedStatementMock, times(1)).executeUpdate(); verify(preparedStatementMock, times(1)).close(); verify(loggerMock, times(1)).debug(contains("Exception")); }