Skip to content

Commit 74aabfd

Browse files
committed
Fix MATLAB context creation in tests
Consolidated context management to setup/teardown methods.
1 parent 6f360a2 commit 74aabfd

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

src/test/java/org/scijava/plugins/scripting/matlab/MATLABIT.java

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
import javax.script.ScriptEngine;
4343
import javax.script.ScriptException;
4444

45+
import org.junit.After;
46+
import org.junit.Before;
4547
import org.junit.Test;
4648
import org.scijava.Context;
4749
import org.scijava.script.ScriptLanguage;
@@ -55,6 +57,23 @@
5557
*/
5658
public class MATLABIT {
5759

60+
private Context context;
61+
private ScriptService scriptService;
62+
63+
@Before
64+
public void setUp() {
65+
context = new Context();
66+
scriptService = context.getService(ScriptService.class);
67+
68+
}
69+
70+
@After
71+
public void tearDown() {
72+
context.dispose();
73+
context = null;
74+
scriptService = null;
75+
}
76+
5877
/**
5978
* Simple script test for executing a basic MATLAB command and checking the
6079
* return value.
@@ -63,12 +82,10 @@ public class MATLABIT {
6382
public void testBasic() throws InterruptedException, ExecutionException,
6483
IOException, ScriptException
6584
{
66-
final Context context = new Context(ScriptService.class);
67-
final ScriptService scriptService = context.getService(ScriptService.class);
6885
final String script = "(1+2)";
6986
final ScriptModule m = scriptService.run("add.m", script, true).get();
70-
assertTrue(equalDoubleArrays(new double[] { 3.0 }, (double[]) ((Object[]) m
71-
.getReturnValue())[0]));
87+
assertTrue(equalDoubleArrays(new double[] { 3.0 }, (double[]) m
88+
.getReturnValue()));
7289
}
7390

7491
/**
@@ -79,22 +96,17 @@ public void testBasic() throws InterruptedException, ExecutionException,
7996
public void testBasicMultiline() throws InterruptedException,
8097
ExecutionException, IOException, ScriptException
8198
{
82-
final Context context = new Context(ScriptService.class);
83-
final ScriptService scriptService = context.getService(ScriptService.class);
8499
final String script = "(1+...\n2)";
85100
final ScriptModule m = scriptService.run("add.m", script, true).get();
86-
assertTrue(equalDoubleArrays(new double[] { 3.0 }, (double[]) ((Object[]) m
87-
.getReturnValue())[0]));
101+
assertTrue(equalDoubleArrays(new double[] { 3.0 }, (double[]) m
102+
.getReturnValue()));
88103
}
89104

90105
/**
91106
* Test the setting and retrieval of local variables within MATLAB.
92107
*/
93108
@Test
94109
public void testLocals() throws ScriptException {
95-
final Context context = new Context(ScriptService.class);
96-
final ScriptService scriptService = context.getService(ScriptService.class);
97-
98110
final ScriptLanguage language = scriptService.getLanguageByExtension("m");
99111
final ScriptEngine engine = language.getScriptEngine();
100112
assertEquals(MATLABScriptEngine.class, engine.getClass());
@@ -117,8 +129,6 @@ public void testLocals() throws ScriptException {
117129
public void testIfElse() throws InterruptedException, ExecutionException,
118130
IOException, ScriptException
119131
{
120-
final Context context = new Context(ScriptService.class);
121-
final ScriptService scriptService = context.getService(ScriptService.class);
122132
final String script =
123133
"if (1 == 2)\n" + " testVar = 75\n" + "else\n" + " testVar = 42\n"
124134
+ "end\n";
@@ -141,8 +151,6 @@ public void testIfElse() throws InterruptedException, ExecutionException,
141151
public void testComments() throws IOException, ScriptException,
142152
InterruptedException, ExecutionException
143153
{
144-
final Context context = new Context(ScriptService.class);
145-
final ScriptService scriptService = context.getService(ScriptService.class);
146154
final String script =
147155
"% comment line one\n" + "% comment line two\n" + "if (1 == 2)\n"
148156
+ " testVar = 75\n" + "else\n" + "% comment line three\n"

0 commit comments

Comments
 (0)