summaryrefslogtreecommitdiff
path: root/lib/jinterface/test/jinterface_SUITE.erl
blob: 5c4618dcdec71b4879282f6eaf8582900c588d78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2004-2013. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%
-module(jinterface_SUITE).

-export([all/0, suite/0,groups/0,init_per_group/2,end_per_group/2, 
	 init_per_suite/1, end_per_suite/1,
	 init_per_testcase/2, end_per_testcase/2]).

-export([transport_factory/1,
	 nodename/1, register_and_whereis/1, get_names/1, boolean_atom/1,
	 node_ping/1, mbox_ping/1,
	 java_erlang_send_receive/1,
	 java_internal_send_receive_same_node/1,
	 java_internal_send_receive_different_nodes/1,
	 java_internal_send_receive_self/1,
	 java_link_and_exit/1, erl_link_and_exit/1,
	 erl_link_java_exit/1, java_link_erl_exit/1,
	 internal_link_linking_exits/1, internal_link_linked_exits/1,
	 internal_unlink_linking_exits/1, internal_unlink_linked_exits/1,
	 normal_exit/1, kill_mbox/1,kill_erl_proc_from_java/1,
	 kill_mbox_from_erlang/1,
	 erl_exit_with_reason_any_term/1,
	 java_exit_with_reason_any_term/1,
	 status_handler_localStatus/1, status_handler_remoteStatus/1,
	 status_handler_connAttempt/1,
	 maps/1,
	 fun_equals/1,
	 core_match_bind/1
     ]).

-include_lib("common_test/include/ct.hrl").

-define(debug,true).
-ifdef(debug).
-define(dbg(Str,Args), io:format(Str,Args)).
-else.
-define(dbg(Str,Args), ok).
-endif.

-define(link_test_reason,link_test_reason).

%% Test cases in MboxSendReceive.java
-define(java_erlang_send_receive,1).
-define(java_internal_send_receive_same_node,2).
-define(java_internal_send_receive_different_nodes,3).
-define(java_internal_send_receive_self,4).

%% Test cases in MboxLinkUnlink.java
-define(java_link_and_exit, 1).
-define(erl_link_and_exit, 2).
-define(erl_link_java_exit, 3).
-define(java_link_erl_exit, 4).
-define(internal_link_linking_exits, 5).
-define(internal_link_linked_exits, 6).
-define(internal_unlink_linking_exits,7).
-define(internal_unlink_linked_exits,8).
-define(normal_exit,9).
-define(kill_mbox,10).
-define(kill_erl_proc_from_java,11).
-define(kill_mbox_from_erlang,12).
-define(erl_exit_with_reason_any_term,13).
-define(java_exit_with_reason_any_term,14).


%% Test cases in NodeStatusHandler.java
-define(status_handler_localStatus,1).
-define(status_handler_remoteStatus,2).
-define(status_handler_connAttempt,3).

%%%-----------------------------------------------------------------
%%% INIT/END
%%%-----------------------------------------------------------------
suite() -> [{ct_hooks,[ts_install_cth]}].

all() -> 
    lists:append([fundamental(), ping(), send_receive(),
		  link_unlink(), status_handler()]).

groups() -> 
    [].

init_per_group(_GroupName, Config) ->
    Config.

end_per_group(_GroupName, Config) ->
    Config.


fundamental() ->
    [
     transport_factory,    % TransportFactoryTest.java
     nodename,             % Nodename.java
     register_and_whereis, % RegisterAndWhereis.java
     get_names,            % GetNames.java
     boolean_atom,         % BooleanAtom.java
     maps,                 % Maps.java
     fun_equals,           % FunEquals.java
     core_match_bind       % CoreMatchBind.java
    ].

ping() ->
    [
     %% Implemented in NodePing.java
     node_ping,

     %% Implemented in MboxPing.java
     mbox_ping
     ].


send_receive() ->
    [
     %% Implemented in MboxSendReceive.java
     java_erlang_send_receive,
     java_internal_send_receive_same_node,
     java_internal_send_receive_different_nodes,
     java_internal_send_receive_self
    ].

%% Note:
%%
%% The test cases in MboxLinkUnlink.java and in
%% NodePing.java, all uses default cookie, and if there
%% is a problem with having the same default cookie in
%% erlang vs jinterface, e.g because the home directory
%% does not get the same in some cases on Windows
%% - they will all fail.

link_unlink() ->
    [
     %% Implemented in MboxLinkUnlink.java
     java_link_and_exit,
     erl_link_and_exit,
     erl_link_java_exit,
     java_link_erl_exit,
     internal_link_linking_exits,
     internal_link_linked_exits,
     internal_unlink_linking_exits,
     internal_unlink_linked_exits,
     normal_exit,
     kill_mbox,
     kill_erl_proc_from_java,
     kill_mbox_from_erlang,
     erl_exit_with_reason_any_term,
     java_exit_with_reason_any_term
    ].

status_handler() ->
    [
     %% Implemented in NodeStatusHandler.java
     status_handler_localStatus,
     status_handler_remoteStatus,
     status_handler_connAttempt
    ].


init_per_suite(Config) when is_list(Config) ->
    case case code:priv_dir(jinterface) of
	     {error,bad_name} -> false;
	     P -> filelib:is_dir(P) end of
	true ->
	    jitu:init_all(Config);
	false ->
	    {skip,"No jinterface application"}
    end.

end_per_suite(Config) when is_list(Config) ->
    jitu:finish_all(Config).

init_per_testcase(Case, _Config) 
  when Case =:= kill_mbox;
       Case =:= kill_mbox_from_erlang ->
    {skip, "Not yet implemented"};
init_per_testcase(_Case,Config) ->
    Dog = ?t:timetrap({seconds,30}),
    [{watch_dog,Dog}|Config].

end_per_testcase(_Case,Config) ->
    case whereis(erl_link_server) of
	undefined -> ok;
	Pid -> exit(Pid,kill)
    end,
    jitu:kill_all_jnodes(),
    ?t:timetrap_cancel(?config(watch_dog,Config)),
    ok.


%%%-----------------------------------------------------------------
%%% TEST CASES
%%%-----------------------------------------------------------------
transport_factory(doc) ->
    ["TransportFactoryTest.java: Test custom OTP Transport Factory"];
transport_factory(suite) ->
    [];
transport_factory(Config) when is_list(Config) ->
    ok = jitu:java(?config(java, Config),
		   ?config(data_dir, Config),
		   "TransportFactoryTest").

%%%-----------------------------------------------------------------
nodename(doc) ->
    ["Nodename.java: "
     "Test OtpNode.node(), OtpNode.alive() and OtpNode.host()"];
nodename(suite) ->
    [];
nodename(Config) when is_list(Config) ->
    [_,Host] = string:tokens(atom_to_list(node()),"@"),
    ok = jitu:java(?config(java, Config),
		   ?config(data_dir, Config),
		   "Nodename",
		   [list_to_atom(Host)]).

%%%-----------------------------------------------------------------
register_and_whereis(doc) ->
    ["RegisterAndWhereis.java: "
     "Test OtpNode.registerName(...), OtpMbox.registerName(...) and "
     "OtpNode.whereis(...)"];
register_and_whereis(suite) ->
    [];
register_and_whereis(Config) when is_list(Config) ->
    ok = jitu:java(?config(java, Config),
		   ?config(data_dir, Config),
		   "RegisterAndWhereis",
		   []).

%%%-----------------------------------------------------------------
get_names(doc) ->
    ["GetNames.java: "
     "Test OtpNode.getNames()"];
get_names(suite) ->
    [];
get_names(Config) when is_list(Config) ->
    ok = jitu:java(?config(java, Config),
		   ?config(data_dir, Config),
		   "GetNames",
		   []).

%%%-----------------------------------------------------------------
boolean_atom(doc) ->
    ["BooleanAtom.java: "
     "Test OtpErlangAtom.booleanValue()"];
boolean_atom(suite) ->
    [];
boolean_atom(Config) when is_list(Config) ->
    ok = jitu:java(?config(java, Config),
		   ?config(data_dir, Config),
		   "BooleanAtom",
		   []).

%%%-----------------------------------------------------------------
node_ping(doc) ->
    ["NodePing.java: "
     "Test OtpNode.ping(java.lang.String node, long timeout)"];
node_ping(suite) ->
    [];
node_ping(Config) when is_list(Config) ->
    ok = jitu:java(?config(java, Config),
		   ?config(data_dir, Config),
		   "NodePing",
		   [erlang:get_cookie(),node()]).

%%%-----------------------------------------------------------------
mbox_ping(doc) ->
    ["MboxPing.java: "
     "Test OtpNode.createMbox(...) and OtpMbox.ping(...)"];
mbox_ping(suite) ->
    [];
mbox_ping(Config) when is_list(Config) ->
    ok = jitu:java(?config(java, Config),
		   ?config(data_dir, Config),
		   "MboxPing",
		   [erlang:get_cookie(),node()]).

%%%-----------------------------------------------------------------
java_erlang_send_receive(doc) ->
    ["Test sending/receiving of erlang messages between erlang and java"];
java_erlang_send_receive(suite) ->
    [];
java_erlang_send_receive(Config) when is_list(Config) ->
    send_receive(?java_erlang_send_receive, fun echo_loop/0, Config).

echo_loop() ->
    receive
	{From,Msg} ->
	    ?dbg("erl_send_receive_server received ~p",[{From,Msg}]),
	    ?dbg("erl_send_receive_server sending ~p",[Msg]),
	    From ! Msg,
	    echo_loop();
	done ->
	    ok
    end.


%%%-----------------------------------------------------------------
java_internal_send_receive_same_node(doc) ->
    ["MboxSendReceive.java: "
     "Test sending/receiving of erlang messages between mboxes "
     "on the same java node."];
java_internal_send_receive_same_node(suite) ->
    [];
java_internal_send_receive_same_node(Config) when is_list(Config) ->
    send_receive(?java_internal_send_receive_same_node,
		 fun() -> receive done -> ok end end,
		 Config).

%%%-----------------------------------------------------------------
java_internal_send_receive_different_nodes(doc) ->
    ["MboxSendReceive.java: "
     "Test sending/receiving of erlang messages between mboxes "
     "on different java nodes."];
java_internal_send_receive_different_nodes(suite) ->
    [];
java_internal_send_receive_different_nodes(Config) when is_list(Config) ->
    send_receive(?java_internal_send_receive_different_nodes,
		 fun() -> receive done -> ok end end,
		 Config).

%%%-----------------------------------------------------------------
java_internal_send_receive_self(doc) ->
    ["MboxSendReceive.java: "
     "Test sending/receiving of erlang messages from an mbox to itself"];
java_internal_send_receive_self(suite) ->
    [];
java_internal_send_receive_self(Config) when is_list(Config) ->
    send_receive(?java_internal_send_receive_self,
		 fun() -> receive done -> ok end end,
		 Config).

%%%-----------------------------------------------------------------
java_link_and_exit(doc) ->
    ["MboxLinkUnlink.java: "
     "Test link between erlang process and java mailbox."
     "Java mailbox links and exits"];
java_link_and_exit(suite) ->
    [];
java_link_and_exit(Config) when is_list(Config) ->
    LinkFun =
	fun(Mbox) ->
		Mbox ! {?java_link_and_exit,self(),?link_test_reason},
		receive after infinity -> ok end
	end,
    erl_java_link(LinkFun,java_link_and_exit,Config).


%%%-----------------------------------------------------------------
erl_link_and_exit(doc) ->
    ["MboxLinkUnlink.java: "
     "Test link between erlang process and java mailbox."
     "Erlang process links and exits"];
erl_link_and_exit(suite) ->
    [];
erl_link_and_exit(Config) when is_list(Config) ->
    LinkFun = fun(Mbox) ->
		      link(Mbox),
		      Mbox ! {?erl_link_and_exit,self(),?link_test_reason},
		      receive ok -> ok end,
		      exit(?link_test_reason)
	      end,
    erl_java_link(LinkFun,erl_link_and_exit,Config).

%%%-----------------------------------------------------------------
erl_link_java_exit(doc) ->
    ["MboxLinkUnlink.java: "
     "Test link between erlang process and java mailbox."
     "Erlang process links and java mailbox exits"];
erl_link_java_exit(suite) ->
    [];
erl_link_java_exit(Config) when is_list(Config) ->
    LinkFun = fun(Mbox) ->
		      link(Mbox),
		      Mbox ! {?erl_link_java_exit,self(),?link_test_reason},
		      receive after infinity -> ok end
	      end,
    erl_java_link(LinkFun,erl_link_java_exit,Config).


%%%-----------------------------------------------------------------
java_link_erl_exit(doc) ->
    ["MboxLinkUnlink.java: "
     "Test link between erlang process and java mailbox."
     "Java mailbox links and erlang process exits"];
java_link_erl_exit(suite) ->
    [];
java_link_erl_exit(Config) when is_list(Config) ->
    LinkFun =
	fun(Mbox) ->
		Mbox ! {?java_link_erl_exit,self(),?link_test_reason},
		receive ok -> ok end,
		exit(?link_test_reason)
	end,
    erl_java_link(LinkFun,java_link_erl_exit,Config).

%%%-----------------------------------------------------------------
internal_link_linking_exits(doc) ->
    ["MboxLinkUnlink.java: "
     "Test link between two java mailboxes."
     "The mailbox which creates the link is the one exiting"];
internal_link_linking_exits(suite) ->
    [];
internal_link_linking_exits(Config) when is_list(Config) ->
     internal_link_unlink(?internal_link_linking_exits,
			  internal_link_linking_exits,
			  Config).

%%%-----------------------------------------------------------------
internal_link_linked_exits(doc) ->
    ["MboxLinkUnlink.java: "
     "Test link between two java mailboxes."
     "The mailbox which dies not create the link is the one exiting"];
internal_link_linked_exits(suite) ->
    [];
internal_link_linked_exits(Config) when is_list(Config) ->
    internal_link_unlink(?internal_link_linked_exits,
			 internal_link_linked_exits,
			 Config).

%%%-----------------------------------------------------------------
internal_unlink_linking_exits(doc) ->
    ["MboxLinkUnlink.java: "
     "Test link and unlink between two java mailboxes. "
     "Mailbox1 creates a link to mailbox2 and removes it. "
     "Then it creates another link and mailbox2 removes is. "
     "Finally mailbox1 exits - mailbox2 must survive"];
internal_unlink_linking_exits(suite) ->
    [];
internal_unlink_linking_exits(Config) when is_list(Config) ->
     internal_link_unlink(?internal_unlink_linking_exits,
			  internal_unlink_linking_exits,
			  Config).

%%%-----------------------------------------------------------------
internal_unlink_linked_exits(doc) ->
    ["MboxLinkUnlink.java: "
     "Test link and unlink between two java mailboxes. "
     "Mailbox1 creates a link to mailbox2 and removes it. "
     "Then it creates another link and mailbox2 removes is. "
     "Finally mailbox2 exits - mailbox1 must survive"];
internal_unlink_linked_exits(suite) ->
    [];
internal_unlink_linked_exits(Config) when is_list(Config) ->
    internal_link_unlink(?internal_unlink_linked_exits,
			 internal_unlink_linked_exits,
			 Config).

%%%-----------------------------------------------------------------
normal_exit(doc) ->
    ["MboxLinkUnlink.java: "
     "Test that mbox.close() uses exit reason 'normal', i.e. "
     "that linked processes are not terminated."];
normal_exit(suite) ->
    [];
normal_exit(Config) when is_list(Config) ->
    Fun =
	fun() ->
		register(erl_link_server,self()),
		process_flag(trap_exit,true),
		receive
		    {Main,Mbox} when is_pid(Main), is_pid(Mbox) ->
			?dbg("Erlang sending \"~p\"",[normal_exit]),
			link(Mbox),
			Pid = spawn_link(fun() ->
						 link(Mbox),
						 Mbox ! {?normal_exit},
						 receive after infinity -> ok end
					 end),
			receive
			    {'EXIT',Mbox,normal} ->
				%% Make sure that we don't get the
				%% exit signal from Pid, and Pid
				%% should still be alive.
				receive
				    {'EXIT',Pid,Reason} ->
					?dbg("Got unexpected exit signal: ~p",
					     [{'EXIT',Pid,Reason}]),
					exit({unexpected,{'EXIT',Pid,Reason}})
				after 500 ->
					true = erlang:is_process_alive(Pid),
					exit(Pid,kill)
				end,
				receive done -> Main ! done end
			after 1000 ->
				receive
				    Other ->
					?dbg("Got garbage when waiting for exit:"
					     " ~p", [Other]),
					Main ! done,
					exit({got_unexpected,Other})
				after 0 ->
					ok
				end
			end;
		    Other ->
			?dbg("Got garbage: ~p",[Other]),
			exit(Other)
		end
	end,

    spawn_link(Fun),
    ok = jitu:java(?config(java, Config),
		   ?config(data_dir, Config),
		   "MboxLinkUnlink",
		   [erlang:get_cookie(),node()]).


%%%-----------------------------------------------------------------
kill_mbox(doc) ->
    ["MboxLinkUnlink.java: "
     "Test that mbox.exit(new OtpErlangAtom(\"kill\") causes linked "
     "processes to exit with reason 'killed', which can be trapped."];
kill_mbox(suite) ->
    {skip, "Not yet implemented"};
kill_mbox(Config) when is_list(Config) ->
    Fun =
	fun() ->
		register(erl_link_server,self()),
		process_flag(trap_exit,true),
		receive
		    {Main,Mbox} when is_pid(Main), is_pid(Mbox) ->
			?dbg("Erlang sending \"~p\"",[kill_mbox]),
			Pid = spawn_link(fun() ->
						 process_flag(trap_exit,true),
						 link(Mbox),
						 Mbox ! {?kill_mbox},
						 receive
						     {'EXIT',Mbox,killed} ->
							 exit(correct_reason);
						     {'EXIT',Mbox,R} ->
							 exit({faulty_reason,R})
						 end
					 end),
			receive
			    {'EXIT',Pid,{faulty_reason,Reason}} ->
				receive done -> Main ! done end,
				exit({faulty_reason,Reason});
			    {'EXIT',Pid,im_killed} ->
				receive done -> Main ! done end
			after 1000 ->
				receive
				    Other ->
					?dbg("Got garbage when waiting for exit:"
					     " ~p", [Other]),
					Main ! done,
					exit({got_unexpected,Other})
				after 0 ->
					ok
				end
			end;
		    Other ->
			?dbg("Got garbage: ~p",[Other]),
			exit(Other)
		end
	end,

    spawn_link(Fun),
    ok = jitu:java(?config(java, Config),
		   ?config(data_dir, Config),
		   "MboxLinkUnlink",
		   [erlang:get_cookie(),node()]).

%%%-----------------------------------------------------------------
kill_erl_proc_from_java(doc) ->
    ["MboxLinkUnlink.java: "
     "Test that mbox.exit(pid, new OtpErlangAtom(\"kill\") causes erlang "
     "processes <pid> to be killed, even if trapping exits"];
kill_erl_proc_from_java(suite) ->
    [];
kill_erl_proc_from_java(Config) when is_list(Config) ->
    LinkFun = fun(Mbox) ->
		      process_flag(trap_exit,true),
		      link(Mbox),
		      Mbox ! {?kill_erl_proc_from_java, self()},
		      receive after infinity -> ok end
	      end,
    erl_java_link(LinkFun,kill_erl_proc_from_java,killed,Config).

%%%-----------------------------------------------------------------
kill_mbox_from_erlang(doc) ->
    ["MboxLinkUnlink.java: "
     "Test that exit(Mbox,kill) causes linked the Mbox to be killed, and"
     "linked processes to exit with reason 'killed', even if trapping exits"];
kill_mbox_from_erlang(suite) ->
    {skip, "Not yet implemented"};
kill_mbox_from_erlang(Config) when is_list(Config) ->
    LinkFun = fun(Mbox) ->
		      link(Mbox),
		      Mbox ! {?kill_mbox_from_erlang},
		      exit(Mbox,kill),
		      receive after infinity -> ok end
	      end,
    erl_java_link(LinkFun,kill_mbox_from_erlang,killed,Config).

%%%-----------------------------------------------------------------
erl_exit_with_reason_any_term(doc) ->
    ["MboxLinkUnlink.java: "
     "Test that any erlang term can be used as exit reason when erlang "
     "process exits and is linked to an mbox."];
erl_exit_with_reason_any_term(suite) ->
    [];
erl_exit_with_reason_any_term(Config) when is_list(Config) ->
    Reason = [hei,self(),{this,is,"a",[different,"reason"]}],
    LinkFun = fun(Mbox) ->
		      link(Mbox),
		      Mbox ! {?erl_exit_with_reason_any_term,self(),Reason},
		      receive ok -> ok end,
		      exit(Reason)
	      end,
    erl_java_link(LinkFun,erl_exit_with_reason_any_term,Reason,Config).

%%%-----------------------------------------------------------------
java_exit_with_reason_any_term(doc) ->
    ["MboxLinkUnlink.java: "
     "Test that any erlang term can be used as exit reason when mbox "
     "exits and is linked to an erlang process."];
java_exit_with_reason_any_term(suite) ->
    [];
java_exit_with_reason_any_term(Config) when is_list(Config) ->
    Reason = [hei,self(),{this,is,"a",[different,"reason"]}],
    LinkFun =
	fun(Mbox) ->
		Mbox ! {?java_exit_with_reason_any_term,self(),Reason},
		receive after infinity -> ok end
	end,
    erl_java_link(LinkFun,java_exit_with_reason_any_term,Reason,Config).


%%%-----------------------------------------------------------------
status_handler_localStatus(doc) ->
    ["NodeStatusHandler.java: "
     "Test OtpNode.registerStatusHandler(...) and the callback "
     "OtpNodeStatus.localStatus(...)"];
status_handler_localStatus(suite) ->
    [];
status_handler_localStatus(Config) when is_list(Config) ->
    spawn_link(fun() ->
		       erl_status_server([{opt,{localStatus,"javanode1",true}},
					  {localStatus,"javanode1",false}])
	       end),
    ok = jitu:java(?config(java, Config),
		   ?config(data_dir, Config),
		   "NodeStatusHandler",
		   [erlang:get_cookie(),node(),?status_handler_localStatus]).

%%%-----------------------------------------------------------------
status_handler_remoteStatus(doc) ->
    ["NodeStatusHandler.java: "
     "Test OtpNode.registerStatusHandler(...) and the callback "
     "OtpNodeStatus.remoteStatus(...)"];
status_handler_remoteStatus(suite) ->
    [];
status_handler_remoteStatus(Config) when is_list(Config) ->
    spawn_link(fun() ->
		       erl_status_server([{opt,{localStatus,"javanode1",true}},
					  {remoteStatus,"javanode2",true},
					  {remoteStatus,"javanode2",false}])
	       end),
    ok = jitu:java(?config(java, Config),
		   ?config(data_dir, Config),
		   "NodeStatusHandler",
		   [erlang:get_cookie(),node(),?status_handler_remoteStatus]).


%%%-----------------------------------------------------------------
status_handler_connAttempt(doc) ->
    ["NodeStatusHandler.java: "
     "Test OtpNode.registerStatusHandler(...) and the callback "
     "OtpNodeStatus.connAttempt(...)"];
status_handler_connAttempt(suite) ->
    [];
status_handler_connAttempt(Config) when is_list(Config) ->
    spawn_link(fun() ->
		       erl_status_server([{opt,{localStatus,"javanode1",true}},
					  {connAttempt,"unknown",true},
					  {connAttempt,"javanode3",false}])
	       end),
    ok = jitu:java(?config(java, Config),
		   ?config(data_dir, Config),
		   "NodeStatusHandler",
		   [erlang:get_cookie(),node(),?status_handler_connAttempt]).

%%%-----------------------------------------------------------------
maps(doc) ->
    ["Maps.java: "
     "Tests OtpErlangMap encoding, decoding, toString, get"];
maps(suite) ->
    [];
maps(Config) when is_list(Config) ->
    ok = jitu:java(?config(java, Config),
           ?config(data_dir, Config),
           "Maps",
           []).

%%%-----------------------------------------------------------------
fun_equals(doc) ->
    ["FunEquals.java: "
     "Test OtpErlangFun.equals()"];
fun_equals(suite) ->
    [];
fun_equals(Config) when is_list(Config) ->
    ok = jitu:java(?config(java, Config),
           ?config(data_dir, Config),
           "FunEquals",
           []).

%%%-----------------------------------------------------------------
core_match_bind(doc) ->
    ["CoreMatchBind.java: "
     "Test OtpErlangObject.match() and bind()"];
core_match_bind(suite) ->
    [];
core_match_bind(Config) when is_list(Config) ->
    ok = jitu:java(?config(java, Config),
           ?config(data_dir, Config),
           "CoreMatchBind",
           []).

%%%-----------------------------------------------------------------
%%% INTERNAL FUNCTIONS
%%%-----------------------------------------------------------------
send_receive(TestCaseTag,Fun,Config) ->
    spawn(fun() ->
		  register(erl_send_receive_server,self()),
		  receive
		      From when is_pid(From) ->
			  JavaNode = node(From),
			  [JavaNode] = nodes(hidden),
			  From ! {TestCaseTag,self()},
			  Fun(),
			  unregister(erl_send_receive_server)
		  end
	  end),
    ok = jitu:java(?config(java, Config),
		   ?config(data_dir, Config),
		   "MboxSendReceive",
		   [erlang:get_cookie(),node()]).


internal_link_unlink(Tag,Msg,Config) ->
    Fun =
	fun() ->
		register(erl_link_server,self()),
		process_flag(trap_exit,true),
		receive
		    {Main,Mbox} when is_pid(Main), is_pid(Mbox) ->
			?dbg("Erlang sending \"~p\"",[Msg]),
			Mbox ! {Tag,self(),?link_test_reason},
			receive done -> Main ! done end;
		    Other ->
			?dbg("Got garbage: ~p",[Other]),
			exit(Other)
		end
	end,

    spawn_link(Fun),
    ok = jitu:java(?config(java, Config),
		   ?config(data_dir, Config),
		   "MboxLinkUnlink",
		   [erlang:get_cookie(),node()]).


erl_java_link(LinkFun,Msg,Config) ->
    erl_java_link(LinkFun,Msg,?link_test_reason,Config).

erl_java_link(LinkFun,Msg,Reason,Config) ->
    Fun =
	fun() ->
		register(erl_link_server,self()),
		process_flag(trap_exit,true),
		receive
		    {Main,Mbox} when is_pid(Mbox), is_pid(Mbox) ->
			?dbg("Erlang sending \"~p\"",[Msg]),
			Pid = spawn_link(fun() -> LinkFun(Mbox) end),
			receive
			    {'EXIT',Pid,Reason} ->
				receive done -> Main ! done end
			after 1000 ->
				receive
				    Other ->
					?dbg("Got garbage when waiting for exit:"
					     " ~p", [Other]),
					Main ! done,
					exit({got_unexpected,Other})
				after 0 ->
					ok
				end
			end;
		    Other ->
			?dbg("Got garbage: ~p",[Other]),
			exit(Other)
		end
	end,

    spawn_link(Fun),
    ok = jitu:java(?config(java, Config),
		   ?config(data_dir, Config),
		   "MboxLinkUnlink",
		   [erlang:get_cookie(),node()]).

erl_status_server(List) ->
    register(erl_status_server,self()),
    erl_status_server(List,undefined).
erl_status_server([{opt,{Tag,NodeName,Up}},{Tag2,NodeName2,Up2}|Rest],_) ->
    receive
	{Tag,Node,Up,From} = M ->
	    ?dbg("erl_status_server got: ~p",[M]),
	    true = lists:prefix(NodeName,Node),
	    erl_status_server([{Tag2,NodeName2,Up2}|Rest],From);
	{Tag2,Node2,Up2,From2} = M2 ->
	    ?dbg("erl_status_server got: ~p",[M2]),
	    true = lists:prefix(NodeName2,Node2),
	    erl_status_server(Rest,From2)
    end;
erl_status_server([{Tag,NodeName,Up}|Rest],_) ->
    receive
	{Tag,Node,Up,From} = M ->
	    ?dbg("erl_status_server got: ~p",[M]),
	    true = lists:prefix(NodeName,Node),
	    erl_status_server(Rest,From);
	Other ->
	    ?dbg("erl_status_server got garbage: ~p",[Other]),
	    exit(Other)
    end;
erl_status_server([],From) ->
    From ! done.