[BAEL-2397] Text Blocks
Added code/configuration for the article about Text Blocks
This commit is contained in:
parent
33849011c8
commit
d59bd5f066
1
core-java-modules/core-java-14/.mvn/jvm.config
Normal file
1
core-java-modules/core-java-14/.mvn/jvm.config
Normal file
@ -0,0 +1 @@
|
|||||||
|
--enable-preview
|
@ -16,7 +16,26 @@
|
|||||||
<version>1.0.0-SNAPSHOT</version>
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
<relativePath>../../</relativePath>
|
<relativePath>../../</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.assertj</groupId>
|
||||||
|
<artifactId>assertj-core</artifactId>
|
||||||
|
<version>${assertj.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
|
<version>${junit-jupiter.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
|
<version>${junit-jupiter.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
@ -24,13 +43,8 @@
|
|||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
<version>${maven-compiler-plugin.version}</version>
|
<version>${maven-compiler-plugin.version}</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<source>${maven.compiler.source.version}</source>
|
<release>${maven.compiler.release}</release>
|
||||||
<target>${maven.compiler.target.version}</target>
|
<compilerArgs>--enable-preview</compilerArgs>
|
||||||
<compilerArgs>
|
|
||||||
<compilerArg>
|
|
||||||
--enable-preview
|
|
||||||
</compilerArg>
|
|
||||||
</compilerArgs>
|
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
@ -45,8 +59,9 @@
|
|||||||
</build>
|
</build>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source.version>14</maven.compiler.source.version>
|
<maven.compiler.release>14</maven.compiler.release>
|
||||||
<maven.compiler.target.version>14</maven.compiler.target.version>
|
<assertj.version>3.6.1</assertj.version>
|
||||||
|
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
|
||||||
<surefire.plugin.version>3.0.0-M3</surefire.plugin.version>
|
<surefire.plugin.version>3.0.0-M3</surefire.plugin.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
@ -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
|
* @author Donato Rimenti
|
||||||
*/
|
*/
|
||||||
public class MySerialClass implements Serializable {
|
public class MySerialClass implements Serializable {
|
||||||
|
|
||||||
@Serial
|
|
||||||
private static final ObjectStreamField[] serialPersistentFields = null;
|
|
||||||
|
|
||||||
@Serial
|
|
||||||
private static final long serialVersionUID = 1;
|
|
||||||
|
|
||||||
@Serial
|
@Serial
|
||||||
private void writeObject(ObjectOutputStream stream) throws IOException {
|
private static final ObjectStreamField[] serialPersistentFields = null;
|
||||||
// ...
|
|
||||||
}
|
|
||||||
|
|
||||||
@Serial
|
@Serial
|
||||||
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
|
private static final long serialVersionUID = 1;
|
||||||
// ...
|
|
||||||
}
|
|
||||||
|
|
||||||
@Serial
|
@Serial
|
||||||
private void readObjectNoData() throws ObjectStreamException {
|
private void writeObject(ObjectOutputStream stream) throws IOException {
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
|
|
||||||
@Serial
|
@Serial
|
||||||
private Object writeReplace() throws ObjectStreamException {
|
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
|
||||||
// ...
|
// ...
|
||||||
return null;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Serial
|
@Serial
|
||||||
private Object readResolve() throws ObjectStreamException {
|
private void readObjectNoData() throws ObjectStreamException {
|
||||||
// ...
|
// ...
|
||||||
return null;
|
}
|
||||||
}
|
|
||||||
|
@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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user