Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using text blocks instead of String literals #2791

Merged
merged 1 commit into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions combinator/src/main/java/com/iluwatar/combinator/CombinatorApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@
@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
Expand All @@ -69,19 +83,8 @@ public static void main(String[] args) {
}

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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
25 changes: 13 additions & 12 deletions combinator/src/test/java/com/iluwatar/combinator/FindersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,19 @@ void expandedFinderTest() {

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.""";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,14 @@
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";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,14 @@

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
*/
package com.iluwatar.embedded.value;

import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;

/**
Expand Down Expand Up @@ -73,7 +72,7 @@ public static void main(String[] args) throws Exception {
}

// 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);
Expand All @@ -83,7 +82,7 @@ public static void main(String[] args) throws Exception {

// 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));
Expand All @@ -93,7 +92,7 @@ public static void main(String[] args) throws Exception {
//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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public String getSchema() {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,12 @@ public void runInteractiveMode() {
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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@
@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.
Expand Down
24 changes: 21 additions & 3 deletions property/src/test/java/com/iluwatar/property/CharacterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,33 @@ void testToString() {
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());

}

Expand Down