use of org.jpox.samples.types.enums.Colour in project tests by datanucleus.
the class EnumTest method testQueryEnumCollectionContainsEnum.
/**
* Test use of JDOQL for Collection<Enum>.contains(enum).
*/
public void testQueryEnumCollectionContainsEnum() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Palette[] p = new Palette[2];
Object[] id = new Object[2];
try {
tx.begin();
p[0] = new Palette();
p[0].addColour(Colour.RED);
p[0].addColour(Colour.BLUE);
p[0].setAmount(100);
p[0].setColour(Colour.RED);
p[1] = new Palette();
p[1].addColour(Colour.GREEN);
p[1].addColour(Colour.BLUE);
p[1].setAmount(100);
p[1].setColour(Colour.BLUE);
pm.makePersistentAll(p);
id[0] = JDOHelper.getObjectId(p[0]);
id[1] = JDOHelper.getObjectId(p[1]);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Collection<Colour> enumColl = new HashSet<>();
enumColl.add(Colour.RED);
enumColl.add(Colour.GREEN);
Collection c = (Collection) pm.newQuery(Palette.class, ":param.contains(colour)").execute(enumColl);
assertEquals(1, c.size());
Palette pr = (Palette) c.iterator().next();
assertEquals(100, pr.getAmount());
HashSet<Colour> colours = pr.getColours();
assertEquals(2, colours.size());
assertTrue(colours.contains(Colour.BLUE));
assertTrue(colours.contains(Colour.RED));
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Palette.class);
}
}
Aggregations