aboutsummaryrefslogtreecommitdiffstats
path: root/lib/jinterface/test/jitu.erl
blob: 0de074a9c9cc7f9799fe050a7d878ed8c42f6f8f (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
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2004-2013. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%

%%----------------------------------------------------------------------
%% JInterface Test Utils
%%----------------------------------------------------------------------
-module(jitu).


-export([java/3,
	 java/4,
	 java/5,
	 init_all/1,
	 finish_all/1]).

%%
%% Lots of stuff here are originating from java_client_erl_server_SUITE.erl
%% (ic) ...
%%



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

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

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




init_all(Config) when list(Config) ->
    case find_executable(["java"]) of
	false -> {skip,"Found no Java VM"};
	Path -> [{java,Path}|Config]
    end.

finish_all(Config) -> Config.

%%
%% Internal stuff...
%%


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

to_string([H|T]) when integer(H) ->
    integer_to_list(H)++" "++to_string(T);
to_string([H|T]) when atom(H) ->
    atom_to_list(H)++" "++to_string(T);
to_string([H|T]) when pid(H) ->
    pid_to_list(H)++" "++to_string(T);
to_string([H|T]) when 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,Quote,EscSpace} =
	case os:type() of
	    {win32, _} -> {";","\"",""};
	    _          -> {":","","\\"}
	end,
    es(Dir++PS++
	filename:join([code:lib_dir(jinterface),"priv","OtpErlang.jar"])++PS++
	case os:getenv("CLASSPATH") of
	    false -> "";
	    Classpath -> Classpath
	end,
       Quote,
       EscSpace).

es(L,Quote,EscSpace) ->
    Quote++lists:flatmap(fun($ ) ->
				EscSpace++" ";
			   (C) ->
				[C]
			end,lists:flatten(L)) ++ Quote.

cmd(Cmd) ->
    PortOpts = [{line,80},eof,exit_status,stderr_to_stdout],
    io:format("cmd: ~s~n", [Cmd]),
    case catch open_port({spawn,Cmd}, PortOpts) of
	Port when port(Port) ->
	    Result = cmd_loop(Port, []),
	    io:format("cmd res: ~w~n", [Result]),
	    case Result of
		0 -> ok;
		ExitCode when 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([]) ->
    [].