DbUpgradeUtils { public static void dropTableColumnsIfExist(Connection conn, String tableName, List<String> columns) { for (String columnName : columns) { if (dao.columnExists(conn, tableName, columnName)) { dao.dropColumn(conn, tableName, columnName); } } }  static void dropKeysIfExist(Connection conn, String tableName, List<String> keys, boolean isForeignKey); static void dropPrimaryKeyIfExists(Connection conn, String tableName); static void dropTableColumnsIfExist(Connection conn, String tableName, List<String> columns);  }
@Test public void testDropTableColumnsIfExistWhenNoKeysAreSupplied() throws Exception { Connection conn = connectionMock; String tableName = "tableName"; List<String> columns = new ArrayList<String>(); DbUpgradeUtils.dropTableColumnsIfExist(conn, tableName, columns); verify(daoMock, times(0)).columnExists(eq(conn), eq(tableName), anyString()); verify(daoMock, times(0)).dropColumn(eq(conn), eq(tableName), anyString()); }
@Test public void testDropTableColumnsIfExistWhenOneKeysIsSuppliedAndColumnExists() throws Exception { Connection conn = connectionMock; String tableName = "tableName"; String column = "column"; when(daoMock.columnExists(conn, tableName, column)).thenReturn(true); List<String> columns = Arrays.asList(new String[] {column}); DbUpgradeUtils.dropTableColumnsIfExist(conn, tableName, columns); verify(daoMock, times(1)).columnExists(conn, tableName, column); verify(daoMock, times(1)).dropColumn(conn, tableName, column); }
@Test public void testDropTableColumnsIfExistWhenOneKeysIsSuppliedAndColumnDoesNotExists() throws Exception { Connection conn = connectionMock; String tableName = "tableName"; String column = "column"; when(daoMock.columnExists(conn, tableName, column)).thenReturn(false); List<String> columns = Arrays.asList(new String[] {column}); DbUpgradeUtils.dropTableColumnsIfExist(conn, tableName, columns); verify(daoMock, times(1)).columnExists(conn, tableName, column); verify(daoMock, times(0)).dropColumn(conn, tableName, column); }
@Test public void testDropTableColumnsIfExistWhenThreeKeysAreSuppliedAnOneDoesnotExist() throws Exception { Connection conn = connectionMock; String tableName = "tableName"; String column1 = "column1"; String column2 = "column2"; String column3 = "column3"; when(daoMock.columnExists(conn, tableName, column1)).thenReturn(true); when(daoMock.columnExists(conn, tableName, column2)).thenReturn(false); when(daoMock.columnExists(conn, tableName, column3)).thenReturn(true); List<String> keys = Arrays.asList(new String[] {column1, column2, column3}); DbUpgradeUtils.dropTableColumnsIfExist(conn, tableName, keys); verify(daoMock, times(1)).columnExists(conn, tableName, column1); verify(daoMock, times(1)).dropColumn(conn, tableName, column1); verify(daoMock, times(1)).columnExists(conn, tableName, column2); verify(daoMock, times(0)).dropColumn(conn, tableName, column2); verify(daoMock, times(1)).columnExists(conn, tableName, column3); verify(daoMock, times(1)).dropColumn(conn, tableName, column3); }