Skip to content

Commit 3ddc8ea

Browse files
committed
[BAEL-2397] Text Blocks
Added code/configuration for the article about Text Blocks
1 parent 2a91ba9 commit 3ddc8ea

7 files changed

Lines changed: 195 additions & 43 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--enable-preview

core-java-modules/core-java-14/pom.xml

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,35 @@
1616
<version>1.0.0-SNAPSHOT</version>
1717
<relativePath>../../</relativePath>
1818
</parent>
19-
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.assertj</groupId>
22+
<artifactId>assertj-core</artifactId>
23+
<version>${assertj.version}</version>
24+
<scope>test</scope>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.junit.jupiter</groupId>
28+
<artifactId>junit-jupiter-engine</artifactId>
29+
<version>${junit-jupiter.version}</version>
30+
<scope>test</scope>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.junit.jupiter</groupId>
34+
<artifactId>junit-jupiter-api</artifactId>
35+
<version>${junit-jupiter.version}</version>
36+
<scope>test</scope>
37+
</dependency>
38+
</dependencies>
2039
<build>
2140
<plugins>
2241
<plugin>
2342
<groupId>org.apache.maven.plugins</groupId>
2443
<artifactId>maven-compiler-plugin</artifactId>
2544
<version>${maven-compiler-plugin.version}</version>
2645
<configuration>
27-
<source>${maven.compiler.source.version}</source>
28-
<target>${maven.compiler.target.version}</target>
29-
<compilerArgs>
30-
<compilerArg>
31-
--enable-preview
32-
</compilerArg>
33-
</compilerArgs>
46+
<release>${maven.compiler.release}</release>
47+
<compilerArgs>--enable-preview</compilerArgs>
3448
</configuration>
3549
</plugin>
3650
<plugin>
@@ -45,8 +59,9 @@
4559
</build>
4660

4761
<properties>
48-
<maven.compiler.source.version>14</maven.compiler.source.version>
49-
<maven.compiler.target.version>14</maven.compiler.target.version>
62+
<maven.compiler.release>14</maven.compiler.release>
63+
<assertj.version>3.6.1</assertj.version>
64+
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
5065
<surefire.plugin.version>3.0.0-M3</surefire.plugin.version>
5166
</properties>
5267

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.java14.textblocks;
2+
3+
public class TextBlocks13 {
4+
public String getBlockOfHtml() {
5+
return """
6+
<html>
7+
8+
<body>
9+
<p>example text</p>
10+
</body>
11+
</html>""";
12+
}
13+
14+
public String getNonStandardIndent() {
15+
return """
16+
Indent
17+
""";
18+
}
19+
20+
public String getQuery() {
21+
return """
22+
select "id", "user"
23+
from "table"
24+
""";
25+
}
26+
27+
public String getTextWithCarriageReturns() {
28+
return """
29+
separated with\r
30+
carriage returns""";
31+
}
32+
33+
public String getTextWithEscapes() {
34+
return """
35+
fun with\n
36+
whitespace\t\r
37+
and other escapes \"""
38+
""";
39+
}
40+
41+
public String getFormattedText(String parameter) {
42+
return """
43+
Some parameter: %s
44+
""".formatted(parameter);
45+
}
46+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.java14.textblocks;
2+
3+
public class TextBlocks14 {
4+
public String getIgnoredNewLines() {
5+
return """
6+
This is a long test which looks to \
7+
have a newline but actually does not""";
8+
}
9+
10+
public String getEscapedSpaces() {
11+
return """
12+
line 1
13+
line 2 \s
14+
""";
15+
}
16+
}

core-java-modules/core-java-14/src/main/java/com/baeldung/serial/MySerialClass.java

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,37 @@
1414
* @author Donato Rimenti
1515
*/
1616
public class MySerialClass implements Serializable {
17-
18-
@Serial
19-
private static final ObjectStreamField[] serialPersistentFields = null;
20-
21-
@Serial
22-
private static final long serialVersionUID = 1;
23-
24-
@Serial
25-
private void writeObject(ObjectOutputStream stream) throws IOException {
26-
// ...
27-
}
28-
29-
@Serial
30-
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
31-
// ...
32-
}
33-
34-
@Serial
35-
private void readObjectNoData() throws ObjectStreamException {
36-
// ...
37-
}
38-
39-
@Serial
40-
private Object writeReplace() throws ObjectStreamException {
41-
// ...
42-
return null;
43-
}
44-
45-
@Serial
46-
private Object readResolve() throws ObjectStreamException {
47-
// ...
48-
return null;
49-
}
17+
18+
@Serial
19+
private static final ObjectStreamField[] serialPersistentFields = null;
20+
21+
@Serial
22+
private static final long serialVersionUID = 1;
23+
24+
@Serial
25+
private void writeObject(ObjectOutputStream stream) throws IOException {
26+
// ...
27+
}
28+
29+
@Serial
30+
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
31+
// ...
32+
}
33+
34+
@Serial
35+
private void readObjectNoData() throws ObjectStreamException {
36+
// ...
37+
}
38+
39+
@Serial
40+
private Object writeReplace() throws ObjectStreamException {
41+
// ...
42+
return null;
43+
}
44+
45+
@Serial
46+
private Object readResolve() throws ObjectStreamException {
47+
// ...
48+
return null;
49+
}
5050
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.java14.textblocks;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class TextBlocks13UnitTest {
8+
private TextBlocks13 subject = new TextBlocks13();
9+
10+
@Test
11+
void givenAnOldStyleMultilineString_whenComparing_thenEqualsTextBlock() {
12+
String expected = "<html>\n" + "\n" + " <body>\n" + " <p>example text</p>\n" + " </body>\n" + "</html>";
13+
assertThat(subject.getBlockOfHtml()).isEqualTo(expected);
14+
}
15+
16+
@Test
17+
void givenAnOldStyleString_whenComparing_thenEqualsTextBlock() {
18+
String expected = "<html>\n\n <body>\n <p>example text</p>\n </body>\n</html>";
19+
assertThat(subject.getBlockOfHtml()).isEqualTo(expected);
20+
}
21+
22+
@Test
23+
void givenAnIndentedString_thenMatchesIndentedOldStyle() {
24+
assertThat(subject.getNonStandardIndent()).isEqualTo(" Indent\n");
25+
}
26+
27+
@Test
28+
void givenAMultilineQuery_thenItCanContainUnescapedQuotes() {
29+
assertThat(subject.getQuery()).contains("select \"id\", \"user\"");
30+
}
31+
32+
@Test
33+
void givenAMultilineQuery_thenItEndWithANewline() {
34+
assertThat(subject.getQuery()).endsWith("\n");
35+
}
36+
37+
@Test
38+
void givenATextWithCarriageReturns_thenItContainsBoth() {
39+
assertThat(subject.getTextWithCarriageReturns()).isEqualTo("separated with\r\ncarriage returns");
40+
}
41+
42+
@Test
43+
void givenAStringWithEscapedWhitespace_thenItAppearsInTheResultingString() {
44+
assertThat(subject.getTextWithEscapes()).contains("fun with\n\n")
45+
.contains("whitespace\t\r\n")
46+
.contains("and other escapes \"\"\"");
47+
}
48+
49+
@Test
50+
void givenAFormattedString_thenTheParameterIsReplaced() {
51+
assertThat(subject.getFormattedText("parameter")).contains("Some parameter: parameter");
52+
}
53+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.java14.textblocks;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class TextBlocks14UnitTest {
8+
private TextBlocks14 subject = new TextBlocks14();
9+
10+
@Test
11+
void givenAStringWithEscapedNewLines_thenTheResultHasNoNewLines() {
12+
String expected = "This is a long test which looks to have a newline but actually does not";
13+
assertThat(subject.getIgnoredNewLines()).isEqualTo(expected);
14+
}
15+
16+
@Test
17+
void givenAStringWithEscapesSpaces_thenTheResultHasLinesEndingWithSpaces() {
18+
String expected = "line 1\nline 2 \n";
19+
assertThat(subject.getEscapedSpaces()).isEqualTo(expected);
20+
}
21+
}

0 commit comments

Comments
 (0)