use of org.eclipse.persistence.testing.models.jpa.inherited.Record in project eclipselink by eclipse-ee4j.
the class InheritedModelJunitTest method testCreateExpertBeerConsumer.
public void testCreateExpertBeerConsumer() {
EntityManager em = createEntityManager();
beginTransaction(em);
ExpertBeerConsumer beerConsumer = new ExpertBeerConsumer();
try {
beerConsumer.setName("Expert Beer Consumer");
beerConsumer.setIQ(110);
beerConsumer.getAcclaims().add("A");
beerConsumer.getAcclaims().add("B");
beerConsumer.getAcclaims().add("C");
// Commenting out this mapping until bug 272298 is resolved.
// The compareObjects check below would fail in some configurations
// as a result of this.
// beerConsumer.getAudio().add(new byte[]{1});
// beerConsumer.getAudio().add(new byte[]{2});
// beerConsumer.getAudio().add(new byte[]{3});
beerConsumer.getAwards().put("A", "A");
beerConsumer.getAwards().put("B", "B");
beerConsumer.getAwards().put("C", "C");
beerConsumer.getDesignations().add("A");
beerConsumer.getDesignations().add("B");
m_quote1Stamp = Helper.timestampFromDate(Helper.dateFromYearMonthDate(2009, 1, 1));
beerConsumer.getQuotes().put(m_quote1Stamp, QUOTE_ONE);
m_quote2Stamp = Helper.timestampFromDate(Helper.dateFromYearMonthDate(2005, 7, 9));
beerConsumer.getQuotes().put(m_quote2Stamp, QUOTE_TWO);
Record record1 = new Record();
record1.setDescription("Fastest beer ever consumed - 10 ms");
record1.setDate(Helper.dateFromYearMonthDate(2009, 10, 10));
record1.setLocation(new Location("Ottawa", "Canada"));
Venue venue1 = new Venue();
venue1.setAttendance(10000);
venue1.setName("Scotiabank PLace");
record1.setVenue(venue1);
beerConsumer.getRecords().add(record1);
Record record2 = new Record();
record2.setDescription("Most beers consumed upside down - 12");
record2.setDate(Helper.dateFromYearMonthDate(2007, 11, 11));
record2.setLocation(new Location("Sydney", "Australia"));
Venue venue2 = new Venue();
venue2.setAttendance(2678);
venue2.setName("Opera House");
record2.setVenue(venue2);
beerConsumer.getRecords().add(record2);
Record record3 = new Record();
record3.setDescription("Most beers consumed in a second - 5");
record3.setDate(Helper.dateFromYearMonthDate(2005, 12, 12));
record3.setLocation(new Location("Miami", "USA"));
Venue venue3 = new Venue();
venue3.setAttendance(63000);
venue3.setName("Dolphin Stadium");
record3.setVenue(venue3);
beerConsumer.getRecords().add(record3);
Accredidation accredidation = new Accredidation();
accredidation.setDetails("Elite, absolutely elite!");
Witness witness1 = new Witness();
witness1.setName("Big Bobby");
accredidation.addWitness(witness1);
Witness witness2 = new Witness();
witness2.setName("Little Bobby");
accredidation.addWitness(witness2);
Official official = new Official();
official.setName("Authority Joe");
accredidation.addOfficial(official);
beerConsumer.setAccredidation(accredidation);
Birthday birthday1 = new Birthday();
birthday1.setDay(9);
birthday1.setMonth(7);
birthday1.setYear(2005);
beerConsumer.addCelebration(birthday1, "Drank a 24 of Heineken");
Birthday birthday2 = new Birthday();
birthday2.setDay(10);
birthday2.setMonth(7);
birthday2.setYear(2006);
beerConsumer.addCelebration(birthday2, "Drank a 24 of Becks");
Committee committee1 = new Committee();
committee1.setDescription("New beer committee");
CommitteeDates committee1Dates = new CommitteeDates();
committee1Dates.setStartDate("Jan 1, 2010");
committee1.setCommitteeDates(committee1Dates);
beerConsumer.addCommittee(committee1);
Committee committee2 = new Committee();
committee2.setDescription("Alcohol content regulation");
CommitteeDates committee2Dates = new CommitteeDates();
committee2Dates.setStartDate("Jan 1, 1970");
committee2Dates.setEndDate("Jan 1, 2001");
committee2.setCommitteeDates(committee2Dates);
beerConsumer.addCommittee(committee2);
em.persist(beerConsumer);
m_expertBeerConsumerId = beerConsumer.getId();
commitTransaction(em);
} catch (RuntimeException e) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
fail("An exception was caught during create operation for an expert beer consumer: [" + e.getMessage() + "]");
}
closeEntityManager(em);
clearCache();
em = createEntityManager();
BeerConsumer refreshedBC = em.find(BeerConsumer.class, m_expertBeerConsumerId);
assertTrue("The expert beer consumer read back did not match the original", getServerSession().compareObjects(beerConsumer, refreshedBC));
}
use of org.eclipse.persistence.testing.models.jpa.inherited.Record 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));
}
use of org.eclipse.persistence.testing.models.jpa.inherited.Record in project eclipselink by eclipse-ee4j.
the class InheritedModelJunitTest method testExpertBeerConsumerRecordsCRUD.
// Bug 296606 - issues with ElementCollections
// This test should be run after testCreateExpertBearConsumer
// This test makes changes, so testReadExpertBeerConsumer will fail after this test.
public void testExpertBeerConsumerRecordsCRUD() {
String errorMsg = "";
int nRecords;
int nRecordsExpected = 2;
clearCache();
EntityManager em = createEntityManager();
try {
// read all Records inside transaction
beginTransaction(em);
ExpertBeerConsumer consumer = em.find(ExpertBeerConsumer.class, m_expertBeerConsumerId);
// trigger indirection
nRecords = consumer.getRecords().size();
// Bug 296606 - issues with ElementCollections cause commit to fail with NPE
// Before the bug was fixed, the workaround was to annotate Record with @ChangeTracking(ChangeTrackingType.DEFERRED)
commitTransaction(em);
closeEntityManager(em);
if (nRecords != nRecordsExpected) {
errorMsg += "wrong number of records after read; ";
}
// remove Record
em = createEntityManager();
beginTransaction(em);
consumer = em.find(ExpertBeerConsumer.class, m_expertBeerConsumerId);
Record recordToRemove = consumer.getRecords().iterator().next();
consumer.getRecords().remove(recordToRemove);
commitTransaction(em);
closeEntityManager(em);
nRecordsExpected--;
// verify in cache
em = createEntityManager();
consumer = em.find(ExpertBeerConsumer.class, m_expertBeerConsumerId);
closeEntityManager(em);
nRecords = consumer.getRecords().size();
if (nRecords != nRecordsExpected) {
errorMsg += "cache: wrong number of records after remove; ";
}
// verify in db
clearCache();
em = createEntityManager();
consumer = em.find(ExpertBeerConsumer.class, m_expertBeerConsumerId);
closeEntityManager(em);
nRecords = consumer.getRecords().size();
if (nRecords != nRecordsExpected) {
errorMsg += "db: wrong number of records after remove; ";
}
// add Record
Record record1 = new Record();
record1.setDescription("Original");
record1.setDate(Helper.dateFromYearMonthDate(2009, 1, 1));
record1.setLocation(new Location("Ottawa", "Canada"));
Venue venue1 = new Venue();
venue1.setAttendance(10);
venue1.setName("Original");
record1.setVenue(venue1);
em = createEntityManager();
beginTransaction(em);
consumer = em.find(ExpertBeerConsumer.class, m_expertBeerConsumerId);
consumer.getRecords().add(record1);
commitTransaction(em);
closeEntityManager(em);
nRecordsExpected++;
// verify in cache
em = createEntityManager();
consumer = em.find(ExpertBeerConsumer.class, m_expertBeerConsumerId);
closeEntityManager(em);
nRecords = consumer.getRecords().size();
if (nRecords != nRecordsExpected) {
errorMsg += "cache: wrong number of records after add; ";
}
// verify in db
clearCache();
em = createEntityManager();
consumer = em.find(ExpertBeerConsumer.class, m_expertBeerConsumerId);
closeEntityManager(em);
nRecords = consumer.getRecords().size();
if (nRecords != nRecordsExpected) {
errorMsg += "db: wrong number of records after add; ";
}
// update all Records one by one.
String newDescription = "New Description ";
String newName = "New Name ";
em = createEntityManager();
beginTransaction(em);
consumer = em.find(ExpertBeerConsumer.class, m_expertBeerConsumerId);
int i = 0;
Iterator<Record> it = consumer.getRecords().iterator();
while (it.hasNext()) {
Record record = it.next();
String index = Integer.toString(i++);
record.setDescription(newDescription + index);
record.getVenue().setName(newName + index);
}
// Bug 296606 - issues with ElementCollections cause commit to fail with NPE
// Before the bug was fixed, the workaround was to annotate both Record and Venue with @ChangeTracking(ChangeTrackingType.DEFERRED).
// With that workaround the test still used to fail - Record and Venue were not updated in the data base.
// Before the bug was fixed, the workaround for that was to set property "eclipselink.weaving.internal" to "false" in persistence.xml.
commitTransaction(em);
// verify in cache
em = createEntityManager();
consumer = em.find(ExpertBeerConsumer.class, m_expertBeerConsumerId);
closeEntityManager(em);
nRecords = consumer.getRecords().size();
if (nRecords != nRecordsExpected) {
errorMsg += "cache: wrong number of records after update; ";
}
HashSet<String> usedDescriptions = new HashSet(nRecords);
HashSet<String> usedNames = new HashSet(nRecords);
it = consumer.getRecords().iterator();
while (it.hasNext()) {
Record record = it.next();
String description = record.getDescription();
if (!description.startsWith(newDescription)) {
errorMsg += "cache: wrong record description after update; ";
}
usedDescriptions.add(description);
String name = record.getVenue().getName();
if (!name.startsWith(newName)) {
errorMsg += "cache: wrong venue name after update; ";
}
usedNames.add(name);
}
if (usedDescriptions.size() != nRecords) {
errorMsg += "cache: records with same description; ";
}
if (usedNames.size() != nRecords) {
errorMsg += "cache: venues with same name; ";
}
// verify in db
clearCache();
em = createEntityManager();
consumer = em.find(ExpertBeerConsumer.class, m_expertBeerConsumerId);
closeEntityManager(em);
nRecords = consumer.getRecords().size();
if (nRecords != nRecordsExpected) {
errorMsg += "db: wrong number of records after update; ";
}
usedDescriptions.clear();
usedNames.clear();
it = consumer.getRecords().iterator();
while (it.hasNext()) {
Record record = it.next();
String description = record.getDescription();
if (!description.startsWith(newDescription)) {
errorMsg += "db: wrong record description after update; ";
}
usedDescriptions.add(description);
String name = record.getVenue().getName();
if (!name.startsWith(newName)) {
errorMsg += "db: wrong venue name after update; ";
}
usedNames.add(name);
}
if (usedDescriptions.size() != nRecords) {
errorMsg += "db: records with same description; ";
}
if (usedNames.size() != nRecords) {
errorMsg += "db: venues with same name; ";
}
if (errorMsg.length() > 0) {
fail(errorMsg);
}
} finally {
if (em != null) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
if (isOnServer()) {
closeEntityManager(em);
} else if (em.isOpen()) {
closeEntityManager(em);
}
}
}
}
Aggregations