@Test public void getIdByIndex_tableItemRemoved_resultChanges() throws SQLException { Object id = container.getIdByIndex(2); assertTrue(container.removeItem(id)); Assert.assertNotSame(id, container.getIdByIndex(2)); }
@Test public void prevItemId_tableItemRemoved_skipsRemovedItem() throws SQLException { Object first = container.getIdByIndex(0); Object second = container.getIdByIndex(1); Object third = container.getIdByIndex(2); assertTrue(container.removeItem(second)); assertEquals(first, container.prevItemId(third)); }
@Test public void indexOfId_tableItemRemoved_returnsNegOne() throws SQLException { Object id = container.getIdByIndex(2); assertTrue(container.removeItem(id)); assertEquals(-1, container.indexOfId(id)); }
@Test public void getIdByIndex_tableOneItemAddedLastIndexInContainer_returnsAddedItem() throws SQLException { Object id = container.addItem(); assertEquals(id, container.getIdByIndex(container.size() - 1)); }
@Test public void allIdsFound_table5000RowsLastId_shouldSucceed() throws SQLException { DataGenerator.addFiveThousandPeople(connectionPool); for (int i = 0; i < 5000; i++) { assertTrue(container.containsId(container.getIdByIndex(i))); } }
@Test public void getIdByIndex_tableAddedItemRemoved_resultChanges() throws SQLException { Object id = container.addItem(); container.addItem(); int index = container.indexOfId(id); assertTrue(container.removeItem(id)); Assert.assertNotSame(id, container.getIdByIndex(index)); }
@Test public void getIdByIndex_table5000rowsIndex1337_returnsRowId1337() throws SQLException { DataGenerator.addFiveThousandPeople(connectionPool); Object itemId = container.getIdByIndex(1337); if (SQLTestsConstants.db == DB.ORACLE) { assertEquals(new RowId(new Object[] {1337 + offset}).toString(), itemId.toString()); } else { assertEquals(new RowId(new Object[] {1337 + offset}), itemId); } }
@Test public void prevItemId_tableCurrentItem1337_returns1336() throws SQLException { DataGenerator.addFiveThousandPeople(connectionPool); Object itemId = container.getIdByIndex(1337); if (SQLTestsConstants.db == DB.ORACLE) { assertEquals( new RowId(new Object[] {1336 + offset}).toString(), container.prevItemId(itemId).toString()); } else { assertEquals(new RowId(new Object[] {1336 + offset}), container.prevItemId(itemId)); } }
@Test public void nextItemId_tableCurrentItem1337_returnsItem1338() throws SQLException { DataGenerator.addFiveThousandPeople(connectionPool); SQLContainer container = new SQLContainer(new TableQuery("people", connectionPool, SQLTestsConstants.sqlGen)); Object itemId = container.getIdByIndex(1337); if (SQLTestsConstants.db == DB.ORACLE) { assertEquals( new RowId(new Object[] {1338 + offset}).toString(), container.nextItemId(itemId).toString()); } else { assertEquals(new RowId(new Object[] {1338 + offset}), container.nextItemId(itemId)); } }
@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 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()); }