1+ /* This file is part of KeY - https://key-project.org
2+ * KeY is licensed under the GNU General Public License Version 2
3+ * SPDX-License-Identifier: GPL-2.0-only */
4+ package de .uka .ilkd .key .java .transformations .pipeline ;
5+
6+ import com .github .javaparser .StaticJavaParser ;
7+ import com .github .javaparser .ast .CompilationUnit ;
8+ import org .junit .jupiter .api .Test ;
9+
10+ import static org .assertj .core .api .Assertions .assertThat ;
11+
12+ /**
13+ * Tests for the {@link MultiCatchReducer} transformer.
14+ *
15+ * @author Alexander Weigl
16+ * @version 1 (12.07.26)
17+ */
18+ class MultiCatchReducerTest {
19+ @ Test
20+ void testSimpleMultiCatch () {
21+ String source = """
22+ class Demo {
23+ void run() {
24+ try {
25+ doSomething();
26+ } catch (Exception1 | Exception2 e) {
27+ System.out.println("caught: " + e);
28+ }
29+ }
30+ void doSomething() throws Exception1, Exception2 {}
31+ }
32+ """ ;
33+
34+ CompilationUnit cu = StaticJavaParser .parse (source );
35+ var t = new MultiCatchReducer ();
36+ t .apply (cu );
37+ var actual = cu .toString ();
38+
39+ var expected = """
40+ class Demo {
41+
42+ void run() {
43+ try {
44+ doSomething();
45+ } catch (Exception1 e) {
46+ System.out.println("caught: " + e);
47+ } catch (Exception2 e) {
48+ System.out.println("caught: " + e);
49+ }
50+ }
51+
52+ void doSomething() throws Exception1, Exception2 {
53+ }
54+ }
55+ """ ;
56+
57+ assertThat (actual ).isEqualToNormalizingWhitespace (expected );
58+ }
59+
60+ @ Test
61+ void testThreeExceptionTypes () {
62+ String source = """
63+ class Demo {
64+ void run() {
65+ try {
66+ doSomething();
67+ } catch (IOException | SQLException | RuntimeException e) {
68+ handle(e);
69+ }
70+ }
71+ void doSomething() throws IOException, SQLException, RuntimeException {}
72+ void handle(Exception e) {}
73+ }
74+ """ ;
75+
76+ CompilationUnit cu = StaticJavaParser .parse (source );
77+ var t = new MultiCatchReducer ();
78+ t .apply (cu );
79+ var actual = cu .toString ();
80+
81+ var expected = """
82+ class Demo {
83+
84+ void run() {
85+ try {
86+ doSomething();
87+ } catch (IOException e) {
88+ handle(e);
89+ } catch (SQLException e) {
90+ handle(e);
91+ } catch (RuntimeException e) {
92+ handle(e);
93+ }
94+ }
95+
96+ void doSomething() throws IOException, SQLException, RuntimeException {
97+ }
98+
99+ void handle(Exception e) {
100+ }
101+ }
102+ """ ;
103+
104+ assertThat (actual ).isEqualToNormalizingWhitespace (expected );
105+ }
106+
107+ @ Test
108+ void testMixedSingleAndMultiCatch () {
109+ String source = """
110+ class Demo {
111+ void run() {
112+ try {
113+ doSomething();
114+ } catch (IllegalArgumentException e) {
115+ log("illegal arg");
116+ } catch (IOException | SQLException e) {
117+ log("io/sql error");
118+ } catch (RuntimeException e) {
119+ throw e;
120+ }
121+ }
122+ void doSomething() throws IOException, SQLException {}
123+ void log(String s) {}
124+ }
125+ """ ;
126+
127+ CompilationUnit cu = StaticJavaParser .parse (source );
128+ var t = new MultiCatchReducer ();
129+ t .apply (cu );
130+ var actual = cu .toString ();
131+
132+ var expected = """
133+ class Demo {
134+
135+ void run() {
136+ try {
137+ doSomething();
138+ } catch (IllegalArgumentException e) {
139+ log("illegal arg");
140+ } catch (IOException e) {
141+ log("io/sql error");
142+ } catch (SQLException e) {
143+ log("io/sql error");
144+ } catch (RuntimeException e) {
145+ throw e;
146+ }
147+ }
148+
149+ void doSomething() throws IOException, SQLException {
150+ }
151+
152+ void log(String s) {
153+ }
154+ }
155+ """ ;
156+
157+ assertThat (actual ).isEqualToNormalizingWhitespace (expected );
158+ }
159+
160+ @ Test
161+ void testMultiCatchWithFinally () {
162+ String source = """
163+ class Demo {
164+ void run() {
165+ try {
166+ doSomething();
167+ } catch (Exception1 | Exception2 e) {
168+ handle(e);
169+ } finally {
170+ cleanup();
171+ }
172+ }
173+ void doSomething() throws Exception1, Exception2 {}
174+ void handle(Exception e) {}
175+ void cleanup() {}
176+ }
177+ """ ;
178+
179+ CompilationUnit cu = StaticJavaParser .parse (source );
180+ var t = new MultiCatchReducer ();
181+ t .apply (cu );
182+ var actual = cu .toString ();
183+
184+ var expected = """
185+ class Demo {
186+
187+ void run() {
188+ try {
189+ doSomething();
190+ } catch (Exception1 e) {
191+ handle(e);
192+ } catch (Exception2 e) {
193+ handle(e);
194+ } finally {
195+ cleanup();
196+ }
197+ }
198+
199+ void doSomething() throws Exception1, Exception2 {
200+ }
201+
202+ void handle(Exception e) {
203+ }
204+
205+ void cleanup() {
206+ }
207+ }
208+ """ ;
209+
210+ assertThat (actual ).isEqualToNormalizingWhitespace (expected );
211+ }
212+
213+ @ Test
214+ void testNoMultiCatchUnchanged () {
215+ String source = """
216+ class Demo {
217+ void run() {
218+ try {
219+ doSomething();
220+ } catch (Exception e) {
221+ handle(e);
222+ }
223+ }
224+ void doSomething() throws Exception {}
225+ void handle(Exception e) {}
226+ }
227+ """ ;
228+
229+ CompilationUnit cu = StaticJavaParser .parse (source );
230+ var original = cu .toString ();
231+ var t = new MultiCatchReducer ();
232+ t .apply (cu );
233+ var actual = cu .toString ();
234+
235+ // Should remain unchanged since there's no multi-catch
236+ assertThat (actual ).isEqualToNormalizingWhitespace (original );
237+ }
238+ }
0 commit comments