Bael 7050 Java 21 New Features (#15870)

* BAEL-7050 Java 21 New Features

* BAEL-7050 Formatted the pom.xml file

* BAEL-7050 Moved the codebase to existing core-java-21 project

* Removed the old pom.xml reference

* Removed the old core-java-21-new-features project

* BAEL-7050 PR Review Changes

* BAEL-7050 Using JUnit5 in test cases

* BAEL-7050 Using JUnit5 in test cases

* BAEL-7050 Removed the public modifier from JUnit class and methods
This commit is contained in:
Somnath Musib 2024-04-03 16:01:54 +03:00 committed by GitHub
parent 6719beae86
commit 39427baf50
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 242 additions and 0 deletions

View File

@ -0,0 +1,32 @@
package com.baeldung.java21;
public class PatternCaseLabels {
static String processInputOld(String input) {
String output;
switch (input) {
case null -> output = "Oops, null";
case String s -> {
if ("Yes".equalsIgnoreCase(s)) {
output = "It's Yes";
} else if ("No".equalsIgnoreCase(s)) {
output = "It's No";
} else {
output = "Try Again";
}
}
}
return output;
}
static String processInputNew(String input) {
String output;
switch (input) {
case null -> output = "Oops, null";
case String s when "Yes".equalsIgnoreCase(s) -> output = "It's Yes";
case String s when "No".equalsIgnoreCase(s) -> output = "It's No";
case String s -> output = "Try Again";
}
return output;
}
}

View File

@ -0,0 +1,36 @@
package com.baeldung.java21;
public class RecordPattern {
record Point(int x, int y) {}
public static int beforeRecordPattern(Object obj) {
int sum = 0;
if(obj instanceof Point p) {
int x = p.x();
int y = p.y();
sum = x+y;
}
return sum;
}
public static int afterRecordPattern(Object obj) {
if(obj instanceof Point(int x, int y)) {
return x+y;
}
return 0;
}
enum Color {RED, GREEN, BLUE}
record ColoredPoint(Point point, Color color) {}
record RandomPoint(ColoredPoint cp) {}
public static Color getRamdomPointColor(RandomPoint r) {
if(r instanceof RandomPoint(ColoredPoint cp)) {
return cp.color();
}
return null;
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.java21;
public class StringTemplates {
public String getStringTemplate() {
String name = "Baeldung";
return STR."Welcome to \{name}";
}
}

View File

@ -0,0 +1,56 @@
package com.baeldung.java21;
public class SwitchPattern {
static class Account{
double getBalance() {
return 0;
}
}
static class SavingsAccount extends Account {
@Override
double getBalance() {
return 100;
}
}
static class TermAccount extends Account {
@Override
double getBalance() {
return 1000;
}
}
static class CurrentAccount extends Account {
@Override
double getBalance() {
return 10000;
}
}
static double getBalanceWithOutSwitchPattern(Account account) {
double balance = 0;
if(account instanceof SavingsAccount sa) {
balance = sa.getBalance();
}
else if(account instanceof TermAccount ta) {
balance = ta.getBalance();
}
else if(account instanceof CurrentAccount ca) {
balance = ca.getBalance();
}
return balance;
}
static double getBalanceWithSwitchPattern(Account account) {
double result;
switch (account) {
case null -> throw new IllegalArgumentException("Oops, account is null");
case SavingsAccount sa -> result = sa.getBalance();
case TermAccount ta -> result = ta.getBalance();
case CurrentAccount ca -> result = ca.getBalance();
default -> result = account.getBalance();
}
return result;
}
}

View File

@ -0,0 +1,48 @@
package com.baeldung.java21;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class PatternCaseLabelsUnitTest {
@Test
void whenProcessInputOldWayWithYes_thenReturnOutput() {
assertEquals("It's Yes", PatternCaseLabels.processInputOld("Yes"));
}
@Test
void whenProcessInputOldWayWithNo_thenReturnOutput() {
assertEquals("It's No", PatternCaseLabels.processInputOld("No"));
}
@Test
void whenProcessInputOldWayWithNull_thenReturnOutput() {
assertEquals("Oops, null", PatternCaseLabels.processInputOld(null));
}
@Test
void whenProcessInputOldWayWithInvalidOption_thenReturnOutput() {
assertEquals("Try Again", PatternCaseLabels.processInputOld("Invalid Option"));
}
@Test
void whenProcessInputNewWayWithYes_thenReturnOutput() {
assertEquals("It's Yes", PatternCaseLabels.processInputNew("Yes"));
}
@Test
void whenProcessInputNewWayWithNo_thenReturnOutput() {
assertEquals("It's No", PatternCaseLabels.processInputNew("No"));
}
@Test
void whenProcessInputNewWayWithNull_thenReturnOutput() {
assertEquals("Oops, null", PatternCaseLabels.processInputNew(null));
}
@Test
void whenProcessInputNewWayWithInvalidOption_thenReturnOutput() {
assertEquals("Try Again", PatternCaseLabels.processInputNew("Invalid Option"));
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.java21;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import com.baeldung.java21.RecordPattern.Color;
import com.baeldung.java21.RecordPattern.ColoredPoint;
import com.baeldung.java21.RecordPattern.Point;
import com.baeldung.java21.RecordPattern.RandomPoint;
class RecordPatternUnitTest {
@Test
void whenNoRecordPattern_thenReturnOutput() {
assertEquals(5, RecordPattern.beforeRecordPattern(new Point(2, 3)));
}
@Test
void whenRecordPattern_thenReturnOutput() {
assertEquals(5, RecordPattern.afterRecordPattern(new Point(2, 3)));
}
@Test
void whenRecordPattern_thenReturnColorOutput() {
ColoredPoint coloredPoint = new ColoredPoint(new Point(2, 3), Color.GREEN);
RandomPoint randomPoint = new RandomPoint(coloredPoint);
assertEquals(Color.GREEN, RecordPattern.getRamdomPointColor(randomPoint));
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.java21;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class StringTemplateUnitTest {
@Test
void whenNoSwitchPattern_thenReturnSavingsAccountBalance() {
StringTemplates stringTemplates = new StringTemplates();
assertEquals("Welcome to Baeldung", stringTemplates.getStringTemplate());
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.java21;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class SwitchPatternUnitTest {
@Test
void whenNoSwitchPattern_thenReturnSavingsAccountBalance() {
SwitchPattern.SavingsAccount savingsAccount = new SwitchPattern.SavingsAccount();
assertEquals(100, SwitchPattern.getBalanceWithOutSwitchPattern(savingsAccount), 0);
}
@Test
void whenSwitchPattern_thenReturnSavingsAccountBalance() {
SwitchPattern.SavingsAccount savingsAccount = new SwitchPattern.SavingsAccount();
assertEquals(100, SwitchPattern.getBalanceWithSwitchPattern(savingsAccount), 0);
}
}