Skip to content

Commit 78f857b

Browse files
committed
added tests for static method calls
1 parent d9ba051 commit 78f857b

4 files changed

Lines changed: 117 additions & 2 deletions

File tree

src/methodCallBaton.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,16 @@ void StaticMethodCallBaton::execute(JNIEnv *env) {
151151

152152
m_resultType = javaGetType(env, returnType);
153153
jobject result = env->CallObjectMethod(m_method, method_invoke, NULL, m_args);
154-
m_result = env->NewGlobalRef(result);
154+
155155
jthrowable err = env->ExceptionOccurred();
156156
if(err) {
157157
m_error = (jthrowable)env->NewGlobalRef(err);
158158
m_errorString = "Error running static method";
159+
env->ExceptionClear();
160+
return;
159161
}
162+
163+
m_result = env->NewGlobalRef(result);
160164
}
161165

162166
void InstanceMethodCallBaton::execute(JNIEnv *env) {
@@ -176,12 +180,16 @@ void InstanceMethodCallBaton::execute(JNIEnv *env) {
176180

177181
m_resultType = javaGetType(env, returnType);
178182
jobject result = env->CallObjectMethod(m_method, method_invoke, m_javaObject->getObject(), m_args);
179-
m_result = env->NewGlobalRef(result);
183+
180184
jthrowable err = env->ExceptionOccurred();
181185
if(err) {
182186
m_error = (jthrowable)env->NewGlobalRef(err);
183187
m_errorString = "Error running instance method";
188+
env->ExceptionClear();
189+
return;
184190
}
191+
192+
m_result = env->NewGlobalRef(result);
185193
}
186194

187195
NewInstanceBaton::NewInstanceBaton(

test/Test.class

289 Bytes
Binary file not shown.

test/Test.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ public Test() {}
66
public Test(int i) { this.i = i; }
77

88
public int getInt() { return i; }
9+
10+
public static String staticMethod() { return "staticMethod called"; }
11+
public static int staticMethod(int i) { return i + 1; }
12+
public static void staticMethodThrows(Exception ex) throws Exception { throw ex; }
913
}

test/java-callStaticMethod-test.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
2+
var java = require("./testHelpers").java;
3+
4+
var nodeunit = require("nodeunit");
5+
var util = require("util");
6+
7+
exports['Java - Call Static Method'] = nodeunit.testCase({
8+
"callStaticMethod": function(test) {
9+
java.callStaticMethod("Test", "staticMethod", function(err, result) {
10+
test.ok(result);
11+
test.equal(result, "staticMethod called");
12+
test.done();
13+
});
14+
},
15+
16+
"callStaticMethodSync": function(test) {
17+
var result = java.callStaticMethodSync("Test", "staticMethod");
18+
test.ok(result);
19+
test.equal(result, "staticMethod called");
20+
test.done();
21+
},
22+
23+
"callStaticMethod with args": function(test) {
24+
java.callStaticMethod("Test", "staticMethod", 42, function(err, result) {
25+
test.ok(result);
26+
test.equal(result, 43);
27+
test.done();
28+
});
29+
},
30+
31+
"callStaticMethodSync with args": function(test) {
32+
var result = java.callStaticMethodSync("Test", "staticMethod", 42);
33+
test.ok(result);
34+
test.equal(result, 43);
35+
test.done();
36+
},
37+
38+
"callStaticMethod bad class name": function(test) {
39+
java.callStaticMethod("BadClassName", "staticMethod", function(err, result) {
40+
test.ok(err);
41+
test.ok(!result);
42+
test.done();
43+
});
44+
},
45+
46+
"callStaticMethodSync bad class name": function(test) {
47+
test.throws(function() {
48+
java.callStaticMethodSync("BadClassName", "staticMethod");
49+
});
50+
test.done();
51+
},
52+
53+
"callStaticMethod bad arg types": function(test) {
54+
java.callStaticMethod("Test", "staticMethod", "z", function(err, result) {
55+
test.ok(err);
56+
test.ok(!result);
57+
test.done();
58+
});
59+
},
60+
61+
"callStaticMethodSync bad arg types": function(test) {
62+
test.throws(function() {
63+
java.callStaticMethodSync("Test", "staticMethod", "z");
64+
});
65+
test.done();
66+
},
67+
68+
"callStaticMethod bad number of args": function(test) {
69+
java.callStaticMethod("Test", "staticMethod", 42, "z", function(err, result) {
70+
test.ok(err);
71+
test.ok(!result);
72+
test.done();
73+
});
74+
},
75+
76+
"callStaticMethodSync bad number of args": function(test) {
77+
test.throws(function() {
78+
java.callStaticMethodSync("Test", "staticMethod", 42, "z");
79+
});
80+
test.done();
81+
},
82+
83+
"callStaticMethod exception thrown from method": function(test) {
84+
var ex = java.newInstanceSync("java.lang.Exception", "my exception");
85+
java.callStaticMethod("Test", "staticMethodThrows", ex, function(err, result) {
86+
test.ok(err);
87+
test.ok(err.toString().match(/my exception/));
88+
test.ok(!result);
89+
test.done();
90+
});
91+
},
92+
93+
"callStaticMethodSync exception thrown from method": function(test) {
94+
var ex = java.newInstanceSync("java.lang.Exception", "my exception");
95+
try {
96+
java.callStaticMethodSync("Test", "staticMethodThrows", ex);
97+
test.fail("should throw");
98+
} catch(err) {
99+
test.ok(err.toString().match(/my exception/));
100+
}
101+
test.done();
102+
},
103+
});

0 commit comments

Comments
 (0)