1: %%
    2: %% %CopyrightBegin%
    3: %% 
    4: %% Copyright Ericsson AB 2003-2012. All Rights Reserved.
    5: %% 
    6: %% The contents of this file are subject to the Erlang Public License,
    7: %% Version 1.1, (the "License"); you may not use this file except in
    8: %% compliance with the License. You should have received a copy of the
    9: %% Erlang Public License along with this software. If not, it can be
   10: %% retrieved online at http://www.erlang.org/.
   11: %% 
   12: %% Software distributed under the License is distributed on an "AS IS"
   13: %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
   14: %% the License for the specific language governing rights and limitations
   15: %% under the License.
   16: %% 
   17: %% %CopyrightEnd%
   18: %%
   19: %%
   20: %%%----------------------------------------------------------------------
   21: %%% Purpose : Test suite for the backends of the IDL compiler
   22: %%%----------------------------------------------------------------------
   23: 
   24: -module(java_client_erl_server_SUITE).
   25: -include_lib("test_server/include/test_server.hrl").
   26: 
   27: 
   28: -export([all/0, suite/0,groups/0,init_per_group/2,end_per_group/2,
   29: 	 init_per_suite/1,end_per_suite/1,
   30: 	 init_per_testcase/2,end_per_testcase/2]).
   31: -export([marshal_ll/1,marshal_ull/1,
   32: 	 marshal_l/1,marshal_ul/1,
   33: 	 marshal_s/1,marshal_us/1,
   34: 	 marshal_c/1,marshal_wc/1,
   35: 	 marshal_str/1,
   36: 	 marshal_any_3/1,marshal_any_2/1]).
   37: 
   38: 
   39: %% Top of cases
   40: 
   41: suite() -> [{ct_hooks,[ts_install_cth]}].
   42: 
   43: all() -> 
   44:     cases().
   45: 
   46: groups() -> 
   47:     [].
   48: 
   49: init_per_group(_GroupName, Config) ->
   50:     Config.
   51: 
   52: end_per_group(_GroupName, Config) ->
   53:     Config.
   54: 
   55: 
   56: cases() -> 
   57:     [marshal_ll, marshal_ull, marshal_l, marshal_ul,
   58:      marshal_s, marshal_us, marshal_c, marshal_wc,
   59:      marshal_str, marshal_any_3, marshal_any_2].
   60: 
   61: init_per_suite(Config) when is_list(Config) ->
   62:     case case code:priv_dir(jinterface) of
   63: 	     {error,bad_name} -> 
   64: 		 false;
   65: 	     P ->
   66: 		 case filelib:wildcard(filename:join(P, "*.jar")) of
   67: 		     [_|_] ->
   68: 			 true;
   69: 		     [] ->
   70: 			 false
   71: 		 end
   72: 	 end
   73: 	of
   74: 	true -> 
   75: 	    case find_executable(["java"]) of
   76: 		false -> 
   77: 		    {skip,"Found no Java VM"};
   78: 		Path -> 
   79: 		    [{java,Path}|Config]
   80: 	    end;
   81: 	false ->
   82: 	    {skip,"No jinterface application"}
   83:     end.
   84: 
   85:     
   86: find_executable([]) ->
   87:     false;
   88: find_executable([E|T]) ->
   89:     case os:find_executable(E) of
   90: 	false -> find_executable(T);
   91: 	Path -> Path
   92:     end.
   93: 
   94: end_per_suite(Config) -> Config.
   95: 
   96: 
   97: 
   98: %% Add/remove code path and watchdog before/after each test case.
   99: %%
  100: init_per_testcase(_Case, Config) ->
  101:     DataDir = ?config(data_dir, Config),
  102:     code:add_patha(DataDir),
  103: 
  104:     %% Since other test suites use the module m_i et,al, we have
  105:     %% to make sure we are using the right modules.
  106:     code:purge(m_i),
  107:     code:purge(m_i_impl),
  108:     code:purge(oe_java_erl_test),
  109:     code:load_file(m_i),
  110:     code:load_file(m_i_impl),
  111:     code:load_file(oe_java_erl_test),
  112: 
  113:     WatchDog = test_server:timetrap(test_server:seconds(20)),
  114:     [{watchdog, WatchDog}| Config].
  115: 
  116: end_per_testcase(_Case, Config) ->
  117:     DataDir = ?config(data_dir, Config),
  118:     code:del_path(DataDir),
  119:     WatchDog = ?config(watchdog, Config),
  120:     test_server:timetrap_cancel(WatchDog).
  121: 
  122: 
  123: 
  124: %%--------------------------------------------------------------------
  125: %%
  126: %% Test cases
  127: 
  128: marshal_ll(doc) ->
  129:     ["Testing marshalling of IDL long long"];
  130: marshal_ll(suite) -> [];
  131: marshal_ll(Config) when is_list(Config) ->
  132:     ?line DataDir = ?config(data_dir, Config),
  133:     ?line {ok,Server} = m_i:oe_create_link([], {local,marshal_ll}),
  134:     ?line ok = java(?config(java, Config), DataDir, "JavaClient",
  135: 		    ["JavaClient",node(),erlang:get_cookie(),marshal_ll]),
  136:     ?line ok = m_i:stop(Server),
  137:     ok.
  138: 
  139: marshal_ull(doc) ->
  140:     ["Testing marshalling of IDL unsigned long long"];
  141: marshal_ull(suite) -> [];
  142: marshal_ull(Config) when is_list(Config) ->
  143:     ?line DataDir = ?config(data_dir, Config),
  144:     ?line {ok,Server} = m_i:oe_create_link([], {local,marshal_ull}),
  145:     ?line ok = java(?config(java, Config), DataDir, "JavaClient",
  146: 		    ["JavaClient",node(),erlang:get_cookie(),marshal_ull]),
  147:     ?line ok = m_i:stop(Server),
  148:     ok.
  149: 
  150: marshal_l(doc) ->
  151:     ["Testing marshalling of IDL long"];
  152: marshal_l(suite) -> [];
  153: marshal_l(Config) when is_list(Config) ->
  154:     ?line DataDir = ?config(data_dir, Config),
  155:     ?line {ok,Server} = m_i:oe_create_link([], {local,marshal_l}),
  156:     ?line ok = java(?config(java, Config), DataDir, "JavaClient",
  157: 		    ["JavaClient",node(),erlang:get_cookie(),marshal_l]),
  158:     ?line ok = m_i:stop(Server),
  159:     ok.
  160: 
  161: marshal_ul(doc) ->
  162:     ["Testing marshalling of IDL unsigned long"];
  163: marshal_ul(suite) -> [];
  164: marshal_ul(Config) when is_list(Config) ->
  165:     ?line DataDir = ?config(data_dir, Config),
  166:     ?line {ok,Server} = m_i:oe_create_link([], {local,marshal_ul}),
  167:     ?line ok = java(?config(java, Config), DataDir, "JavaClient",
  168: 		    ["JavaClient",node(),erlang:get_cookie(),marshal_ul]),
  169:     ?line ok = m_i:stop(Server),
  170:     ok.
  171: 
  172: marshal_s(doc) ->
  173:     ["Testing marshalling of IDL short"];
  174: marshal_s(suite) -> [];
  175: marshal_s(Config) when is_list(Config) ->
  176:     ?line DataDir = ?config(data_dir, Config),
  177:     ?line {ok,Server} = m_i:oe_create_link([], {local,marshal_s}),
  178:     ?line ok = java(?config(java, Config), DataDir, "JavaClient",
  179: 		    ["JavaClient",node(),erlang:get_cookie(),marshal_s]),
  180:     ?line ok = m_i:stop(Server),
  181:     ok.
  182: 
  183: marshal_us(doc) ->
  184:     ["Testing marshalling of IDL unsigned short"];
  185: marshal_us(suite) -> [];
  186: marshal_us(Config) when is_list(Config) ->
  187:     ?line DataDir = ?config(data_dir, Config),
  188:     ?line {ok,Server} = m_i:oe_create_link([], {local,marshal_us}),
  189:     ?line ok = java(?config(java, Config), DataDir, "JavaClient",
  190: 		    ["JavaClient",node(),erlang:get_cookie(),marshal_us]),
  191:     ?line ok = m_i:stop(Server),
  192:     ok.
  193: 
  194: marshal_c(doc) ->
  195:     ["Testing marshalling of IDL char"];
  196: marshal_c(suite) -> [];
  197: marshal_c(Config) when is_list(Config) ->
  198:     ?line DataDir = ?config(data_dir, Config),
  199:     ?line {ok,Server} = m_i:oe_create_link([], {local,marshal_c}),
  200:     ?line ok = java(?config(java, Config), DataDir, "JavaClient",
  201: 		    ["JavaClient",node(),erlang:get_cookie(),marshal_c]),
  202:     ?line ok = m_i:stop(Server),
  203:     ok.
  204: 
  205: marshal_wc(doc) ->
  206:     ["Testing marshalling of IDL char"];
  207: marshal_wc(suite) -> [];
  208: marshal_wc(Config) when is_list(Config) ->
  209:     ?line DataDir = ?config(data_dir, Config),
  210:     ?line {ok,Server} = m_i:oe_create_link([], {local,marshal_wc}),
  211:     ?line ok = java(?config(java, Config), DataDir, "JavaClient",
  212: 		    ["JavaClient",node(),erlang:get_cookie(),marshal_wc]),
  213:     ?line ok = m_i:stop(Server),
  214:     ok.
  215: 
  216: marshal_str(doc) ->
  217:     ["Testing marshalling of IDL string"];
  218: marshal_str(suite) -> [];
  219: marshal_str(Config) when is_list(Config) ->
  220:     ?line DataDir = ?config(data_dir, Config),
  221:     ?line {ok,Server} = m_i:oe_create_link([], {local,marshal_str}),
  222:     ?line ok = java(?config(java, Config), DataDir, 
  223: %%% 		    "-DOtpConnection.trace=4 "
  224: 		    "JavaClient",
  225: 		    ["JavaClient",node(),erlang:get_cookie(),marshal_str]),
  226:     ?line ok = m_i:stop(Server),
  227:     ok.
  228: 
  229: marshal_any_3(doc) ->
  230:     ["Testing marshalling of IDL any"];
  231: marshal_any_3(suite) -> [];
  232: marshal_any_3(Config) when is_list(Config) ->
  233:     ?line DataDir = ?config(data_dir, Config),
  234:     ?line {ok,Server} = m_i:oe_create_link([], {local,marshal_any_3}),
  235:     ?line ok = java(?config(java, Config), DataDir, "JavaClient",
  236: 		    ["JavaClient",node(),erlang:get_cookie(),marshal_any_3]),
  237:     ?line ok = m_i:stop(Server),
  238:     ok.
  239: 
  240: marshal_any_2(doc) ->
  241:     ["Testing marshalling of IDL any"];
  242: marshal_any_2(suite) -> [];
  243: marshal_any_2(Config) when is_list(Config) ->
  244:     ?line DataDir = ?config(data_dir, Config),
  245:     ?line {ok,Server} = m_i:oe_create_link([], {local,marshal_any_2}),
  246:     ?line ok = java(?config(java, Config), DataDir, "JavaClient",
  247: 		    ["JavaClient",node(),erlang:get_cookie(),marshal_any_2]),
  248:     ?line ok = m_i:stop(Server),
  249:     ok.
  250: 
  251: %%--------------------------------------------------------------------
  252: %%
  253: %% Utilities
  254: 
  255: 
  256: java(Java, Dir, ClassAndArgs) ->
  257:     cmd(Java++" -classpath \""++classpath(Dir)++"\" "++ClassAndArgs).
  258: 
  259: java(Java, Dir, Class, Args) ->
  260:     java(Java, Dir, Class++" "++to_string(Args)).
  261: 
  262: to_string([H|T]) when is_integer(H) ->
  263:     integer_to_list(H)++" "++to_string(T);
  264: to_string([H|T]) when is_atom(H) ->
  265:     atom_to_list(H)++" "++to_string(T);
  266: to_string([H|T]) when is_list(H) ->
  267:     lists:flatten(H)++" "++to_string(T);
  268: to_string([]) -> [].
  269: 
  270: % javac(Dir, File) ->
  271: %     cmd("javac -d "++Dir++" -classpath "++classpath(Dir)++" "++
  272: % 	filename:join(Dir, File)).
  273: 
  274: classpath(Dir) ->
  275:     PS = 
  276: 	case os:type() of
  277: 	    {win32, _} -> ";";
  278: 	    _          -> ":"
  279: 	end,
  280:     Dir++PS++
  281: 	filename:join([code:lib_dir(ic),"priv","ic.jar"])++PS++
  282: 	filename:join([code:lib_dir(jinterface),"priv","OtpErlang.jar"])++PS++
  283: 	case os:getenv("CLASSPATH") of
  284: 	    false -> "";
  285: 	    Classpath -> Classpath
  286: 	end.
  287: 
  288: 
  289: cmd(Cmd) ->
  290:     PortOpts = [{line,80},eof,exit_status,stderr_to_stdout],
  291:     io:format("<cmd> ~s~n", [Cmd]),
  292:     case catch open_port({spawn,Cmd}, PortOpts) of
  293: 	Port when is_port(Port) ->
  294: 	    Result = cmd_loop(Port, []),
  295: 	    io:format("<cmd=~w>~n", [Result]),
  296: 	    case Result of
  297: 		0 -> ok;
  298: 		ExitCode when is_integer(ExitCode) -> {error,ExitCode};
  299: 		Error -> Error
  300: 	    end;
  301: 	{'EXIT',Reason} ->
  302: 	    {error,Reason}
  303:     end.
  304: 
  305: cmd_loop(Port, Line) ->
  306:     receive
  307: 	{Port,eof} ->
  308: 	    receive 
  309: 		{Port,{exit_status,ExitStatus}} ->
  310: 		    ExitStatus
  311: 	    after 1 -> 
  312: 		    undefined 
  313: 	    end;
  314: 	{Port,{exit_status,ExitStatus}} ->
  315: 	    receive 
  316: 		{Port,eof} -> 
  317: 		    ok after 1 -> ok end,
  318: 	    ExitStatus;
  319: 	{Port,{data,{Tag,Data}}} ->
  320: 	    case Tag of
  321: 		eol ->
  322: 		    io:put_chars([Line|cr_to_nl(Data)]),
  323: 		    io:nl(),
  324: 		    cmd_loop(Port, []);
  325: 		noeol ->
  326: 		    cmd_loop(Port, [Line|cr_to_nl(Data)])
  327: 	    end;
  328: 	{'EXIT',Port,Reason} ->
  329: 	    {error,Reason};
  330: 	Other ->
  331: 	    io:format("WARNING: Unexpected at ~s:~p: ~p~n", 
  332: 		      [?MODULE_STRING,?LINE,Other]),
  333: 	    cmd_loop(Port, Line)
  334:     end.
  335: 
  336: %% Convert lonely CR to NL, and CRLF to NL
  337: %%
  338: cr_to_nl([$\r,$\n|T]) ->
  339:     [$\n|cr_to_nl(T)];
  340: cr_to_nl([$\r|T]) ->
  341:     [$\n|cr_to_nl(T)];
  342: cr_to_nl([C|T]) ->
  343:     [C|cr_to_nl(T)];
  344: cr_to_nl([]) ->
  345:     [].