[BAEL-2397] Text Blocks
Added code/configuration for the article about Text Blocks
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.java14.textblocks;
|
||||
|
||||
public class TextBlocks13 {
|
||||
public String getBlockOfHtml() {
|
||||
return """
|
||||
<html>
|
||||
|
||||
<body>
|
||||
<p>example text</p>
|
||||
</body>
|
||||
</html>""";
|
||||
}
|
||||
|
||||
public String getNonStandardIndent() {
|
||||
return """
|
||||
Indent
|
||||
""";
|
||||
}
|
||||
|
||||
public String getQuery() {
|
||||
return """
|
||||
select "id", "user"
|
||||
from "table"
|
||||
""";
|
||||
}
|
||||
|
||||
public String getTextWithCarriageReturns() {
|
||||
return """
|
||||
separated with\r
|
||||
carriage returns""";
|
||||
}
|
||||
|
||||
public String getTextWithEscapes() {
|
||||
return """
|
||||
fun with\n
|
||||
whitespace\t\r
|
||||
and other escapes \"""
|
||||
""";
|
||||
}
|
||||
|
||||
public String getFormattedText(String parameter) {
|
||||
return """
|
||||
Some parameter: %s
|
||||
""".formatted(parameter);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.java14.textblocks;
|
||||
|
||||
public class TextBlocks14 {
|
||||
public String getIgnoredNewLines() {
|
||||
return """
|
||||
This is a long test which looks to \
|
||||
have a newline but actually does not""";
|
||||
}
|
||||
|
||||
public String getEscapedSpaces() {
|
||||
return """
|
||||
line 1
|
||||
line 2 \s
|
||||
""";
|
||||
}
|
||||
}
|
||||
@@ -14,37 +14,37 @@ import java.io.Serializable;
|
||||
* @author Donato Rimenti
|
||||
*/
|
||||
public class MySerialClass implements Serializable {
|
||||
|
||||
@Serial
|
||||
private static final ObjectStreamField[] serialPersistentFields = null;
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1;
|
||||
|
||||
@Serial
|
||||
private void writeObject(ObjectOutputStream stream) throws IOException {
|
||||
// ...
|
||||
}
|
||||
@Serial
|
||||
private static final ObjectStreamField[] serialPersistentFields = null;
|
||||
|
||||
@Serial
|
||||
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
|
||||
// ...
|
||||
}
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1;
|
||||
|
||||
@Serial
|
||||
private void readObjectNoData() throws ObjectStreamException {
|
||||
// ...
|
||||
}
|
||||
@Serial
|
||||
private void writeObject(ObjectOutputStream stream) throws IOException {
|
||||
// ...
|
||||
}
|
||||
|
||||
@Serial
|
||||
private Object writeReplace() throws ObjectStreamException {
|
||||
// ...
|
||||
return null;
|
||||
}
|
||||
@Serial
|
||||
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
|
||||
// ...
|
||||
}
|
||||
|
||||
@Serial
|
||||
private Object readResolve() throws ObjectStreamException {
|
||||
// ...
|
||||
return null;
|
||||
}
|
||||
@Serial
|
||||
private void readObjectNoData() throws ObjectStreamException {
|
||||
// ...
|
||||
}
|
||||
|
||||
@Serial
|
||||
private Object writeReplace() throws ObjectStreamException {
|
||||
// ...
|
||||
return null;
|
||||
}
|
||||
|
||||
@Serial
|
||||
private Object readResolve() throws ObjectStreamException {
|
||||
// ...
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.java14.textblocks;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class TextBlocks13UnitTest {
|
||||
private TextBlocks13 subject = new TextBlocks13();
|
||||
|
||||
@Test
|
||||
void givenAnOldStyleMultilineString_whenComparing_thenEqualsTextBlock() {
|
||||
String expected = "<html>\n" + "\n" + " <body>\n" + " <p>example text</p>\n" + " </body>\n" + "</html>";
|
||||
assertThat(subject.getBlockOfHtml()).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenAnOldStyleString_whenComparing_thenEqualsTextBlock() {
|
||||
String expected = "<html>\n\n <body>\n <p>example text</p>\n </body>\n</html>";
|
||||
assertThat(subject.getBlockOfHtml()).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenAnIndentedString_thenMatchesIndentedOldStyle() {
|
||||
assertThat(subject.getNonStandardIndent()).isEqualTo(" Indent\n");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenAMultilineQuery_thenItCanContainUnescapedQuotes() {
|
||||
assertThat(subject.getQuery()).contains("select \"id\", \"user\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenAMultilineQuery_thenItEndWithANewline() {
|
||||
assertThat(subject.getQuery()).endsWith("\n");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenATextWithCarriageReturns_thenItContainsBoth() {
|
||||
assertThat(subject.getTextWithCarriageReturns()).isEqualTo("separated with\r\ncarriage returns");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenAStringWithEscapedWhitespace_thenItAppearsInTheResultingString() {
|
||||
assertThat(subject.getTextWithEscapes()).contains("fun with\n\n")
|
||||
.contains("whitespace\t\r\n")
|
||||
.contains("and other escapes \"\"\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenAFormattedString_thenTheParameterIsReplaced() {
|
||||
assertThat(subject.getFormattedText("parameter")).contains("Some parameter: parameter");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.java14.textblocks;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class TextBlocks14UnitTest {
|
||||
private TextBlocks14 subject = new TextBlocks14();
|
||||
|
||||
@Test
|
||||
void givenAStringWithEscapedNewLines_thenTheResultHasNoNewLines() {
|
||||
String expected = "This is a long test which looks to have a newline but actually does not";
|
||||
assertThat(subject.getIgnoredNewLines()).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenAStringWithEscapesSpaces_thenTheResultHasLinesEndingWithSpaces() {
|
||||
String expected = "line 1\nline 2 \n";
|
||||
assertThat(subject.getEscapedSpaces()).isEqualTo(expected);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user