Skip to content

Commit aefd6c2

Browse files
committed
Change the Java API to get rid of Factory and to use AutoCloseable Runner. robotframework#2090
1 parent 0327c73 commit aefd6c2

File tree

5 files changed

+74
-71
lines changed

5 files changed

+74
-71
lines changed

src/java/org/robotframework/RobotFramework.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515

1616
package org.robotframework;
1717

18-
import org.robotframework.RunnerFactory;
19-
import org.robotframework.RobotRunner;
20-
2118
/**
2219
*
2320
* Entry point for using Robot Framework from Java programs.
@@ -59,12 +56,8 @@ public static void main(String[] args) {
5956
* for meaning of different return codes.
6057
*/
6158
public static int run(String[] args) {
62-
RunnerFactory context = new RunnerFactory();
63-
RobotRunner runner = context.createRunner();
64-
try {
59+
try (RobotRunner runner = new RobotRunner()) {
6560
return runner.run(args);
66-
} finally {
67-
context.cleanup();
6861
}
6962
}
7063
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.robotframework;
2+
3+
/**
4+
* Interface used by {@link org.robotframework.RobotRunner} internally to
5+
* construct the Robot Framework Python class.
6+
*/
7+
public interface RobotPythonRunner {
8+
9+
public int run(String[] args);
10+
11+
}

src/java/org/robotframework/RobotRunner.java

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,68 @@
1515

1616
package org.robotframework;
1717

18+
import org.python.core.PyObject;
19+
import org.python.util.PythonInterpreter;
20+
1821
/**
19-
* An interface class that is used for creating a Jython object capable of
20-
* running Robot Framework.
22+
* AutoCloseable Interface class that internally creates a Jython interpreter,
23+
* allows running Robot tests with it, and cleans up the interpreter afterwards
24+
* in close.<p>
25+
*
26+
* Example:
27+
* <pre>
28+
*
29+
* {@code
30+
* try (RobotRunner runner = new RobotRunner()) {
31+
* runner.run(new String[] {"mytests.txt"});
32+
* }
33+
* }
34+
* </pre>
2135
*/
22-
public interface RobotRunner {
36+
public class RobotRunner implements AutoCloseable {
37+
38+
private RobotPythonRunner runner;
39+
private PythonInterpreter interpreter;
40+
41+
public RobotRunner() {
42+
interpreter = new PythonInterpreter();
43+
runner = createRunner();
44+
}
45+
46+
/**
47+
* Creates and returns an instance of the robot.JarRunner (implemented in
48+
* Python), which can be used to execute tests.
49+
*/
50+
private RobotPythonRunner createRunner() {
51+
PyObject runnerClass = importRunnerClass();
52+
PyObject runnerObject = runnerClass.__call__();
53+
return (RobotPythonRunner) runnerObject.__tojava__(RobotPythonRunner.class);
54+
}
55+
56+
private PyObject importRunnerClass() {
57+
interpreter.exec("import robot; from robot.jarrunner import JarRunner");
58+
return interpreter.get("JarRunner");
59+
}
2360

24-
public int run(String[] args);
61+
/**
62+
* Runs the tests, but does not cleanup the interpreter afterwards.
63+
*
64+
* @param args
65+
* The command line options to Robot Framework.
66+
*
67+
* @return Robot Framework return code. See
68+
* <a href="http://robotframework.org/robotframework/#user-guide"
69+
* target="_top">Robot Framework User Guide</a>
70+
* for meaning of different return codes.
71+
*/
72+
public int run(String[] args) {
73+
return runner.run(args);
74+
}
2575

76+
/**
77+
* Cleans up the Jython interpreter.
78+
*/
79+
public void close() {
80+
interpreter.cleanup();
81+
}
2682
}

src/java/org/robotframework/RunnerFactory.java

Lines changed: 0 additions & 57 deletions
This file was deleted.

src/robot/jarrunner.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from org.robotframework import RobotRunner
15+
from org.robotframework import RobotPythonRunner
1616

1717
from robot.errors import INFO_PRINTED
1818
from robot.libdoc import libdoc_cli
@@ -44,7 +44,7 @@
4444
"""
4545

4646

47-
class JarRunner(RobotRunner):
47+
class JarRunner(RobotPythonRunner):
4848
"""Used for Java-Jython interop when RF is executed from .jar file."""
4949
_commands = {'run': run_cli, 'rebot': rebot_cli, 'tidy': tidy_cli,
5050
'libdoc': libdoc_cli, 'testdoc': testdoc_cli}

0 commit comments

Comments
 (0)