Search in sources :

Example 1 with NoviceBeerConsumer

use of org.eclipse.persistence.testing.models.jpa.inherited.NoviceBeerConsumer in project eclipselink by eclipse-ee4j.

the class InheritedModelJunitTest method testOrderColumnNoviceBeerConsumerDesignations.

public void testOrderColumnNoviceBeerConsumerDesignations() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    NoviceBeerConsumer beerConsumer;
    try {
        // These are the designations we have right now ...
        // index: 0 - 1 - 2 - 3 - 4
        // item: 5 - 4 - 2 - 3 - 1
        beerConsumer = em.find(NoviceBeerConsumer.class, m_noviceBeerConsumerId);
        // String stringFour = beerConsumer.getDesignations().remove(1);
        // String stringTwo = beerConsumer.getDesignations().remove(1);
        // beerConsumer.getDesignations().add(stringTwo);
        // This is what we have done
        // index: 0 - 1 - 2 - 3
        // item: 5 - 3 - 1 - 2
        commitTransaction(em);
    } catch (RuntimeException e) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        throw e;
    }
    closeEntityManager(em);
    clearCache();
    em = createEntityManager();
    BeerConsumer refreshedBC = em.find(BeerConsumer.class, m_noviceBeerConsumerId);
    assertTrue("The novice beer consumer read back did not match the original", getServerSession().compareObjects(beerConsumer, refreshedBC));
}
Also used : EntityManager(jakarta.persistence.EntityManager) NoviceBeerConsumer(org.eclipse.persistence.testing.models.jpa.inherited.NoviceBeerConsumer) ExpertBeerConsumer(org.eclipse.persistence.testing.models.jpa.inherited.ExpertBeerConsumer) BeerConsumer(org.eclipse.persistence.testing.models.jpa.inherited.BeerConsumer) NoviceBeerConsumer(org.eclipse.persistence.testing.models.jpa.inherited.NoviceBeerConsumer)

Example 2 with NoviceBeerConsumer

use of org.eclipse.persistence.testing.models.jpa.inherited.NoviceBeerConsumer in project eclipselink by eclipse-ee4j.

the class InheritedModelJunitTest method testBreakOrder_CorrectionType_EXCEPTION.

public void testBreakOrder_CorrectionType_EXCEPTION() {
    // create BeerConsumer with designations
    EntityManager em = createEntityManager();
    beginTransaction(em);
    NoviceBeerConsumer beerConsumer = new NoviceBeerConsumer();
    beerConsumer.setAccredidation(new Accredidation());
    beerConsumer.setName("Broken order");
    beerConsumer.getDesignations().add("0");
    beerConsumer.getDesignations().add("1");
    beerConsumer.getDesignations().add("2");
    try {
        em.persist(beerConsumer);
        commitTransaction(em);
    } catch (RuntimeException e) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        fail("failed to create beerConsumer");
    }
    Integer id = beerConsumer.getId();
    try {
        // break the order of designations
        beginTransaction(em);
        try {
            em.createNativeQuery("UPDATE NOVICE_CONSUMER_DESIGNATIONS SET ORDER_COLUMN = null WHERE NOVICE_CONSUMER_ID = " + id + " AND ORDER_COLUMN = 0").executeUpdate();
            commitTransaction(em);
        } catch (RuntimeException e) {
            if (isTransactionActive(em)) {
                rollbackTransaction(em);
            }
            fail("failed to break order of designations");
        } finally {
            closeEntityManager(em);
        }
        // remove beerConsumer from the cache
        clearCache();
        // read back the beer consumer, reading of the list of designations with the broken order should trigger exception
        em = createEntityManager();
        try {
            beerConsumer = em.find(NoviceBeerConsumer.class, id);
            // trigger indirection
            beerConsumer.getDesignations().size();
            fail("Exception was expected - but not thrown");
        } catch (RuntimeException e) {
            boolean isCorrectException = false;
            if (e instanceof QueryException) {
                QueryException queryException = (QueryException) e;
                if (((QueryException) e).getErrorCode() == QueryException.LIST_ORDER_FIELD_WRONG_VALUE) {
                    isCorrectException = true;
                }
            }
            if (!isCorrectException) {
                throw e;
            }
        } finally {
            closeEntityManager(em);
        }
    } finally {
        // clean up
        em = createEntityManager();
        beginTransaction(em);
        try {
            beerConsumer = em.find(NoviceBeerConsumer.class, id);
            em.remove(beerConsumer);
            commitTransaction(em);
        } catch (RuntimeException e) {
            if (isTransactionActive(em)) {
                rollbackTransaction(em);
            }
        } finally {
            closeEntityManager(em);
        }
    }
}
Also used : BigInteger(java.math.BigInteger) EntityManager(jakarta.persistence.EntityManager) QueryException(org.eclipse.persistence.exceptions.QueryException) NoviceBeerConsumer(org.eclipse.persistence.testing.models.jpa.inherited.NoviceBeerConsumer) Accredidation(org.eclipse.persistence.testing.models.jpa.inherited.Accredidation)

Example 3 with NoviceBeerConsumer

use of org.eclipse.persistence.testing.models.jpa.inherited.NoviceBeerConsumer in project eclipselink by eclipse-ee4j.

the class InheritedModelJunitTest method testRedStripeNoviceConsumer.

public void testRedStripeNoviceConsumer() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    NoviceBeerConsumer initialNBC = new NoviceBeerConsumer();
    initialNBC.setAccredidation(new Accredidation());
    int beerConsumerId = 0;
    try {
        RedStripe redStripe1 = new RedStripe();
        redStripe1.setAlcoholContent(5.0);
        initialNBC.addRedStripeBeersToConsume(redStripe1, "3");
        RedStripe redStripe2 = new RedStripe();
        redStripe2.setAlcoholContent(5.0);
        initialNBC.addRedStripeBeersToConsume(redStripe2, "4");
        initialNBC.setName("Novice Red Stripe Consumer");
        em.persist(initialNBC);
        beerConsumerId = initialNBC.getId();
        commitTransaction(em);
    } catch (RuntimeException e) {
        e.printStackTrace();
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        fail("An exception was caught during create operation: [" + e.getMessage() + "]");
    }
    closeEntityManager(em);
    clearCache();
    em = createEntityManager();
    NoviceBeerConsumer refreshedNBC = em.find(NoviceBeerConsumer.class, beerConsumerId);
    assertTrue("The novice beer consumer read back did not match the original", getServerSession().compareObjects(initialNBC, refreshedNBC));
}
Also used : EntityManager(jakarta.persistence.EntityManager) NoviceBeerConsumer(org.eclipse.persistence.testing.models.jpa.inherited.NoviceBeerConsumer) RedStripe(org.eclipse.persistence.testing.models.jpa.inherited.RedStripe) Accredidation(org.eclipse.persistence.testing.models.jpa.inherited.Accredidation)

Example 4 with NoviceBeerConsumer

use of org.eclipse.persistence.testing.models.jpa.inherited.NoviceBeerConsumer in project eclipselink by eclipse-ee4j.

the class InheritedModelJunitTest method testReadNoviceBeerConsumer.

public void testReadNoviceBeerConsumer() {
    NoviceBeerConsumer consumer = createEntityManager().find(NoviceBeerConsumer.class, m_noviceBeerConsumerId);
    assertTrue("Error on reading back a NoviceBeerConsumer", consumer != null);
    assertTrue("IQ Level was not persisted.", consumer.getIQ() == 100);
    assertTrue("Incorrect number of acclaims returned.", consumer.getAcclaims().size() == 3);
    assertTrue("Missing acclaim - 1", consumer.getAcclaims().contains(1));
    assertTrue("Missing acclaim - 2", consumer.getAcclaims().contains(2));
    assertTrue("Missing acclaim - 3", consumer.getAcclaims().contains(3));
    assertTrue("Incorrect number of awards returned.", consumer.getAwards().size() == 3);
    Integer awardCode = consumer.getAwards().get(1);
    assertFalse("Missing award code - 1", awardCode == null);
    assertTrue("Award code 1 is incorrect", awardCode.equals(1));
    awardCode = consumer.getAwards().get(2);
    assertFalse("Missing award code - 2", awardCode == null);
    assertTrue("Award code 2 is incorrect", awardCode.equals(2));
    awardCode = consumer.getAwards().get(3);
    assertFalse("Missing award code - 3", awardCode == null);
    assertTrue("Award code 3 is incorrect", awardCode.equals(3));
    assertTrue("Incorrect number of designations returned.", consumer.getDesignations().size() == 5);
    assertTrue("Missing designation - 5 at index 0", consumer.getDesignations().get(0).equals("5"));
    assertTrue("Missing designation - 4 at index 1", consumer.getDesignations().get(1).equals("4"));
    assertTrue("Missing designation - 2 at index 2", consumer.getDesignations().get(2).equals("2"));
    assertTrue("Missing designation - 3 at index 3", consumer.getDesignations().get(3).equals("3"));
    assertTrue("Missing designation - 1 at index 4", consumer.getDesignations().get(4).equals("1"));
    assertTrue("Incorrect number of records returned.", consumer.getRecords().size() == 1);
}
Also used : BigInteger(java.math.BigInteger) NoviceBeerConsumer(org.eclipse.persistence.testing.models.jpa.inherited.NoviceBeerConsumer)

Example 5 with NoviceBeerConsumer

use of org.eclipse.persistence.testing.models.jpa.inherited.NoviceBeerConsumer in project eclipselink by eclipse-ee4j.

the class InheritedModelJunitTest method testCreateNoviceBeerConsumer.

public void testCreateNoviceBeerConsumer() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    NoviceBeerConsumer beerConsumer = new NoviceBeerConsumer();
    try {
        beerConsumer.setName("Novice Beer Consumer");
        beerConsumer.setIQ(100);
        beerConsumer.getAcclaims().add(1);
        beerConsumer.getAcclaims().add(2);
        beerConsumer.getAcclaims().add(3);
        beerConsumer.getAwards().put(1, 1);
        beerConsumer.getAwards().put(2, 2);
        beerConsumer.getAwards().put(3, 3);
        beerConsumer.getDesignations().add("5");
        beerConsumer.getDesignations().add("4");
        beerConsumer.getDesignations().add("2");
        beerConsumer.getDesignations().add("3");
        beerConsumer.getDesignations().add("1");
        Record record1 = new Record();
        record1.setDescription("Slowest beer ever consumed - 10 hours");
        record1.setDate(Helper.dateFromYearMonthDate(2008, 1, 1));
        record1.setLocation(new Location("Paris", "France"));
        Venue venue1 = new Venue();
        venue1.setAttendance(10);
        venue1.setName("Champs-Elysees");
        record1.setVenue(venue1);
        beerConsumer.getRecords().add(record1);
        Accredidation accredidation = new Accredidation();
        accredidation.setDetails("Superb, just superb!");
        Witness witness1 = new Witness();
        witness1.setName("Mickey Blue Eyes");
        accredidation.addWitness(witness1);
        Witness witness2 = new Witness();
        witness2.setName("Donny Trafalgo");
        accredidation.addWitness(witness2);
        beerConsumer.setAccredidation(accredidation);
        Committee committee1 = new Committee();
        committee1.setDescription("Moral labelling");
        beerConsumer.addCommittee(committee1);
        Committee committee2 = new Committee();
        committee2.setDescription("Crimes against beer");
        beerConsumer.addCommittee(committee2);
        Committee committee3 = new Committee();
        committee3.setDescription("BADD - Beers against drunk dorks");
        beerConsumer.addCommittee(committee3);
        em.persist(beerConsumer);
        m_noviceBeerConsumerId = beerConsumer.getId();
        commitTransaction(em);
    } catch (RuntimeException e) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        fail("An exception was caught during create operation for a novice beer consumer: [" + e.getMessage() + "]");
    }
    closeEntityManager(em);
    clearCache();
    em = createEntityManager();
    BeerConsumer refreshedBC = em.find(BeerConsumer.class, m_noviceBeerConsumerId);
    assertTrue("The novice beer consumer read back did not match the original", getServerSession().compareObjects(beerConsumer, refreshedBC));
}
Also used : EntityManager(jakarta.persistence.EntityManager) NoviceBeerConsumer(org.eclipse.persistence.testing.models.jpa.inherited.NoviceBeerConsumer) Venue(org.eclipse.persistence.testing.models.jpa.inherited.Venue) Accredidation(org.eclipse.persistence.testing.models.jpa.inherited.Accredidation) Witness(org.eclipse.persistence.testing.models.jpa.inherited.Witness) Record(org.eclipse.persistence.testing.models.jpa.inherited.Record) ExpertBeerConsumer(org.eclipse.persistence.testing.models.jpa.inherited.ExpertBeerConsumer) BeerConsumer(org.eclipse.persistence.testing.models.jpa.inherited.BeerConsumer) NoviceBeerConsumer(org.eclipse.persistence.testing.models.jpa.inherited.NoviceBeerConsumer) Committee(org.eclipse.persistence.testing.models.jpa.inherited.Committee) Location(org.eclipse.persistence.testing.models.jpa.inherited.Location)

Aggregations

NoviceBeerConsumer (org.eclipse.persistence.testing.models.jpa.inherited.NoviceBeerConsumer)5 EntityManager (jakarta.persistence.EntityManager)4 Accredidation (org.eclipse.persistence.testing.models.jpa.inherited.Accredidation)3 BigInteger (java.math.BigInteger)2 BeerConsumer (org.eclipse.persistence.testing.models.jpa.inherited.BeerConsumer)2 ExpertBeerConsumer (org.eclipse.persistence.testing.models.jpa.inherited.ExpertBeerConsumer)2 QueryException (org.eclipse.persistence.exceptions.QueryException)1 Committee (org.eclipse.persistence.testing.models.jpa.inherited.Committee)1 Location (org.eclipse.persistence.testing.models.jpa.inherited.Location)1 Record (org.eclipse.persistence.testing.models.jpa.inherited.Record)1 RedStripe (org.eclipse.persistence.testing.models.jpa.inherited.RedStripe)1 Venue (org.eclipse.persistence.testing.models.jpa.inherited.Venue)1 Witness (org.eclipse.persistence.testing.models.jpa.inherited.Witness)1