@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(),