views:

40

answers:

1

I am currently using an online system running in Google App Engine to enable students to practice coding in python. The system is inspired by codingbat by Nick Parlante and the public version of Beanshell running on appengine.

I would like to extend our system to support Java. In our current system, the Python doctest feature makes it very easy for instructors to type out a series of variable declarations and doctests which can then be executed against student-submitted code. It is very intuitive for the instructors which is important to ensure lots of people are writing tests.

How do I replace the following comments with Java code to eval and execute tests in Java?

String solution = “a=1;”;
solution += “b=1;”;

String problem = “self.assertEqual(a,1);”;
problem += “c=3;”;
problem += “self.assertEqual(b,2);”;

//eval the solution string
//eval the problem string possibly wrapped by additional junit or other Java code.
//results = Run all the tests
System.out.println(“Test       expected     received   result”);
//For result in results
//  print test, expected, received, result(pass/fail)

Desired output:

Test                   expected  received  result
self.assertEqual(a,1)  1         1         pass
self.assertEqual(b,2)  2         1         fail

Ideally any number of tests could be included in the problem string above and any number of Java statements could be included in the solution string in order to pass the tests included in the problem string.

+1  A: 

To the best of my knowledge, you can't. Compiling Java code at runtime requires access to APIs that aren't available on App Engine; that's why things like BeanShell and LOTRepls don't support Java.

Nick Johnson
What about setting setStrictJava(true) in beanshell? Do you think that would suffice to teach a good portion of Java syntax? Are there any GAE constraints that you are aware of?
Chris
I'm not familiar with beanshell, sorry, so I'm not the right person to ask. The only constraints that should matter are the class whitelist, and the inability to run commands (such as javac).
Nick Johnson
Thanks Nick. I can run setStrictJava(true) in beanshell in LOTRepls and ensure that the java syntax is obeyed. And it appears that I can also import static org.junit.Assert.*; Is there any architectural reason that I shouldn't be able to get Assert.assertEquals(x,y); to run under the Java version of GAE?
Chris
Not that I'm aware of. The issue is solely one of being able to compile Java source to bytecode on App Engine. If you're happy with a language like beanshell, which doesn't require that, you should be fine.
Nick Johnson