package modTransf.script;
import junit.framework.TestCase;
import modTransf.model.ModelHelper;
import modTransf.model.mockJavaModel.BankCAContainer;
import modTransf.model.mockJavaModel.Bank;
import modTransf.model.mockJavaModel.TreeModelContainer;
import modTransf.model.mockJavaModel.TreeNode;
import java.io.IOException;
import javax.script.CompiledScript;
import javax.script.ScriptException;
import javax.script.GenericScriptContext;
import javax.script.SimpleNamespace;
import javax.script.ScriptContext;
import javax.script.ScriptException;
import javax.script.Compilable;
import javax.script.ScriptEngineManager;
import javax.script.Namespace;
/**
* <p>Titre : TopmodL</p>
* <p>Description : </p>
* <p>Copyright : Copyright (c) 2004</p>
* <p>Soci�t� : Dream Factory</p>
* @author Cedric
* @version 1.0
*/
/**
* Test case for expression languages.
* This test case build a populated context.
* The sub classes should implement methods testing query with the ScriptEngine to test.
*
* <p>Titre : ModTransf</p>
* <p>Description : </p>
* <p>Copyright : Copyright (c) 2004</p>
* <p>Soci�t� : Dream Factory</p>
* @author Cedric
* @version 1.0
*/
public abstract class RegisteredScriptTestCase extends TestCase
{
/** The Script manager used to create scripts */
private ScriptEngineManager scriptManager = new ScriptEngineManager();
/** The compiler */
protected Compilable compiler;
/** The context used */
protected GenericScriptContext context;
/** The modelContainer used for tests */
protected ModelHelper bankModel;
/** The root of the bank model */
protected Bank bank;
/** The root of the mock model */
protected TreeModelContainer treeModel;
protected TreeNode tree;
protected String attr1 = new String("attr1");
protected String attr2 = new String("attr2");
protected String attr3 = new String("attr3");
protected String attr1Name = "attr1";
protected String attr2Name = "attr2";
protected String attr3Name = "attr3";
public RegisteredScriptTestCase (String name)
{
super(name);
}
/**
*
* @return String
*/
public String getLanguage()
{
return "el";
}
/**
* Create or get the compiler.
* Should be implemented by subclasses
* @return Compilable
*/
abstract protected Compilable createCompiler();
/*
{
return lookupCompiler("el");
}
*/
protected Compilable lookupCompiler( String name )
{
return (Compilable)scriptManager.getEngineByName(name);
}
/**
* Initialize the context for a new test.
* This method is called befor each test
* @throws IOException
*/
public void setUp() throws IOException
{
// Create compiler
compiler = createCompiler();
GenericScriptContext context = new GenericScriptContext();
Namespace globalNamespace = new SimpleNamespace();
context.setNamespace(globalNamespace, ScriptContext.GLOBAL_SCOPE);
Namespace localNamespace = new SimpleNamespace();
context.setNamespace(localNamespace, ScriptContext.ENGINE_SCOPE);
this.context = context;
// Bank model
BankCAContainer bankModel = new BankCAContainer();
this.bankModel = bankModel;
// Set a model
globalNamespace.put( "bankModel", bankModel );
// Set the root of the model as attribute
bank = bankModel.getBank();
localNamespace.put("bank", bankModel.getBank());
// Tree model
TreeModelContainer treeModel = new TreeModelContainer("root", 4);
this.treeModel = treeModel;
globalNamespace.put( "treeModel", treeModel );
tree = treeModel.getNode();
localNamespace.put("tree", treeModel.getNode() );
// Set attributes
localNamespace.put(attr1Name, attr1);
localNamespace.put(attr2Name, attr2);
localNamespace.put(attr3Name, attr3);
}
/**
* Tear down the test.
* This method is called after each test.
*/
public void tearDown()
{
compiler = null;
context = null;
bankModel = null;
treeModel = null;
}
public CompiledScript compileScript( String script)
{
try
{
CompiledScript res = compiler.compile(script );
assertNotNull( "Script '" + script + "'", res );
return res;
}
catch(ScriptException ex)
{
fail( "Script '" + script + "' - exception: " + ex.getMessage() );
return null;
}
}
public void failExprTest( String script)
{
try
{
CompiledScript res = compiler.compile(script);
fail( "Script '" + script + "' should fail " );
}
catch(ScriptException ex)
{ // Should normally fail
}
}
/**
* Parse the expression and evaluate it. Check if the result is equals to the
* expected result.
* @param bean Object The bean used as context when evaluating the expression.
* @param expr String The expression
* @param expectedResult Object The expected result.
* @return Object The result.
*/
public Object doQuery( Object bean, String expr, Object expectedResult)
{
CompiledScript cscript = compileScript(expr);
try
{
if(bean!=null)
{
context.setAttribute( "this", bean, ScriptContext.ENGINE_SCOPE );
context.setAttribute( "self", bean, ScriptContext.ENGINE_SCOPE );
}
Object res = cscript.eval(context);
assertEquals( expr, expectedResult, res);
return res;
}
catch(ScriptException ex)
{
ex.printStackTrace();
fail( "expr '" + expr + "' - exception: " + ex.getMessage() );
// Never return, but need it ...
return null;
}
}
public Object doQuery( String expr, Object expectedResult)
{
return doQuery( null, expr, expectedResult );
}
public void doFailQuery( String expr )
{
CompiledScript cscript = compileScript(expr);
try
{
Object res = cscript.eval(context);
fail( "expr '" + expr + "' should fail (got '" + res + "')" );
}
catch(ScriptException ex)
{ // Should fail with this exception
}
catch(Exception ex)
{
ex.printStackTrace();
fail( "expr '" + expr + "' fail with wrong exception - exception: " + ex.getMessage() );
}
}
/**
* get "attr1"
*/
public void testCompilerFound()
{
assertNotNull("Compiler found", compiler);
}
}