Skip to content

Commit 7cc0fbf

Browse files
committed
expand tests for fields and exceptions
1 parent 7973dc7 commit 7cc0fbf

File tree

1 file changed

+54
-8
lines changed

1 file changed

+54
-8
lines changed

test/runtests.jl

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ System = @jimport java.lang.System
3333
System_out = jfield(System, "out", @jimport java.io.PrintStream )
3434
@info "Java Version: ", jcall(System, "getProperty", JString, (JString,), "java.version")
3535

36+
@testset "JavaCall" begin
37+
3638
@testset "unsafe_strings_1" begin
3739
a=JString("how are you")
3840
@test Ptr(a) != C_NULL
@@ -92,11 +94,60 @@ end
9294
@test typeof(h)==jint
9395
end
9496

97+
@testset "exceptions_1" begin
98+
j_u_arrays = @jimport java.util.Arrays
99+
j_math = @jimport java.lang.Math
100+
j_is = @jimport java.io.InputStream
101+
102+
# JavaCall.JavaCallError("Error calling Java: java.lang.ArithmeticException: / by zero")
103+
@test_throws JavaCall.JavaCallError jcall(j_math, "floorDiv", jint, (jint, jint), 1, 0)
104+
@test isnothing(JavaCall.geterror())
105+
106+
# JavaCall.JavaCallError("Error calling Java: java.lang.ArrayIndexOutOfBoundsException: Array index out of range: -1")
107+
@test_throws JavaCall.JavaCallError jcall(j_u_arrays, "sort", Nothing, (Array{jint,1}, jint, jint), [10,20], -1, -1)
108+
@test isnothing(JavaCall.geterror())
109+
110+
# JavaCall.JavaCallError("Error calling Java: java.lang.IllegalArgumentException: fromIndex(1) > toIndex(0)")
111+
@test_throws JavaCall.JavaCallError jcall(j_u_arrays, "sort", Nothing, (Array{jint,1}, jint, jint), [10,20], 1, 0)
112+
@test isnothing(JavaCall.geterror())
113+
end
114+
115+
@testset "fields_1" begin
116+
JTest = @jimport(Test)
117+
t=JTest(())
118+
t_fields = Dict(getname(f) => f for f in listfields(t))
119+
120+
@testset "$ftype" for (name, ftype, valtest) in [ ("booleanField", jboolean, ==(true)) ,
121+
("integerField", jint, ==(100)) ,
122+
("stringField", JString, ==("A STRING")) ,
123+
("objectField", JObject, x -> Ptr(x) == C_NULL) ]
124+
@test valtest(jfield(t, name, ftype))
125+
@test valtest(t_fields[name](t))
126+
@test valtest(jfield(t, t_fields[name]))
127+
end
128+
129+
@test jfield(@jimport(java.lang.Math), "E", jdouble) == 2.718281828459045
130+
@test jfield(@jimport(java.lang.Math), "PI", jdouble) == 3.141592653589793
131+
@test jfield(@jimport(java.lang.Byte), "MAX_VALUE", jbyte) == 1<<7-1
132+
@test jfield(@jimport(java.lang.Integer), "MAX_VALUE", jint) == 1<<31-1
133+
@test jfield(@jimport(java.lang.Long), "MAX_VALUE", jlong) == 1<<63-1
134+
135+
j_l_bool = @jimport(java.lang.Boolean)
136+
@test jcall(jfield(j_l_bool, "TRUE", j_l_bool), "booleanValue", jboolean, ()) == true
137+
@test jcall(jfield(j_l_bool, "FALSE", j_l_bool), "booleanValue", jboolean, ()) == false
138+
139+
@test jfield(@jimport(java.text.NumberFormat), "INTEGER_FIELD", jint) == 0
140+
locale = @jimport java.util.Locale
141+
lc = jfield(locale, "CANADA", locale)
142+
@test jcall(lc, "getCountry", JString, ()) == "CA" #TODO: remove?
143+
#Instance field access
144+
end
145+
95146
#Test NULL
96147
@testset "null_1" begin
97148
H=@jimport java.util.HashMap
98149
a=jcall(T, "testNull", H, ())
99-
@test_throws ErrorException jcall(a, "toString", JString, ())
150+
@test_throws JavaCall.JavaCallError jcall(a, "toString", JString, ())
100151

101152
jlist = @jimport java.util.ArrayList
102153
@test jcall( jlist(), "add", jboolean, (JObject,), JObject(C_NULL)) === 0x01
@@ -171,15 +222,8 @@ end
171222
t=JTest(())
172223
inner = TestInner((JTest,), t)
173224
@test jcall(inner, "innerString", JString, ()) == "from inner"
174-
@test jfield(@jimport(java.lang.Math), "E", jdouble) == 2.718281828459045
175-
@test jfield(@jimport(java.lang.Math), "PI", jdouble) == 3.141592653589793
176-
@test jfield(@jimport(java.text.NumberFormat), "INTEGER_FIELD", jint) == 0
177-
Locale = @jimport java.util.Locale
178-
lc = jfield(@jimport(java.util.Locale), "CANADA", @jimport(java.util.Locale))
179-
#Instance field access
180225
#Disabled for now. Need to verify stability
181226
@test jfield(@jimport(java.util.logging.Logger), "GLOBAL_LOGGER_NAME", JString ) == "global"
182-
@test jcall(lc, "getCountry", JString, ()) == "CA"
183227
@test jfield(t, "integerField", jint) == 100
184228
@test jfield(t, "stringField", JString) == "A STRING"
185229
end
@@ -319,6 +363,8 @@ end
319363
end
320364
end
321365

366+
end
367+
322368
# Test downstream dependencies
323369
try
324370
using Pkg

0 commit comments

Comments
 (0)