DatabaseAccessObject { public void dropPrimaryKey(Connection conn, String tableName) { try(PreparedStatement pstmt = conn.prepareStatement("ALTER TABLE " + tableName + " DROP PRIMARY KEY ");) { pstmt.executeUpdate(); s_logger.debug("Primary key is dropped successfully from the table " + tableName); } catch (SQLException e) { s_logger.debug("Ignored SQL Exception when trying to drop primary 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 testDropPrimaryKey() throws Exception { when(connectionMock.prepareStatement(contains("DROP PRIMARY KEY"))).thenReturn(preparedStatementMock); Connection conn = connectionMock; String tableName = "tableName"; dao.dropPrimaryKey(conn, tableName); 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 testDropPrimaryKeyWhenConnectionIsNull() throws Exception { Connection conn = null; String tableName = "tableName"; dao.dropPrimaryKey(conn, tableName); }
@Test public void testDropPrimaryKeyWhenTableNameIsNull() throws Exception { SQLException sqlException = new SQLException(); when(connectionMock.prepareStatement(contains("null DROP PRIMARY KEY"))).thenReturn(preparedStatementMock); when(preparedStatementMock.executeUpdate()).thenThrow(sqlException); Connection conn = connectionMock; String tableName = null; dao.dropPrimaryKey(conn, tableName); 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 testDropPrimaryKeyWhenPrepareStatementResultsInException() throws Exception { SQLException sqlException = new SQLException(); when(connectionMock.prepareStatement(contains("DROP PRIMARY KEY"))).thenThrow(sqlException); Connection conn = connectionMock; String tableName = null; dao.dropPrimaryKey(conn, tableName); 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 testDropPrimaryKeyWhenExecuteUpdateResultsInException() throws Exception { SQLException sqlException = new SQLException(); when(connectionMock.prepareStatement(contains("DROP PRIMARY KEY"))).thenReturn(preparedStatementMock); when(preparedStatementMock.executeUpdate()).thenThrow(sqlException); Connection conn = connectionMock; String tableName = null; dao.dropPrimaryKey(conn, tableName); verify(connectionMock, times(1)).prepareStatement(anyString()); verify(preparedStatementMock, times(1)).executeUpdate(); verify(preparedStatementMock, times(1)).close(); verify(loggerMock, times(1)).debug(contains("Exception")); }