long endTime = System.currentTimeMillis() + waitInMsec;
EntityManager em = null;
Integer id = 1;
LockEmployee employee = null;
LockModeType lockMode = null;
Act curAction = null;
int actIndex = 0;
Object[][] threadSequence = actions[threadToRun];
for (Object[] args : threadSequence) {
curAction = (Act) args[0];
String curAct = "Act[t" + threadToRun + ":" + (++actIndex) +"]=" + Arrays.toString(args);
log.trace("** " + curAct);
try {
switch (curAction) {
case CreateEm:
em = emf.createEntityManager();
break;
case CloseEm:
if (em != null && em.isOpen()) {
em.close();
em = null;
}
break;
case Clear:
em.clear();
break;
case Flush:
em.flush();
break;
case Find:
id = 1;
if (args.length > 1) {
id = (Integer)args[1];
}
employee = em.find(LockEmployee.class, id);
log.trace("Employee=" + employee);
if( employee != null ) {
employees.put(id, employee);
} else {
employees.remove(id);
}
break;
case FindWithLock:
id = 1;
if (args[1] != null) {
id = (Integer)args[1];
}
lockMode = LockModeType.NONE;
if (args[2] != null) {
lockMode = (LockModeType)args[2];
}
Map<String, Object> findProps = buildPropsMap(args, 3);
if (findProps != null) {
employee = em.find(LockEmployee.class, id,
lockMode, findProps);
} else {
employee = em
.find(LockEmployee.class, id, lockMode);
}
log.trace("Employee=" + employee);
if( employee != null ) {
employees.put(id, employee);
} else {
employees.remove(id);
}
break;
case FindObject:
em.find((Class<?>)args[1], args[2],
(LockModeType) args[3]);
// log.trace("Employee=" + employee);
break;
case NamedQueryWithLock:
String namedQuery = "????";
if (args.length > 1) {
namedQuery = (String)args[1];
}
id = 1;
if (args.length > 2) {
id = (Integer)args[2];
}
lockMode = null;
if (args.length > 3) {
lockMode = (LockModeType)args[3];
}
Map<String, Object> queryProps = buildPropsMap(args, 4);
//TypedQuery<LockEmployee> q = em.createNamedQuery(namedQuery, LockEmployee.class);
Query q = em.createNamedQuery(namedQuery);
if( lockMode != null) {
q.setLockMode(lockMode);
}
if( queryProps != null) {
for( String name : queryProps.keySet()) {
q.setHint(name, queryProps.get(name));
}
}
q.setParameter("id", id);
employee = (LockEmployee)q.getSingleResult();
log.trace("Employee=" + employee);
if( employee != null ) {
employees.put(id, employee);
} else {
employees.remove(id);
}
break;
case Persist:
id = 1;
if (args[1] != null) {
id = (Integer)args[1];
}
String firstName = (String)args[2];
employee = new LockEmployee();
employee.setId(id);
employee.setFirstName(firstName);
log.trace("Employee=" + employee);
em.persist(employee);
break;
case Remove:
id = 1;
if (args.length > 1) {
id = (Integer)args[1];
}
employee = employees.get(id);
log.trace("Employee=" + employee);
em.remove(employee);
break;
case Refresh:
id = 1;
if (args.length > 1) {
id = (Integer)args[1];
}
employee = employees.get(id);
log.trace("Employee(before)=" + employee);
em.refresh(employee);
log.trace("Employee(after) =" + employee);
break;
case RefreshWithLock:
id = 1;
if (args[1] != null) {
id = (Integer)args[1];
}
lockMode = LockModeType.NONE;
if (args[2] != null) {
lockMode = (LockModeType)args[2];
}
employee = employees.get(id);
log.trace("Employee(before)=" + employee);
Map<String, Object> refreshProps = buildPropsMap(args,
3);
if (refreshProps != null) {
em.refresh(employee, lockMode, refreshProps);
} else {
em.refresh(employee, lockMode);
}
log.trace("Employee(after) =" + employee);
break;
case RefreshObject:
em.refresh(args[1], (LockModeType) args[2]);
break;
case Lock:
id = 1;
if (args[1] != null) {
id = (Integer)args[1];
}
lockMode = LockModeType.NONE;
if (args[2] != null) {
lockMode = (LockModeType)args[2];
}
employee = employees.get(id);
log.trace("Employee=" + employee);
Map<String, Object> lockProps = buildPropsMap(args, 3);
if (lockProps != null) {
em.lock(employee, lockMode, lockProps);
} else {
em.lock(employee, lockMode);
}
break;
case LockObject:
em.lock(args[1], (LockModeType) args[2]);
break;
case UpdateEmployee:
id = 1;
if (args.length > 1) {
id = (Integer) args[1];
}
employee = employees.get(id);
log.trace("Employee (before):" + employee);
String newFirstName = "Unknown";
if (args.length > 2) {
newFirstName = (String) args[2];
} else {
newFirstName = (new Date()).toString();
}
employee.setFirstName(newFirstName);
log.trace("Employee (after) :" + employee);
break;
case Detach:
id = 1;
if (args[1] != null) {
id = (Integer)args[1];
}
employee = employees.get(id);
log.trace("Employee (before) :" + employee);
LockEmployee detEmployee = ((OpenJPAEntityManager) em
.getDelegate()).detachCopy(employee);
employees.put((Integer)args[2], detEmployee);
log.trace("Employee (after) :" + detEmployee);
break;
case StartTx:
em.getTransaction().begin();
break;
case CommitTx:
em.getTransaction().commit();
break;
case RollbackTx:
em.getTransaction().rollback();
break;
case NewThread:
int childToRun = (Integer) args[1];
TestThread t1 = new TestThread(childToRun, actions);
threads.add(t1);
break;
case StartThread:
threads.get((Integer) args[1]).start();
break;
case Notify:
// sleep and let other threads has a chance to wait,
// otherwise this notify may trigger before the other
// thread has a chance to wait.
Thread.sleep(500);
int notifyThreadid = 0;
if (args.length > 1 && args[1] != null) {
notifyThreadid = (Integer) args[1];
}
if (args.length > 2) {
Thread.sleep((Integer) args[2]);
}
if( notifyThreadid == 0) {
notifyParent();
} else {
threads.get(notifyThreadid).notifyThread();
}
break;
case Wait:
int waitThreadid = threadToRun;
if (args.length > 1 && args[1] != null) {
waitThreadid = (Integer) args[1];
}
int waitTime = (int)(waitInMsec / 5);
if (args.length > 2 && args[2] != null) {
waitTime = (Integer) args[2];
}
if (waitTime < MinThreadWaitInMs / 2)
waitTime = MinThreadWaitInMs / 2;
log.trace(">> Started wait for " + waitTime + " ms");
if( waitThreadid != 0) {
thisThread.wait(waitTime);
} else {
synchronized (this) {
wait(waitTime);
}
}
log.trace("<< Ended wait");
break;
case EmployeeNotNull:
id = 1;
if (args[1] != null) {
id = (Integer)args[1];
}
employee = employees.get(id);
assertNotNull(curAct, employee);
break;
case TestEmployee:
id = 1;
if (args[1] != null) {
id = (Integer)args[1];
}
employee = employees.get(id);
switch (args.length) {
case 4:
if (args[3] != null) {
assertEquals(curAct, saveVersion
+ (Integer) args[3], employee.getVersion());
}
case 3:
if (args[2] != null) {
assertEquals(curAct, (String) args[2], employee
.getFirstName());
}
case 2:
if (args[1] != null) {
assertEquals(curAct, id.intValue(),
employee.getId());
}
break;
case 1:
assertNull(curAct, employee);
}
break;
case SaveVersion:
id = 1;
if (args.length > 1) {
id = (Integer) args[1];
}
employee = employees.get(id);
saveVersion = employee.getVersion();
log.trace("save version= " + saveVersion);
break;
case TestVersion:
id = 1;
if (args[1] != null) {
id = (Integer) args[1];
}
int increment = (Integer)args[2];
employee = employees.get(id);
log.trace("test version: expected="
+ (saveVersion + increment) + ", testing="
+ employee.getVersion());
assertEquals(curAct, saveVersion + increment, employee
.getVersion());
break;
case TestLockMode:
id = 1;
if (args[1] != null) {
id = (Integer) args[1];
}
employee = employees.get(id);
LockModeType expectedlockMode = (LockModeType)args[2];
LockModeType testinglockMode = em.getLockMode(employee);
log.trace("test version: expected=" + expectedlockMode
+ ", testing=" + testinglockMode);
assertEquals(curAct, getCanonical(expectedlockMode),
getCanonical(testinglockMode));