package de.linwave.junit.nativequery;
import java.util.Comparator;
import java.util.concurrent.atomic.AtomicInteger;
import org.odbms.FUNCTION;
import org.odbms.OP;
import org.odbms.ObjectSet;
import org.odbms.Predicate;
import org.odbms.Query;
import de.linwave.junit.AbstractTestCase;
import freedb.Disc;
public class NativeQueryWithComparator extends AbstractTestCase
{
/**
*
*/
public void testWithComparator()
{
final AtomicInteger cnt = new AtomicInteger();
Comparator<Disc> compByAge = new Comparator<Disc>() {
public int compare(Disc o1, Disc o2)
{
return o1.getAge().compareTo(o2.getAge());
}
};
Query query = db.query(new Predicate<Disc>() {
public boolean match(Disc disc)
{
int age = disc.getAge();
boolean valid = (age >= 0 && age <= 3);
if (valid) {
cnt.incrementAndGet();
return true;
} else {
return false;
}
}
}, compByAge);
ObjectSet<Disc> discs = query.execute();
if (cnt.intValue() == 0)
fail("No rows found");
int lastAge = -1;
for (Disc disc : discs) {
int age = disc.getAge();
if (age > lastAge)
lastAge = age;
else if (age < lastAge)
break;
if (age >= 0 && age <= 3)
cnt.decrementAndGet();
}
assertEquals(0, cnt.intValue());
}
/**
*
*/
public void testWithComparatorAndConstrain()
{
final AtomicInteger cnt = new AtomicInteger();
Comparator<Disc> compByAge = new Comparator<Disc>() {
public int compare(Disc o1, Disc o2)
{
return o1.getAge().compareTo(o2.getAge());
}
};
Query query = db.query(new Predicate<Disc>() {
public boolean match(Disc disc)
{
int age = disc.getAge();
boolean valid = (age >= 0 && age <= 3) && disc.getDTITLE().contains("Band");
if (valid) {
cnt.incrementAndGet();
return true;
} else {
return false;
}
}
}, compByAge);
query.constrain("DTITLE", OP.CONTAINS, "Band");
ObjectSet<Disc> discs = query.execute();
if (cnt.intValue() == 0)
fail("No rows found");
for (Disc disc : discs) {
boolean valid = (disc.getAge() >= 0 && disc.getAge() <= 3) && disc.getDTITLE().contains("Band");
if (valid)
cnt.decrementAndGet();
}
assertEquals(0, cnt.intValue());
}
/**
*
*/
public void testLast3YearsWithJohnInTitle()
{
final AtomicInteger cnt = new AtomicInteger();
Query query = db.query(new Predicate<Disc>() {
public boolean match(Disc disc)
{
int age = disc.getAge();
boolean valid = (age <= 3 && age != -1);
if (valid) {
cnt.incrementAndGet();
return true;
} else {
return false;
}
}
});
query.constrain("DTITLE", OP.STARTS_WITH, "John");
ObjectSet<Disc> discs = query.execute();
int processed = 0;
for (Disc disc : discs) {
int age = disc.getAge();
boolean valid = (age <= 3 && age != -1) && disc.getDTITLE().startsWith("John");
if (valid)
processed++;
}
if (cnt.intValue() == 0)
fail("No rows found");
assertEquals(processed, cnt.intValue());
}
/**
*
*/
public void testAgeOver10StartsWithTHEGenreJazz()
{
Query query = db.query(new Predicate<Disc>() {
public boolean match(Disc disc)
{
return disc.getAge() > 10;
}
});
query.constrain("DTITLE", OP.STARTS_WITH, "The");
Query query2 = db.query(new Predicate<Disc>() {
public boolean match(Disc disc)
{
return disc.getAge() > 10;
}
});
query2.constrain("DGENRE", OP.EQUALS, FUNCTION.TO_UPPER, "JAZZ");
ObjectSet<Disc> discs = query.AND(query2);
int processed = 0;
for (Disc disc : discs) {
int age = disc.getAge();
boolean valid = (age > 10) && disc.getDTITLE().startsWith("The") && disc.getDGENRE().toUpperCase().equals("JAZZ");
if (valid)
processed++;
}
if (processed == 0)
fail("No rows found");
}
}