Skip to content

Commit 10ff415

Browse files
author
Laurens Westerlaken
authored
Add tests for flexible constructor bodies (#6004)
* Have Claude add tests * Revert unnecessary annotation changes * Cleanup tests * Cleaned up too much * Move tests to nested test class
1 parent 8e3b3ee commit 10ff415

1 file changed

Lines changed: 121 additions & 0 deletions

File tree

rewrite-java-tck/src/main/java/org/openrewrite/java/tree/ConstructorTest.java

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
*/
1616
package org.openrewrite.java.tree;
1717

18+
import org.junit.jupiter.api.Nested;
1819
import org.junit.jupiter.api.Test;
1920
import org.openrewrite.Issue;
2021
import org.openrewrite.java.MinimumJava21;
22+
import org.openrewrite.java.MinimumJava25;
2123
import org.openrewrite.test.RewriteTest;
2224

2325
import static org.openrewrite.java.Assertions.java;
@@ -173,4 +175,123 @@ public A(String a) {
173175
)
174176
);
175177
}
178+
179+
@Issue("https://openjdk.org/jeps/513")
180+
@MinimumJava25
181+
@Nested
182+
class FlexibleConstructors {
183+
184+
@Test
185+
void constructorThisWithPrologueAndEpilogue() {
186+
rewriteRun(
187+
java(
188+
"""
189+
class A {
190+
String stringA;
191+
A(){}
192+
A(String a) {
193+
String validated = a.trim();
194+
this();
195+
this.stringA = validated;
196+
}
197+
}
198+
"""
199+
)
200+
);
201+
}
202+
203+
@Test
204+
void constructorSuperWithPrologueAndEpilogue() {
205+
rewriteRun(
206+
java(
207+
"""
208+
class Parent {
209+
String parentString;
210+
Parent(String value) {
211+
this.parentString = value;
212+
}
213+
}
214+
215+
class Child extends Parent {
216+
String childString;
217+
Child(int number, String value) {
218+
String formatted = String.format("Number: %d", number);
219+
super(formatted);
220+
this.childString = value;
221+
}
222+
}
223+
"""
224+
)
225+
);
226+
}
227+
228+
@Test
229+
void recordConstructorWithValidation() {
230+
rewriteRun(
231+
java(
232+
"""
233+
record Point(int x, int y) {
234+
Point {
235+
if (x < 0 || y < 0) {
236+
throw new IllegalArgumentException("Coordinates must be non-negative");
237+
}
238+
}
239+
240+
Point(String coords) {
241+
String[] parts = coords.split(",");
242+
int parsedX = Integer.parseInt(parts[0].trim());
243+
int parsedY = Integer.parseInt(parts[1].trim());
244+
this(parsedX, parsedY);
245+
}
246+
}
247+
"""
248+
)
249+
);
250+
}
251+
252+
@Test
253+
void constructorWithEarlyReturn() {
254+
rewriteRun(
255+
java(
256+
"""
257+
class A {
258+
String value;
259+
A(){}
260+
A(String input) {
261+
if (input == null) {
262+
this();
263+
return;
264+
}
265+
if (input.isEmpty()) {
266+
throw new IllegalArgumentException("Empty input");
267+
}
268+
this();
269+
this.value = input;
270+
}
271+
}
272+
"""
273+
)
274+
);
275+
}
276+
277+
@Test
278+
void assertStatementBeforeConstructor() {
279+
rewriteRun(
280+
java(
281+
"""
282+
class A {
283+
String someString;
284+
A(){}
285+
A(String input) {
286+
assert input != null : "Input must not be null";
287+
assert !input.isEmpty() : "Input must not be empty";
288+
this();
289+
someString = input;
290+
}
291+
}
292+
"""
293+
)
294+
);
295+
}
296+
}
176297
}

0 commit comments

Comments
 (0)