use of org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.LargeProject in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method internalDeleteAllProjectsWithNullTeamLeader.
protected void internalDeleteAllProjectsWithNullTeamLeader(String className) {
String name = "testDeleteAllProjectsWithNull";
// setup
SmallProject sp = new SmallProject();
sp.setName(name);
LargeProject lp = new LargeProject();
lp.setName(name);
EntityManager em = createEntityManager();
try {
beginTransaction(em);
// make sure there are no pre-existing objects with this name
em.createQuery("DELETE FROM " + className + " p WHERE p.name = '" + name + "'").executeUpdate();
em.persist(sp);
em.persist(lp);
commitTransaction(em);
} catch (RuntimeException ex) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
throw ex;
} finally {
closeEntityManager(em);
}
// test
em = createEntityManager();
beginTransaction(em);
try {
em.createQuery("DELETE FROM " + className + " p WHERE p.name = '" + name + "' AND p.teamLeader IS NULL").executeUpdate();
commitTransaction(em);
} catch (RuntimeException e) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
throw e;
} finally {
closeEntityManager(em);
}
// verify
String error = null;
em = createEntityManager();
List result = em.createQuery("SELECT OBJECT(p) FROM Project p WHERE p.name = '" + name + "'").getResultList();
if (result.isEmpty()) {
if (!className.equals("Project")) {
error = "Target Class " + className + ": no objects left";
}
} else {
if (result.size() > 1) {
error = "Target Class " + className + ": too many objects left: " + result.size();
} else {
Project p = (Project) result.get(0);
if (p.getClass().getName().endsWith(className)) {
error = "Target Class " + className + ": object of wrong type left: " + p.getClass().getName();
}
}
}
// clean up
try {
beginTransaction(em);
// make sure there are no pre-existing objects with this name
em.createQuery("DELETE FROM " + className + " p WHERE p.name = '" + name + "'").executeUpdate();
commitTransaction(em);
} catch (RuntimeException ex) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
throw ex;
} finally {
closeEntityManager(em);
}
if (error != null) {
fail(error);
}
}
use of org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.LargeProject in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method testWeaving.
/**
* Test that all of the classes in the advanced model were weaved as expected.
*/
public void testWeaving() {
// Only test if weaving was on, test runs without weaving must set this system property.
if (JUnitTestCase.isWeavingEnabled("fieldaccess")) {
internalTestWeaving(new Employee(), true, true);
internalTestWeaving(new FormerEmployment(), true, false);
internalTestWeaving(new Address(), true, false);
internalTestWeaving(new PhoneNumber(), true, false);
internalTestWeaving(new EmploymentPeriod(), true, false);
// field-locking
internalTestWeaving(new Buyer(), false, false);
// field-locking
internalTestWeaving(new GoldBuyer(), false, false);
// field-locking
internalTestWeaving(new PlatinumBuyer(), false, false);
internalTestWeaving(new Department(), true, false);
internalTestWeaving(new Golfer(), true, false);
internalTestWeaving(new GolferPK(), true, false);
internalTestWeaving(new SmallProject(), true, false);
internalTestWeaving(new LargeProject(), true, false);
internalTestWeaving(new Man(), true, false);
internalTestWeaving(new Woman(), true, false);
// serialized
internalTestWeaving(new Vegetable(), false, false);
internalTestWeaving(new VegetablePK(), false, false);
internalTestWeaving(new WorldRank(), true, false);
internalTestWeaving(new Equipment(), true, false);
internalTestWeaving(new EquipmentCode(), true, false);
internalTestWeaving(new PartnerLink(), true, false);
}
if (((OneToOneMapping) getDatabaseSession().getDescriptor(DatabaseField.class).getMappingForAttributeName("table")).usesIndirection()) {
fail("LAZY mapping should have been reverted as class was not weaved.");
}
}
use of org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.LargeProject in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method internalTestUpdateAllProjects.
protected void internalTestUpdateAllProjects(Class<?> cls) {
if (getDatabaseSession().getPlatform().isSymfoware()) {
getDatabaseSession().logMessage("Test testUpdateAll*Projects skipped for this platform, " + "Symfoware doesn't support UpdateAll/DeleteAll on multi-table objects (see rfe 298193).");
return;
}
String className = Helper.getShortClassName(cls);
String name = "testUpdateAllProjects";
String newName = "testUpdateAllProjectsNEW";
HashMap map = null;
boolean ok = false;
try {
// setup
// populate Projects - necessary only if no SmallProject and/or LargeProject objects already exist.
createProjectsWithName(name, null);
// save the original names of projects: will set them back in cleanup
// to restore the original state.
EntityManager em = createEntityManager();
List projects = em.createQuery("SELECT OBJECT(p) FROM Project p").getResultList();
map = new HashMap(projects.size());
for (int i = 0; i < projects.size(); i++) {
Project p = (Project) projects.get(i);
map.put(p.getId(), p.getName());
}
// test
beginTransaction(em);
try {
em.createQuery("UPDATE " + className + " p set p.name = '" + newName + "'").executeUpdate();
commitTransaction(em);
} catch (RuntimeException ex) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
throw ex;
} finally {
closeEntityManager(em);
}
// verify
em = createEntityManager();
String errorMsg = "";
projects = em.createQuery("SELECT OBJECT(p) FROM Project p").getResultList();
for (int i = 0; i < projects.size(); i++) {
Project p = (Project) projects.get(i);
String readName = p.getName();
if (cls.isInstance(p)) {
if (!newName.equals(readName)) {
errorMsg = errorMsg + "haven't updated name: " + p + "; ";
}
} else {
if (newName.equals(readName)) {
errorMsg = errorMsg + "have updated name: " + p + "; ";
}
}
}
closeEntityManager(em);
if (errorMsg.length() > 0) {
fail(errorMsg);
} else {
ok = true;
}
} finally {
// clean-up
try {
if (map != null) {
EntityManager em = createEntityManager();
beginTransaction(em);
List projects = em.createQuery("SELECT OBJECT(p) FROM Project p").getResultList();
try {
for (int i = 0; i < projects.size(); i++) {
Project p = (Project) projects.get(i);
String oldName = (String) map.get(((Project) projects.get(i)).getId());
p.setName(oldName);
}
commitTransaction(em);
} catch (RuntimeException ex) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
throw ex;
} finally {
closeEntityManager(em);
}
}
// delete projects that createProjectsWithName has created in setup
deleteProjectsWithName(name);
} catch (RuntimeException ex) {
// eat clean-up exception in case the test failed
if (ok) {
throw ex;
}
}
}
}
use of org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.LargeProject in project eclipselink by eclipse-ee4j.
the class AdvancedJPAJunitTest method testTransientConstructorSetFields.
/**
* test for bug 292385 - Transient fields not initialized for Entities
*/
public void testTransientConstructorSetFields() {
EntityManager em = createEntityManager("fieldaccess");
Project project = new Project();
LargeProject lproject = new LargeProject();
SmallProject sproject = new SmallProject();
project.setName("TCSetFieldsProject");
lproject.setName("TCSetFieldsLProject");
sproject.setName("TCSetFieldsSProject");
beginTransaction(em);
project = em.merge(project);
lproject = em.merge(lproject);
sproject = em.merge(sproject);
assertTrue("Project's default constructor wasn't used to create merged entity", project.getFieldOnlySetThroughConstructor() == 1);
assertTrue("LargeProject's default constructor wasn't used to create merged entity", lproject.getFieldOnlySetThroughConstructor() == 2);
assertTrue("Project's default constructor wasn't used to create merged SmallProject", sproject.getFieldOnlySetThroughConstructor() == 1);
try {
em.flush();
this.commitTransaction(em);
closeEntityManager(em);
} catch (RuntimeException e) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
// Re-throw exception to ensure stacktrace appears in test result.
throw e;
}
em = createEntityManager("fieldaccess");
project = em.find(Project.class, project.getId());
lproject = em.find(LargeProject.class, lproject.getId());
sproject = em.find(SmallProject.class, sproject.getId());
closeEntityManager(em);
assertTrue("Project's default constructor wasn't used to create managed entity", project.getFieldOnlySetThroughConstructor() == 1);
assertTrue("LargeProject's default constructor wasn't used to create managed entity", lproject.getFieldOnlySetThroughConstructor() == 2);
assertTrue("Project's default constructor wasn't used to create managed SmallProject", sproject.getFieldOnlySetThroughConstructor() == 1);
}
Aggregations