BAEL-4751: Java 14 New Features (#10319)

* BAEL-4751: Java 14 New Features

* BAEL-4751: Code formatting

* BAEL-4751: Consistent excpetion message

* BAEL-4751: Changed assert
This commit is contained in:
Sampada 2020-12-21 01:19:26 +05:30 committed by GitHub
parent f542a4afab
commit 17d2870daa
3 changed files with 114 additions and 0 deletions

View File

@ -0,0 +1,26 @@
package com.baeldung.java14.newfeatues;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class MultilineUnitTest {
@SuppressWarnings("preview")
String multiline = """
A quick brown fox jumps over a lazy dog; \
the lazy dog howls loudly.""";
@SuppressWarnings("preview")
String anotherMultiline = """
A quick brown fox jumps over a lazy dog;
the lazy dog howls loudly.""";
@Test
public void givenMultilineString_whenSlashUsed_thenNoNewLine() {
assertFalse(multiline.contains("\n"));
assertTrue(anotherMultiline.contains("\n"));
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.java14.newfeatues;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class RecordUnitTest {
@SuppressWarnings("preview")
public record User(int id, String password) {
};
private User user1 = new User(0, "UserOne");
@Test
public void givenRecord_whenObjInitialized_thenValuesCanBeFetchedWithGetters() {
assertEquals(0, user1.id());
assertEquals("UserOne", user1.password());
}
@Test
public void whenRecord_thenEqualsImplemented() {
User user2 = user1;
assertEquals(user1, user2);
}
@Test
public void whenRecord_thenToStringImplemented() {
assertTrue(user1.toString()
.contains("UserOne"));
}
}

View File

@ -0,0 +1,50 @@
package com.baeldung.java14.newfeatues;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class SwitchExprUnitTest {
@Test
public void givenDay_whenSunday_thenWeekend() {
assertTrue(isTodayHolidayInJava8("SUNDAY"));
assertTrue(isTodayHolidayInJava14("SUNDAY"));
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> isTodayHolidayInJava8("SOMEDAY"));
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> isTodayHolidayInJava14("SOMEDAY"));
}
private boolean isTodayHolidayInJava8(String day) {
boolean isTodayHoliday;
switch (day) {
case "MONDAY":
case "TUESDAY":
case "WEDNESDAY":
case "THURSDAY":
case "FRIDAY":
isTodayHoliday = false;
break;
case "SATURDAY":
case "SUNDAY":
isTodayHoliday = true;
break;
default:
throw new IllegalArgumentException("What's a " + day);
}
return isTodayHoliday;
}
private boolean isTodayHolidayInJava14(String day) {
return switch (day) {
case "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY" -> false;
case "SATURDAY", "SUNDAY" -> true;
default -> throw new IllegalArgumentException("What's a " + day);
};
}
}