mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-14 06:58:54 +00:00
feat: Using text blocks instead of String literals (#2791)
This commit is contained in:
@@ -44,6 +44,20 @@ import lombok.extern.slf4j.Slf4j;
|
||||
@Slf4j
|
||||
public class CombinatorApp {
|
||||
|
||||
private static final String TEXT = """
|
||||
It was many and many a year ago,
|
||||
In a kingdom by the sea,
|
||||
That a maiden there lived whom you may know
|
||||
By the name of ANNABEL LEE;
|
||||
And this maiden she lived with no other thought
|
||||
Than to love and be loved by me.
|
||||
I was a child and she was a child,
|
||||
In this kingdom by the sea;
|
||||
But we loved with a love that was more than love-
|
||||
I and my Annabel Lee;
|
||||
With a love that the winged seraphs of heaven
|
||||
Coveted her and me.""";
|
||||
|
||||
/**
|
||||
* main.
|
||||
* @param args args
|
||||
@@ -69,19 +83,8 @@ public class CombinatorApp {
|
||||
}
|
||||
|
||||
private static String text() {
|
||||
return
|
||||
"It was many and many a year ago,\n"
|
||||
+ "In a kingdom by the sea,\n"
|
||||
+ "That a maiden there lived whom you may know\n"
|
||||
+ "By the name of ANNABEL LEE;\n"
|
||||
+ "And this maiden she lived with no other thought\n"
|
||||
+ "Than to love and be loved by me.\n"
|
||||
+ "I was a child and she was a child,\n"
|
||||
+ "In this kingdom by the sea;\n"
|
||||
+ "But we loved with a love that was more than love-\n"
|
||||
+ "I and my Annabel Lee;\n"
|
||||
+ "With a love that the winged seraphs of heaven\n"
|
||||
+ "Coveted her and me.";
|
||||
|
||||
return TEXT;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -32,7 +32,10 @@ class FinderTest {
|
||||
|
||||
@Test
|
||||
void contains() {
|
||||
var example = "the first one \nthe second one \n";
|
||||
var example = """
|
||||
the first one
|
||||
the second one\s
|
||||
""";
|
||||
|
||||
var result = Finder.contains("second").find(example);
|
||||
assertEquals(1, result.size());
|
||||
|
||||
@@ -67,18 +67,19 @@ class FindersTest {
|
||||
|
||||
private String text() {
|
||||
return
|
||||
"It was many and many a year ago,\n"
|
||||
+ "In a kingdom by the sea,\n"
|
||||
+ "That a maiden there lived whom you may know\n"
|
||||
+ "By the name of ANNABEL LEE;\n"
|
||||
+ "And this maiden she lived with no other thought\n"
|
||||
+ "Than to love and be loved by me.\n"
|
||||
+ "I was a child and she was a child,\n"
|
||||
+ "In this kingdom by the sea;\n"
|
||||
+ "But we loved with a love that was more than love-\n"
|
||||
+ "I and my Annabel Lee;\n"
|
||||
+ "With a love that the winged seraphs of heaven\n"
|
||||
+ "Coveted her and me.";
|
||||
"""
|
||||
It was many and many a year ago,
|
||||
In a kingdom by the sea,
|
||||
That a maiden there lived whom you may know
|
||||
By the name of ANNABEL LEE;
|
||||
And this maiden she lived with no other thought
|
||||
Than to love and be loved by me.
|
||||
I was a child and she was a child,
|
||||
In this kingdom by the sea;
|
||||
But we loved with a love that was more than love-
|
||||
I and my Annabel Lee;
|
||||
With a love that the winged seraphs of heaven
|
||||
Coveted her and me.""";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -40,9 +40,14 @@ import java.io.PrintWriter;
|
||||
public final class AppServlet extends HttpServlet {
|
||||
private static final String CONTENT_TYPE = "text/html";
|
||||
private String msgPartOne = "<h1>This Server Doesn't Support";
|
||||
private String msgPartTwo = "Requests</h1>\n"
|
||||
+ "<h2>Use a GET request with boolean values for the following parameters<h2>\n"
|
||||
+ "<h3>'name'</h3>\n<h3>'bus'</h3>\n<h3>'sports'</h3>\n<h3>'sci'</h3>\n<h3>'world'</h3>";
|
||||
private String msgPartTwo = """
|
||||
Requests</h1>
|
||||
<h2>Use a GET request with boolean values for the following parameters<h2>
|
||||
<h3>'name'</h3>
|
||||
<h3>'bus'</h3>
|
||||
<h3>'sports'</h3>
|
||||
<h3>'sci'</h3>
|
||||
<h3>'world'</h3>""";
|
||||
|
||||
private String destination = "newsDisplay.jsp";
|
||||
|
||||
|
||||
@@ -40,9 +40,14 @@ and https://stackoverflow.com/questions/50211433/servlets-unit-testing
|
||||
|
||||
class AppServletTest extends Mockito{
|
||||
private String msgPartOne = "<h1>This Server Doesn't Support";
|
||||
private String msgPartTwo = "Requests</h1>\n"
|
||||
+ "<h2>Use a GET request with boolean values for the following parameters<h2>\n"
|
||||
+ "<h3>'name'</h3>\n<h3>'bus'</h3>\n<h3>'sports'</h3>\n<h3>'sci'</h3>\n<h3>'world'</h3>";
|
||||
private String msgPartTwo = """
|
||||
Requests</h1>
|
||||
<h2>Use a GET request with boolean values for the following parameters<h2>
|
||||
<h3>'name'</h3>
|
||||
<h3>'bus'</h3>
|
||||
<h3>'sports'</h3>
|
||||
<h3>'sci'</h3>
|
||||
<h3>'world'</h3>""";
|
||||
private String destination = "newsDisplay.jsp";
|
||||
|
||||
@Test
|
||||
|
||||
@@ -24,7 +24,6 @@
|
||||
*/
|
||||
package com.iluwatar.embedded.value;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
@@ -73,7 +72,7 @@ public class App {
|
||||
}
|
||||
|
||||
// Initially, database is empty
|
||||
LOGGER.info("Orders Query: {}", dataSource.queryOrders().collect(Collectors.toList()));
|
||||
LOGGER.info("Orders Query: {}", dataSource.queryOrders().toList());
|
||||
|
||||
//Insert orders where shippingAddress is mapped to different columns of the same table
|
||||
dataSource.insertOrder(order1);
|
||||
@@ -83,7 +82,7 @@ public class App {
|
||||
|
||||
// Query orders.
|
||||
// We'll create ShippingAddress object from city, state, pincode values from the table and add it to Order object
|
||||
LOGGER.info("Orders Query: {}", dataSource.queryOrders().collect(Collectors.toList()) + "\n");
|
||||
LOGGER.info("Orders Query: {}", dataSource.queryOrders().toList() + "\n");
|
||||
|
||||
//Query order by given id
|
||||
LOGGER.info("Query Order with id=2: {}", dataSource.queryOrder(2));
|
||||
@@ -93,7 +92,7 @@ public class App {
|
||||
//Since we'd mapped address in the same table, deleting order will also take out the shipping address details.
|
||||
LOGGER.info("Remove Order with id=1");
|
||||
dataSource.removeOrder(1);
|
||||
LOGGER.info("\nOrders Query: {}", dataSource.queryOrders().collect(Collectors.toList()) + "\n");
|
||||
LOGGER.info("\nOrders Query: {}", dataSource.queryOrders().toList() + "\n");
|
||||
|
||||
//After successful demonstration of the pattern, drop the table
|
||||
if (dataSource.deleteSchema()) {
|
||||
|
||||
@@ -87,7 +87,7 @@ public class DataSource implements DataSourceInterface {
|
||||
var resultSet = getschema.executeQuery(GET_SCHEMA);
|
||||
var sb = new StringBuilder();
|
||||
while (resultSet.next()) {
|
||||
sb.append("Col name: " + resultSet.getString(1) + ", Col type: " + resultSet.getString(2) + "\n");
|
||||
sb.append("Col name: ").append(resultSet.getString(1)).append(", Col type: ").append(resultSet.getString(2)).append("\n");
|
||||
}
|
||||
getschema.close();
|
||||
return sb.toString();
|
||||
|
||||
@@ -150,7 +150,12 @@ public class App {
|
||||
var option = -1;
|
||||
while (option != 4) {
|
||||
LOGGER.info("Hello. Would you like to boil some eggs?");
|
||||
LOGGER.info("(1) BOIL AN EGG \n(2) STOP BOILING THIS EGG \n(3) HOW ARE MY EGGS? \n(4) EXIT");
|
||||
LOGGER.info("""
|
||||
(1) BOIL AN EGG
|
||||
(2) STOP BOILING THIS EGG
|
||||
(3) HOW ARE MY EGGS?
|
||||
(4) EXIT
|
||||
""");
|
||||
LOGGER.info("Choose [1,2,3,4]: ");
|
||||
option = s.nextInt();
|
||||
|
||||
|
||||
@@ -34,13 +34,13 @@ import org.h2.jdbcx.JdbcDataSource;
|
||||
@Slf4j
|
||||
public class DatabaseUtil {
|
||||
private static final String DB_URL = "jdbc:h2:mem:metamapping";
|
||||
private static final String CREATE_SCHEMA_SQL = "DROP TABLE IF EXISTS `user_account`;"
|
||||
+ "CREATE TABLE `user_account` (\n"
|
||||
+ " `id` int(11) NOT NULL AUTO_INCREMENT,\n"
|
||||
+ " `username` varchar(255) NOT NULL,\n"
|
||||
+ " `password` varchar(255) NOT NULL,\n"
|
||||
+ " PRIMARY KEY (`id`)\n"
|
||||
+ ");";
|
||||
private static final String CREATE_SCHEMA_SQL = """
|
||||
DROP TABLE IF EXISTS `user_account`;CREATE TABLE `user_account` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`username` varchar(255) NOT NULL,
|
||||
`password` varchar(255) NOT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
);""";
|
||||
|
||||
/**
|
||||
* Hide constructor.
|
||||
|
||||
@@ -79,15 +79,33 @@ class CharacterTest {
|
||||
prototype.set(Stats.ARMOR, 1);
|
||||
prototype.set(Stats.AGILITY, 2);
|
||||
prototype.set(Stats.INTELLECT, 3);
|
||||
assertEquals("Stats:\n - AGILITY:2\n - ARMOR:1\n - INTELLECT:3\n", prototype.toString());
|
||||
var message = """
|
||||
Stats:
|
||||
- AGILITY:2
|
||||
- ARMOR:1
|
||||
- INTELLECT:3
|
||||
""";
|
||||
assertEquals(message, prototype.toString());
|
||||
|
||||
final var stupid = new Character(Type.ROGUE, prototype);
|
||||
stupid.remove(Stats.INTELLECT);
|
||||
assertEquals("Character type: ROGUE\nStats:\n - AGILITY:2\n - ARMOR:1\n", stupid.toString());
|
||||
String expectedStupidString = """
|
||||
Character type: ROGUE
|
||||
Stats:
|
||||
- AGILITY:2
|
||||
- ARMOR:1
|
||||
""";
|
||||
assertEquals(expectedStupidString, stupid.toString());
|
||||
|
||||
final var weak = new Character("weak", prototype);
|
||||
weak.remove(Stats.ARMOR);
|
||||
assertEquals("Player: weak\nStats:\n - AGILITY:2\n - INTELLECT:3\n", weak.toString());
|
||||
String expectedWeakString = """
|
||||
Player: weak
|
||||
Stats:
|
||||
- AGILITY:2
|
||||
- INTELLECT:3
|
||||
""";
|
||||
assertEquals(expectedWeakString, weak.toString());
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user