package janino; import org.codehaus.janino.ClassBodyEvaluator; import org.codehaus.janino.CompileException; import org.codehaus.janino.SimpleCompiler; import junit.framework.TestCase; public class JaninoTest extends TestCase { static String code = " class Price {\n" + " public int getPriceA() {\n" + " return 1;\n" + " }\n" + " \n" + " public int getPriceB() {\n" + " return 2;\n" + " }\n" + " }\n" + "\n" + " // create an instance of the inner class \n" + " Price price;\n" + " \n" + " public int assign() {\n" + " // this should not compile\n" + " return price.Rate();\n" + " }\n" + " \n" + " int Rate () {\n" + " return 17;\n" + " }\n"; public void testCBE() { try { new ClassBodyEvaluator(code); // shouldn't compile fail(); } catch (CompileException exc) { // compile exception thrown, it was expected, test passes } catch (Exception exc) { // any other exception fails fail(); } } public void testSC() { try { new SimpleCompiler("code.txt", Janino.class.getResourceAsStream("/code.txt")); // shouldn't compile fail(); } catch (CompileException exc) { // compile exception thrown, it was expected, test passes } catch (Exception exc) { // any other exception fails fail(); } } }