Rename REAMDE to README.md
[nit.git] / tests / test_jvm.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
4 # Copyright 2014 Romain Chanoir <romain.chanoir@courrier.uqam.ca>
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 # No alt works
19 # alt1 sets a wrong classpath and should fail on class loading
20 # alt2 looks for a non existant class
21 # alt3-4 looks for non existant methods
22
23 import jvm
24
25 redef extern class JniEnv
26 fun print_error(msg: String)
27 do
28 print "Error: {msg}"
29 if "NIT_TESTING".environ != "true" then exception_describe
30 exit 1
31 end
32 end
33
34 print "Compilation des classes Java ..."
35 assert sys.system("javac test_jvm/Queue.java") == 0
36 assert sys.system("javac test_jvm/Test2.java") == 0
37 assert sys.system("javac test_jvm/TestJvm.java") == 0
38
39 print "Initialisation de la JVM ..."
40
41 var builder = new JavaVMBuilder
42 builder.options.add "-Djava.class.path=."
43 #alt1#builder.options.clear
44 #alt1#builder.options.add "-Djava.class.path=/tmp/"
45 var jvm = builder.create_jvm
46 var env = builder.jni_env
47 assert env != null
48
49 # Test JavaVM::env
50 assert not jvm.env.address_is_null
51
52 print "---------------------Test 1----------------------"
53 # get the class
54 var queue_c = env.find_class("test_jvm/Queue")
55 #alt2#queue_c = env.find_class("test_jvm/Queue_error_in_name")
56 if queue_c.address_is_null then env.print_error("Queue class not found")
57
58 # get the methods of the class
59 var f_init = env.get_method_id(queue_c, "<init>", "()V")
60 if f_init.address_is_null then env.print_error("fInit not found")
61
62 var f_push = env.get_method_id(queue_c, "push", "(Ljava/lang/String;)V")
63 #alt3#f_push = env.get_method_id(queue_c, "push_error", "(Ljava/lang/String;)V")
64 #alt4#f_push = env.get_method_id(queue_c, "push", "(Ljava/lang/String;ZZZ)V")
65 if f_push.address_is_null then env.print_error("fPush not found")
66
67 var f_pop = env.get_method_id(queue_c, "pop", "()Ljava/lang/String;")
68 if f_pop.address_is_null then env.print_error("fPop not found")
69
70 var element1 = "premier"
71 var element2 = "deuxième"
72 var element3 = "troisième"
73
74 # instanciate class
75 var queue = env.new_object(queue_c, f_init)
76 if queue.address_is_null then env.print_error("object not initialized")
77
78 var arg_tab = new Array[Object].with_items(element1)
79
80 # first call to push method
81 env.call_void_method(queue, f_push, arg_tab)
82 arg_tab = new Array[Object].with_items(element2)
83 env.call_void_method(queue, f_push, arg_tab)
84 arg_tab = new Array[Object].with_items(element3)
85 env.call_void_method(queue, f_push, arg_tab)
86
87 var el = env.call_string_method(queue, f_pop, null)
88 print el.to_s
89 el = env.call_string_method(queue, f_pop, null)
90 print el.to_s
91 el = env.call_string_method(queue, f_pop, null)
92 print el.to_s
93
94 print "--------------------Test 2---------------------"
95
96 var test_c = env.find_class("test_jvm/TestJvm")
97 if test_c.address_is_null then env.print_error("TestJvm class not found")
98
99 f_init = env.get_method_id(test_c, "<init>", "()V")
100 if f_init.address_is_null then env.print_error("finit not found")
101
102 # Get the different methods ids
103 var m_bool = env.get_method_id(test_c, "isBool", "()Z")
104 if m_bool.address_is_null then env.print_error("mbool not found")
105 var m_char = env.get_method_id(test_c, "getC", "()C")
106 if m_char.address_is_null then env.print_error("mchar not found")
107 var m_i = env.get_method_id(test_c, "getI", "()I")
108 if m_i.address_is_null then env.print_error ("mi not found")
109 var m_f = env.get_method_id(test_c, "getF", "()F")
110 if m_f.address_is_null then env.print_error("mf not found")
111 var m_test = env.get_method_id(test_c, "getTest", "()Ltest_jvm/Test2;")
112 if m_test.address_is_null then env.print_error("mtest not found")
113
114 # Get the Field ids
115 var f_bool =env.get_field_id(test_c, "bool", "Z")
116 if f_bool.address_is_null then env.print_error("fbool not found")
117 var f_char = env.get_field_id(test_c, "c", "C")
118 if f_char.address_is_null then env.print_error("fchar not found")
119 var f_i =env.get_field_id(test_c, "i", "I")
120 if f_i.address_is_null then env.print_error("fi not found")
121 var f_f = env.get_field_id(test_c, "f", "F")
122 if f_f.address_is_null then env.print_error("ff not found")
123 var f_test = env.get_field_id(test_c, "test", "Ltest_jvm/TestJvm;")
124
125 # Instanciate
126 var test = env.new_object(test_c, f_init)
127 if test.address_is_null then env.print_error("object test not initialized")
128
129 # Retrieve field value with field ids
130 var v_bool = env.get_boolean_field(test, f_bool)
131 var v_char = env.get_char_field(test, f_char)
132 var v_i = env.get_int_field(test, f_i)
133 var v_f = env.get_float_field(test, f_f)
134 var v_test1 = env.get_object_field(test, f_test)
135
136 # Set the new values for the fields
137 env.set_boolean_field(test, f_bool, true)
138 env.set_char_field(test, f_char, 'D')
139 env.set_int_field(test, f_i, 10)
140 env.set_float_field(test, f_f, 15.5)
141 env.set_object_field(test, f_test, test)
142
143 # get the values using getter method
144 v_bool = env.call_boolean_method(test, m_bool, null)
145 v_char = env.call_char_method(test, m_char, null)
146 v_i = env.call_int_method(test, m_i, null)
147 v_f = env.call_float_method(test, m_f, null)
148 var v_test2 = env.call_object_method(test, m_test, null)
149
150 # assert the values of the fields
151 print v_bool
152 print v_char
153 print v_i
154 print v_f
155
156 jvm.destroy