Search in sources :

Example 6 with GymEquipment

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

the class JDOQLContainerTest method testQueryUsesContainsValueOnceOnOneUnboundVariableUsingParametersInverse.

/**
 * test query with "field.containsValue(x)" using a workaround
 */
public void testQueryUsesContainsValueOnceOnOneUnboundVariableUsingParametersInverse() {
    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);
    gym1.getWardrobesInverse().put(w3.getModel(), w3);
    gym1.getWardrobesInverse().put(w4.getModel(), w4);
    Gym gym2 = new Gym();
    gym2.setName("Shopping");
    gym2.setLocation("Second floor");
    gym2.getWardrobesInverse().put(w1.getModel(), w1);
    gym2.getWardrobesInverse().put(w2.getModel(), w2);
    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(GymEquipment.class);
        q.setFilter("name == 'Pilates'");
        q.setUnique(true);
        GymEquipment ge = (GymEquipment) q.execute();
        q = pm.newQuery(Gym.class);
        q.setFilter("name == 'Shopping'");
        q.setUnique(true);
        Gym g = (Gym) q.execute();
        // MAKE sure field is loaded, becase the query will use it
        g.getEquipmentsInverse();
        q = pm.newQuery(Gym.class);
        q.setFilter("equipmentsInverse.containsValue(ge) && g.equipmentsInverse.containsValue(ge)");
        q.declareParameters("GymEquipment ge, Gym g");
        Collection c = (Collection) q.execute(ge, g);
        assertEquals(1, 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) 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 7 with GymEquipment

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

the class JDOQLContainerTest method testQueryUsesContainsValueTwiceOnFieldUsingWorkaround.

/**
 * test query with "field.containsValue(x) && field.containsValue(y)" using "or"
 * workaround. Use the workaround to bypass a deficiency on query generation
 */
public void testQueryUsesContainsValueTwiceOnFieldUsingWorkaround() {
    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) && wardrobes.containsValue(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("wardrobes.containsValue(w1) && wardrobes.containsValue(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 8 with GymEquipment

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

the class JDOQLContainerTest method testQueryUsesContainsKeyOnceOnOneUnboundVariableUsingParametersInverse.

/**
 * test query with "field.containsKey(x)" using a workaround
 */
public void testQueryUsesContainsKeyOnceOnOneUnboundVariableUsingParametersInverse() {
    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(GymEquipment.class);
        q.setFilter("name == 'Pilates'");
        q.setUnique(true);
        GymEquipment ge = (GymEquipment) q.execute();
        q = pm.newQuery(Gym.class);
        q.setFilter("name == 'Shopping'");
        q.setUnique(true);
        Gym g = (Gym) q.execute();
        // MAKE sure field is loaded, becase the query will use it
        g.getEquipmentsInverse2();
        q = pm.newQuery(Gym.class);
        q.setFilter("equipmentsInverse2.containsKey(ge) && g.equipmentsInverse2.containsKey(ge)");
        q.declareParameters("GymEquipment ge, Gym g");
        Collection c = (Collection) q.execute(ge, g);
        assertEquals(1, 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) 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 9 with GymEquipment

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

the class JDOQLContainerTest method testQueryUsesContainsKeyTwiceOnFieldWithNamespace.

/**
 * test query with "field.containsKey(x) && field.containsKey(y)"
 *
 * namespace put related expressions inside parentheses
 */
public void testQueryUsesContainsKeyTwiceOnFieldWithNamespace() {
    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.getWardrobes2().put(w1, w1.getModel());
        gym1.getWardrobes2().put(w2, w2.getModel());
        gym1.getWardrobes2().put(w3, w3.getModel());
        gym1.getWardrobes2().put(w4, w4.getModel());
        Gym gym2 = new Gym();
        gym2.setName("Shopping");
        gym2.setLocation("Second floor");
        gym2.getWardrobes2().put(w1, w1.getModel());
        gym2.getWardrobes2().put(w2, w2.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.getEquipments2().put(ge1, ge1.getName());
        gym1.getEquipments2().put(ge2, ge2.getName());
        gym2.getEquipments2().put(ge3, ge3.getName());
        gym2.getEquipments2().put(ge4, ge4.getName());
        gym1.getPartners2().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("(wardrobes2.containsKey(w1) && w1.model == \"4 door\") && (wardrobes2.containsKey(w2) && w2.model == \"3 door\")");
            q.declareVariables("Wardrobe w1; Wardrobe w2");
            Collection c = (Collection) q.execute();
            assertEquals(1, c.size());
            q = pm.newQuery(Gym.class);
            q.setFilter("(wardrobes2.containsKey(w1) && w1.model == \"2 door\") && (wardrobes2.containsKey(w2) && w2.model == \"3 door\")");
            q.declareVariables("Wardrobe w1; Wardrobe w2");
            c = (Collection) q.execute();
            assertEquals(2, c.size());
            q = pm.newQuery(Gym.class);
            q.setFilter("(wardrobes2.containsKey(w1) && w1.model == \"2 door\") || (wardrobes2.containsKey(w2) && w2.model == \"5 door\")");
            q.declareVariables("Wardrobe w1; Wardrobe w2");
            c = (Collection) q.execute();
            assertEquals(2, 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 10 with GymEquipment

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

the class JDOQLContainerTest method testQueryUsesContainsValueOnceOnOneUnboundVariableUsingParameters.

/**
 * test query with "field.containsValue(x)" using a workaround
 */
public void testQueryUsesContainsValueOnceOnOneUnboundVariableUsingParameters() {
    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(GymEquipment.class);
        q.setFilter("name == 'Pilates'");
        q.setUnique(true);
        GymEquipment ge = (GymEquipment) q.execute();
        q = pm.newQuery(Gym.class);
        q.setFilter("name == 'Shopping'");
        q.setUnique(true);
        Gym g = (Gym) q.execute();
        // MAKE sure field is loaded, becase the query will use it
        g.getEquipments();
        q = pm.newQuery(Gym.class);
        q.setFilter("equipments.containsValue(ge) && g.equipments.containsValue(ge)");
        q.declareParameters("GymEquipment ge, Gym g");
        Collection c = (Collection) q.execute(ge, g);
        assertEquals(1, 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) 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)

Aggregations

Collection (java.util.Collection)25 PersistenceManager (javax.jdo.PersistenceManager)25 Query (javax.jdo.Query)25 Transaction (javax.jdo.Transaction)25 Gym (org.jpox.samples.models.fitness.Gym)25 GymEquipment (org.jpox.samples.models.fitness.GymEquipment)25 Wardrobe (org.jpox.samples.models.fitness.Wardrobe)25 Cloth (org.jpox.samples.models.fitness.Cloth)23 JDOUserException (javax.jdo.JDOUserException)4