Skip to content

Commit e377047

Browse files
feat: Using text blocks instead of String literals (#2791)
1 parent 90f5b38 commit e377047

File tree

10 files changed

+87
-48
lines changed

10 files changed

+87
-48
lines changed

Diff for: combinator/src/main/java/com/iluwatar/combinator/CombinatorApp.java

+16-13
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@
4444
@Slf4j
4545
public class CombinatorApp {
4646

47+
private static final String TEXT = """
48+
It was many and many a year ago,
49+
In a kingdom by the sea,
50+
That a maiden there lived whom you may know
51+
By the name of ANNABEL LEE;
52+
And this maiden she lived with no other thought
53+
Than to love and be loved by me.
54+
I was a child and she was a child,
55+
In this kingdom by the sea;
56+
But we loved with a love that was more than love-
57+
I and my Annabel Lee;
58+
With a love that the winged seraphs of heaven
59+
Coveted her and me.""";
60+
4761
/**
4862
* main.
4963
* @param args args
@@ -69,19 +83,8 @@ public static void main(String[] args) {
6983
}
7084

7185
private static String text() {
72-
return
73-
"It was many and many a year ago,\n"
74-
+ "In a kingdom by the sea,\n"
75-
+ "That a maiden there lived whom you may know\n"
76-
+ "By the name of ANNABEL LEE;\n"
77-
+ "And this maiden she lived with no other thought\n"
78-
+ "Than to love and be loved by me.\n"
79-
+ "I was a child and she was a child,\n"
80-
+ "In this kingdom by the sea;\n"
81-
+ "But we loved with a love that was more than love-\n"
82-
+ "I and my Annabel Lee;\n"
83-
+ "With a love that the winged seraphs of heaven\n"
84-
+ "Coveted her and me.";
86+
87+
return TEXT;
8588
}
8689

8790
}

Diff for: combinator/src/test/java/com/iluwatar/combinator/FinderTest.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ class FinderTest {
3232

3333
@Test
3434
void contains() {
35-
var example = "the first one \nthe second one \n";
35+
var example = """
36+
the first one
37+
the second one\s
38+
""";
3639

3740
var result = Finder.contains("second").find(example);
3841
assertEquals(1, result.size());

Diff for: combinator/src/test/java/com/iluwatar/combinator/FindersTest.java

+13-12
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,19 @@ void expandedFinderTest() {
6767

6868
private String text() {
6969
return
70-
"It was many and many a year ago,\n"
71-
+ "In a kingdom by the sea,\n"
72-
+ "That a maiden there lived whom you may know\n"
73-
+ "By the name of ANNABEL LEE;\n"
74-
+ "And this maiden she lived with no other thought\n"
75-
+ "Than to love and be loved by me.\n"
76-
+ "I was a child and she was a child,\n"
77-
+ "In this kingdom by the sea;\n"
78-
+ "But we loved with a love that was more than love-\n"
79-
+ "I and my Annabel Lee;\n"
80-
+ "With a love that the winged seraphs of heaven\n"
81-
+ "Coveted her and me.";
70+
"""
71+
It was many and many a year ago,
72+
In a kingdom by the sea,
73+
That a maiden there lived whom you may know
74+
By the name of ANNABEL LEE;
75+
And this maiden she lived with no other thought
76+
Than to love and be loved by me.
77+
I was a child and she was a child,
78+
In this kingdom by the sea;
79+
But we loved with a love that was more than love-
80+
I and my Annabel Lee;
81+
With a love that the winged seraphs of heaven
82+
Coveted her and me.""";
8283
}
8384

8485
}

Diff for: composite-view/src/main/java/com/iluwatar/compositeview/AppServlet.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,14 @@
4040
public final class AppServlet extends HttpServlet {
4141
private static final String CONTENT_TYPE = "text/html";
4242
private String msgPartOne = "<h1>This Server Doesn't Support";
43-
private String msgPartTwo = "Requests</h1>\n"
44-
+ "<h2>Use a GET request with boolean values for the following parameters<h2>\n"
45-
+ "<h3>'name'</h3>\n<h3>'bus'</h3>\n<h3>'sports'</h3>\n<h3>'sci'</h3>\n<h3>'world'</h3>";
43+
private String msgPartTwo = """
44+
Requests</h1>
45+
<h2>Use a GET request with boolean values for the following parameters<h2>
46+
<h3>'name'</h3>
47+
<h3>'bus'</h3>
48+
<h3>'sports'</h3>
49+
<h3>'sci'</h3>
50+
<h3>'world'</h3>""";
4651

4752
private String destination = "newsDisplay.jsp";
4853

Diff for: composite-view/src/test/java/com/iluwatar/compositeview/AppServletTest.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,14 @@
4040

4141
class AppServletTest extends Mockito{
4242
private String msgPartOne = "<h1>This Server Doesn't Support";
43-
private String msgPartTwo = "Requests</h1>\n"
44-
+ "<h2>Use a GET request with boolean values for the following parameters<h2>\n"
45-
+ "<h3>'name'</h3>\n<h3>'bus'</h3>\n<h3>'sports'</h3>\n<h3>'sci'</h3>\n<h3>'world'</h3>";
43+
private String msgPartTwo = """
44+
Requests</h1>
45+
<h2>Use a GET request with boolean values for the following parameters<h2>
46+
<h3>'name'</h3>
47+
<h3>'bus'</h3>
48+
<h3>'sports'</h3>
49+
<h3>'sci'</h3>
50+
<h3>'world'</h3>""";
4651
private String destination = "newsDisplay.jsp";
4752

4853
@Test

Diff for: embedded-value/src/main/java/com/iluwatar/embedded/value/App.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
*/
2525
package com.iluwatar.embedded.value;
2626

27-
import java.util.stream.Collectors;
2827
import lombok.extern.slf4j.Slf4j;
2928

3029
/**
@@ -73,7 +72,7 @@ public static void main(String[] args) throws Exception {
7372
}
7473

7574
// Initially, database is empty
76-
LOGGER.info("Orders Query: {}", dataSource.queryOrders().collect(Collectors.toList()));
75+
LOGGER.info("Orders Query: {}", dataSource.queryOrders().toList());
7776

7877
//Insert orders where shippingAddress is mapped to different columns of the same table
7978
dataSource.insertOrder(order1);
@@ -83,7 +82,7 @@ public static void main(String[] args) throws Exception {
8382

8483
// Query orders.
8584
// We'll create ShippingAddress object from city, state, pincode values from the table and add it to Order object
86-
LOGGER.info("Orders Query: {}", dataSource.queryOrders().collect(Collectors.toList()) + "\n");
85+
LOGGER.info("Orders Query: {}", dataSource.queryOrders().toList() + "\n");
8786

8887
//Query order by given id
8988
LOGGER.info("Query Order with id=2: {}", dataSource.queryOrder(2));
@@ -93,7 +92,7 @@ public static void main(String[] args) throws Exception {
9392
//Since we'd mapped address in the same table, deleting order will also take out the shipping address details.
9493
LOGGER.info("Remove Order with id=1");
9594
dataSource.removeOrder(1);
96-
LOGGER.info("\nOrders Query: {}", dataSource.queryOrders().collect(Collectors.toList()) + "\n");
95+
LOGGER.info("\nOrders Query: {}", dataSource.queryOrders().toList() + "\n");
9796

9897
//After successful demonstration of the pattern, drop the table
9998
if (dataSource.deleteSchema()) {

Diff for: embedded-value/src/main/java/com/iluwatar/embedded/value/DataSource.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public String getSchema() {
8787
var resultSet = getschema.executeQuery(GET_SCHEMA);
8888
var sb = new StringBuilder();
8989
while (resultSet.next()) {
90-
sb.append("Col name: " + resultSet.getString(1) + ", Col type: " + resultSet.getString(2) + "\n");
90+
sb.append("Col name: ").append(resultSet.getString(1)).append(", Col type: ").append(resultSet.getString(2)).append("\n");
9191
}
9292
getschema.close();
9393
return sb.toString();

Diff for: event-asynchronous/src/main/java/com/iluwatar/event/asynchronous/App.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,12 @@ public void runInteractiveMode() {
150150
var option = -1;
151151
while (option != 4) {
152152
LOGGER.info("Hello. Would you like to boil some eggs?");
153-
LOGGER.info("(1) BOIL AN EGG \n(2) STOP BOILING THIS EGG \n(3) HOW ARE MY EGGS? \n(4) EXIT");
153+
LOGGER.info("""
154+
(1) BOIL AN EGG
155+
(2) STOP BOILING THIS EGG
156+
(3) HOW ARE MY EGGS?
157+
(4) EXIT
158+
""");
154159
LOGGER.info("Choose [1,2,3,4]: ");
155160
option = s.nextInt();
156161

Diff for: metadata-mapping/src/main/java/com/iluwatar/metamapping/utils/DatabaseUtil.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@
3434
@Slf4j
3535
public class DatabaseUtil {
3636
private static final String DB_URL = "jdbc:h2:mem:metamapping";
37-
private static final String CREATE_SCHEMA_SQL = "DROP TABLE IF EXISTS `user_account`;"
38-
+ "CREATE TABLE `user_account` (\n"
39-
+ " `id` int(11) NOT NULL AUTO_INCREMENT,\n"
40-
+ " `username` varchar(255) NOT NULL,\n"
41-
+ " `password` varchar(255) NOT NULL,\n"
42-
+ " PRIMARY KEY (`id`)\n"
43-
+ ");";
37+
private static final String CREATE_SCHEMA_SQL = """
38+
DROP TABLE IF EXISTS `user_account`;CREATE TABLE `user_account` (
39+
`id` int(11) NOT NULL AUTO_INCREMENT,
40+
`username` varchar(255) NOT NULL,
41+
`password` varchar(255) NOT NULL,
42+
PRIMARY KEY (`id`)
43+
);""";
4444

4545
/**
4646
* Hide constructor.

Diff for: property/src/test/java/com/iluwatar/property/CharacterTest.java

+21-3
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,33 @@ void testToString() {
7979
prototype.set(Stats.ARMOR, 1);
8080
prototype.set(Stats.AGILITY, 2);
8181
prototype.set(Stats.INTELLECT, 3);
82-
assertEquals("Stats:\n - AGILITY:2\n - ARMOR:1\n - INTELLECT:3\n", prototype.toString());
82+
var message = """
83+
Stats:
84+
- AGILITY:2
85+
- ARMOR:1
86+
- INTELLECT:3
87+
""";
88+
assertEquals(message, prototype.toString());
8389

8490
final var stupid = new Character(Type.ROGUE, prototype);
8591
stupid.remove(Stats.INTELLECT);
86-
assertEquals("Character type: ROGUE\nStats:\n - AGILITY:2\n - ARMOR:1\n", stupid.toString());
92+
String expectedStupidString = """
93+
Character type: ROGUE
94+
Stats:
95+
- AGILITY:2
96+
- ARMOR:1
97+
""";
98+
assertEquals(expectedStupidString, stupid.toString());
8799

88100
final var weak = new Character("weak", prototype);
89101
weak.remove(Stats.ARMOR);
90-
assertEquals("Player: weak\nStats:\n - AGILITY:2\n - INTELLECT:3\n", weak.toString());
102+
String expectedWeakString = """
103+
Player: weak
104+
Stats:
105+
- AGILITY:2
106+
- INTELLECT:3
107+
""";
108+
assertEquals(expectedWeakString, weak.toString());
91109

92110
}
93111

0 commit comments

Comments
 (0)