Merge pull request #8609 from martinvw/feature/BAEL-2397-text-blocks

Feature/bael 2397 text blocks
This commit is contained in:
Jonathan Cook 2020-02-07 16:01:27 +01:00 committed by GitHub
commit 4e3191c488
8 changed files with 237 additions and 75 deletions

View File

@ -0,0 +1 @@
--enable-preview

View File

@ -0,0 +1,7 @@
## Core Java 14
This module contains articles about Java 14.
### Relevant articles
- [Guide to the @Serial Annotation in Java 14](https://www.baeldung.com/java-14-serial-annotation)

View File

@ -1,53 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>core-java-14</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>core-java-14</name>
<packaging>jar</packaging>
<url>http://maven.apache.org</url>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-14</artifactId>
<name>core-java-14</name>
<packaging>jar</packaging>
<url>http://maven.apache.org</url>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../</relativePath>
</parent>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../</relativePath>
</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>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<release>${maven.compiler.release}</release>
<compilerArgs>--enable-preview</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.plugin.version}</version>
<configuration>
<argLine>--enable-preview</argLine>
</configuration>
</plugin>
</plugins>
</build>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${maven.compiler.source.version}</source>
<target>${maven.compiler.target.version}</target>
<compilerArgs>
<compilerArg>
--enable-preview
</compilerArg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.plugin.version}</version>
<configuration>
<argLine>--enable-preview</argLine>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source.version>14</maven.compiler.source.version>
<maven.compiler.target.version>14</maven.compiler.target.version>
<properties>
<maven.compiler.release>14</maven.compiler.release>
<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>
</properties>
</properties>
</project>

View File

@ -0,0 +1,46 @@
package com.baeldung.java14.textblocks;
public class TextBlocks13 {
public String getBlockOfHtml() {
return """
<html>
<body>
<span>example text</span>
</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);
}
}

View File

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

View File

@ -10,41 +10,41 @@ import java.io.Serializable;
/**
* Class showcasing the usage of the Java 14 @Serial annotation.
*
*
* @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;
}
}

View File

@ -0,0 +1,58 @@
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"
+ " <span>example text</span>\n"
+ " </body>\n"
+ "</html>";
assertThat(subject.getBlockOfHtml()).isEqualTo(expected);
}
@Test
void givenAnOldStyleString_whenComparing_thenEqualsTextBlock() {
String expected = "<html>\n\n <body>\n <span>example text</span>\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");
}
}

View File

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