}
@SuppressWarnings("unchecked")
@Test
public void addOrderBy_freeform_shouldReorderResults() throws SQLException {
FreeformQuery query = new FreeformQuery("SELECT * FROM people",
connectionPool, "ID");
FreeformQueryDelegate delegate = EasyMock
.createMock(FreeformQueryDelegate.class);
final ArrayList<OrderBy> orderBys = new ArrayList<OrderBy>();
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().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
List<OrderBy> orders = (List<OrderBy>) EasyMock
.getCurrentArguments()[0];
orderBys.clear();
orderBys.addAll(orders);
return null;
}
}).anyTimes();
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) {
SQLGenerator gen = new MSSQLGenerator();
if (orderBys == null || orderBys.isEmpty()) {
List<OrderBy> ob = new ArrayList<OrderBy>();
ob.add(new OrderBy("ID", true));
return gen.generateSelectQuery("people", null,
ob, offset, limit, null)
.getQueryString();
} else {
return gen.generateSelectQuery("people", null,
orderBys, offset, limit, null)
.getQueryString();
}
} else if (SQLTestsConstants.db == DB.ORACLE) {
SQLGenerator gen = new OracleGenerator();
if (orderBys == null || orderBys.isEmpty()) {
List<OrderBy> ob = new ArrayList<OrderBy>();
ob.add(new OrderBy("ID", true));
return gen.generateSelectQuery("people", null,
ob, offset, limit, null)
.getQueryString();
} else {
return gen.generateSelectQuery("people", null,
orderBys, offset, limit, null)
.getQueryString();
}
} else {
StringBuffer query = new StringBuffer(
"SELECT * FROM people");
if (!orderBys.isEmpty()) {
query.append(" ORDER BY ");
for (OrderBy orderBy : orderBys) {
query.append("\"" + orderBy.getColumn()
+ "\"");
if (orderBy.isAscending()) {
query.append(" ASC");
} else {
query.append(" DESC");
}
}
}
query.append(" LIMIT ").append(limit)
.append(" OFFSET ").append(offset);
return query.toString();
}
}
}).anyTimes();
EasyMock.expect(delegate.getCountQuery())
.andThrow(new UnsupportedOperationException()).anyTimes();
EasyMock.replay(delegate);
query.setDelegate(delegate);
SQLContainer container = new SQLContainer(query);
// Ville, Kalle, Pelle, Börje
Assert.assertEquals("Ville",
container.getContainerProperty(container.firstItemId(), "NAME")
.getValue());