{
boolean bInit = m_globalEnv == null;
if (bInit)
{
m_globalEnv = new GlobalEnvironment(metadata.getGlobalEnvironment());
}
InvocationContext context = (InvocationContext)metadata.getComponent("System.InvocationContext").getInstance(null);
context.setAudited(false);
context.initialize(null, m_globalEnv);
context.setLocale(m_locale);
Machine machine = context.getMachine();
if (bInit)
{
InputStream istream = null;
Reader reader = null;
try
{
istream = URLUtil.openResource(UnitTest.class, INITIAL_SCRIPT);
reader = new BufferedReader(new InputStreamReader(istream, XMLUtil.ENCODING));
Intrinsic.load(reader, INITIAL_SCRIPT_URL, machine);
}
catch (IOException e)
{
throw new MetadataException("err.meta.resourceOpen", new Object[]{INITIAL_SCRIPT}, e);
}
finally
{
IOUtil.close(reader);
IOUtil.close(istream);
}
}
UnitTest utest = (UnitTest)m_unitTestList.get(nUnitTest);
for (Iterator argItr = new ArgumentIterator(context, utest); argItr.hasNext(); )
{
Object[] variableArray = (Object[])argItr.next();
String sTestArguments = formatLoopVariables(variableArray, utest.getLoopCount());
DataLoader dataLoader = new DataLoader(context);
URL dumpURL = getDumpURL(utest);
boolean bResetDB = dumpURL != null;
m_unitTestLogger.begin(utest, sTestArguments);
if (bResetDB && !bReady)
{
if (m_container != null)
{
m_container.suspend();
}
bReady = true;
if (nUnitTest == 0)
{
try
{
dataLoader.recreateSchema((Set)null);
}
catch (Throwable e)
{
m_unitTestLogger.err(utest, e);
return;
}
}
else
{
bFirstRun = false;
}
}
byte nMode = utest.getMode();
int nTestCase = 0;
if (utest.hasInitializer())
{
++nTestCase;
}
if (utest.hasFinalizer())
{
++nTestCase;
}
Object[] functionObjArray = null;
if (utest.getFunction() != null)
{
functionObjArray = (Object[])context.getMachine().invoke(utest.getFunction(), variableArray);
}
boolean bFirstTestCase = true;
for (Iterator itr = utest.getUnitTestCaseIterator(); itr.hasNext();
bFirstRun = false, bFirstTestCase = false, bResetDB = dumpURL != null && nMode != UnitTest.MODE_DIRTY)
{
UnitTestCase testCase = (UnitTestCase)itr.next();
if (!isEnabled(utest, testCase))
{
nTestCase++;
continue;
}
if (bResetDB)
{
if (!bFirstTestCase && m_container != null)
{
m_container.suspend();
}
if (!bFirstRun)
{
try
{
dataLoader.deleteData((Set)null);
}
catch (Throwable e)
{
m_unitTestLogger.err(utest, e);
return;
}
}
InputStream in = null;
try
{
in = new BufferedInputStream(URLUtil.openStream(dumpURL));
dataLoader.importData(in, true);
}
catch (Throwable e)
{
m_unitTestLogger.err(utest, e);
return;
}
finally
{
IOUtil.close(in);
in = null;
}
}
context.complete(true);
context.getMachine().cleanup();
context.initUnitOfWork();
final InvocationContext testContext = (InvocationContext)metadata
.getComponent("System.InvocationContext").getInstance(null);
try
{
testContext.setAudited(false);
testContext.getGlobalCache().clear();
testContext.initialize((m_sSystemUser == null) ? null : new SimplePrincipal(m_sSystemUser),
new GlobalEnvironment(m_globalEnv));
if (m_container != null && m_sSystemUser != null)
{
Event grantDebugEvent = testContext.getUserClass().findEvent(GRANT_DEBUG, 0);