Search in sources :

Example 1 with Wardrobe

use of org.jpox.samples.models.fitness.Wardrobe in project tests by datanucleus.

the class JDOQLContainerTest method testGetInOrderingInMapFields.

/**
 * Tests get method used in ordering
 */
public void testGetInOrderingInMapFields() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // Create some data
            tx.begin();
            Gym gym = new Gym();
            gym.setLocation("downtown");
            Gym gym2 = new Gym();
            gym2.setLocation("village");
            Wardrobe wardrobe1 = new Wardrobe();
            wardrobe1.setModel("1 door");
            Wardrobe wardrobe2 = new Wardrobe();
            wardrobe2.setModel("2 doors");
            Wardrobe wardrobe3 = new Wardrobe();
            wardrobe3.setModel("3 doors");
            gym.getWardrobes().put(wardrobe2.getModel(), wardrobe2);
            gym.getWardrobes().put(wardrobe3.getModel(), wardrobe3);
            gym2.getWardrobes().put(wardrobe1.getModel(), wardrobe1);
            pm.makePersistent(gym);
            pm.makePersistent(gym2);
            Cloth whiteShirt = new Cloth();
            whiteShirt.setName("white shirt");
            Cloth blackShirt = new Cloth();
            blackShirt.setName("black shirt");
            Cloth skirt = new Cloth();
            skirt.setName("skirt");
            wardrobe3.getClothes().add(whiteShirt);
            wardrobe3.getClothes().add(skirt);
            wardrobe2.getClothes().add(blackShirt);
            tx.commit();
            // Query the data
            tx.begin();
            Query q = pm.newQuery("SELECT FROM org.jpox.samples.models.fitness.Gym " + "PARAMETERS org.jpox.samples.models.fitness.Wardrobe wrd");
            q.setOrdering("this.wardrobes.get(wrd.model).model ascending");
            List results = (List) q.execute(wardrobe3);
            assertEquals(2, results.size());
            assertEquals("downtown", ((Gym) results.get(0)).getLocation());
            assertEquals("village", ((Gym) results.get(1)).getLocation());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown during query", e);
            fail("Exception thrown while performing test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        FitnessHelper.cleanFitnessData(pmf);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Cloth(org.jpox.samples.models.fitness.Cloth) Gym(org.jpox.samples.models.fitness.Gym) List(java.util.List) Wardrobe(org.jpox.samples.models.fitness.Wardrobe)

Example 2 with Wardrobe

use of org.jpox.samples.models.fitness.Wardrobe in project tests by datanucleus.

the class JDOQLContainerTest method testQueryUsesContainsKeyTwiceOnFieldUsingWorkaroundInverse.

/**
 * test query with "field.containsKey(x) && field.containsKey(y)" using "or"
 * workaround. Use the workaround to bypass a deficiency on query generation
 */
public void testQueryUsesContainsKeyTwiceOnFieldUsingWorkaroundInverse() {
    try {
        Gym gym1 = new Gym();
        gym1.setName("Cinema");
        gym1.setLocation("First floor");
        Wardrobe w1 = new Wardrobe();
        Wardrobe w2 = new Wardrobe();
        Wardrobe w3 = new Wardrobe();
        Wardrobe w4 = new Wardrobe();
        w1.setModel("2 door");
        w2.setModel("3 door");
        w3.setModel("4 door");
        w4.setModel("5 door");
        gym1.getWardrobesInverse2().put(w1, w1.getModel());
        gym1.getWardrobesInverse2().put(w2, w2.getModel());
        Gym gym2 = new Gym();
        gym2.setName("Shopping");
        gym2.setLocation("Second floor");
        gym2.getWardrobesInverse2().put(w3, w3.getModel());
        gym2.getWardrobesInverse2().put(w4, w4.getModel());
        Cloth c1 = new Cloth();
        c1.setName("green shirt");
        Cloth c2 = new Cloth();
        c2.setName("red shirt");
        Cloth c3 = new Cloth();
        c3.setName("blue shirt");
        GymEquipment ge1 = new GymEquipment();
        ge1.setName("Weight");
        GymEquipment ge2 = new GymEquipment();
        ge2.setName("Yoga");
        GymEquipment ge3 = new GymEquipment();
        ge3.setName("Pilates");
        GymEquipment ge4 = new GymEquipment();
        ge4.setName("Abdominal");
        gym1.getEquipmentsInverse2().put(ge1, ge1.getName());
        gym1.getEquipmentsInverse2().put(ge2, ge2.getName());
        gym2.getEquipmentsInverse2().put(ge3, ge3.getName());
        gym2.getEquipmentsInverse2().put(ge4, ge4.getName());
        gym1.getPartnersInverse2().put(gym2, gym2.getName());
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            pm.makePersistent(gym1);
            pm.makePersistent(gym2);
            tx.commit();
            tx.begin();
            Query q = pm.newQuery(Gym.class);
            q.setFilter("wardrobesInverse2.containsKey(w1) && wardrobesInverse2.containsKey(w2) && (w1.model == \"2 door\" || w2.model == \"3 door\")");
            q.declareVariables("Wardrobe w1; Wardrobe w2");
            Collection c = (Collection) q.execute();
            assertEquals(2, c.size());
            q = pm.newQuery(Gym.class);
            q.setFilter("wardrobesInverse2.containsKey(w1) && wardrobesInverse2.containsKey(w2) && (w1.model == \"4 door\" || w2.model == \"5 door\")");
            q.declareVariables("Wardrobe w1; Wardrobe w2");
            c = (Collection) q.execute();
            assertEquals(1, c.size());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        FitnessHelper.cleanFitnessData(pmf);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Cloth(org.jpox.samples.models.fitness.Cloth) Collection(java.util.Collection) Gym(org.jpox.samples.models.fitness.Gym) Wardrobe(org.jpox.samples.models.fitness.Wardrobe) GymEquipment(org.jpox.samples.models.fitness.GymEquipment)

Example 3 with Wardrobe

use of org.jpox.samples.models.fitness.Wardrobe in project tests by datanucleus.

the class JDOQLContainerTest method testMapGetAsLiteralWithKeyAsExpression.

/**
 * Tests the map get method with a map as literal (parameter) and
 * the key value is obtained from an expression (from the database)
 */
public void testMapGetAsLiteralWithKeyAsExpression() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Gym gym = new Gym();
        gym.setLocation("downtown");
        Gym gym2 = new Gym();
        gym2.setLocation("village");
        Wardrobe wardrobe2 = new Wardrobe();
        wardrobe2.setModel("2 doors");
        Wardrobe wardrobe3 = new Wardrobe();
        wardrobe3.setModel("3 doors");
        Wardrobe wardrobe4 = new Wardrobe();
        wardrobe4.setModel("4 doors");
        gym.getWardrobes().put(wardrobe2.getModel(), wardrobe2);
        gym.getWardrobes().put(wardrobe3.getModel(), wardrobe3);
        gym.getWardrobes().put(wardrobe4.getModel(), wardrobe4);
        gym2.getWardrobes().put(wardrobe4.getModel(), wardrobe4);
        pm.makePersistent(gym);
        pm.makePersistent(gym2);
        Cloth whiteShirt = new Cloth();
        whiteShirt.setName("white shirt");
        Cloth blackShirt = new Cloth();
        blackShirt.setName("black shirt");
        Cloth skirt = new Cloth();
        skirt.setName("skirt");
        wardrobe3.getClothes().add(whiteShirt);
        wardrobe3.getClothes().add(skirt);
        wardrobe2.getClothes().add(blackShirt);
        tx.commit();
        tx.begin();
        // test map.get in map literals
        Map map1 = new HashMap();
        map1.put("2 doors", wardrobe2);
        Query q = pm.newQuery(Wardrobe.class, "this == map.get(this.model)");
        q.declareParameters("java.util.Map map");
        Collection c = (Collection) q.execute(map1);
        assertEquals(1, c.size());
        Map map2 = new HashMap();
        q = pm.newQuery(Wardrobe.class, "this == map.get(this.model)");
        q.declareParameters("java.util.Map map");
        c = (Collection) q.execute(map2);
        assertEquals(0, c.size());
        Map map3 = new HashMap();
        map3.put("2 doors", wardrobe2);
        map3.put("3 doors", wardrobe3);
        q = pm.newQuery(Wardrobe.class, "this == map.get(this.model)");
        q.declareParameters("java.util.Map map");
        c = (Collection) q.execute(map3);
        assertEquals(2, c.size());
        Map map4 = new HashMap();
        map4.put("5 doors", wardrobe2);
        q = pm.newQuery(Wardrobe.class, "this == map.get(this.model)");
        q.declareParameters("java.util.Map map");
        c = (Collection) q.execute(map3);
        assertEquals(0, c.size());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        // Clean out our data
        FitnessHelper.cleanFitnessData(pmf);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) HashMap(java.util.HashMap) Cloth(org.jpox.samples.models.fitness.Cloth) Collection(java.util.Collection) Gym(org.jpox.samples.models.fitness.Gym) Wardrobe(org.jpox.samples.models.fitness.Wardrobe) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with Wardrobe

use of org.jpox.samples.models.fitness.Wardrobe in project tests by datanucleus.

the class AttachDetachTest method testAggregatedDetachAttachFieldMap.

/**
 * test pc objects aggregating other pcs. associations 1-n with default fetch group
 */
public void testAggregatedDetachAttachFieldMap() {
    try {
        // Create a Gym with 3 Wardrobes
        Gym testGym = new Gym();
        Map<String, Wardrobe> wardrobes = new HashMap<>();
        Wardrobe wSmall = new Wardrobe();
        wSmall.setModel("small");
        Wardrobe wMedium = new Wardrobe();
        wMedium.setModel("medium");
        Wardrobe wLarge = new Wardrobe();
        wMedium.setModel("large");
        wardrobes.put("small", wSmall);
        wardrobes.put("medium", wMedium);
        wardrobes.put("large", wLarge);
        testGym.setWardrobes(wardrobes);
        PersistenceManager pm = newPM();
        Transaction tx = pm.currentTransaction();
        Gym detachedGym;
        Gym detachedGym2;
        Gym detachedGym3;
        Object gymID = null;
        try {
            // Persist the objects and detach them all
            tx.begin();
            pm.makePersistent(testGym);
            pm.getFetchPlan().clearGroups();
            pm.getFetchPlan().setGroup("Gym.wardrobes");
            detachedGym = (Gym) pm.detachCopy(testGym);
            tx.commit();
            assertEquals("1) Gym.wardrobes.size()", 3, detachedGym.getWardrobes().size());
            gymID = pm.getObjectId(detachedGym);
            // Attach the (unchanged) objects, and detach just the Gym since we're only updating that
            tx.begin();
            pm.makePersistent(detachedGym);
            pm.getFetchPlan().clearGroups();
            pm.getFetchPlan().setGroup(FetchPlan.DEFAULT);
            detachedGym2 = (Gym) pm.detachCopy(pm.getObjectById(gymID));
            tx.commit();
            try {
                Map testMap = detachedGym2.getWardrobes();
                assertEquals("X) Gym.wardrobes.size() == 3", testMap.size(), 3);
            } catch (JDODetachedFieldAccessException e) {
                fail("Field 'Gym.wardrobes' should have been detached since was loaded at detach!");
            }
            // Update a field in Gym
            detachedGym2.setLocation("Freiburg");
            // Attach the objects, and detach them once more
            tx.begin();
            pm.makePersistent(detachedGym2);
            pm.getFetchPlan().clearGroups();
            pm.getFetchPlan().setGroup("Gym.wardrobes");
            detachedGym3 = (Gym) pm.detachCopy(pm.getObjectById(gymID));
            tx.commit();
            assertEquals("2) Gym.wardrobes.size()", 3, detachedGym3.getWardrobes().size());
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail(e.toString());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // again with another pm
        pm = newPM();
        tx = pm.currentTransaction();
        try {
            if (gymID != null) {
                tx.begin();
                pm.getFetchPlan().clearGroups();
                pm.getFetchPlan().setGroup(FetchPlan.DEFAULT);
                detachedGym = (Gym) pm.detachCopy(pm.getObjectById(gymID));
                tx.commit();
                detachedGym.setLocation("Basel");
                tx.begin();
                pm.makePersistent(detachedGym);
                tx.commit();
                tx.begin();
                pm.getFetchPlan().clearGroups();
                pm.getFetchPlan().setGroup("Gym.wardrobes");
                detachedGym2 = (Gym) pm.detachCopy(pm.getObjectById(gymID));
                tx.commit();
                assertEquals("3) Gym.wardrobes.size()", 3, detachedGym2.getWardrobes().size());
            }
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail(e.toString());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        FitnessHelper.cleanFitnessData(pmf);
    }
}
Also used : JDODetachedFieldAccessException(javax.jdo.JDODetachedFieldAccessException) Transaction(javax.jdo.Transaction) HashMap(java.util.HashMap) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Gym(org.jpox.samples.models.fitness.Gym) Wardrobe(org.jpox.samples.models.fitness.Wardrobe) Map(java.util.Map) HashMap(java.util.HashMap) JDODetachedFieldAccessException(javax.jdo.JDODetachedFieldAccessException) JDOUserException(javax.jdo.JDOUserException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 5 with Wardrobe

use of org.jpox.samples.models.fitness.Wardrobe in project tests by datanucleus.

the class JDOQLContainerTest method testNotContainsKeysInMapFields.

/**
 * Tests NOT contains in Map.keys
 */
public void testNotContainsKeysInMapFields() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // Create some data
            tx.begin();
            Gym gym = new Gym();
            gym.setLocation("downtown");
            Gym gym2 = new Gym();
            gym2.setLocation("village");
            Wardrobe wardrobe1 = new Wardrobe();
            wardrobe1.setModel("1 door");
            Wardrobe wardrobe2 = new Wardrobe();
            wardrobe2.setModel("2 doors");
            Wardrobe wardrobe3 = new Wardrobe();
            wardrobe3.setModel("3 doors");
            gym.getWardrobes2().put(wardrobe2, wardrobe2.getModel());
            gym.getWardrobes2().put(wardrobe3, wardrobe3.getModel());
            gym2.getWardrobes2().put(wardrobe1, wardrobe1.getModel());
            pm.makePersistent(gym);
            pm.makePersistent(gym2);
            Cloth whiteShirt = new Cloth();
            whiteShirt.setName("white shirt");
            Cloth blackShirt = new Cloth();
            blackShirt.setName("black shirt");
            Cloth skirt = new Cloth();
            skirt.setName("skirt");
            wardrobe3.getClothes().add(whiteShirt);
            wardrobe3.getClothes().add(skirt);
            wardrobe2.getClothes().add(blackShirt);
            tx.commit();
            // Query the data
            tx.begin();
            Query q = pm.newQuery("SELECT FROM org.jpox.samples.models.fitness.Gym " + "WHERE !this.wardrobes2.containsKey(wrd) " + "PARAMETERS org.jpox.samples.models.fitness.Wardrobe wrd");
            List results = (List) q.execute(wardrobe3);
            assertEquals(1, results.size());
            assertEquals("village", ((Gym) results.get(0)).getLocation());
            // two !contains
            q = pm.newQuery("SELECT FROM org.jpox.samples.models.fitness.Gym " + "WHERE !this.wardrobes2.containsKey(wrd) && !this.wardrobes2.containsKey(wrd2) " + "PARAMETERS org.jpox.samples.models.fitness.Wardrobe wrd,org.jpox.samples.models.fitness.Wardrobe wrd2");
            results = (List) q.execute(wardrobe3, wardrobe2);
            assertEquals(1, results.size());
            assertEquals("village", ((Gym) results.get(0)).getLocation());
            // two !contains and one contains
            q = pm.newQuery("SELECT FROM org.jpox.samples.models.fitness.Gym " + "WHERE !this.wardrobes2.containsKey(wrd) && !this.wardrobes2.containsKey(wrd2) && this.wardrobes2.containsKey(wrd1) " + "PARAMETERS org.jpox.samples.models.fitness.Wardrobe wrd,org.jpox.samples.models.fitness.Wardrobe wrd2,org.jpox.samples.models.fitness.Wardrobe wrd1");
            results = (List) q.execute(wardrobe3, wardrobe2, wardrobe1);
            assertEquals(1, results.size());
            assertEquals("village", ((Gym) results.get(0)).getLocation());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown during test", e);
            fail("Exception thrown while performing test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        FitnessHelper.cleanFitnessData(pmf);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Cloth(org.jpox.samples.models.fitness.Cloth) Gym(org.jpox.samples.models.fitness.Gym) List(java.util.List) Wardrobe(org.jpox.samples.models.fitness.Wardrobe) JDOUserException(javax.jdo.JDOUserException)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)42 Transaction (javax.jdo.Transaction)42 Wardrobe (org.jpox.samples.models.fitness.Wardrobe)42 Query (javax.jdo.Query)40 Gym (org.jpox.samples.models.fitness.Gym)39 Cloth (org.jpox.samples.models.fitness.Cloth)36 Collection (java.util.Collection)32 GymEquipment (org.jpox.samples.models.fitness.GymEquipment)25 JDOUserException (javax.jdo.JDOUserException)14 List (java.util.List)8 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Iterator (java.util.Iterator)1 JDODetachedFieldAccessException (javax.jdo.JDODetachedFieldAccessException)1 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)1 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)1