fix test case problem for MySQL (microseconds are not stored in the MySQL Temporal column)

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@812689 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Fay Wang 2009-09-08 20:45:07 +00:00
parent 45f79b59af
commit 702a9c6616
5 changed files with 38 additions and 10 deletions

View File

@ -78,4 +78,26 @@ public class Employee {
}
return true;
}
/*
* The following change is for the comparison of Date object. In MySQL,
* "microseconds cannot be stored into a column of any temporal data type.
* Any microseconds part is discarded. " (http://dev.mysql.com/doc/refman/5.1/en/datetime.html).
* As a result, when the value retrieved from the database will be different from the
* original value in the memory for the loss of microsecond. The fix is to call toString
* (which will strip the microseconds0 on the Date object and compare the String values.
*/
public static Employee findEmpl(Map<EmployeePK, Employee> map, EmployeePK key) {
String name = key.getName();
String bDateStr = key.getBDay().toString();
Set<EmployeePK> keys = map.keySet();
for (EmployeePK thisKey : keys) {
if (name.equals(thisKey.getName()) &&
bDateStr.equals(thisKey.getBDay().toString())) {
return map.get(thisKey);
}
}
return null;
}
}

View File

@ -49,7 +49,7 @@ public class EmployeePK implements Serializable {
return false;
EmployeePK pk = (EmployeePK) o;
if (pk.name.equals(name) &&
pk.bDay.equals(bDay))
pk.bDay.toString().equals(bDay.toString()))
return true;
return false;
}

View File

@ -70,11 +70,15 @@ public class PhoneNumber {
(Collection<Map.Entry<EmployeePK, Employee>>) emps.entrySet();
for (Map.Entry<EmployeePK, Employee> entry : entries) {
EmployeePK key = entry.getKey();
Employee e0 = map.get(key);
Employee e = emps.get(key);
Employee e0 = Employee.findEmpl(map, key);
Employee e = Employee.findEmpl(emps, key);
if ((e == null && e0 != null) || (e != null && e0 == null))
return false;
if (!e.getEmpPK().equals(e0.getEmpPK()))
return false;
}
return true;
}
}

View File

@ -46,8 +46,8 @@ public class TestMany2ManyMapEx10 extends SQLListenerTestCase {
public List<EmployeePK> empPKs = new ArrayList<EmployeePK>();
public List<PhonePK> phonePKs = new ArrayList<PhonePK>();
public Map<EmployeePK, Employee> empMap =
new HashMap<EmployeePK, Employee>();
public Map<String, Employee> empMap =
new HashMap<String, Employee>();
public Map<PhonePK, PhoneNumber> phoneMap =
new HashMap<PhonePK, PhoneNumber>();
@ -190,7 +190,7 @@ public class TestMany2ManyMapEx10 extends SQLListenerTestCase {
EntityTransaction tran = em.getTransaction();
for (int i = 0; i < numEmployees; i++) {
Employee e = createEmployee(em, empId++);
empMap.put(e.getEmpPK(), e);
empMap.put(e.getEmpPK().getName(), e);
}
tran.begin();
em.flush();
@ -200,7 +200,8 @@ public class TestMany2ManyMapEx10 extends SQLListenerTestCase {
public Employee createEmployee(EntityManager em, int id) {
Employee e = new Employee();
EmployeePK empPK = new EmployeePK("e" + id, new Date());
Date bDay = new Date(System.currentTimeMillis() - 1000000);
EmployeePK empPK = new EmployeePK("e" + id, bDay);
empPKs.add(empPK);
e.setEmpPK(empPK);
e.setSalary(1000);
@ -264,7 +265,7 @@ public class TestMany2ManyMapEx10 extends SQLListenerTestCase {
public void assertEmployee(Employee e) throws Exception {
EmployeePK empPK = e.getEmpPK();
Employee e0 = empMap.get(empPK);
Employee e0 = empMap.get(empPK.getName());
Map<PhonePK, PhoneNumber> phones = e.getPhoneNumbers();
Map<PhonePK, PhoneNumber> phones0 = e0.getPhoneNumbers();
Assert.assertEquals(phones0.size(), phones.size());
@ -300,7 +301,8 @@ public class TestMany2ManyMapEx10 extends SQLListenerTestCase {
for (Map.Entry<EmployeePK, Employee> entry0 : entrySets0) {
EmployeePK key0 = entry0.getKey();
Employee e0 = entry0.getValue();
Employee e = es.get(key0);
//Employee e = es.get(key0);
Employee e = Employee.findEmpl(es, key0);
if (!e0.equals(e))
throw new Exception("Assertion failure");
}

View File

@ -85,7 +85,7 @@ public class TestMultipleEntityProjection extends SingleEMFTestCase {
} catch (ParseException e) {
mag.setDatePublished(null);
}
mag.setTsPublished(new Timestamp(System.currentTimeMillis()));
mag.setTsPublished(new Timestamp(System.currentTimeMillis() - 100000));
em.persist(pub);
}