BAEL-5981 - String Interpolation in Java (#13092)
This commit is contained in:
parent
53dbea8d56
commit
965cf501f5
@ -20,6 +20,11 @@
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${apache.commons.lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-text</artifactId>
|
||||
<version>${commons-text.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@ -39,6 +44,7 @@
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<apache.commons.lang3.version>3.12.0</apache.commons.lang3.version>
|
||||
<commons-text.version>1.10.0</commons-text.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
@ -0,0 +1,84 @@
|
||||
package com.baeldung.string_interpolation;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.text.StringSubstitutor;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class StringInterpolationUnitTest {
|
||||
private final String EXPECTED_STRING = "String Interpolation in Java with some Java examples.";
|
||||
|
||||
@Test
|
||||
public void givenTwoString_thenInterpolateWithPlusSign() {
|
||||
String EXPECTED_STRING = "String Interpolation in Java with some Java examples.";
|
||||
String first = "Interpolation";
|
||||
String second = "Java";
|
||||
String result = "String " + first + " in " + second + " with some " + second + " examples.";
|
||||
assertEquals(EXPECTED_STRING, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoString_thenInterpolateWithFormat() {
|
||||
String first = "Interpolation";
|
||||
String second = "Java";
|
||||
String result = String.format("String %s in %s with some %s examples.", first, second, second);
|
||||
assertEquals(EXPECTED_STRING, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoString_thenInterpolateWithFormatted() {
|
||||
String first = "Interpolation";
|
||||
String second = "Java";
|
||||
String result = String.format("String %s in %s with some %s examples.", first, second, second);
|
||||
assertEquals(EXPECTED_STRING, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoString_thenInterpolateWithFormatStringReference() {
|
||||
String first = "Interpolation";
|
||||
String second = "Java";
|
||||
String result = String.format("String %1$s in %2$s with some %2$s examples.", first, second);
|
||||
assertEquals(EXPECTED_STRING, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoString_thenInterpolateWithStringBuilder() {
|
||||
String first = "Interpolation";
|
||||
String second = "Java";
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("String ")
|
||||
.append(first)
|
||||
.append(" in ")
|
||||
.append(second)
|
||||
.append(" with some ")
|
||||
.append(second)
|
||||
.append(" examples.");
|
||||
String result = builder.toString();
|
||||
assertEquals(EXPECTED_STRING, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoString_thenInterpolateWithMessageFormat() {
|
||||
String first = "Interpolation";
|
||||
String second = "Java";
|
||||
String result = MessageFormat.format("String {0} in {1} with some {1} examples.", first, second);
|
||||
assertEquals(EXPECTED_STRING, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoString_thenInterpolateWithStringSubstitutor() {
|
||||
String baseString = "String ${first} in ${second} with some ${second} examples.";
|
||||
String first = "Interpolation";
|
||||
String second = "Java";
|
||||
Map<String, String> parameters = new HashMap<>();
|
||||
parameters.put("first", first);
|
||||
parameters.put("second", second);
|
||||
StringSubstitutor substitutor = new StringSubstitutor(parameters);
|
||||
String result = substitutor.replace(baseString);
|
||||
assertEquals(EXPECTED_STRING, result);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user