public void populate(DatabaseSession session) { Employee instance; PopulationManager manager = PopulationManager.getDefaultManager(); instance = Employee.example1(); session.writeObject(instance); manager.registerObject(instance, "example1"); manager.registerObject(instance.computer, "example1"); manager.registerObject(instance.shipments.firstElement(), "example1"); manager.registerObject( ((Shipment) instance.shipments.firstElement()).orders.firstElement(), "example1"); instance = Employee.example2(); session.writeObject(instance); manager.registerObject(instance, "example2"); manager.registerObject(instance.computer, "example2"); manager.registerObject(instance.shipments.firstElement(), "example2"); manager.registerObject( ((Shipment) instance.shipments.firstElement()).orders.firstElement(), "example2"); instance = Employee.example3(); session.writeObject(instance); manager.registerObject(instance, "example3"); manager.registerObject(instance.computer, "example3"); manager.registerObject(instance.shipments.firstElement(), "example3"); manager.registerObject( ((Shipment) instance.shipments.firstElement()).orders.firstElement(), "example3"); }
/** * Return the first employee that has a long enough name for the test. If no match is found throw * a warning exception. See bug 223005 * * @param vectorOfEmployees * @param minFirstNameLength * @param testName * @return */ public Employee getEmployeeWithRequiredNameLength( Vector vectorOfEmployees, int minFirstNameLength, String testName) { Employee empMatch = null; Vector<Employee> employees = vectorOfEmployees; String firstName; StringBuffer partialFirstName; // Loop through the collection of employees to find one that matches our test requirements for (int i = 0; i < employees.size(); i++) { empMatch = employees.get(i); firstName = empMatch.getFirstName(); // Verify length criteria if (firstName.length() >= minFirstNameLength) { // exit the for loop - return the last empMatch i = employees.size(); } } // If we could not find a proper employee for testing - throw a warning if (null == empMatch) { throw new RuntimeException( testName + " Setup Failed: unable to find an Employee with firstName size of at least " + minFirstNameLength); } else { return empMatch; } }
public void test() { // test readAll getSession().readAllObjects(Employee.class); getSession().getIdentityMapAccessor().initializeIdentityMap(Employee.class); // test readObject Employee employee = (Employee) getSession().readObject(Employee.class, new ExpressionBuilder().get("id").equal(99)); // test delete with an employee read from the database employee = (Employee) getSession().readObject(Employee.class); try { getAbstractSession().deleteObject(employee); } catch (DatabaseException exc) { // if we get an integrity exception here, the query went to the DB and was not redirected redirectedDeleteObject = false; } UnitOfWork uow = getSession().acquireUnitOfWork(); // test update with an employee read from the database employee = (Employee) uow.readObject(Employee.class); employee.setFirstName(employee.getFirstName() + "-changed"); // insert an employee to test the insert behavior employee = new Employee(); employee.setFirstName("Paul"); employee.setLastName("Sheldon"); uow.registerObject(employee); uow.commit(); }
protected void verify() { if (!employee.getAddress().getCity().equals("null")) { throw new TestErrorException("Null value not converted correctly for string."); } if (employee.getSalary() != -1) { throw new TestErrorException("Null value not converted correctly for int."); } }
protected void test() { UnitOfWork uow = getSession().acquireUnitOfWork(); ReadObjectQuery query = new ReadObjectQuery(Employee.class); query.setLockMode(ObjectLevelReadQuery.LOCK_NOWAIT); Employee emp = (Employee) uow.executeQuery(query); emp.setSalary(emp.getSalary() + 1); uow.commit(); }
protected void test() { UnitOfWork uow = getSession().acquireUnitOfWork(); Employee empInsert = new Employee(); empInsert.setFirstName("TestPerson"); empInsert.setFemale(); empInsert.setLastName("Smith"); empInsert.setSalary(55555); uow.registerObject(empInsert); uow.commit(); }
private void setArgumentsForTestUsing(Vector employees) { setArguments(new Vector()); Enumeration names = getExpressionParameters().elements(); Enumeration employeeEnum = employees.elements(); while (names.hasMoreElements()) { Employee emp = (Employee) employeeEnum.nextElement(); getArguments().add(emp.getId()); names.nextElement(); } }
public void setup() { // This method tests the specail operations: employee = new Employee(); policy = new QueryByExamplePolicy(); employee.setFirstName("J__l"); employee.setLastName("M%"); employee.setSalary(60000); policy.addSpecialOperation(Integer.class, "lessThan"); policy.addSpecialOperation(String.class, "like"); }
public void test() { UnitOfWork uow = getSession().acquireUnitOfWork(); Employee managerClone = (Employee) uow.registerObject(employee[0]); // remove all managed Employees (1 and 2) managerClone.getManagedEmployees().clear(); // add to managed list new Employees (3 and 4) Employee employee3Clone = (Employee) uow.registerObject(employee[3]); Employee employee4Clone = (Employee) uow.registerObject(employee[4]); managerClone.addManagedEmployee(employee3Clone); managerClone.addManagedEmployee(employee4Clone); // after commit the versions of all Employees should be changed. uow.commit(); }
public void test() { int size = getCacheIdentityMap().getMaxSize() * factor; for (int i = 0; i < size; i++) { BigDecimal id = new java.math.BigDecimal(i); Employee employee = new Employee(); Vector pk = new Vector(1); employee.setId(id); employee.setFirstName("Joe"); employee.setLastName("Blow"); pk.add(id); getPrimaryKeys().add(pk); getCacheIdentityMap().put(primaryKeys, employee, null, 0); } }
public void test() { UnitOfWork uow = getSession().acquireUnitOfWork(); this.emp = Employee.example1(); this.emp.asset1.asset = Vehicle.example1(); uow.registerObject(this.emp); uow.commit(); uow = getSession().acquireUnitOfWork(); Employee empClone = (Employee) uow.registerObject(this.emp); Employee newClone = (Employee) empClone.clone(); ((Vehicle) newClone.asset1.asset).make = "Beamer"; uow.deepMergeClone(newClone); uow.commit(); if (!((AbstractSession) getSession()).compareObjects(newClone, this.emp)) { throw new TestErrorException("Failed to merge the Variable 1 to 1 properly"); } }
public void test() { ReadObjectQuery query = new ReadObjectQuery(); query.setReferenceClass(Employee.class); query.setExampleObject(employee); query.setQueryByExamplePolicy(policy); Employee emp = new Employee(); emp = (Employee) getSession().executeQuery(query); if (!(emp.getFirstName().charAt(0) == 'J') && (emp.getFirstName().charAt(3) == 'l') && (emp.getLastName().charAt(0) == 'M')) { throw (new TestErrorException("Error in using specail operators.")); } if (!(emp.getSalary() < 60000)) { throw (new TestErrorException("Error is using spcial operators.")); } }
public void setup() { if ((getSession().getLogin().getPlatform().isSQLServer())) { throw new TestWarningException( "This test is not supported on SQL Server. Because 'LENGTH' is not a recognized function name on SQL Server."); } Employee emp = (Employee) getSomeEmployees().firstElement(); String ejbqlString; ejbqlString = "SELECT OBJECT(emp) FROM Employee emp WHERE "; ejbqlString = ejbqlString + emp.getFirstName().length(); ejbqlString = ejbqlString + " = LENGTH(emp.firstName)"; setEjbqlString(ejbqlString); setOriginalOject(emp); super.setup(); }
public void test() { UnitOfWork uow = getSession().acquireUnitOfWork(); employee = (Employee) uow.readObject(Employee.class); originalReadTime = ((AbstractSession) getSession()) .getIdentityMapAccessorInstance() .getCacheKeyForObject(employee) .getReadTime(); employee.setFirstName(employee.getFirstName() + "-mutated"); try { Thread.sleep(100); } catch (InterruptedException exc) { } uow.commit(); secondReadTime = getAbstractSession() .getIdentityMapAccessorInstance() .getCacheKeyForObject(employee) .getReadTime(); }
long getVersion(Employee emp) { Vector pk = new Vector(1); pk.add(emp.getId()); return ((Long) getSession() .getDescriptor(Employee.class) .getOptimisticLockingPolicy() .getWriteLockValue(emp, pk, getAbstractSession())) .longValue(); }
private void createEmployeeAndSearchExpression() { // Create the example employee employee = (org.eclipse.persistence.testing.models.employee.domain.Employee) new org.eclipse.persistence.testing.models.employee.domain.EmployeePopulator() .basicEmployeeExample1(); employee.setFirstName("Timugen"); employee.setLastName("Singaera"); employee.addResponsibility("Answer the phones."); // Create an expression to retreive the employee from the database ExpressionBuilder expressionBuilder = new ExpressionBuilder(); Expression exp1; Expression exp2; Expression expression; exp1 = expressionBuilder.get("firstName").equal(employee.getFirstName()); exp2 = expressionBuilder.get("lastName").equal(employee.getLastName()); searchExpression = exp1.or(exp2); }
protected void changeObject() { UnitOfWork uow = (UnitOfWork) getSession(); Employee cloneEmployee = (Employee) workingCopy; for (int i = 0; i < managedEmployeesToAdd.size(); i++) { Employee cloneEmployeeToAdd = (Employee) uow.registerObject(managedEmployeesToAdd.get(i)); cloneEmployee.addManagedEmployee(cloneEmployeeToAdd); } for (int i = 0; i < managedEmployeesToRemove.size(); i++) { Employee cloneEmployeeToRemove = (Employee) uow.registerObject(managedEmployeesToRemove.get(i)); cloneEmployee.removeManagedEmployee(cloneEmployeeToRemove); } for (int i = 0; i < phonesToRemove.size(); i++) { PhoneNumber clonePhoneToRemove = (PhoneNumber) uow.registerObject(phonesToRemove.get(i)); cloneEmployee.removePhoneNumber(clonePhoneToRemove); } for (int i = 0; i < phonesToAdd.size(); i++) { PhoneNumber clonePhoneToAdd = (PhoneNumber) uow.registerObject(phonesToAdd.get(i)); cloneEmployee.addPhoneNumber(clonePhoneToAdd); } }
public static TestSuite getComplexUpdateObjectTestSuite() { TestSuite suite = new TestSuite(); suite.setName("EmployeeComplexUpdateTestSuite"); suite.setDescription( "This suite tests the updating of each an employee by adding and/or removing managed employees and/or phones."); Class employeeClass = Employee.class; PopulationManager manager = PopulationManager.getDefaultManager(); Employee originalEmployee = (Employee) manager.getObject(employeeClass, "0001"); Employee otherEmployee = (Employee) manager.getObject(employeeClass, "0002"); // add a managed Employee from other Employee managed List; remove the first managed Employee. suite.addTest( new EmployeeComplexUpdateTest( originalEmployee, otherEmployee.getManagedEmployees().get(0), originalEmployee.getManagedEmployees().get(0))); // remove the first Phone. suite.addTest( new EmployeeComplexUpdateTest( originalEmployee, (Object) null, originalEmployee.getPhoneNumbers().get(0))); // add a managed Employee from other Employee managed List and new phone; // remove the first two managed Employees and the first Phone. Employee newEmployee = new Employee(); newEmployee.setFirstName("New"); PhoneNumber newPhoneNumber = new PhoneNumber("home", "001", "0000001"); suite.addTest( new EmployeeComplexUpdateTest( originalEmployee, new Object[] {otherEmployee.getManagedEmployees().get(0), newEmployee, newPhoneNumber}, new Object[] { originalEmployee.getManagedEmployees().get(0), originalEmployee.getManagedEmployees().get(1), originalEmployee.getPhoneNumbers().get(0) })); suite.addTest(new CascadeLockingTest()); suite.addTest(new TargetLockingTest_AddRemoveTarget()); return suite; }
protected void setup() { // save current null values for later restoration saveDefaultDefaultNullValues = ConversionManager.getDefaultManager().getDefaultNullValues(); saveDefaultNullValues = getSession().getLogin().getPlatform().getConversionManager().getDefaultNullValues(); getSession() .getLogin() .getPlatform() .getConversionManager() .setDefaultNullValues(new Hashtable()); getSession().getLogin().setDefaultNullValue(String.class, "null"); getSession().getLogin().setDefaultNullValue(int.class, new Integer(-1)); // Reinit mappings. for (DatabaseMapping mapping : getSession().getDescriptor(Address.class).getMappings()) { if (mapping.isDirectToFieldMapping()) { mapping.preInitialize(getAbstractSession()); } } getAbstractSession().beginTransaction(); employee = new Employee(); employee.setFirstName("Fred"); employee.setLastName("Flintstone"); employee.setSalary(22); employee.setGender("Male"); Address address = new Address(); address.setCity(null); employee.setAddress(address); getAbstractSession().writeObject(employee); // force the salary to be NULL getSession() .executeNonSelectingCall( new SQLCall("update SALARY set SALARY = null where EMP_ID = " + employee.getId())); }
public static TestSuite getReadObjectTestSuite() { TestSuite suite = new TestSuite(); suite.setName("EmployeeReadObjectTestSuite"); suite.setDescription("This suite test the reading of each object in the employee demo."); Class employeeClass = Employee.class; PopulationManager manager = PopulationManager.getDefaultManager(); suite.addTest(new ReadObjectTest(manager.getObject(employeeClass, "0001"))); suite.addTest(new ReadObjectTest(manager.getObject(employeeClass, "0002"))); suite.addTest(new ReadObjectTest(manager.getObject(employeeClass, "0003"))); suite.addTest(new ReadObjectTest(manager.getObject(employeeClass, "0004"))); suite.addTest(new ReadObjectTest(manager.getObject(employeeClass, "0005"))); Employee employee = (Employee) manager.getObject(employeeClass, "0001"); suite.addTest( new ReadObjectCallTest( employeeClass, new SQLCall( "SELECT VERSION, EMP_ID, L_NAME, F_NAME FROM UNIDIR_EMPLOYEE WHERE F_NAME = '" + employee.getFirstName() + "' AND L_NAME = '" + employee.getLastName() + "'"))); employee = (Employee) manager.getObject(employeeClass, "0002"); suite.addTest( new ReadObjectCallTest( employeeClass, new SQLCall( "SELECT VERSION, EMP_ID, L_NAME, F_NAME FROM UNIDIR_EMPLOYEE WHERE F_NAME = '" + employee.getFirstName() + "' AND L_NAME = '" + employee.getLastName() + "'"))); employee = (Employee) manager.getObject(employeeClass, "0003"); suite.addTest( new ReadObjectCallTest( employeeClass, new SQLCall( "SELECT VERSION, EMP_ID, L_NAME, F_NAME FROM UNIDIR_EMPLOYEE WHERE F_NAME = '" + employee.getFirstName() + "' AND L_NAME = '" + employee.getLastName() + "'"))); return suite; }
public EmployeeComplexUpdateTest( Employee originalEmployee, Object[] objectsToAdd, Object[] objectsToRemove) { super(originalEmployee); this.usesUnitOfWork = true; if (objectsToAdd != null) { for (int i = 0; i < objectsToAdd.length; i++) { Object objectToAdd = objectsToAdd[i]; if (objectToAdd instanceof Employee) { if (!originalEmployee.getManagedEmployees().contains(objectToAdd)) { managedEmployeesToAdd.add((Employee) objectToAdd); } else { throw new TestWarningException( "OriginalEmployee: " + originalEmployee + " already manages employee to be added: " + objectToAdd); } } else { // must be Phone if (!originalEmployee.getPhoneNumbers().contains(objectToAdd)) { phonesToAdd.add((PhoneNumber) objectToAdd); } else { throw new TestWarningException( "OriginalEmployee: " + originalEmployee + " already has the phonee to be added: " + objectToAdd); } } } } if (objectsToRemove != null) { for (int i = 0; i < objectsToRemove.length; i++) { Object objectToRemove = objectsToRemove[i]; if (objectToRemove instanceof Employee) { if (originalEmployee.getManagedEmployees().contains(objectToRemove)) { managedEmployeesToRemove.add((Employee) objectToRemove); } else { throw new TestWarningException( "OriginalEmployee: " + originalEmployee + " doesn't manage employee to be removed: " + objectToRemove); } } else { // must be Phone if (originalEmployee.getPhoneNumbers().contains(objectToRemove)) { phonesToRemove.add((PhoneNumber) objectToRemove); } else { throw new TestWarningException( "OriginalEmployee: " + originalEmployee + " doesn't have the phonee to be removed: " + objectToRemove); } } } } // generate a meaningful test name String employeeString = ""; if (managedEmployeesToAdd.size() > 0 || managedEmployeesToRemove.size() > 0) { String addString = ""; if (managedEmployeesToAdd.size() > 0) { addString = "add " + managedEmployeesToAdd.size(); } String removeString = ""; if (managedEmployeesToRemove.size() > 0) { removeString = "remove " + managedEmployeesToRemove.size(); } employeeString = addString + (addString.length() > 0 && removeString.length() > 0 ? " and " : " ") + removeString + " Employees"; } String phoneString = ""; if (phonesToAdd.size() > 0 || phonesToRemove.size() > 0) { String addString = ""; if (phonesToAdd.size() > 0) { addString = "add " + phonesToAdd.size(); } String removeString = ""; if (phonesToRemove.size() > 0) { removeString = "remove " + phonesToRemove.size(); } phoneString = addString + (addString.length() > 0 && removeString.length() > 0 ? " and " : "") + removeString + " Phones"; } setName( "EmployeeComplexUpdateTest: " + employeeString + (employeeString.length() > 0 && phoneString.length() > 0 ? "; " : "") + phoneString + ";"); setDescription( "The test updates original Employee object: " + originalObject.toString() + " from the database by adding and/or removing managedEmployees and/or PhoneNumbers and verifies that the object updated correctly."); }
public void test() { // setup Employee manager = new Employee(); manager.setFirstName("Manager"); Employee employee = new Employee(); employee.setFirstName("Employee"); UnitOfWork uow = getSession().acquireUnitOfWork(); uow.registerObject(manager); uow.registerObject(employee); uow.commit(); Vector pk = new Vector(1); pk.add(manager.getId()); version[0] = ((Long) getSession() .getDescriptor(Employee.class) .getOptimisticLockingPolicy() .getWriteLockValue(manager, pk, getAbstractSession())) .longValue(); // test1 - add managed employee, manager's version should increment. uow = getSession().acquireUnitOfWork(); Employee managerClone = (Employee) uow.registerObject(manager); Employee employeeClone = (Employee) uow.registerObject(employee); managerClone.addManagedEmployee(employeeClone); uow.commit(); version[1] = ((Long) getSession() .getDescriptor(Employee.class) .getOptimisticLockingPolicy() .getWriteLockValue(manager, pk, getAbstractSession())) .longValue(); // test2 - alter managed employee, manager's version should NOT increment. uow = getSession().acquireUnitOfWork(); employeeClone = (Employee) uow.registerObject(employee); employeeClone.setFirstName("Altered"); uow.commit(); version[2] = ((Long) getSession() .getDescriptor(Employee.class) .getOptimisticLockingPolicy() .getWriteLockValue(manager, pk, getAbstractSession())) .longValue(); // test3- remove managed employee, manager's version should increment. uow = getSession().acquireUnitOfWork(); managerClone = (Employee) uow.registerObject(manager); employeeClone = (Employee) uow.registerObject(employee); managerClone.removeManagedEmployee(employeeClone); uow.commit(); version[3] = ((Long) getSession() .getDescriptor(Employee.class) .getOptimisticLockingPolicy() .getWriteLockValue(manager, pk, getAbstractSession())) .longValue(); PhoneNumber phone = new PhoneNumber("home", "613", "1111111"); // test4 - add phone, manager's version should increment. uow = getSession().acquireUnitOfWork(); managerClone = (Employee) uow.registerObject(manager); PhoneNumber phoneClone = (PhoneNumber) uow.registerObject(phone); managerClone.addPhoneNumber(phoneClone); uow.commit(); version[4] = ((Long) getSession() .getDescriptor(Employee.class) .getOptimisticLockingPolicy() .getWriteLockValue(manager, pk, getAbstractSession())) .longValue(); // test5- alter phone, manager's version should increment. uow = getSession().acquireUnitOfWork(); phoneClone = (PhoneNumber) uow.registerObject(phone); phoneClone.setType("work"); uow.commit(); version[5] = ((Long) getSession() .getDescriptor(Employee.class) .getOptimisticLockingPolicy() .getWriteLockValue(manager, pk, getAbstractSession())) .longValue(); // test6- remove phone, manager's version should increment. uow = getSession().acquireUnitOfWork(); managerClone = (Employee) uow.registerObject(manager); phoneClone = (PhoneNumber) uow.registerObject(phone); managerClone.removePhoneNumber(phoneClone); uow.commit(); version[6] = ((Long) getSession() .getDescriptor(Employee.class) .getOptimisticLockingPolicy() .getWriteLockValue(manager, pk, getAbstractSession())) .longValue(); }
public void test() { // clear cache getSession().getIdentityMapAccessor().initializeAllIdentityMaps(); // create batch read query, set its selectionCriteria ReadAllQuery query = new ReadAllQuery(Employee.class); setSelectionCriteria(query); // before adding batch read attributes clone the query to create control query ReadAllQuery controlQuery = (ReadAllQuery) query.clone(); // add batch read attributes Expression managedEmployees = query.getExpressionBuilder().get("managedEmployees"); Expression managedEmployeesPhoneNumbers = managedEmployees.get("phoneNumbers"); query.addBatchReadAttribute(managedEmployeesPhoneNumbers); // execute the query List employees = (List) getSession().executeQuery(query); if (employees.isEmpty()) { throw new TestProblemException("No Employees were read"); } // need to instantiate only a single Phone on a single managed Employee to trigger sql that // reads data from the db for all. // still need to trigger all the indirections - but (except the first one) they are not // accessing the db // (the data is already cached in the value holders). printDebug("Trigger batch reading results"); boolean isConnected = true; for (int i = 0; i < employees.size(); i++) { Employee manager = (Employee) employees.get(i); if (!manager.getManagedEmployees().isEmpty()) { printDebug("Manager = " + manager); for (int j = 0; j < manager.getManagedEmployees().size(); j++) { Employee emp = (Employee) manager.getManagedEmployees().get(j); printDebug(" " + emp); for (int k = 0; k < emp.getPhoneNumbers().size(); k++) { if (isConnected) { // need to instantiate only a single Phone on a single managed Employee to trigger // sql that reads data from the db for all. // to ensure that no other sql is issued close connection. ((AbstractSession) getSession()).getAccessor().closeConnection(); isConnected = false; } PhoneNumber phone = (PhoneNumber) emp.getPhoneNumbers().get(k); printDebug(" " + phone); } } } else { printDebug(manager.toString()); } } if (!isConnected) { // reconnect connection ((AbstractSession) getSession()) .getAccessor() .reestablishConnection((AbstractSession) getSession()); } printDebug(""); // obtain control results // clear cache getSession().getIdentityMapAccessor().initializeAllIdentityMaps(); // execute control query List controlEmployees = (List) getSession().executeQuery(controlQuery); // instantiate all value holders that the batch query expected to instantiate printDebug("Trigger control results"); for (int i = 0; i < controlEmployees.size(); i++) { Employee manager = (Employee) controlEmployees.get(i); if (!manager.getManagedEmployees().isEmpty()) { printDebug("Manager = " + manager); for (int j = 0; j < manager.getManagedEmployees().size(); j++) { Employee emp = (Employee) manager.getManagedEmployees().get(j); printDebug(" " + emp); for (int k = 0; k < emp.getPhoneNumbers().size(); k++) { PhoneNumber phone = (PhoneNumber) emp.getPhoneNumbers().get(k); printDebug(" " + phone); } } } else { printDebug(manager.toString()); } } // compare results String errorMsg = JoinedAttributeTestHelper.compareCollections( employees, controlEmployees, getSession().getClassDescriptor(Employee.class), ((AbstractSession) getSession())); if (errorMsg.length() > 0) { throw new TestErrorException(errorMsg); } }