Search in sources :

Example 1 with Colour

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);
    }
}
Also used : AlternativePalette(org.jpox.samples.types.enums.AlternativePalette) Palette(org.jpox.samples.types.enums.Palette) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection) AlternativeColour(org.jpox.samples.types.enums.AlternativeColour) Colour(org.jpox.samples.types.enums.Colour) HashSet(java.util.HashSet)

Aggregations

Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 PersistenceManager (javax.jdo.PersistenceManager)1 Transaction (javax.jdo.Transaction)1 AlternativeColour (org.jpox.samples.types.enums.AlternativeColour)1 AlternativePalette (org.jpox.samples.types.enums.AlternativePalette)1 Colour (org.jpox.samples.types.enums.Colour)1 Palette (org.jpox.samples.types.enums.Palette)1