diff --git a/core-java-modules/core-java-14/src/test/java/com/baeldung/java14/newfeatues/MultilineUnitTest.java b/core-java-modules/core-java-14/src/test/java/com/baeldung/java14/newfeatues/MultilineUnitTest.java new file mode 100644 index 0000000000..4efd1ff362 --- /dev/null +++ b/core-java-modules/core-java-14/src/test/java/com/baeldung/java14/newfeatues/MultilineUnitTest.java @@ -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")); + } + +} diff --git a/core-java-modules/core-java-14/src/test/java/com/baeldung/java14/newfeatues/RecordUnitTest.java b/core-java-modules/core-java-14/src/test/java/com/baeldung/java14/newfeatues/RecordUnitTest.java new file mode 100644 index 0000000000..3d7a55d3e6 --- /dev/null +++ b/core-java-modules/core-java-14/src/test/java/com/baeldung/java14/newfeatues/RecordUnitTest.java @@ -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")); + } + +} diff --git a/core-java-modules/core-java-14/src/test/java/com/baeldung/java14/newfeatues/SwitchExprUnitTest.java b/core-java-modules/core-java-14/src/test/java/com/baeldung/java14/newfeatues/SwitchExprUnitTest.java new file mode 100644 index 0000000000..896b3ec7de --- /dev/null +++ b/core-java-modules/core-java-14/src/test/java/com/baeldung/java14/newfeatues/SwitchExprUnitTest.java @@ -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); + }; + } + +}