aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ic/test/java_client_erl_server_SUITE.erl
blob: 9fe52249bab1651ebd4b6f486e6af4a575fbbf15 (plain) (blame)
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
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 2003-2016. 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%
%%
%%
%%%----------------------------------------------------------------------
%%% Purpose : Test suite for the backends of the IDL compiler
%%%----------------------------------------------------------------------

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


-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([marshal_ll/1,marshal_ull/1,
	 marshal_l/1,marshal_ul/1,
	 marshal_s/1,marshal_us/1,
	 marshal_c/1,marshal_wc/1,
	 marshal_str/1,
	 marshal_any_3/1,marshal_any_2/1]).


%% Top of cases

suite() -> [{ct_hooks,[ts_install_cth]}].

all() -> 
    cases().

groups() -> 
    [].

init_per_group(_GroupName, Config) ->
    Config.

end_per_group(_GroupName, Config) ->
    Config.


cases() -> 
    [marshal_ll, marshal_ull, marshal_l, marshal_ul,
     marshal_s, marshal_us, marshal_c, marshal_wc,
     marshal_str, marshal_any_3, marshal_any_2].

init_per_suite(Config) when is_list(Config) ->
    case case code:priv_dir(jinterface) of
	     {error,bad_name} -> 
		 false;
	     P ->
		 case filelib:wildcard(filename:join(P, "*.jar")) of
		     [_|_] ->
			 true;
		     [] ->
			 false
		 end
	 end
	of
	true -> 
	    case find_executable(["java"]) of
		false -> 
		    {skip,"Found no Java VM"};
		Path -> 
		    [{java,Path}|Config]
	    end;
	false ->
	    {skip,"No jinterface application"}
    end.

    
find_executable([]) ->
    false;
find_executable([E|T]) ->
    case os:find_executable(E) of
	false -> find_executable(T);
	Path -> Path
    end.

end_per_suite(Config) -> Config.



%% Add/remove code path and watchdog before/after each test case.
%%
init_per_testcase(_Case, Config) ->
    DataDir = proplists:get_value(data_dir, Config),
    code:add_patha(DataDir),

    %% Since other test suites use the module m_i et,al, we have
    %% to make sure we are using the right modules.
    code:purge(m_i),
    code:purge(m_i_impl),
    code:purge(oe_java_erl_test),
    code:load_file(m_i),
    code:load_file(m_i_impl),
    code:load_file(oe_java_erl_test),

    WatchDog = test_server:timetrap(test_server:seconds(20)),
    [{watchdog, WatchDog}| Config].

end_per_testcase(_Case, Config) ->
    DataDir = proplists:get_value(data_dir, Config),
    code:del_path(DataDir),
    WatchDog = proplists:get_value(watchdog, Config),
    test_server:timetrap_cancel(WatchDog).



%%--------------------------------------------------------------------
%%
%% Test cases

%% Testing marshalling of IDL long long
marshal_ll(Config) when is_list(Config) ->
    DataDir = proplists:get_value(data_dir, Config),
    {ok,Server} = m_i:oe_create_link([], {local,marshal_ll}),
    ok = java(proplists:get_value(java, Config), DataDir, "JavaClient",
		    ["JavaClient",node(),erlang:get_cookie(),marshal_ll]),
    ok = m_i:stop(Server),
    ok.

%% Testing marshalling of IDL unsigned long long
marshal_ull(Config) when is_list(Config) ->
    DataDir = proplists:get_value(data_dir, Config),
    {ok,Server} = m_i:oe_create_link([], {local,marshal_ull}),
    ok = java(proplists:get_value(java, Config), DataDir, "JavaClient",
		    ["JavaClient",node(),erlang:get_cookie(),marshal_ull]),
    ok = m_i:stop(Server),
    ok.

%% Testing marshalling of IDL long
marshal_l(Config) when is_list(Config) ->
    DataDir = proplists:get_value(data_dir, Config),
    {ok,Server} = m_i:oe_create_link([], {local,marshal_l}),
    ok = java(proplists:get_value(java, Config), DataDir, "JavaClient",
		    ["JavaClient",node(),erlang:get_cookie(),marshal_l]),
    ok = m_i:stop(Server),
    ok.

%% Testing marshalling of IDL unsigned long
marshal_ul(Config) when is_list(Config) ->
    DataDir = proplists:get_value(data_dir, Config),
    {ok,Server} = m_i:oe_create_link([], {local,marshal_ul}),
    ok = java(proplists:get_value(java, Config), DataDir, "JavaClient",
		    ["JavaClient",node(),erlang:get_cookie(),marshal_ul]),
    ok = m_i:stop(Server),
    ok.

%% Testing marshalling of IDL short
marshal_s(Config) when is_list(Config) ->
    DataDir = proplists:get_value(data_dir, Config),
    {ok,Server} = m_i:oe_create_link([], {local,marshal_s}),
    ok = java(proplists:get_value(java, Config), DataDir, "JavaClient",
		    ["JavaClient",node(),erlang:get_cookie(),marshal_s]),
    ok = m_i:stop(Server),
    ok.

%% Testing marshalling of IDL unsigned short
marshal_us(Config) when is_list(Config) ->
    DataDir = proplists:get_value(data_dir, Config),
    {ok,Server} = m_i:oe_create_link([], {local,marshal_us}),
    ok = java(proplists:get_value(java, Config), DataDir, "JavaClient",
		    ["JavaClient",node(),erlang:get_cookie(),marshal_us]),
    ok = m_i:stop(Server),
    ok.

%% Testing marshalling of IDL char
marshal_c(Config) when is_list(Config) ->
    DataDir = proplists:get_value(data_dir, Config),
    {ok,Server} = m_i:oe_create_link([], {local,marshal_c}),
    ok = java(proplists:get_value(java, Config), DataDir, "JavaClient",
		    ["JavaClient",node(),erlang:get_cookie(),marshal_c]),
    ok = m_i:stop(Server),
    ok.

%% Testing marshalling of IDL char
marshal_wc(Config) when is_list(Config) ->
    DataDir = proplists:get_value(data_dir, Config),
    {ok,Server} = m_i:oe_create_link([], {local,marshal_wc}),
    ok = java(proplists:get_value(java, Config), DataDir, "JavaClient",
		    ["JavaClient",node(),erlang:get_cookie(),marshal_wc]),
    ok = m_i:stop(Server),
    ok.

%% Testing marshalling of IDL string
marshal_str(Config) when is_list(Config) ->
    DataDir = proplists:get_value(data_dir, Config),
    {ok,Server} = m_i:oe_create_link([], {local,marshal_str}),
    ok = java(proplists:get_value(java, Config), DataDir, 
%%% 		    "-DOtpConnection.trace=4 "
		    "JavaClient",
		    ["JavaClient",node(),erlang:get_cookie(),marshal_str]),
    ok = m_i:stop(Server),
    ok.

%% Testing marshalling of IDL any
marshal_any_3(Config) when is_list(Config) ->
    DataDir = proplists:get_value(data_dir, Config),
    {ok,Server} = m_i:oe_create_link([], {local,marshal_any_3}),
    ok = java(proplists:get_value(java, Config), DataDir, "JavaClient",
		    ["JavaClient",node(),erlang:get_cookie(),marshal_any_3]),
    ok = m_i:stop(Server),
    ok.

marshal_any_2(Config) when is_list(Config) ->
    DataDir = proplists:get_value(data_dir, Config),
    {ok,Server} = m_i:oe_create_link([], {local,marshal_any_2}),
    ok = java(proplists:get_value(java, Config), DataDir, "JavaClient",
		    ["JavaClient",node(),erlang:get_cookie(),marshal_any_2]),
    ok = m_i:stop(Server),
    ok.

%%--------------------------------------------------------------------
%%
%% Utilities


java(Java, Dir, ClassAndArgs) ->
    cmd(Java++" -classpath \""++classpath(Dir)++"\" "++ClassAndArgs).

java(Java, Dir, Class, Args) ->
    java(Java, Dir, Class++" "++to_string(Args)).

to_string([H|T]) when is_integer(H) ->
    integer_to_list(H)++" "++to_string(T);
to_string([H|T]) when is_atom(H) ->
    atom_to_list(H)++" "++to_string(T);
to_string([H|T]) when is_list(H) ->
    lists:flatten(H)++" "++to_string(T);
to_string([]) -> [].

% javac(Dir, File) ->
%     cmd("javac -d "++Dir++" -classpath "++classpath(Dir)++" "++
% 	filename:join(Dir, File)).

classpath(Dir) ->
    PS = 
	case os:type() of
	    {win32, _} -> ";";
	    _          -> ":"
	end,
    Dir++PS++
	filename:join([code:lib_dir(ic),"priv","ic.jar"])++PS++
	filename:join([code:lib_dir(jinterface),"priv","OtpErlang.jar"])++PS++
	os:getenv("CLASSPATH", "").

cmd(Cmd) ->
    PortOpts = [{line,80},eof,exit_status,stderr_to_stdout],
    io:format("<cmd> ~ts~n", [Cmd]),
    case catch open_port({spawn,Cmd}, PortOpts) of
	Port when is_port(Port) ->
	    Result = cmd_loop(Port, []),
	    io:format("<cmd=~w>~n", [Result]),
	    case Result of
		0 -> ok;
		ExitCode when is_integer(ExitCode) -> {error,ExitCode};
		Error -> Error
	    end;
	{'EXIT',Reason} ->
	    {error,Reason}
    end.

cmd_loop(Port, Line) ->
    receive
	{Port,eof} ->
	    receive 
		{Port,{exit_status,ExitStatus}} ->
		    ExitStatus
	    after 1 -> 
		    undefined 
	    end;
	{Port,{exit_status,ExitStatus}} ->
	    receive 
		{Port,eof} -> 
		    ok after 1 -> ok end,
	    ExitStatus;
	{Port,{data,{Tag,Data}}} ->
	    case Tag of
		eol ->
		    io:put_chars([Line|cr_to_nl(Data)]),
		    io:nl(),
		    cmd_loop(Port, []);
		noeol ->
		    cmd_loop(Port, [Line|cr_to_nl(Data)])
	    end;
	{'EXIT',Port,Reason} ->
	    {error,Reason};
	Other ->
	    io:format("WARNING: Unexpected at ~s:~p: ~p~n", 
		      [?MODULE_STRING,?LINE,Other]),
	    cmd_loop(Port, Line)
    end.

%% Convert lonely CR to NL, and CRLF to NL
%%
cr_to_nl([$\r,$\n|T]) ->
    [$\n|cr_to_nl(T)];
cr_to_nl([$\r|T]) ->
    [$\n|cr_to_nl(T)];
cr_to_nl([C|T]) ->
    [C|cr_to_nl(T)];
cr_to_nl([]) ->
    [].