use of org.eclipse.persistence.testing.models.jpa.advanced.Address in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method testPersistenceProperties.
// The test removed because we moved back to binding literals
// on platforms other than DB2 and Derby
/* public void testDontBindLiteral() {
EntityManager em = createEntityManager();
Query controlQuery = em.createQuery("SELECT OBJECT(p) FROM SmallProject p WHERE p.name = CONCAT(:param1, :param2)");
controlQuery.setParameter("param1", "A").setParameter("param2", "B");
List controlResults = controlQuery.getResultList();
int nControlParams = ((ExpressionQueryMechanism)((EJBQueryImpl)controlQuery).getDatabaseQuery().getQueryMechanism()).getCall().getParameters().size();
if(nControlParams != 2) {
fail("controlQuery has wrong number of parameters = "+nControlParams+"; 2 is expected");
}
Query query = em.createQuery("SELECT OBJECT(p) FROM SmallProject p WHERE p.name = CONCAT('A', 'B')");
List results = query.getResultList();
int nParams = ((ExpressionQueryMechanism)((EJBQueryImpl)query).getDatabaseQuery().getQueryMechanism()).getCall().getParameters().size();
if(nParams > 0) {
fail("Query processed literals as parameters");
}
closeEntityManager(em);
}*/
public void testPersistenceProperties() {
// Different properties are used on the server.
if (isOnServer()) {
return;
}
EntityManager em = createEntityManager();
ServerSession ss = ((org.eclipse.persistence.internal.jpa.EntityManagerImpl) em).getServerSession();
// these properties were set in persistence unit
// and overridden in CMP3TestModel.setup - the values should be overridden.
boolean isReadShared = (ss.getReadConnectionPool() instanceof ReadConnectionPool);
if (isReadShared != Boolean.parseBoolean((String) JUnitTestCaseHelper.propertiesMap.get(PersistenceUnitProperties.JDBC_READ_CONNECTIONS_SHARED))) {
fail("isReadShared is wrong");
}
int writeMin = ss.getDefaultConnectionPool().getMinNumberOfConnections();
if (writeMin != Integer.parseInt((String) JUnitTestCaseHelper.propertiesMap.get(PersistenceUnitProperties.JDBC_WRITE_CONNECTIONS_MIN))) {
fail("writeMin is wrong");
}
int writeInitial = ss.getDefaultConnectionPool().getInitialNumberOfConnections();
if (writeInitial != Integer.parseInt((String) JUnitTestCaseHelper.propertiesMap.get(PersistenceUnitProperties.JDBC_WRITE_CONNECTIONS_INITIAL))) {
fail("writeInitial is wrong");
}
int writeMax = ss.getDefaultConnectionPool().getMaxNumberOfConnections();
if (writeMax != Integer.parseInt((String) JUnitTestCaseHelper.propertiesMap.get(PersistenceUnitProperties.JDBC_WRITE_CONNECTIONS_MAX))) {
fail("writeMax is wrong");
}
int readMin = ss.getReadConnectionPool().getMinNumberOfConnections();
if (readMin != Integer.parseInt((String) JUnitTestCaseHelper.propertiesMap.get(PersistenceUnitProperties.JDBC_READ_CONNECTIONS_MIN))) {
fail("readMin is wrong");
}
int readMax = ss.getReadConnectionPool().getMaxNumberOfConnections();
if (readMax != Integer.parseInt((String) JUnitTestCaseHelper.propertiesMap.get(PersistenceUnitProperties.JDBC_READ_CONNECTIONS_MAX))) {
fail("readMax is wrong");
}
int batchSize = ss.getPlatform().getMaxBatchWritingSize();
if (batchSize != Integer.parseInt((String) JUnitTestCaseHelper.propertiesMap.get(PersistenceUnitProperties.BATCH_WRITING_SIZE))) {
fail("batchSize is wrong");
}
// these properties were set in persistence unit - the values should be the same as in persistence.xml
/*
<property name="eclipselink.session-name" value="default-session"/>
<property name="eclipselink.cache.size.default" value="500"/>
<property name="eclipselink.cache.size.Employee" value="550"/>
<property name="eclipselink.cache.size.org.eclipse.persistence.testing.models.jpa.advanced.Address" value="555"/>
<property name="eclipselink.cache.type.default" value="Full"/>
<property name="eclipselink.cache.type.Employee" value="Weak"/>
<property name="eclipselink.cache.type.org.eclipse.persistence.testing.models.jpa.advanced.Address" value="HardWeak"/>
<property name="eclipselink.session.customizer" value="org.eclipse.persistence.testing.models.jpa.advanced.Customizer"/>
<property name="eclipselink.descriptor.customizer.Employee" value="org.eclipse.persistence.testing.models.jpa.advanced.Customizer"/>
<property name="eclipselink.descriptor.customizer.org.eclipse.persistence.testing.models.jpa.advanced.Address" value="org.eclipse.persistence.testing.models.jpa.advanced.Customizer"/>
<property name="eclipselink.descriptor.customizer.Project" value="org.eclipse.persistence.testing.models.jpa.advanced.Customizer"/>
*/
String sessionName = ss.getName();
if (!sessionName.equals("default-session")) {
fail("sessionName is wrong: " + sessionName);
}
int defaultCacheSize = ss.getDescriptor(Project.class).getIdentityMapSize();
if (defaultCacheSize != 500) {
fail("defaultCacheSize is wrong: " + defaultCacheSize);
}
int employeeCacheSize = ss.getDescriptor(Employee.class).getIdentityMapSize();
if (employeeCacheSize != 550) {
fail("employeeCacheSize is wrong: " + employeeCacheSize);
}
int addressCacheSize = ss.getDescriptor(Address.class).getIdentityMapSize();
if (addressCacheSize != 555) {
fail("addressCacheSize is wrong: " + addressCacheSize);
}
// Department cache size specified in @Cache annotation - that should override default property
int departmentCacheSize = ss.getDescriptor(Department.class).getIdentityMapSize();
if (departmentCacheSize != 777) {
fail("departmentCacheSize is wrong: " + departmentCacheSize);
}
Class<?> defaultCacheType = ss.getDescriptor(Project.class).getIdentityMapClass();
if (!Helper.getShortClassName(defaultCacheType).equals("FullIdentityMap")) {
fail("defaultCacheType is wrong: " + Helper.getShortClassName(defaultCacheType));
}
Class<?> employeeCacheType = ss.getDescriptor(Employee.class).getIdentityMapClass();
if (!Helper.getShortClassName(employeeCacheType).equals("WeakIdentityMap")) {
fail("employeeCacheType is wrong: " + Helper.getShortClassName(employeeCacheType));
}
Class<?> addressCacheType = ss.getDescriptor(Address.class).getIdentityMapClass();
if (!Helper.getShortClassName(addressCacheType).equals("HardCacheWeakIdentityMap")) {
fail("addressCacheType is wrong: " + Helper.getShortClassName(addressCacheType));
}
// Department cache type specified in @Cache annotation - that should override default property
Class<?> departmentCacheType = ss.getDescriptor(Department.class).getIdentityMapClass();
if (!Helper.getShortClassName(departmentCacheType).equals("SoftCacheWeakIdentityMap")) {
fail("departmentCacheType is wrong: " + Helper.getShortClassName(departmentCacheType));
}
int numSessionCalls = Customizer.getNumberOfCallsForSession(ss.getName());
if (numSessionCalls == 0) {
fail("session customizer hasn't been called");
}
int numProjectCalls = Customizer.getNumberOfCallsForClass(Project.class.getName());
if (numProjectCalls == 0) {
fail("Project customizer hasn't been called");
}
int numEmployeeCalls = Customizer.getNumberOfCallsForClass(Employee.class.getName());
if (numEmployeeCalls == 0) {
fail("Employee customizer hasn't been called");
}
int numAddressCalls = Customizer.getNumberOfCallsForClass(Address.class.getName());
if (numAddressCalls == 0) {
fail("Address customizer hasn't been called");
}
IdValidation employeeIdValidation = ss.getDescriptor(Employee.class).getIdValidation();
if (employeeIdValidation != IdValidation.ZERO) {
fail("employeeIdValidation is wrong, IdValidation.ZERO assigned through PrimaryKey annotation was expected");
}
IdValidation addressIdValidation = ss.getDescriptor(Address.class).getIdValidation();
if (addressIdValidation != IdValidation.NEGATIVE) {
fail("addressIdValidation is wrong, IdValidation.NEGATIVE set as a default value in persistence.xml was expected");
}
closeEntityManager(em);
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Address in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method testReadOnlyCachedLazyAssociation.
public void testReadOnlyCachedLazyAssociation() {
EntityManager em = createEntityManager();
Integer empId = null;
Employee emp = null;
try {
// setup
try {
beginTransaction(em);
emp = new Employee();
emp.setFirstName("Mark");
emp.setLastName("Dowder");
final PhoneNumber phone = new PhoneNumber("work", "613", "5555555");
emp.addPhoneNumber(phone);
final PhoneNumber newPhone = new PhoneNumber("home", "613", "4444444");
emp.addPhoneNumber(newPhone);
final Address address = new Address("SomeStreet", "somecity", "province", "country", "postalcode");
emp.setAddress(address);
em.persist(emp);
em.flush();
commitTransaction(em);
empId = emp.getId();
// clear cache
em.getEntityManagerFactory().getCache().evictAll();
getServerSession().getIdentityMapAccessor().initializeIdentityMaps();
} catch (RuntimeException ex) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
throw ex;
}
final Employee employee = em.find(Employee.class, empId);
Assert.assertNotNull("No Employee retrieved", employee);
// Trigger Bug#474232
employee.getAddress();
final Employee cachedEmployee = em.createNamedQuery("findEmployeeByPK", Employee.class).setParameter("id", empId).setHint(QueryHints.READ_ONLY, HintValues.TRUE).getSingleResult();
Assert.assertNotNull("Employee not found", cachedEmployee);
final Address address = cachedEmployee.getAddress();
Assert.assertNotNull("Address of employee not retrieved", address);
} finally {
// Clean up
if (empId != null) {
beginTransaction(em);
em.remove(em.merge(emp));
commitTransaction(em);
}
closeEntityManager(em);
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Address in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method testNoPersistOnFlushProperties.
// Test Not using the persist operation on commit.
public void testNoPersistOnFlushProperties() {
// Properties only works in jse.
if (isOnServer()) {
return;
}
Map properties = new HashMap();
properties.putAll(JUnitTestCaseHelper.getDatabaseProperties());
properties.put(EntityManagerProperties.PERSISTENCE_CONTEXT_COMMIT_WITHOUT_PERSIST_RULES, "true");
EntityManager em = createEntityManager(properties);
beginTransaction(em);
Employee employee = new Employee();
employee.setLastName("SomeName");
Address addr = new Address();
addr.setCity("Douglas");
try {
em.persist(employee);
em.flush();
commitTransaction(em);
verifyObjectInCacheAndDatabase(employee);
closeEntityManager(em);
em = createEntityManager(properties);
beginTransaction(em);
employee = em.find(Employee.class, employee.getId());
employee.setAddress(addr);
addr.getEmployees().add(employee);
em.flush();
commitTransaction(em);
verifyObjectInCacheAndDatabase(employee);
closeEntityManager(em);
em = createEntityManager(properties);
clearCache();
beginTransaction(em);
employee = em.find(Employee.class, employee.getId());
employee.getAddress().setCountry("country");
employee.getAddress().getEmployees().size();
employee.setAddress((Address) null);
em.remove(employee);
commitTransaction(em);
em = createEntityManager(properties);
beginTransaction(em);
employee = em.find(Employee.class, employee.getId());
assertNull("Employee Not Deleted", employee);
commitTransaction(em);
closeEntityManager(em);
} catch (RuntimeException exception) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
throw exception;
} finally {
try {
em = createEntityManager();
clearCache();
beginTransaction(em);
em.remove(em.find(Address.class, addr.getID()));
commitTransaction(em);
} catch (RuntimeException ex) {
// ignore
}
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Address in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method testPersistOnCommit.
// Test using PERSISTENCE_CONTEXT_PERSIST_ON_COMMIT option to avoid the discover on commit.
public void testPersistOnCommit() {
// Properties only works in jse.
if (isOnServer()) {
return;
}
Map properties = new HashMap();
properties.put(PersistenceUnitProperties.PERSISTENCE_CONTEXT_PERSIST_ON_COMMIT, "false");
EntityManager em = createEntityManager(properties);
beginTransaction(em);
try {
Employee emp = new Employee();
emp.setFirstName("Douglas");
emp.setLastName("McRae");
em.persist(emp);
commitTransaction(em);
verifyObjectInCacheAndDatabase(emp);
closeEntityManager(em);
em = createEntityManager(properties);
beginTransaction(em);
Address address = new Address();
emp = em.find(Employee.class, emp.getId());
emp.setAddress(address);
em.persist(address);
commitTransaction(em);
verifyObjectInCacheAndDatabase(emp);
em = createEntityManager(properties);
beginTransaction(em);
emp = em.find(Employee.class, emp.getId());
em.remove(emp);
commitTransaction(em);
closeEntityManager(em);
} catch (RuntimeException exception) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
throw exception;
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Address in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method testCloneable.
// Test the clone method works correctly with lazy attributes.
public void testCloneable() {
EntityManager em = createEntityManager();
beginTransaction(em);
Employee employee = new Employee();
employee.setFirstName("Owen");
employee.setLastName("Hargreaves");
employee.getAddress();
Employee clone = employee.clone();
Address address = new Address();
address.setCity("Munich");
clone.setAddress(address);
clone.getAddress();
em.persist(clone);
if (employee.getAddress() == clone.getAddress()) {
fail("Changing clone address changed original.");
}
commitTransaction(em);
clearCache();
closeEntityManager(em);
em = createEntityManager();
beginTransaction(em);
employee = em.find(Employee.class, clone.getId());
clone = employee.clone();
address = new Address();
address.setCity("Not Munich");
clone.setAddress(address);
clone.getAddress();
if (employee.getAddress() == clone.getAddress()) {
fail("Changing clone address changed original.");
}
if (employee.getAddress() == null) {
fail("Changing clone address reset original to null.");
}
if (clone.getAddress() != address) {
fail("Changing clone did not work.");
}
commitTransaction(em);
closeEntityManager(em);
em = createEntityManager();
beginTransaction(em);
employee = em.find(Employee.class, clone.getId());
clone = employee.clone();
clone.setId(null);
em.persist(clone);
commitTransaction(em);
if (clone.getId() == null) {
fail("Clone was not persisted.");
}
beginTransaction(em);
employee = em.find(Employee.class, clone.getId());
em.remove(employee);
commitTransaction(em);
closeEntityManager(em);
}
Aggregations