Skip to content

Commit b18c8e8

Browse files
author
Sampada
authored
BAEL-4751: Java 14 New Features (eugenp#10319)
* BAEL-4751: Java 14 New Features * BAEL-4751: Code formatting * BAEL-4751: Consistent excpetion message * BAEL-4751: Changed assert
1 parent 282a057 commit b18c8e8

3 files changed

Lines changed: 114 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.java14.newfeatues;
2+
3+
import static org.junit.Assert.assertFalse;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import org.junit.Test;
7+
8+
public class MultilineUnitTest {
9+
10+
@SuppressWarnings("preview")
11+
String multiline = """
12+
A quick brown fox jumps over a lazy dog; \
13+
the lazy dog howls loudly.""";
14+
15+
@SuppressWarnings("preview")
16+
String anotherMultiline = """
17+
A quick brown fox jumps over a lazy dog;
18+
the lazy dog howls loudly.""";
19+
20+
@Test
21+
public void givenMultilineString_whenSlashUsed_thenNoNewLine() {
22+
assertFalse(multiline.contains("\n"));
23+
assertTrue(anotherMultiline.contains("\n"));
24+
}
25+
26+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.java14.newfeatues;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import org.junit.Test;
7+
8+
public class RecordUnitTest {
9+
10+
@SuppressWarnings("preview")
11+
public record User(int id, String password) {
12+
};
13+
14+
private User user1 = new User(0, "UserOne");
15+
16+
@Test
17+
public void givenRecord_whenObjInitialized_thenValuesCanBeFetchedWithGetters() {
18+
19+
assertEquals(0, user1.id());
20+
assertEquals("UserOne", user1.password());
21+
}
22+
23+
@Test
24+
public void whenRecord_thenEqualsImplemented() {
25+
26+
User user2 = user1;
27+
28+
assertEquals(user1, user2);
29+
}
30+
31+
@Test
32+
public void whenRecord_thenToStringImplemented() {
33+
34+
assertTrue(user1.toString()
35+
.contains("UserOne"));
36+
}
37+
38+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.baeldung.java14.newfeatues;
2+
3+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import org.junit.Test;
7+
8+
public class SwitchExprUnitTest {
9+
10+
@Test
11+
public void givenDay_whenSunday_thenWeekend() {
12+
assertTrue(isTodayHolidayInJava8("SUNDAY"));
13+
14+
assertTrue(isTodayHolidayInJava14("SUNDAY"));
15+
16+
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> isTodayHolidayInJava8("SOMEDAY"));
17+
18+
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> isTodayHolidayInJava14("SOMEDAY"));
19+
}
20+
21+
private boolean isTodayHolidayInJava8(String day) {
22+
23+
boolean isTodayHoliday;
24+
switch (day) {
25+
case "MONDAY":
26+
case "TUESDAY":
27+
case "WEDNESDAY":
28+
case "THURSDAY":
29+
case "FRIDAY":
30+
isTodayHoliday = false;
31+
break;
32+
case "SATURDAY":
33+
case "SUNDAY":
34+
isTodayHoliday = true;
35+
break;
36+
default:
37+
throw new IllegalArgumentException("What's a " + day);
38+
}
39+
return isTodayHoliday;
40+
}
41+
42+
private boolean isTodayHolidayInJava14(String day) {
43+
return switch (day) {
44+
case "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY" -> false;
45+
case "SATURDAY", "SUNDAY" -> true;
46+
default -> throw new IllegalArgumentException("What's a " + day);
47+
};
48+
}
49+
50+
}

0 commit comments

Comments
 (0)