@SuppressWarnings("unchecked") @Test public void getIdByIndex_freeformWithPaging5000rowsIndex1337_returnsRowId1337() throws SQLException { DataGenerator.addFiveThousandPeople(connectionPool); FreeformQuery query = new FreeformQuery("SELECT * FROM people", connectionPool, "ID"); FreeformQueryDelegate delegate = EasyMock .createMock(FreeformQueryDelegate.class); EasyMock.expect( delegate.getQueryString(EasyMock.anyInt(), EasyMock.anyInt())) .andAnswer(new IAnswer<String>() { @Override public String answer() throws Throwable { Object[] args = EasyMock.getCurrentArguments(); int offset = (Integer) (args[0]); int limit = (Integer) (args[1]); if (SQLTestsConstants.db == DB.MSSQL) { int start = offset + 1; int end = offset + limit + 1; String q = "SELECT * FROM (SELECT row_number() OVER" + " ( ORDER BY \"ID\" ASC) AS rownum, * FROM people)" + " AS a WHERE a.rownum BETWEEN " + start + " AND " + end; return q; } else if (SQLTestsConstants.db == DB.ORACLE) { int start = offset + 1; int end = offset + limit + 1; String q = "SELECT * FROM (SELECT x.*, ROWNUM AS r FROM" + " (SELECT * FROM people ORDER BY \"ID\" ASC) x) " + " WHERE r BETWEEN " + start + " AND " + end; return q; } else { return "SELECT * FROM people LIMIT " + limit + " OFFSET " + offset; } } }).anyTimes(); delegate.setFilters(null); EasyMock.expectLastCall().anyTimes(); delegate.setFilters(EasyMock.isA(List.class)); EasyMock.expectLastCall().anyTimes(); delegate.setOrderBy(null); EasyMock.expectLastCall().anyTimes(); delegate.setOrderBy(EasyMock.isA(List.class)); EasyMock.expectLastCall().anyTimes(); EasyMock.expect(delegate.getCountQuery()) .andThrow(new UnsupportedOperationException()).anyTimes(); EasyMock.replay(delegate); query.setDelegate(delegate); SQLContainer container = new SQLContainer(query); Object itemId = container.getIdByIndex(1337); if (SQLTestsConstants.db == DB.ORACLE) { Assert.assertEquals( new RowId(new Object[] { 1337 + offset }).toString(), itemId.toString()); } else { Assert.assertEquals(new RowId(new Object[] { 1337 + offset }), itemId); } }
@Override public Object getIdByIndex(int index) { if (index < 0) { throw new IndexOutOfBoundsException("Index is negative! index=" + index); } updateCount(); if (index < size) { if (itemIndexes.keySet().contains(index)) { return itemIndexes.get(index); } updateOffsetAndCache(index); return itemIndexes.get(index); } else { int offset = index - size; return getFilteredAddedItems().get(offset).getId(); } }
SQLContainer implements Container, Container.Filterable,
        Container.Indexed, Container.Sortable, Container.ItemSetChangeNotifier { @Override public Object getIdByIndex(int index) { if (index < 0) { throw new IndexOutOfBoundsException("Index is negative! index=" + index); } updateCount(); if (index < size) { if (itemIndexes.keySet().contains(index)) { return itemIndexes.get(index); } updateOffsetAndCache(index); return itemIndexes.get(index); } else { int offset = index - size; return getFilteredAddedItems().get(offset).getId(); } } }
SQLContainer implements Container, Container.Filterable,
        Container.Indexed, Container.Sortable, Container.ItemSetChangeNotifier { @Override public Object getIdByIndex(int index) { if (index < 0) { throw new IndexOutOfBoundsException("Index is negative! index=" + index); } updateCount(); if (index < size) { if (itemIndexes.keySet().contains(index)) { return itemIndexes.get(index); } updateOffsetAndCache(index); return itemIndexes.get(index); } else { int offset = index - size; return getFilteredAddedItems().get(offset).getId(); } } @SuppressWarnings("unused") private  SQLContainer();  SQLContainer(QueryDelegate delegate); }
SQLContainer implements Container, Container.Filterable,
        Container.Indexed, Container.Sortable, Container.ItemSetChangeNotifier { @Override public Object getIdByIndex(int index) { if (index < 0) { throw new IndexOutOfBoundsException("Index is negative! index=" + index); } updateCount(); if (index < size) { if (itemIndexes.keySet().contains(index)) { return itemIndexes.get(index); } updateOffsetAndCache(index); return itemIndexes.get(index); } else { int offset = index - size; return getFilteredAddedItems().get(offset).getId(); } } @SuppressWarnings("unused") private  SQLContainer();  SQLContainer(QueryDelegate delegate); @Override Object addItem(); @Override boolean containsId(Object itemId); @Override Property getContainerProperty(Object itemId, Object propertyId); @Override Collection<?> getContainerPropertyIds(); @Override Item getItem(Object itemId); Item getItemUnfiltered(Object itemId); @Override Collection<?> getItemIds(); @Override Class<?> getType(Object propertyId); @Override int size(); @Override boolean removeItem(Object itemId); @Override boolean removeAllItems(); @Override void addContainerFilter(Filter filter); @Override void removeContainerFilter(Filter filter); void addContainerFilter(Object propertyId, String filterString,
            boolean ignoreCase, boolean onlyMatchPrefix); void removeContainerFilters(Object propertyId); @Override void removeAllContainerFilters(); boolean hasContainerFilters(); @Override Collection<Filter> getContainerFilters(); @Override int indexOfId(Object itemId); @Override Object getIdByIndex(int index); @Override List<Object> getItemIds(int startIndex, int numberOfIds); @Override Object nextItemId(Object itemId); @Override Object prevItemId(Object itemId); @Override Object firstItemId(); @Override Object lastItemId(); @Override boolean isFirstId(Object itemId); @Override boolean isLastId(Object itemId); @Override void sort(Object[] propertyId, boolean[] ascending); @Override Collection<?> getSortableContainerPropertyIds(); void refresh(); boolean isModified(); void setAutoCommit(boolean autoCommitEnabled); boolean isAutoCommit(); int getPageLength(); void setPageLength(int pageLength); void addOrderBy(OrderBy orderBy); void commit(); void rollback(); @Override boolean addContainerProperty(Object propertyId, Class<?> type,
            Object defaultValue); @Override boolean removeContainerProperty(Object propertyId); @Override Item addItem(Object itemId); @Override Item addItemAfter(Object previousItemId, Object newItemId); @Override Item addItemAt(int index, Object newItemId); @Override Object addItemAt(int index); @Override Object addItemAfter(Object previousItemId); @Override void addItemSetChangeListener(
            Container.ItemSetChangeListener listener); @Override void removeItemSetChangeListener(
            Container.ItemSetChangeListener listener); void addRowIdChangeListener(RowIdChangeListener listener); void removeRowIdChangeListener(RowIdChangeListener listener); void enableCacheFlushNotifications(); void addReference(SQLContainer refdCont, String refingCol,
            String refdCol); boolean removeReference(SQLContainer refdCont); boolean setReferencedItem(Object itemId, Object refdItemId,
            SQLContainer refdCont); Object getReferencedItemId(Object itemId, SQLContainer refdCont); Item getReferencedItem(Object itemId, SQLContainer refdCont); }
SQLContainer implements Container, Container.Filterable,
        Container.Indexed, Container.Sortable, Container.ItemSetChangeNotifier { @Override public Object getIdByIndex(int index) { if (index < 0) { throw new IndexOutOfBoundsException("Index is negative! index=" + index); } updateCount(); if (index < size) { if (itemIndexes.keySet().contains(index)) { return itemIndexes.get(index); } updateOffsetAndCache(index); return itemIndexes.get(index); } else { int offset = index - size; return getFilteredAddedItems().get(offset).getId(); } } @SuppressWarnings("unused") private  SQLContainer();  SQLContainer(QueryDelegate delegate); @Override Object addItem(); @Override boolean containsId(Object itemId); @Override Property getContainerProperty(Object itemId, Object propertyId); @Override Collection<?> getContainerPropertyIds(); @Override Item getItem(Object itemId); Item getItemUnfiltered(Object itemId); @Override Collection<?> getItemIds(); @Override Class<?> getType(Object propertyId); @Override int size(); @Override boolean removeItem(Object itemId); @Override boolean removeAllItems(); @Override void addContainerFilter(Filter filter); @Override void removeContainerFilter(Filter filter); void addContainerFilter(Object propertyId, String filterString,
            boolean ignoreCase, boolean onlyMatchPrefix); void removeContainerFilters(Object propertyId); @Override void removeAllContainerFilters(); boolean hasContainerFilters(); @Override Collection<Filter> getContainerFilters(); @Override int indexOfId(Object itemId); @Override Object getIdByIndex(int index); @Override List<Object> getItemIds(int startIndex, int numberOfIds); @Override Object nextItemId(Object itemId); @Override Object prevItemId(Object itemId); @Override Object firstItemId(); @Override Object lastItemId(); @Override boolean isFirstId(Object itemId); @Override boolean isLastId(Object itemId); @Override void sort(Object[] propertyId, boolean[] ascending); @Override Collection<?> getSortableContainerPropertyIds(); void refresh(); boolean isModified(); void setAutoCommit(boolean autoCommitEnabled); boolean isAutoCommit(); int getPageLength(); void setPageLength(int pageLength); void addOrderBy(OrderBy orderBy); void commit(); void rollback(); @Override boolean addContainerProperty(Object propertyId, Class<?> type,
            Object defaultValue); @Override boolean removeContainerProperty(Object propertyId); @Override Item addItem(Object itemId); @Override Item addItemAfter(Object previousItemId, Object newItemId); @Override Item addItemAt(int index, Object newItemId); @Override Object addItemAt(int index); @Override Object addItemAfter(Object previousItemId); @Override void addItemSetChangeListener(
            Container.ItemSetChangeListener listener); @Override void removeItemSetChangeListener(
            Container.ItemSetChangeListener listener); void addRowIdChangeListener(RowIdChangeListener listener); void removeRowIdChangeListener(RowIdChangeListener listener); void enableCacheFlushNotifications(); void addReference(SQLContainer refdCont, String refingCol,
            String refdCol); boolean removeReference(SQLContainer refdCont); boolean setReferencedItem(Object itemId, Object refdItemId,
            SQLContainer refdCont); Object getReferencedItemId(Object itemId, SQLContainer refdCont); Item getReferencedItem(Object itemId, SQLContainer refdCont); static final int DEFAULT_PAGE_LENGTH; static final int CACHE_RATIO; }