BAEL-6154: Migrate from Java 8 to Java 17 + tests (#13474)

This commit is contained in:
Palaniappan Arunachalam 2023-02-20 09:43:42 +05:30 committed by GitHub
parent a54c2c6f17
commit 8f2e92dacb
7 changed files with 175 additions and 0 deletions

View File

@ -0,0 +1,40 @@
package com.baeldung.java8to17;
public class Address {
private String street;
private String city;
private String pin;
public Address(String street, String city, String pin) {
super();
this.street = street;
this.city = city;
this.pin = pin;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getPin() {
return pin;
}
public void setPin(String pin) {
this.pin = pin;
}
}

View File

@ -0,0 +1,4 @@
package com.baeldung.java8to17;
public record Circle(double radius) implements Shape {
}

View File

@ -0,0 +1,30 @@
package com.baeldung.java8to17;
public class Person {
private String name;
private Address address;
public Person(String name, Address address) {
super();
this.name = name;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}

View File

@ -0,0 +1,4 @@
package com.baeldung.java8to17;
public record Rectangle(double length, double width) implements Shape {
}

View File

@ -0,0 +1,5 @@
package com.baeldung.java8to17;
public interface Shape {
}

View File

@ -0,0 +1,5 @@
package com.baeldung.java8to17;
public record Student(int rollNo, String name) {
}

View File

@ -0,0 +1,87 @@
package com.baeldung.java17;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertThrows;
import java.util.Arrays;
import org.junit.jupiter.api.Test;
import com.baeldung.java8to17.Address;
import com.baeldung.java8to17.Circle;
import com.baeldung.java8to17.Person;
import com.baeldung.java8to17.Rectangle;
import com.baeldung.java8to17.Student;
public class Java8to17ExampleUnitTest {
@Test
void givenMultiLineText_whenUsingTextBlock_thenStringIsReturned() {
String value = """
This is a
Multi-line
Text
""";
assertThat(value).isEqualTo("This is a\nMulti-line\nText\n");
}
@Test
void givenString_whenUsingUtilFunctions_thenReturnsExpectedResult() {
assertThat(" ".isBlank());
assertThat("Twinkle ".repeat(2)).isEqualTo("Twinkle Twinkle ");
assertThat("Format Line".indent(4)).isEqualTo(" Format Line\n");
assertThat("Line 1 \n Line2".lines()).asList().size().isEqualTo(2);
assertThat(" Text with white spaces ".strip()).isEqualTo("Text with white spaces");
assertThat("Car, Bus, Train".transform(s1 -> Arrays.asList(s1.split(","))).get(0)).isEqualTo("Car");
}
@Test
void givenDataModel_whenUsingRecordType_thenBehavesLikeDTO() {
Student student = new Student(10, "Priya");
Student student2 = new Student(10, "Priya");
assertThat(student.rollNo()).isEqualTo(10);
assertThat(student.name()).isEqualTo("Priya");
assertThat(student.equals(student2));
assertThat(student.hashCode()).isEqualTo(student2.hashCode());
}
@Test
void givenObject_whenThrowingNPE_thenReturnsHelpfulMessage() {
Person student = new Person("Lakshmi", new Address("35, West Street", null, null));
Exception exception = assertThrows(NullPointerException.class, () -> {
student.getAddress().getCity().toLowerCase();
});
assertThat(exception.getMessage()).isEqualTo(
"Cannot invoke \"String.toLowerCase()\" because the return value of \"com.baeldung.java8to17.Address.getCity()\" is null");
}
@Test
void givenGenericObject_whenUsingPatternMatching_thenReturnsTargetType() {
String city = null;
Object obj = new Address("35, West Street", "Chennai", "6000041");
if (obj instanceof Address address) {
city = address.getCity();
}
assertThat(city).isEqualTo("Chennai");
}
@Test
void givenGenericObject_whenUsingSwitchExpression_thenPatternMatchesRightObject() {
Object shape = new Rectangle(10, 20);
double circumference = switch (shape) {
case Rectangle r -> 2 * r.length() + 2 * r.width();
case Circle c -> 2 * c.radius() * Math.PI;
default -> throw new IllegalArgumentException("Unknown shape");
};
assertThat(circumference).isEqualTo(60);
}
}