|
| 1 | +create or replace package body ut_runner is |
| 2 | + |
| 3 | + type t_call_param is record ( |
| 4 | + ut_reporter_name varchar2(4000), |
| 5 | + output_file_name varchar2(4000), |
| 6 | + output_to_screen varchar2(3) := 'off', |
| 7 | + output_id varchar2(4000) |
| 8 | + ); |
| 9 | + type tt_call_params is table of t_call_param; |
| 10 | + |
| 11 | + g_call_params tt_call_params; |
| 12 | + |
| 13 | + g_ut_paths varchar2(4000); |
| 14 | + |
| 15 | + function get_streamed_output_type return varchar2 is |
| 16 | + l_result varchar2(255); |
| 17 | + begin |
| 18 | + select type_name |
| 19 | + into l_result |
| 20 | + from user_types where supertype_name = 'UT_OUTPUT_STREAM'; |
| 21 | + return lower(l_result)||'()'; |
| 22 | + end; |
| 23 | + |
| 24 | + procedure run(a_paths in ut_varchar2_list, a_reporter in ut_reporter) is |
| 25 | + l_objects_to_run ut_objects_list; |
| 26 | + l_reporter ut_reporter := a_reporter; |
| 27 | + ut_running_suite ut_test_suite; |
| 28 | + begin |
| 29 | + ut_suite_manager.configure_execution_by_path(a_paths,l_objects_to_run); |
| 30 | + |
| 31 | + if l_objects_to_run.count > 0 then |
| 32 | + l_reporter.before_run(a_suites => l_objects_to_run); |
| 33 | + for i in 1 .. l_objects_to_run.count loop |
| 34 | + |
| 35 | + ut_running_suite := treat(l_objects_to_run(i) as ut_test_suite); |
| 36 | + ut_running_suite.do_execute(l_reporter); |
| 37 | + l_objects_to_run(i) := ut_running_suite; |
| 38 | + |
| 39 | + end loop; |
| 40 | + l_reporter.after_run(a_suites => l_objects_to_run); |
| 41 | + end if; |
| 42 | + end; |
| 43 | + |
| 44 | + |
| 45 | + procedure run(a_path in varchar2, a_reporter in ut_reporter) is |
| 46 | + begin |
| 47 | + run(ut_varchar2_list(coalesce(a_path, sys_context('userenv', 'current_schema'))), a_reporter); |
| 48 | + end run; |
| 49 | + |
| 50 | + function get_optional_params_script(a_params_count integer := 100) return ut_varchar2_list pipelined is |
| 51 | + l_sql_columns varchar2(4000); |
| 52 | + l_params varchar2(4000); |
| 53 | + begin |
| 54 | + for i in 1 .. a_params_count loop |
| 55 | + pipe row ('column '||i||' new_value '||i); |
| 56 | + end loop; |
| 57 | + for i in 1 .. a_params_count loop |
| 58 | + l_sql_columns := l_sql_columns ||'null as "'||i||'",'; |
| 59 | + l_params := l_params || '''&&'||i||''','; |
| 60 | + end loop; |
| 61 | + pipe row ('select '||rtrim(l_sql_columns, ',') ||' from dual where rownum = 0;'); |
| 62 | + pipe row ('' ); |
| 63 | + pipe row ('exec ut_runner.set_call_params(ut_varchar2_list('||rtrim(l_params, ',')||'));' ); |
| 64 | + return; |
| 65 | + end; |
| 66 | + |
| 67 | + procedure set_call_params(a_params ut_varchar2_list) is |
| 68 | + l_call_param t_call_param; |
| 69 | + begin |
| 70 | + g_call_params := tt_call_params(); |
| 71 | + for param in |
| 72 | + ( with |
| 73 | + param_vals as( |
| 74 | + select regexp_substr(column_value,'-([fos])\=?(.*)',1,1,'c',1) param_type, |
| 75 | + regexp_substr(column_value,'-([fos])\=(.*)',1,1,'c',2) param_value |
| 76 | + from table(a_params) |
| 77 | + where column_value is not null) |
| 78 | + select param_type, param_value |
| 79 | + from param_vals |
| 80 | + where param_type is not null) |
| 81 | + loop |
| 82 | + if param.param_type = 'f' then |
| 83 | + g_call_params.extend; |
| 84 | + g_call_params(g_call_params.last) := l_call_param; |
| 85 | + g_call_params(g_call_params.last).ut_reporter_name := param.param_value; |
| 86 | + elsif g_call_params.last is not null then |
| 87 | + if param.param_type = 'o' then |
| 88 | + g_call_params(g_call_params.last).output_file_name := param.param_value; |
| 89 | + elsif param.param_type = 's' then |
| 90 | + g_call_params(g_call_params.last).output_to_screen := 'on'; |
| 91 | + end if; |
| 92 | + end if; |
| 93 | + end loop; |
| 94 | + |
| 95 | + begin |
| 96 | + select ''''||replace(ut_paths,',',''',''')||'''' |
| 97 | + into g_ut_paths |
| 98 | + from (select regexp_substr(column_value,'-p\=(.*)',1,1,'c',1) as ut_paths from table(a_params) ) |
| 99 | + where ut_paths is not null; |
| 100 | + exception |
| 101 | + when no_data_found then |
| 102 | + g_ut_paths := 'user'; |
| 103 | + when too_many_rows then |
| 104 | + raise_application_error(-20000, 'Parameter "-p=ut_paths" defined more than once. Only one "-p=ut_paths" parameter can be used.'); |
| 105 | + end; |
| 106 | + |
| 107 | + end; |
| 108 | + |
| 109 | + function get_run_in_background_script return ut_varchar2_list pipelined is |
| 110 | + l_output_id varchar2(128); |
| 111 | + l_output_type varchar2(256); |
| 112 | + begin |
| 113 | + |
| 114 | + l_output_type := get_streamed_output_type(); |
| 115 | + pipe row( 'set serveroutput on size unlimited format truncated'); |
| 116 | + pipe row( 'set pagesize 0'); |
| 117 | + pipe row( 'set linesize 4000'); |
| 118 | + pipe row( 'spool run_background.log'); |
| 119 | + pipe row( 'declare'); |
| 120 | + pipe row( ' v_reporter ut_reporter;'); |
| 121 | + pipe row( ' v_reporters_list ut_reporters_list := ut_reporters_list();'); |
| 122 | + pipe row( 'begin'); |
| 123 | + for i in 1 .. cardinality(g_call_params) loop |
| 124 | + execute immediate 'begin :l_output_id := '||l_output_type||'.generate_output_id(); end;' |
| 125 | + using out g_call_params(i).output_id; |
| 126 | + pipe row(' v_reporter := '||g_call_params(i).ut_reporter_name||'('||l_output_type||');'); |
| 127 | + pipe row(' v_reporter.output.output_id := '''||g_call_params(i).output_id||''';'); |
| 128 | + pipe row(' v_reporters_list.extend; v_reporters_list(v_reporters_list.last) := v_reporter;'); |
| 129 | + end loop; |
| 130 | + pipe row( ' ut.run( ut_varchar2_list('||g_ut_paths||'), ut_composite_reporter( v_reporters_list ) );'); |
| 131 | + pipe row( 'end;'); |
| 132 | + pipe row( '/'); |
| 133 | + pipe row( 'exit'); |
| 134 | + |
| 135 | + return; |
| 136 | + end; |
| 137 | + |
| 138 | + function get_outputs_script return ut_varchar2_list pipelined is |
| 139 | + begin |
| 140 | + for i in 1 .. cardinality(g_call_params) loop |
| 141 | + pipe row('set termout '||g_call_params(i).output_to_screen); |
| 142 | + if g_call_params(i).output_file_name is not null then |
| 143 | + pipe row('spool '||g_call_params(i).output_file_name); |
| 144 | + pipe row('select * from table( '||get_streamed_output_type()||'.get_lines('''||g_call_params(i).output_id||''') );'); |
| 145 | + pipe row('spool off'); |
| 146 | + else |
| 147 | + pipe row('select * from table( '||get_streamed_output_type()||'.get_lines('''||g_call_params(i).output_id||''') );'); |
| 148 | + end if; |
| 149 | + end loop; |
| 150 | + end; |
| 151 | + |
| 152 | +end ut_runner; |
| 153 | +/ |
0 commit comments