Search in sources :

Example 31 with Wardrobe

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

the class JDOQLContainerTest method testQueryUsesContainsKeyOnceOnOneUnboundVariableInverse.

/**
 * Test for the Map.containsValue() method.
 */
public void testQueryUsesContainsKeyOnceOnOneUnboundVariableInverse() {
    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);
            pm.flush();
            assertEquals(2, gym1.getWardrobesInverse2().size());
            assertEquals(2, gym1.getEquipmentsInverse2().size());
            tx.commit();
            tx.begin();
            Collection c = (Collection) pm.newQuery(Gym.class).execute();
            assertEquals(2, c.size());
            Gym g = (Gym) c.iterator().next();
            assertEquals(2, g.getWardrobesInverse2().size());
            assertEquals(2, g.getEquipmentsInverse2().size());
            g = (Gym) c.iterator().next();
            assertEquals(2, g.getWardrobesInverse2().size());
            assertEquals(2, g.getEquipmentsInverse2().size());
            tx.commit();
            tx.begin();
            Query q = pm.newQuery(Gym.class);
            q.setFilter("wardrobesInverse2.containsKey(w1) && w1.model == \"4 door\"");
            q.declareVariables("Wardrobe w1");
            c = (Collection) q.execute();
            assertEquals(1, c.size());
            q = pm.newQuery(Gym.class);
            q.setFilter("wardrobesInverse2.containsKey(w1) && (w1.model == \"2 door\" || w1.model == \"3 door\")");
            q.declareVariables("Wardrobe w1");
            c = (Collection) q.execute();
            assertEquals(1, c.size());
            q = pm.newQuery(Gym.class);
            q.setFilter("wardrobesInverse2.containsKey(w1) && (w1.model == \"4 door\" || w1.model == \"5 door\")");
            q.declareVariables("Wardrobe w1");
            c = (Collection) q.execute();
            assertEquals(1, c.size());
            q = pm.newQuery(Gym.class);
            q.setFilter("equipmentsInverse2.containsKey(e) && g.equipmentsInverse2.containsKey(e) && e.name =='Yoga'");
            q.declareVariables("GymEquipment e; Gym g");
            c = (Collection) q.execute();
            assertEquals(1, c.size());
            assertEquals("Cinema", ((Gym) c.iterator().next()).getName());
            q = pm.newQuery(Gym.class);
            q.setFilter("equipmentsInverse2.containsKey(e) && g.equipmentsInverse2.containsKey(e) && e.name =='Pilates'");
            q.declareVariables("GymEquipment e; Gym g");
            c = (Collection) q.execute();
            assertEquals(1, c.size());
            assertEquals("Shopping", ((Gym) c.iterator().next()).getName());
            q = pm.newQuery(Gym.class);
            q.setFilter("partnersInverse2.containsKey(g) && (g.equipmentsInverse2.containsKey(e) && e.name =='Pilates')");
            q.declareVariables("GymEquipment e; Gym g");
            c = (Collection) q.execute();
            assertEquals(1, c.size());
            assertEquals("Cinema", ((Gym) c.iterator().next()).getName());
            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 32 with Wardrobe

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

the class JDOQLContainerTest method testQueryUsesContainsValueOnceOnOneUnboundVariableImplicitVariablesInverse.

/**
 * Test for the Map.containsValue() method.
 */
public void testQueryUsesContainsValueOnceOnOneUnboundVariableImplicitVariablesInverse() {
    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.getWardrobesInverse().put(w1.getModel(), w1);
        gym1.getWardrobesInverse().put(w2.getModel(), w2);
        Gym gym2 = new Gym();
        gym2.setName("Shopping");
        gym2.setLocation("Second floor");
        gym2.getWardrobesInverse().put(w3.getModel(), w3);
        gym2.getWardrobesInverse().put(w4.getModel(), w4);
        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.getEquipmentsInverse().put(ge1.getName(), ge1);
        gym1.getEquipmentsInverse().put(ge2.getName(), ge2);
        gym2.getEquipmentsInverse().put(ge3.getName(), ge3);
        gym2.getEquipmentsInverse().put(ge4.getName(), ge4);
        gym1.getPartnersInverse().put(gym2.getName(), gym2);
        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("wardrobesInverse.containsValue(w1) && w1.model == \"4 door\"");
            Collection c = (Collection) q.execute();
            assertEquals(1, c.size());
            q = pm.newQuery(Gym.class);
            q.setFilter("wardrobesInverse.containsValue(w1) && (w1.model == \"2 door\" || w1.model == \"4 door\")");
            c = (Collection) q.execute();
            assertEquals(2, c.size());
            q = pm.newQuery(Gym.class);
            q.setFilter("wardrobesInverse.containsValue(w1) && (w1.model == \"4 door\" || w1.model == \"5 door\")");
            c = (Collection) q.execute();
            assertEquals(1, c.size());
            try {
                q = pm.newQuery(Gym.class);
                q.setFilter("equipmentsInverse.containsValue(e) && g.equipmentsInverse.containsValue(e) && e.name =='Yoga'");
                c = (Collection) q.execute();
                fail("expected JDOUserException");
            } catch (JDOUserException e) {
            // expected
            }
            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) JDOUserException(javax.jdo.JDOUserException) GymEquipment(org.jpox.samples.models.fitness.GymEquipment)

Example 33 with Wardrobe

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

the class JDOQLContainerTest method testQueryUsesContainsValueOnceOnOneUnboundVariable.

/**
 * Test for the Map.containsValue() method.
 */
public void testQueryUsesContainsValueOnceOnOneUnboundVariable() {
    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.getWardrobes().put(w1.getModel(), w1);
        gym1.getWardrobes().put(w2.getModel(), w2);
        gym1.getWardrobes().put(w3.getModel(), w3);
        gym1.getWardrobes().put(w4.getModel(), w4);
        Gym gym2 = new Gym();
        gym2.setName("Shopping");
        gym2.setLocation("Second floor");
        gym2.getWardrobes().put(w1.getModel(), w1);
        gym2.getWardrobes().put(w2.getModel(), w2);
        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.getEquipments().put(ge1.getName(), ge1);
        gym1.getEquipments().put(ge2.getName(), ge2);
        gym2.getEquipments().put(ge3.getName(), ge3);
        gym2.getEquipments().put(ge4.getName(), ge4);
        gym1.getPartners().put(gym2.getName(), gym2);
        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("wardrobes.containsValue(w1) && w1.model == \"4 door\"");
            q.declareVariables("Wardrobe w1");
            Collection c = (Collection) q.execute();
            assertEquals(1, c.size());
            q = pm.newQuery(Gym.class);
            q.setFilter("wardrobes.containsValue(w1) && (w1.model == \"2 door\" || w1.model == \"3 door\")");
            q.declareVariables("Wardrobe w1");
            c = (Collection) q.execute();
            assertEquals(2, c.size());
            q = pm.newQuery(Gym.class);
            q.setFilter("wardrobes.containsValue(w1) && (w1.model == \"4 door\" || w1.model == \"5 door\")");
            q.declareVariables("Wardrobe w1");
            c = (Collection) q.execute();
            assertEquals(1, c.size());
            q = pm.newQuery(Gym.class);
            q.setFilter("equipments.containsValue(e) && g.equipments.containsValue(e) && e.name =='Yoga'");
            q.declareVariables("GymEquipment e; Gym g");
            c = (Collection) q.execute();
            assertEquals(1, c.size());
            assertEquals("Cinema", ((Gym) c.iterator().next()).getName());
            q = pm.newQuery(Gym.class);
            q.setFilter("equipments.containsValue(e) && g.equipments.containsValue(e) && e.name =='Pilates'");
            q.declareVariables("GymEquipment e; Gym g");
            c = (Collection) q.execute();
            assertEquals(1, c.size());
            assertEquals("Shopping", ((Gym) c.iterator().next()).getName());
            q = pm.newQuery(Gym.class);
            q.setFilter("partners.containsValue(g) && (g.equipments.containsValue(e) && e.name =='Pilates')");
            q.declareVariables("GymEquipment e; Gym g");
            c = (Collection) q.execute();
            assertEquals(1, c.size());
            assertEquals("Cinema", ((Gym) c.iterator().next()).getName());
            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 34 with Wardrobe

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

the class JDOQLContainerTest method testQueryUsesContainsValueAndEqualsOnceOnOneUnboundVariable.

/**
 * Test for the Map.containsValue() method.
 */
public void testQueryUsesContainsValueAndEqualsOnceOnOneUnboundVariable() {
    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.getWardrobes().put(w1.getModel(), w1);
        gym1.getWardrobes().put(w2.getModel(), w2);
        gym1.getWardrobes().put(w3.getModel(), w3);
        gym1.getWardrobes().put(w4.getModel(), w4);
        Gym gym2 = new Gym();
        gym2.setName("Shopping");
        gym2.setLocation("Second floor");
        gym2.getWardrobes().put(w1.getModel(), w1);
        gym2.getWardrobes().put(w2.getModel(), w2);
        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.getEquipments().put(ge1.getName(), ge1);
        gym1.getEquipments().put(ge2.getName(), ge2);
        gym2.getEquipments().put(ge3.getName(), ge3);
        gym2.getEquipments().put(ge4.getName(), ge4);
        gym1.getPartners().put(gym2.getName(), gym2);
        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("g.partners.containsValue(this) && g.name == 'Cinema'");
            q.declareVariables("Gym g");
            q.setResult("distinct this");
            Collection c = (Collection) q.execute();
            assertEquals(1, c.size());
            assertEquals("Shopping", ((Gym) c.iterator().next()).getName());
            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 35 with Wardrobe

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

the class JDOQLContainerTest method testContainsInListFields.

/**
 * Tests contains() in List fields
 */
public void testContainsInListFields() {
    Object id = null;
    Object idCloth = null;
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Wardrobe wardrobe2 = new Wardrobe();
        wardrobe2.setModel("2 doors");
        Wardrobe wardrobe3 = new Wardrobe();
        wardrobe3.setModel("3 doors");
        pm.makePersistent(wardrobe2);
        pm.makePersistent(wardrobe3);
        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();
        id = JDOHelper.getObjectId(wardrobe3);
        idCloth = JDOHelper.getObjectId(blackShirt);
        tx.begin();
        Query q = pm.newQuery(Wardrobe.class, "this.clothes.contains(c)");
        q.declareParameters("org.jpox.samples.models.fitness.Cloth c");
        Collection c = (Collection) q.execute(skirt);
        assertEquals(1, c.size());
        assertEquals(id, JDOHelper.getObjectId(c.iterator().next()));
        assertEquals(2, wardrobe3.getClothes().size());
        tx.commit();
        tx.begin();
        Query q1 = pm.newQuery(Cloth.class, "wardrobe.clothes.contains(this) && wardrobe.model ==\"2 doors\"");
        q1.declareVariables("org.jpox.samples.models.fitness.Wardrobe wardrobe");
        Collection c1 = (Collection) q1.execute();
        assertEquals(1, c1.size());
        assertEquals(idCloth, JDOHelper.getObjectId(c1.iterator().next()));
        tx.commit();
        tx.begin();
        Query q2 = pm.newQuery(Cloth.class, "wardrobe.clothes.contains(this)");
        q2.declareParameters("org.jpox.samples.models.fitness.Wardrobe wardrobe");
        Collection c2 = (Collection) q2.execute(wardrobe2);
        assertEquals(1, c2.size());
        assertEquals(idCloth, JDOHelper.getObjectId(c2.iterator().next()));
        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) Cloth(org.jpox.samples.models.fitness.Cloth) Collection(java.util.Collection) Wardrobe(org.jpox.samples.models.fitness.Wardrobe)

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