44import com .github .javaparser .ast .CompilationUnit ;
55import org .junit .jupiter .api .Test ;
66
7+ import static org .assertj .core .api .Assertions .assertThat ;
8+
79/**
810 *
911 * @author Alexander Weigl
@@ -30,6 +32,145 @@ void run() {
3032 CompilationUnit cu = StaticJavaParser .parse (source );
3133 var t = new TryWithResourceReducer ();
3234 t .apply (cu );
33- System .out .println (cu );
35+ var actual = cu .toString ();
36+ var expected = """
37+ class Demo {
38+
39+ void run() {
40+ try {
41+ Resource r1 = new Resource1();
42+ Throwable primaryExc$1 = null;
43+ try {
44+ Resource r2 = new Resource2();
45+ Throwable primaryExc$2 = null;
46+ try {
47+ r1.use();
48+ r2.use();
49+ } catch (Throwable t$2) {
50+ primaryExc$2 = t$2;
51+ throw t$2;
52+ } finally {
53+ if (r2 != null) {
54+ if (primaryExc$2 != null)
55+ try {
56+ r2.close();
57+ } catch (Throwable suppressedExc$2) {
58+ primaryExc$2.addSuppressed(suppressedExc$2);
59+ }
60+ else {
61+ r2.close();
62+ }
63+ }
64+ }
65+ } catch (Throwable t$1) {
66+ primaryExc$1 = t$1;
67+ throw t$1;
68+ } finally {
69+ if (r1 != null) {
70+ if (primaryExc$1 != null)
71+ try {
72+ r1.close();
73+ } catch (Throwable suppressedExc$1) {
74+ primaryExc$1.addSuppressed(suppressedExc$1);
75+ }
76+ else {
77+ r1.close();
78+ }
79+ }
80+ }
81+ } catch (Exception e) {
82+ System.out.println("caught: " + e);
83+ } finally {
84+ System.out.println("done");
85+ }
86+ }
87+ }
88+ """ ;
89+
90+ assertThat (actual ).isEqualToNormalizingWhitespace (expected );
3491 }
92+
93+ @ Test
94+ void testJava9 () {
95+ String source = """
96+ class Demo {
97+ void run() {
98+ Resource r1 = new Resource1();
99+ try (r1; Resource r2 = new Resource2()) {
100+ r1.use();
101+ r2.use();
102+ } catch (Exception e) {
103+ System.out.println("caught: " + e);
104+ } finally {
105+ System.out.println("done");
106+ }
107+ }
108+ }
109+ """ ;
110+
111+ CompilationUnit cu = StaticJavaParser .parse (source );
112+ var t = new TryWithResourceReducer ();
113+ t .apply (cu );
114+ var actual = cu .toString ();
115+
116+
117+ var expected = """
118+ class Demo {
119+
120+ void run() {
121+ Resource r1 = new Resource1();
122+ try {
123+ Throwable primaryExc$1 = null;
124+ try {
125+ Resource r2 = new Resource2();
126+ Throwable primaryExc$2 = null;
127+ try {
128+ r1.use();
129+ r2.use();
130+ } catch (Throwable t$2) {
131+ primaryExc$2 = t$2;
132+ throw t$2;
133+ } finally {
134+ if (r2 != null) {
135+ if (primaryExc$2 != null)
136+ try {
137+ r2.close();
138+ } catch (Throwable suppressedExc$2) {
139+ primaryExc$2.addSuppressed(suppressedExc$2);
140+ }
141+ else {
142+ r2.close();
143+ }
144+ }
145+ }
146+ } catch (Throwable t$1) {
147+ primaryExc$1 = t$1;
148+ throw t$1;
149+ } finally {
150+ if (r1 != null) {
151+ if (primaryExc$1 != null)
152+ try {
153+ r1.close();
154+ } catch (Throwable suppressedExc$1) {
155+ primaryExc$1.addSuppressed(suppressedExc$1);
156+ }
157+ else {
158+ r1.close();
159+ }
160+ }
161+ }
162+ } catch (Exception e) {
163+ System.out.println("caught: " + e);
164+ } finally {
165+ System.out.println("done");
166+ }
167+ }
168+ }
169+ """ ;
170+
171+ System .out .println (actual );
172+
173+ assertThat (actual ).isEqualToNormalizingWhitespace (expected );
174+ }
175+
35176}
0 commit comments