public void testFurnitureTableFilter() {
// 1. Create a home that contains furniture matching catalog furniture
UserPreferences preferences = new DefaultUserPreferences();
List<HomePieceOfFurniture> homeFurniture =
createHomeFurnitureFromCatalog(preferences.getFurnitureCatalog());
Home home = new Home(homeFurniture);
// Check home furniture isn't empty
assertTrue("Home furniture is empty", homeFurniture.size() > 0);
// 2. Create a table that displays home furniture with its controller
FurnitureController furnitureController =
new FurnitureController(home, preferences, new SwingViewFactory());
FurnitureTable table = (FurnitureTable)furnitureController.getView();
assertEquals("Home furniture count and row count different", homeFurniture.size(), table.getRowCount());
// Apply a filter on furniture that refuses pieces that are windows
table.setFurnitureFilter(new FurnitureFilter() {
public boolean include(Home home, HomePieceOfFurniture piece) {
return !piece.isDoorOrWindow();
}
});
// Count how many doors and windows are in home
int doorsAndWindowsCount = 0;
HomePieceOfFurniture doorOrWindowPiece = null;
HomePieceOfFurniture otherPiece = null;
for (HomePieceOfFurniture piece : home.getFurniture()) {
if (piece.isDoorOrWindow()) {
doorsAndWindowsCount++;
doorOrWindowPiece = piece;
} else {
otherPiece = piece;
}
}
// Check there's no door or window in table
int homeFurnitureCount = homeFurniture.size();
int tableFilterRowCount = table.getRowCount();
assertEquals("Home furniture count and row count same",
homeFurnitureCount - doorsAndWindowsCount, tableFilterRowCount);
// 3. Add a door or window to home
home.addPieceOfFurniture(new HomePieceOfFurniture(doorOrWindowPiece));
// Check no row were added in table
assertEquals("Wrong furniture count in home", homeFurnitureCount + 1, home.getFurniture().size());
assertEquals("Wrong row count in table", tableFilterRowCount, table.getRowCount());
// 4. Add an other kind of piece to home
home.addPieceOfFurniture(new HomePieceOfFurniture(otherPiece));
// Check one row was added in table
assertEquals("Wrong furniture count in home", homeFurnitureCount + 2, home.getFurniture().size());
assertEquals("Wrong row count in table", tableFilterRowCount + 1, table.getRowCount());
// 5. Test sort and filter internal buffer of the table
furnitureController.sortFurniture(HomePieceOfFurniture.SortableProperty.NAME);
// Check the alphabetical order of table data
assertFurnitureIsSortedByName(table, true);
// Add a door or window and an other kind of piece to home
home.addPieceOfFurniture(new HomePieceOfFurniture(doorOrWindowPiece));
home.addPieceOfFurniture(new HomePieceOfFurniture(otherPiece));
// Check one row was added in sorted table
assertEquals("Wrong furniture count in home", homeFurnitureCount + 4, home.getFurniture().size());
assertEquals("Wrong row count in table", tableFilterRowCount + 2, table.getRowCount());
assertFurnitureIsSortedByName(table, true);
// 6. Remove filter
table.setFurnitureFilter(null);
// Check missing rows are back
assertEquals("Home furniture count and row count different",
home.getFurniture().size(), table.getRowCount());
assertFurnitureIsSortedByName(table, true);
}