Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b44e879

Browse files
committed
Enable new throws by exception , variable etc.
1 parent 8e0f65b commit b44e879

3 files changed

Lines changed: 251 additions & 4 deletions

File tree

source/core/ut_suite_builder.pkb

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,96 @@ create or replace package body ut_suite_builder is
186186
return l_rollback_type;
187187
end;
188188

189+
function check_exception_type(a_exception_name in varchar2) return varchar2 is
190+
l_a varchar2(250);
191+
l_b varchar2(250);
192+
l_c varchar2(250);
193+
l_dblink varchar2(250);
194+
l_next_pos pls_integer;
195+
l_exception_type varchar2(50) := 'NUMBER';
196+
begin
197+
198+
begin
199+
--check if it is a number first
200+
dbms_utility.name_tokenize(a_exception_name, l_a, l_b, l_c, l_dblink, l_next_pos);
201+
--check if it is a predefined exception
202+
begin
203+
execute immediate 'begin null; exception when '||a_exception_name||' then null; end;';
204+
l_exception_type := 'NAMED';
205+
exception
206+
when others then
207+
if dbms_utility.format_error_stack() like '%PLS-00485%' then
208+
execute immediate 'declare x positiven := -('||a_exception_name||'); begin null; end;';
209+
l_exception_type := 'NUMBER';
210+
else
211+
l_exception_type := 'UNKNOWN';
212+
end if;
213+
end;
214+
exception when others then
215+
null;
216+
end;
217+
return l_exception_type;
218+
end;
219+
220+
221+
function get_exception_number (a_exception_var in varchar2) return number is
222+
l_exception_no number;
223+
l_exception_type varchar2(50);
224+
begin
225+
l_exception_type := check_exception_type(a_exception_var);
226+
if l_exception_type = 'NUMBER' then
227+
execute immediate 'declare
228+
l_exception number;
229+
begin
230+
:l_exception := '||a_exception_var||';
231+
exception
232+
when others then
233+
:l_exception := null;
234+
end;' using in out l_exception_no;
235+
236+
elsif l_exception_type = 'NAMED' then
237+
execute immediate 'begin
238+
raise '||a_exception_var||';
239+
exception
240+
when others then
241+
:l_exception := sqlcode;
242+
end;' using in out l_exception_no;
243+
end if;
244+
return l_exception_no;
245+
end;
246+
247+
function is_valid_qualified_name (a_name varchar2) return boolean is
248+
l_name varchar2(500);
249+
begin
250+
l_name:=dbms_assert.qualified_sql_name(a_name);
251+
return true;
252+
exception when others then
253+
return false;
254+
end;
255+
189256
function build_exception_numbers_list(a_annotation_text in varchar2) return ut_integer_list is
190257
l_throws_list ut_varchar2_list;
191258
l_exception_number_list ut_integer_list := ut_integer_list();
192259
l_regexp_for_excep_nums varchar2(30) := '^-?[[:digit:]]{1,5}$';
193260
begin
194261
/*the a_expected_error_codes is converted to a ut_varchar2_list after that is trimmed and filtered to left only valid exception numbers*/
195-
l_throws_list := ut_utils.string_to_table(a_annotation_text, ',', 'Y');
262+
l_throws_list := ut_utils.trim_list_elements(ut_utils.string_to_table(a_annotation_text, ',', 'Y'));
263+
264+
for i in 1..l_throws_list.count
265+
loop
266+
/**
267+
* First check if its a valid qualified name and if so try to resolve to number
268+
* If not check if it matches the ora regex number pattern.
269+
*/
270+
if is_valid_qualified_name(l_throws_list(i)) then
271+
l_throws_list(i) := get_exception_number(l_throws_list(i));
272+
else
273+
l_throws_list(i) := l_throws_list(i);
274+
end if;
275+
end loop;
276+
196277
l_throws_list := ut_utils.filter_list( ut_utils.trim_list_elements(l_throws_list), l_regexp_for_excep_nums);
278+
197279
l_exception_number_list.extend(l_throws_list.count);
198280
for i in 1 .. l_throws_list.count loop
199281
l_exception_number_list(i) := l_throws_list(i);

test/core/annotations/test_annot_throws_exception.pkb

Lines changed: 142 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,24 @@ is
77

88
l_package_spec varchar2(32737);
99
l_package_body varchar2(32737);
10+
l_exception_spec varchar2(32737);
1011
l_test_results ut3.ut_varchar2_list;
1112
begin
13+
l_exception_spec := q'[
14+
create or replace package exc_pkg is
15+
c_e_single_exc constant number := -20200;
16+
c_e_varch_exc constant varchar2(10) := '-20201';
17+
c_e_list_1 number := -20202;
18+
c_e_list_2 constant number := -20203;
19+
c_e_diff_exc constant number := -20204;
20+
c_e_mix_list constant number := -20205;
21+
c_e_mix_missin constant number := -20206;
22+
23+
e_some_exception exception;
24+
pragma exception_init(e_some_exception, -20207);
25+
26+
end;]';
27+
1228
l_package_spec := '
1329
create package annotated_package_with_throws is
1430
--%suite(Dummy package to test annotation throws)
@@ -45,9 +61,42 @@ is
4561
--%throws(7894562, operaqk, -=1, -1, pow74d, posdfk3)
4662
procedure one_valid_exception_number;
4763

48-
--%test(Givess failure when a exception is expected and nothing is thrown)
64+
--%test(Gives failure when a exception is expected and nothing is thrown)
4965
--%throws(-20459, -20136, -20145)
5066
procedure nothing_thrown;
67+
68+
--%test(Single exception defined as a constant number in package)
69+
--%throws(exc_pkg.c_e_single_exc)
70+
procedure single_exc_const_pkg;
71+
72+
--%test(Gives success when one of annotated exception using constant is thrown)
73+
--%throws(exc_pkg.c_e_list_1,exc_pkg.c_e_list_2)
74+
procedure list_of_exc_constant;
75+
76+
--%test(Gives failure when the raised exception is different that the annotated one using variable)
77+
--%throws(exc_pkg.c_e_diff_exc)
78+
procedure fail_not_match_exc;
79+
80+
--%test(Success when one of exception from mixed list of number and constant is thrown)
81+
--%throws(exc_pkg.c_e_mix_list,-20105)
82+
procedure mixed_exc_list;
83+
84+
--%test(Success when match exception even if other variable on list dont exists)
85+
--%throws(exc_pkg.c_e_mix_missin,utter_rubbish)
86+
procedure mixed_list_notexi;
87+
88+
--%test(Success resolve and match named exception defined in pragma exception init)
89+
--%throws(exc_pkg.e_some_exception)
90+
procedure named_exc_pragma;
91+
92+
--%test(Success resolve and match oracle named exception)
93+
--%throws(NO_DATA_FOUND)
94+
procedure named_exc_ora;
95+
96+
--%test(Success resolve and match oracle named exception dup val index)
97+
--%throws(DUP_VAL_ON_INDEX)
98+
procedure named_exc_ora_dup_ind;
99+
51100
end;
52101
';
53102

@@ -98,12 +147,55 @@ is
98147
begin
99148
null;
100149
end;
150+
151+
procedure single_exc_const_pkg is
152+
begin
153+
raise_application_error(exc_pkg.c_e_single_exc,''Test'');
154+
end;
155+
156+
procedure list_of_exc_constant is
157+
begin
158+
raise_application_error(exc_pkg.c_e_list_1,''Test'');
159+
end;
160+
161+
procedure fail_not_match_exc is
162+
begin
163+
raise NO_DATA_FOUND;
164+
end;
165+
166+
procedure mixed_exc_list is
167+
begin
168+
raise_application_error(exc_pkg.c_e_mix_list,''Test'');
169+
end;
170+
171+
procedure mixed_list_notexi is
172+
begin
173+
raise_application_error(exc_pkg.c_e_mix_missin,''Test'');
174+
end;
175+
176+
procedure named_exc_pragma is
177+
begin
178+
raise exc_pkg.e_some_exception;
179+
end;
180+
181+
procedure named_exc_ora is
182+
begin
183+
raise NO_DATA_FOUND;
184+
end;
185+
186+
procedure named_exc_ora_dup_ind is
187+
begin
188+
raise DUP_VAL_ON_INDEX;
189+
end;
190+
101191
end;
102192
';
103193

194+
execute immediate l_exception_spec;
104195
execute immediate l_package_spec;
105196
execute immediate l_package_body;
106197

198+
107199
select * bulk collect into l_test_results from table(ut3.ut.run(('annotated_package_with_throws')));
108200

109201
g_tests_results := ut3.ut_utils.table_to_clob(l_test_results);
@@ -159,15 +251,63 @@ is
159251

160252
procedure nothing_thrown is
161253
begin
162-
ut.expect(g_tests_results).to_match('^\s*Givess failure when a exception is expected and nothing is thrown \[[\.0-9]+ sec\] \(FAILED - [0-9]+\)\s*$','m');
254+
ut.expect(g_tests_results).to_match('^\s*Gives failure when a exception is expected and nothing is thrown \[[\.0-9]+ sec\] \(FAILED - [0-9]+\)\s*$','m');
163255
ut.expect(g_tests_results).to_match('nothing_thrown\s*Expected one of exceptions \(-20459, -20136, -20145\) but nothing was raised.');
164256
end;
165257

258+
procedure single_exc_const_pkg is
259+
begin
260+
ut.expect(g_tests_results).to_match('^\s*Single exception defined as a constant number in package \[[\.0-9]+ sec\]\s*$','m');
261+
ut.expect(g_tests_results).not_to_match('single_exc_const_pkg');
262+
end;
263+
264+
procedure list_of_exc_constant is
265+
begin
266+
ut.expect(g_tests_results).to_match('^\s*Gives success when one of annotated exception using constant is thrown \[[\.0-9]+ sec\]\s*$','m');
267+
ut.expect(g_tests_results).not_to_match('list_of_exc_constant');
268+
end;
269+
270+
procedure fail_not_match_exc is
271+
begin
272+
ut.expect(g_tests_results).to_match('^\s*Gives failure when the raised exception is different that the annotated one using variable \[[\.0-9]+ sec\] \(FAILED - [0-9]+\)\s*$','m');
273+
ut.expect(g_tests_results).to_match('fail_not_match_exc\s+Actual: -1403 was expected to equal: -20204\s+ORA-01403: no data found\s+ORA-06512: at "UT3_TESTER.ANNOTATED_PACKAGE_WITH_THROWS"');
274+
end;
166275

276+
procedure mixed_exc_list is
277+
begin
278+
ut.expect(g_tests_results).to_match('^\s*Success when one of exception from mixed list of number and constant is thrown \[[\.0-9]+ sec\]\s*$','m');
279+
ut.expect(g_tests_results).not_to_match('mixed_exc_list');
280+
end;
281+
282+
procedure mixed_list_notexi is
283+
begin
284+
ut.expect(g_tests_results).to_match('^\s*Success when match exception even if other variable on list dont exists \[[\.0-9]+ sec\]\s*$','m');
285+
ut.expect(g_tests_results).not_to_match('mixed_list_notexi');
286+
end;
287+
288+
procedure named_exc_pragma is
289+
begin
290+
ut.expect(g_tests_results).to_match('^\s*Success resolve and match named exception defined in pragma exception init \[[\.0-9]+ sec\]\s*$','m');
291+
ut.expect(g_tests_results).not_to_match('mixed_list_notexi');
292+
end;
293+
294+
procedure named_exc_ora is
295+
begin
296+
ut.expect(g_tests_results).to_match('^\s*Success resolve and match oracle named exception \[[\.0-9]+ sec\]\s*$','m');
297+
ut.expect(g_tests_results).not_to_match('named_exc_ora');
298+
end;
299+
300+
procedure named_exc_ora_dup_ind is
301+
begin
302+
ut.expect(g_tests_results).to_match('^\s*Success resolve and match oracle named exception dup val index \[[\.0-9]+ sec\]\s*$','m');
303+
ut.expect(g_tests_results).not_to_match('named_exc_ora_dup_ind');
304+
end;
305+
167306
procedure drop_test_package is
168307
pragma autonomous_transaction;
169308
begin
170309
execute immediate 'drop package annotated_package_with_throws';
310+
execute immediate 'drop package exc_pkg';
171311
end;
172312

173313
end;

test/core/annotations/test_annot_throws_exception.pks

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,34 @@ is
3030
--%test(Detects a valid exception number within many invalid ones)
3131
procedure one_valid_exception_number;
3232

33-
--%test(Givess failure when a exception is expected and nothing is thrown)
33+
--%test(Gives failure when a exception is expected and nothing is thrown)
3434
procedure nothing_thrown;
3535

36+
--%test(Single exception defined as a constant number in package)
37+
procedure single_exc_const_pkg;
38+
39+
--%test(Gives success when one of annotated exception using constant is thrown)
40+
procedure list_of_exc_constant;
41+
42+
--%test(Gives failure when the raised exception is different that the annotated one using variable)
43+
procedure fail_not_match_exc;
44+
45+
--%test(Success when one of exception from mixed list of number and constant is thrown)
46+
procedure mixed_exc_list;
47+
48+
--%test(Success when match exception even if other variable on list dont exists)
49+
procedure mixed_list_notexi;
50+
51+
--%test(Success resolve and match named exception defined in pragma exception init)
52+
procedure named_exc_pragma;
53+
54+
--%test(Success resolve and match oracle named exception)
55+
--%disabled
56+
procedure named_exc_ora;
57+
58+
--%test(Success resolve and match oracle named exception dup val index)
59+
procedure named_exc_ora_dup_ind;
60+
3661
--%afterall
3762
procedure drop_test_package;
3863

0 commit comments

Comments
 (0)