Search in sources :

Example 1 with Palette

use of org.jpox.samples.types.enums.Palette in project tests by datanucleus.

the class ApplicationIdPersistenceTest method testEnum.

/**
 * Test persistence of an enum as a String and as ordinal.
 */
public void testEnum() {
    Palette p;
    Object id = null;
    try {
        // ---------------------
        // RED
        // ---------------------
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            p = new Palette();
            p.setAmount(100);
            p.setColour(Colour.RED);
            p.setColourOrdinal(Colour.RED);
            pm.makePersistent(p);
            id = JDOHelper.getObjectId(p);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pmf.getDataStoreCache().evictAll();
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            p = (Palette) pm.getObjectById(id, true);
            assertEquals(100, p.getAmount());
            assertEquals(Colour.RED, p.getColour());
            assertEquals(Colour.RED, p.getColourOrdinal());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // ---------------------
        // null
        // ---------------------
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            p = new Palette();
            p.setAmount(101);
            p.setColour(null);
            p.setColourOrdinal(null);
            pm.makePersistent(p);
            id = JDOHelper.getObjectId(p);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pmf.getDataStoreCache().evictAll();
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            p = (Palette) pm.getObjectById(id, true);
            assertEquals(101, p.getAmount());
            assertNull(p.getColour());
            assertNull(p.getColourOrdinal());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // ---------------------
        // GREEN
        // ---------------------
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            p = new Palette();
            p.setAmount(102);
            p.setColour(Colour.GREEN);
            p.setColourOrdinal(Colour.GREEN);
            pm.makePersistent(p);
            id = JDOHelper.getObjectId(p);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pmf.getDataStoreCache().evictAll();
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            p = (Palette) pm.getObjectById(id, true);
            assertEquals(102, p.getAmount());
            assertEquals(Colour.GREEN, p.getColour());
            assertEquals(Colour.GREEN, p.getColourOrdinal());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Palette.class);
    }
}
Also used : Palette(org.jpox.samples.types.enums.Palette) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 2 with Palette

use of org.jpox.samples.types.enums.Palette in project tests by datanucleus.

the class ApplicationIdPersistenceTest method testStringEnum.

/**
 * Test persistence of an enum as a String.
 */
public void testStringEnum() {
    Palette p;
    Object id = null;
    try {
        // ---------------------
        // RED
        // ---------------------
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            p = new Palette();
            p.setAmount(100);
            p.setColour(Colour.RED);
            pm.makePersistent(p);
            id = JDOHelper.getObjectId(p);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            p = (Palette) pm.getObjectById(id, true);
            assertEquals(100, p.getAmount());
            assertEquals(Colour.RED, p.getColour());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // ---------------------
        // null
        // ---------------------
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            p = new Palette();
            p.setAmount(101);
            p.setColour(null);
            pm.makePersistent(p);
            id = JDOHelper.getObjectId(p);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            p = (Palette) pm.getObjectById(id, true);
            assertEquals(101, p.getAmount());
            assertNull(p.getColour());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // ---------------------
        // GREEN
        // ---------------------
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            p = new Palette();
            p.setAmount(102);
            p.setColour(Colour.GREEN);
            pm.makePersistent(p);
            id = JDOHelper.getObjectId(p);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            p = (Palette) pm.getObjectById(id, true);
            assertEquals(102, p.getAmount());
            assertEquals(Colour.GREEN, p.getColour());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Palette.class);
    }
}
Also used : Palette(org.jpox.samples.types.enums.Palette) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 3 with Palette

use of org.jpox.samples.types.enums.Palette in project tests by datanucleus.

the class EnumTest method testQueryStringEnum.

/**
 * Test use of JDOQL with enums stored as Strings.
 */
public void testQueryStringEnum() {
    try {
        Palette[] p;
        Object[] id;
        // ---------------------
        // RED
        // ---------------------
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        p = new Palette[5];
        id = new Object[5];
        try {
            tx.begin();
            p[0] = new Palette();
            p[0].setAmount(100);
            p[0].setColour(Colour.RED);
            p[0].setColourOrdinal(Colour.RED);
            p[1] = new Palette();
            p[1].setAmount(101);
            p[1].setColour(null);
            p[2] = new Palette();
            p[2].setAmount(102);
            p[2].setColour(Colour.GREEN);
            p[2].setColourOrdinal(Colour.GREEN);
            p[3] = new Palette();
            p[3].setAmount(103);
            p[3].setColour(Colour.BLUE);
            p[3].setColourOrdinal(Colour.BLUE);
            p[4] = new Palette();
            p[4].setAmount(104);
            p[4].setColour(Colour.RED);
            p[4].setColourOrdinal(Colour.RED);
            pm.makePersistentAll(p);
            id[0] = JDOHelper.getObjectId(p[0]);
            id[1] = JDOHelper.getObjectId(p[1]);
            id[2] = JDOHelper.getObjectId(p[2]);
            id[3] = JDOHelper.getObjectId(p[3]);
            id[4] = JDOHelper.getObjectId(p[4]);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Collection c = (Collection) pm.newQuery(Palette.class, "colour == 'RED'").execute();
            assertEquals(2, c.size());
            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)

Example 4 with Palette

use of org.jpox.samples.types.enums.Palette in project tests by datanucleus.

the class ApplicationIdPersistenceTest method testEnumAsNumeric.

/**
 * Test persistence of an enum as a numeric.
 */
public void testEnumAsNumeric() {
    Palette p;
    Object id = null;
    try {
        // ---------------------
        // RED
        // ---------------------
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            p = new Palette();
            p.setAmount(100);
            p.setColourOrdinal(Colour.RED);
            pm.makePersistent(p);
            id = JDOHelper.getObjectId(p);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            p = (Palette) pm.getObjectById(id, true);
            assertEquals(100, p.getAmount());
            assertEquals(Colour.RED, p.getColourOrdinal());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // ---------------------
        // null
        // ---------------------
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            p = new Palette();
            p.setAmount(101);
            p.setColourOrdinal(null);
            pm.makePersistent(p);
            id = JDOHelper.getObjectId(p);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            p = (Palette) pm.getObjectById(id, true);
            assertEquals(101, p.getAmount());
            assertNull(p.getColourOrdinal());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // ---------------------
        // GREEN
        // ---------------------
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            p = new Palette();
            p.setAmount(102);
            p.setColourOrdinal(Colour.GREEN);
            pm.makePersistent(p);
            id = JDOHelper.getObjectId(p);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            p = (Palette) pm.getObjectById(id, true);
            assertEquals(102, p.getAmount());
            assertEquals(Colour.GREEN, p.getColourOrdinal());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Palette.class);
    }
}
Also used : Palette(org.jpox.samples.types.enums.Palette) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 5 with Palette

use of org.jpox.samples.types.enums.Palette in project tests by datanucleus.

the class EnumTest method testQueryStringEnumAll.

/**
 * Test use of JDOQL and enums for all types.
 */
public void testQueryStringEnumAll() {
    try {
        Palette[] p;
        Object[] id;
        // ---------------------
        // RED
        // ---------------------
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        p = new Palette[5];
        id = new Object[5];
        try {
            tx.begin();
            p[0] = new Palette();
            p[0].setAmount(100);
            p[0].setColour(Colour.RED);
            p[0].setColourOrdinal(Colour.RED);
            p[1] = new Palette();
            p[1].setAmount(101);
            p[1].setColour(null);
            p[2] = new Palette();
            p[2].setAmount(102);
            p[2].setColour(Colour.GREEN);
            p[2].setColourOrdinal(Colour.GREEN);
            p[3] = new Palette();
            p[3].setAmount(103);
            p[3].setColour(Colour.BLUE);
            p[3].setColourOrdinal(Colour.BLUE);
            p[4] = new Palette();
            p[4].setAmount(104);
            p[4].setColour(Colour.RED);
            p[4].setColourOrdinal(Colour.RED);
            pm.makePersistentAll(p);
            id[0] = JDOHelper.getObjectId(p[0]);
            id[1] = JDOHelper.getObjectId(p[1]);
            id[2] = JDOHelper.getObjectId(p[2]);
            id[3] = JDOHelper.getObjectId(p[3]);
            id[4] = JDOHelper.getObjectId(p[4]);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Collection c = (Collection) pm.newQuery(Palette.class, "colourOrdinal == 2 && colour == 'BLUE'").execute();
            assertEquals(1, c.size());
            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)

Aggregations

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