employees.put((Integer)args[2], detEmployee);
log.trace("Employee (after) :" + detEmployee);
break;
case ClearCache:
Cache cache = em.getEntityManagerFactory().getCache();
if (cache != null) {
cache.evictAll();
}
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 thread " + waitThreadid + " 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));
break;
case ResetException:
thisThread.throwable = null;
break;
case TestException:
List<Class<?>> expectedExceptions = null;
if (args.length > 2) {
expectedExceptions = new ArrayList<Class<?>>();
for (int i = 2; i < args.length; ++i) {
if (args[i] instanceof Object[]) {
for (Object o : (Object[]) args[i]) {
if (o != null && o instanceof Class) {
expectedExceptions
.add((Class<?>) o);
}
}
} else {
if (args[i] != null
&& args[i] instanceof Class) {
expectedExceptions
.add((Class<?>) args[i]);
}
}
}
}
int threadId = threadToRun;
if (args.length > 1) {
threadId = (Integer) args[1];
}
if( threadId != -1 ) {
// test exception on a specific thread
String testExClass = null;
Throwable curThrowable = null;
boolean exMatched = false;
TestThread exThread = threads.get(threadId);
curThrowable = exThread.throwable;
testExClass = processException(exThread, curAction, curThrowable);
if (expectedExceptions != null
&& expectedExceptions.size() > 0) {
for (Class<?> expectedException :
expectedExceptions) {
if (matchExpectedException(curAct, expectedException,
curThrowable)) {
exMatched = true;
break;
}
}
} else {
if (curThrowable == null) {
exMatched = true;
}
}
if (!exMatched) {
log.trace(testExClass);
if (curThrowable != null) {
logStack(curThrowable);
}
}
assertTrue(curAct + ":Expecting=" + expectedExceptions
+ ", Testing=" + testExClass, exMatched);
exThread.throwable = null;
} else {
// test exception in any thread; used for deadlock exception testing since db server
// decides on which thread to terminate if deadlock is detected.
if (expectedExceptions == null || expectedExceptions.size() == 0) {
// Expecting no exception in all threads.
boolean noExMatched = true;
String aTestExClass = "[";
for (TestThread aThread : threads) {
Throwable aThrowable = aThread.throwable;
aTestExClass += processException(aThread, curAction, aThrowable) + ", ";
if (aThrowable != null) {
noExMatched = false;
log.trace(aTestExClass);
logStack(aThrowable);
aThread.throwable = null;
}
}
assertTrue(curAct + ":Expecting=[no exception]"
+ ", Testing=" + aTestExClass + ']', noExMatched);
} else {
// Expecting any exception in any threads.
boolean aMatched = false;
String aTestExClass = "[";
for (TestThread aThread : threads) {
Throwable aThrowable = aThread.throwable;
aTestExClass += processException(aThread, curAction, aThrowable) + ", ";
for (Class<?> anExpectedException : expectedExceptions) {
if (matchExpectedException(curAct,
anExpectedException, aThrowable)) {
aMatched = true;
break;
}
}
if (aMatched) {
break;
} else {
if (aThrowable != null) {
logStack(aThrowable);
aThread.throwable = null;
}
}
}
if (!aMatched) {
log.trace(aTestExClass);
}
assertTrue(curAct + ":Expecting=" + expectedExceptions
+ ", Testing=" + aTestExClass + "]", aMatched);
}
}
break;
case TestInCache:
id = 1;
boolean expectedInCache = false;
if (args.length > 1) {
id = (Integer)args[1];
}
if (args.length > 2) {
expectedInCache = (Boolean)args[2];
}
Cache testCache = em.getEntityManagerFactory().getCache();
if (testCache != null) {
boolean inCache = testCache.contains(LockEmployee.class, id);
log.trace("cache.contains(Employee("+id+")) = " + inCache);
assertTrue(curAct + ":TestInCache Expecting=" + expectedInCache
+ ", Testing=" + inCache + "]", inCache == expectedInCache);
} else {
log.info(curAct+":TestInCache - No Cache found.");