@Test public void addItem_tableAddOneNewItem_autoCommit_sizeIsIncreased() throws SQLException { container.setAutoCommit(true); int originalSize = container.size(); container.addItem(); assertEquals(originalSize + 1, container.size()); }
@Test public void refresh_table_sizeShouldUpdate() throws SQLException { assertEquals(4, container.size()); DataGenerator.addFiveThousandPeople(connectionPool); container.refresh(); assertEquals(5000, container.size()); }
@Test public void addItem_tableAddTwoNewItems_shouldChangeSize() throws SQLException { int size = container.size(); Object id1 = container.addItem(); Object id2 = container.addItem(); assertEquals(size + 2, container.size()); Assert.assertNotSame(id1, id2); Assert.assertFalse(id1.equals(id2)); }
@Test public void removeItem_tableOneAddedItem_removesTheAddedItem() throws SQLException { Object id = container.addItem(); int size = container.size(); assertTrue(container.removeItem(id)); Assert.assertFalse(container.containsId(id)); assertEquals(size - 1, container.size()); }
@Test public void refresh_tableWithoutCallingRefresh_sizeShouldNotUpdate() throws SQLException { // Yeah, this is a weird one. We're testing that the size doesn't update // after adding lots of items unless we call refresh inbetween. This to // make sure that the refresh method actually refreshes stuff and isn't // a NOP. assertEquals(4, container.size()); DataGenerator.addFiveThousandPeople(connectionPool); assertEquals(4, container.size()); }
@Test public void removeItem_tableNoAddedItems_removesItemFromContainer() throws SQLException { int originalSize = container.size(); Object id = container.firstItemId(); assertTrue(container.removeItem(id)); Assert.assertNotSame(id, container.firstItemId()); assertEquals(originalSize - 1, container.size()); }
@Test public void rollback_tableItemRemoved_restoresRemovedItem() throws SQLException { int size = container.size(); Object last = container.lastItemId(); container.removeItem(last); assertEquals(size - 1, container.size()); container.rollback(); assertEquals(size, container.size()); assertEquals(last, container.lastItemId()); }
@Test public void addFilter_table_filtersResults() throws SQLException { // Ville, Kalle, Pelle, Börje assertEquals(4, container.size()); assertEquals("Börje", container.getContainerProperty(container.lastItemId(), NAME).getValue()); container.addContainerFilter(new Like(NAME, "%lle")); // Ville, Kalle, Pelle assertEquals(3, container.size()); assertEquals("Pelle", container.getContainerProperty(container.lastItemId(), NAME).getValue()); }
@Test public void rollback_tableItemAdded_discardsAddedItem() throws SQLException { int size = container.size(); Object id = container.addItem(); container.getContainerProperty(id, NAME).setValue("foo"); assertEquals(size + 1, container.size()); container.rollback(); assertEquals(size, container.size()); Assert.assertFalse( "foo".equals(container.getContainerProperty(container.lastItemId(), NAME).getValue())); }
@Test public void addContainerFilter_ignoreCase_filtersResults() throws SQLException { // Ville, Kalle, Pelle, Börje assertEquals(4, container.size()); container.addContainerFilter(NAME, "vi", true, false); // Ville assertEquals(1, container.size()); assertEquals("Ville", container.getContainerProperty(container.lastItemId(), NAME).getValue()); }
@Test public void commit_removeModifiedItem_shouldSucceed() throws SQLException { int size = container.size(); Object key = container.firstItemId(); Item row = container.getItem(key); row.getItemProperty(NAME).setValue("Pekka"); assertTrue(container.removeItem(key)); container.commit(); assertEquals(size - 1, container.size()); }
// Set timeout to ensure there is no infinite looping (#12882) @Test(timeout = 1000) public void removeAllItems_manyItems_commit_shouldSucceed() throws SQLException { final int itemNumber = (SQLContainer.CACHE_RATIO + 1) * SQLContainer.DEFAULT_PAGE_LENGTH + 1; container.removeAllItems(); assertEquals(container.size(), 0); for (int i = 0; i < itemNumber; ++i) { container.addItem(); } container.commit(); assertEquals(container.size(), itemNumber); assertTrue(container.removeAllItems()); container.commit(); assertEquals(container.size(), 0); }
@Test public void removeContainerFilters_table_noFiltering() throws SQLException { // Ville, Kalle, Pelle, Börje assertEquals(4, container.size()); container.addContainerFilter(NAME, "Vi", false, false); // Ville assertEquals(1, container.size()); assertEquals("Ville", container.getContainerProperty(container.lastItemId(), NAME).getValue()); container.removeContainerFilters(NAME); assertEquals(4, container.size()); assertEquals("Börje", container.getContainerProperty(container.lastItemId(), NAME).getValue()); }
@Test public void getIdByIndex_tableOneItemAddedLastIndexInContainer_returnsAddedItem() throws SQLException { Object id = container.addItem(); assertEquals(id, container.getIdByIndex(container.size() - 1)); }
@Test public void removeAllItems_tableAddedItems_shouldSucceed() throws SQLException { container.addItem(); container.addItem(); assertTrue(container.removeAllItems()); assertEquals(0, container.size()); }
@Test public void addFilter_tableBufferedItems_alsoFiltersBufferedItems() throws SQLException { // Ville, Kalle, Pelle, Börje assertEquals(4, container.size()); assertEquals("Börje", container.getContainerProperty(container.lastItemId(), NAME).getValue()); Object id1 = container.addItem(); container.getContainerProperty(id1, NAME).setValue("Palle"); Object id2 = container.addItem(); container.getContainerProperty(id2, NAME).setValue("Bengt"); container.addContainerFilter(new Like(NAME, "%lle")); // Ville, Kalle, Pelle, Palle assertEquals(4, container.size()); assertEquals( "Ville", container.getContainerProperty(container.getIdByIndex(0), NAME).getValue()); assertEquals( "Kalle", container.getContainerProperty(container.getIdByIndex(1), NAME).getValue()); assertEquals( "Pelle", container.getContainerProperty(container.getIdByIndex(2), NAME).getValue()); assertEquals( "Palle", container.getContainerProperty(container.getIdByIndex(3), NAME).getValue()); try { container.getIdByIndex(4); Assert.fail( "SQLContainer.getIdByIndex() returned a value for an index beyond the end of the container"); } catch (IndexOutOfBoundsException e) { // should throw exception - item is filtered out } Assert.assertNull(container.nextItemId(container.getIdByIndex(3))); Assert.assertFalse(container.containsId(id2)); Assert.assertFalse(container.getItemIds().contains(id2)); Assert.assertNull(container.getItem(id2)); assertEquals(-1, container.indexOfId(id2)); Assert.assertNotSame(id2, container.lastItemId()); Assert.assertSame(id1, container.lastItemId()); }
@Test public void size_tableOneAddedItem_returnsFive() throws SQLException { Connection conn = connectionPool.reserveConnection(); Statement statement = conn.createStatement(); if (SQLTestsConstants.db == DB.MSSQL) { statement.executeUpdate("insert into people values('Bengt', 30)"); } else { statement.executeUpdate("insert into people values(default, 'Bengt', 30)"); } statement.close(); conn.commit(); connectionPool.releaseConnection(conn); assertEquals(5, container.size()); }
@Test public void commit_tableTwoAddedItems_shouldBeWrittenToDB() throws SQLException { Object id = container.addItem(); Object id2 = container.addItem(); container.getContainerProperty(id, NAME).setValue("Herbert"); container.getContainerProperty(id2, NAME).setValue("Larry"); assertTrue(id2 instanceof TemporaryRowId); Assert.assertSame(id2, container.lastItemId()); container.commit(); Object nextToLast = container.getIdByIndex(container.size() - 2); Assert.assertFalse(nextToLast instanceof TemporaryRowId); assertEquals("Herbert", container.getContainerProperty(nextToLast, NAME).getValue()); Assert.assertFalse(container.lastItemId() instanceof TemporaryRowId); assertEquals("Larry", container.getContainerProperty(container.lastItemId(), NAME).getValue()); }
@Test public void sort_tableBufferedItems_sortsBufferedItemsLastInOrderAdded() throws SQLException { // Ville, Kalle, Pelle, Börje assertEquals("Ville", container.getContainerProperty(container.firstItemId(), NAME).getValue()); assertEquals("Börje", container.getContainerProperty(container.lastItemId(), NAME).getValue()); Object id1 = container.addItem(); container.getContainerProperty(id1, NAME).setValue("Wilbert"); Object id2 = container.addItem(); container.getContainerProperty(id2, NAME).setValue("Albert"); container.sort(new Object[] {NAME}, new boolean[] {true}); // Börje, Kalle, Pelle, Ville, Wilbert, Albert assertEquals("Börje", container.getContainerProperty(container.firstItemId(), NAME).getValue()); assertEquals( "Wilbert", container .getContainerProperty(container.getIdByIndex(container.size() - 2), NAME) .getValue()); assertEquals("Albert", container.getContainerProperty(container.lastItemId(), NAME).getValue()); }
@Test public void addItem_tableAddOneNewItem_shouldChangeSize() throws SQLException { int size = container.size(); container.addItem(); assertEquals(size + 1, container.size()); }
@Test public void sizeIsReturnedCorrectly() { assertEquals(numberOfRowsInContainer, container.size()); }