// check ignore case in selects
Author author = new Author();
author.setName("AuTHor");
author.save();
Criteria criteria = new Criteria();
criteria.where(AuthorPeer.NAME, author.getName().toLowerCase());
criteria.setIgnoreCase(true);
List<Author> result = AuthorPeer.doSelect(criteria);
assertTrue("Size of result is not 1, but " + result.size(),
result.size() == 1);
// LIKE treatment might be different (e.g. postgres), so check extra
criteria = new Criteria();
criteria.where(
AuthorPeer.NAME,
author.getName().toLowerCase().replace('r', '%'),
Criteria.LIKE);
criteria.setIgnoreCase(true);
result = AuthorPeer.doSelect(criteria);
assertTrue("Size of result is not 1, but " + result.size(),
result.size() == 1);
// Test ignore case in criterion
criteria = new Criteria();
Criterion criterion1 = new Criterion(
AuthorPeer.NAME,
author.getName().toLowerCase(),
Criteria.EQUAL);
criterion1.setIgnoreCase(true);
Criterion criterion2 = new Criterion(
AuthorPeer.AUTHOR_ID, null, Criteria.NOT_EQUAL);
criterion1.and(criterion2);
result = AuthorPeer.doSelect(criteria);
// ignore case should not be set either in Criteria
// nor in other criterions
assertFalse(criteria.isIgnoreCase());
assertFalse(criterion2.isIgnoreCase());
assertTrue("Size of result is not 1, but " + result.size(),
result.size() == 1);
// Test ignore case in attached criterion
criteria = new Criteria();
criterion1 = new Criterion(
AuthorPeer.AUTHOR_ID, null, Criteria.NOT_EQUAL);
criterion2 = new Criterion(
AuthorPeer.NAME,
author.getName().toLowerCase(),
Criteria.EQUAL);
criterion2.setIgnoreCase(true);
criterion1.and(criterion2);
result = AuthorPeer.doSelect(criteria);
// ignore case should not be set either in Criteria
// nor in other criterions
assertFalse(criteria.isIgnoreCase());
assertFalse(criterion1.isIgnoreCase());
assertTrue("Size of result is not 1, but " + result.size(),
result.size() == 1);
// ignore case in "in" query
{
criteria = new Criteria();
Set<String> names = new HashSet<String>();
names.add(author.getName().toLowerCase());
criteria.where(AuthorPeer.NAME, names, Criteria.IN);
criteria.setIgnoreCase(true);
result = AuthorPeer.doSelect(criteria);
assertEquals("Expected result of size 1 but got " + result.size(),
result.size(),
1);
}
// Check that case is not ignored if ignoreCase is not set
// This is known not to work for mysql
author = new Author();
author.setName("author");
author.save();
Adapter adapter = Torque.getAdapter(Torque.getDefaultDB());
if (adapter instanceof MysqlAdapter
|| adapter instanceof MssqlAdapter)
{
log.error("testIgnoreCase(): "
+ "Case sensitive comparisons are known not to work"
+ " with Mysql and MSSQL");
// failing is "expected", so bypass without error
}
else
{
criteria = new Criteria();
criteria.where(AuthorPeer.NAME, author.getName());
result = AuthorPeer.doSelect(criteria);
assertTrue("Size of result is not 1, but " + result.size(),
result.size() == 1);
// again check LIKE treatment
criteria = new Criteria();
criteria.where(
AuthorPeer.NAME,
author.getName().replace('r', '%'),
Criteria.LIKE);
result = AuthorPeer.doSelect(criteria);
assertTrue("Size of result is not 1, but " + result.size(),
result.size() == 1);
// Test different ignore cases in criterions
criteria = new Criteria();
criterion1 = new Criterion(
AuthorPeer.NAME,
author.getName().toLowerCase(),
Criteria.NOT_EQUAL);
criterion2 = new Criterion(
AuthorPeer.NAME,
author.getName().toLowerCase(),
Criteria.EQUAL);
criterion2.setIgnoreCase(true);
criterion1.and(criterion2);
criteria.where(criterion1);
result = AuthorPeer.doSelect(criteria);
assertTrue("Size of result is not 1, but " + result.size(),
result.size() == 1);
// ignore case in "in" query
{
criteria = new Criteria();
Set<String> names = new HashSet<String>();
names.add(author.getName());
criteria.where(AuthorPeer.NAME, names, Criteria.IN);
result = AuthorPeer.doSelect(criteria);
assertEquals("Expected result of size 1 but got " + result.size(),
result.size(),
1);
}
}
cleanBookstore();
author = new Author();
author.setName("AA");
author.save();
author = new Author();
author.setName("BB");
author.save();
author = new Author();
author.setName("ba");
author.save();
author = new Author();
author.setName("ab");
author.save();
// check ignoreCase in Criteria
criteria = new Criteria();
criteria.setIgnoreCase(true);
criteria.addAscendingOrderByColumn(AuthorPeer.NAME);
result = AuthorPeer.doSelect(criteria);
assertTrue("Size of result is not 4, but " + result.size(),
result.size() == 4);
assertEquals(result.get(0).getName(), "AA");
assertEquals(result.get(1).getName(), "ab");
assertEquals(result.get(2).getName(), "ba");
assertEquals(result.get(3).getName(), "BB");
// check ignoreCase in orderBy
criteria = new Criteria();
criteria.addAscendingOrderByColumn(AuthorPeer.NAME, true);
result = AuthorPeer.doSelect(criteria);
assertTrue("Size of result is not 4, but " + result.size(),
result.size() == 4);
assertEquals(result.get(0).getName(), "AA");
assertEquals(result.get(1).getName(), "ab");