Skip to content

Commit 219915a

Browse files
authored
BAEL-6154: Migrate from Java 8 to Java 17 + tests (eugenp#13474)
1 parent dbcda87 commit 219915a

7 files changed

Lines changed: 175 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.java8to17;
2+
3+
public class Address {
4+
5+
private String street;
6+
private String city;
7+
private String pin;
8+
9+
public Address(String street, String city, String pin) {
10+
super();
11+
this.street = street;
12+
this.city = city;
13+
this.pin = pin;
14+
}
15+
16+
public String getStreet() {
17+
return street;
18+
}
19+
20+
public void setStreet(String street) {
21+
this.street = street;
22+
}
23+
24+
public String getCity() {
25+
return city;
26+
}
27+
28+
public void setCity(String city) {
29+
this.city = city;
30+
}
31+
32+
public String getPin() {
33+
return pin;
34+
}
35+
36+
public void setPin(String pin) {
37+
this.pin = pin;
38+
}
39+
40+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.baeldung.java8to17;
2+
3+
public record Circle(double radius) implements Shape {
4+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.java8to17;
2+
3+
public class Person {
4+
5+
private String name;
6+
private Address address;
7+
8+
public Person(String name, Address address) {
9+
super();
10+
this.name = name;
11+
this.address = address;
12+
}
13+
14+
public String getName() {
15+
return name;
16+
}
17+
18+
public void setName(String name) {
19+
this.name = name;
20+
}
21+
22+
public Address getAddress() {
23+
return address;
24+
}
25+
26+
public void setAddress(Address address) {
27+
this.address = address;
28+
}
29+
30+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.baeldung.java8to17;
2+
3+
public record Rectangle(double length, double width) implements Shape {
4+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.java8to17;
2+
3+
public interface Shape {
4+
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.java8to17;
2+
3+
public record Student(int rollNo, String name) {
4+
5+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.baeldung.java17;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.junit.Assert.assertThrows;
5+
6+
import java.util.Arrays;
7+
8+
import org.junit.jupiter.api.Test;
9+
10+
import com.baeldung.java8to17.Address;
11+
import com.baeldung.java8to17.Circle;
12+
import com.baeldung.java8to17.Person;
13+
import com.baeldung.java8to17.Rectangle;
14+
import com.baeldung.java8to17.Student;
15+
16+
public class Java8to17ExampleUnitTest {
17+
18+
@Test
19+
void givenMultiLineText_whenUsingTextBlock_thenStringIsReturned() {
20+
String value = """
21+
This is a
22+
Multi-line
23+
Text
24+
""";
25+
26+
assertThat(value).isEqualTo("This is a\nMulti-line\nText\n");
27+
}
28+
29+
@Test
30+
void givenString_whenUsingUtilFunctions_thenReturnsExpectedResult() {
31+
assertThat(" ".isBlank());
32+
assertThat("Twinkle ".repeat(2)).isEqualTo("Twinkle Twinkle ");
33+
assertThat("Format Line".indent(4)).isEqualTo(" Format Line\n");
34+
assertThat("Line 1 \n Line2".lines()).asList().size().isEqualTo(2);
35+
assertThat(" Text with white spaces ".strip()).isEqualTo("Text with white spaces");
36+
assertThat("Car, Bus, Train".transform(s1 -> Arrays.asList(s1.split(","))).get(0)).isEqualTo("Car");
37+
}
38+
39+
@Test
40+
void givenDataModel_whenUsingRecordType_thenBehavesLikeDTO() {
41+
Student student = new Student(10, "Priya");
42+
Student student2 = new Student(10, "Priya");
43+
44+
assertThat(student.rollNo()).isEqualTo(10);
45+
assertThat(student.name()).isEqualTo("Priya");
46+
assertThat(student.equals(student2));
47+
assertThat(student.hashCode()).isEqualTo(student2.hashCode());
48+
}
49+
50+
@Test
51+
void givenObject_whenThrowingNPE_thenReturnsHelpfulMessage() {
52+
Person student = new Person("Lakshmi", new Address("35, West Street", null, null));
53+
54+
Exception exception = assertThrows(NullPointerException.class, () -> {
55+
student.getAddress().getCity().toLowerCase();
56+
});
57+
58+
assertThat(exception.getMessage()).isEqualTo(
59+
"Cannot invoke \"String.toLowerCase()\" because the return value of \"com.baeldung.java8to17.Address.getCity()\" is null");
60+
}
61+
62+
@Test
63+
void givenGenericObject_whenUsingPatternMatching_thenReturnsTargetType() {
64+
String city = null;
65+
Object obj = new Address("35, West Street", "Chennai", "6000041");
66+
67+
if (obj instanceof Address address) {
68+
city = address.getCity();
69+
}
70+
71+
assertThat(city).isEqualTo("Chennai");
72+
}
73+
74+
@Test
75+
void givenGenericObject_whenUsingSwitchExpression_thenPatternMatchesRightObject() {
76+
Object shape = new Rectangle(10, 20);
77+
78+
double circumference = switch (shape) {
79+
case Rectangle r -> 2 * r.length() + 2 * r.width();
80+
case Circle c -> 2 * c.radius() * Math.PI;
81+
default -> throw new IllegalArgumentException("Unknown shape");
82+
};
83+
84+
assertThat(circumference).isEqualTo(60);
85+
}
86+
87+
}

0 commit comments

Comments
 (0)