This PR related to the article BAEL-7023 (#14993)
* Update pom.xml * This commit related to the article BAEL-7023 This commit aims to add a test class 'CenteringTextUnitTest.java' proposing several ways to center text in Java.
This commit is contained in:
parent
ab62e675ec
commit
0eafba3ba1
|
@ -0,0 +1,2 @@
|
|||
|
||||
### Relevant Articles:
|
|
@ -0,0 +1,72 @@
|
|||
<?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>
|
||||
<artifactId>core-java-string-operations-7</artifactId>
|
||||
<name>core-java-string-operations-7</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<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>
|
||||
<dependency>
|
||||
<groupId>org.liquibase</groupId>
|
||||
<artifactId>liquibase-core</artifactId>
|
||||
<version>4.9.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>5.8.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.liquibase</groupId>
|
||||
<artifactId>liquibase-core</artifactId>
|
||||
<version>4.9.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.13.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source}</source>
|
||||
<target>${maven.compiler.target}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<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,47 @@
|
|||
package com.baeldung.centertext;
|
||||
|
||||
import liquibase.repackaged.org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class CenteringTextUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenTextAndTotalWidth_whenUsingStringFormat_thenTextIsCentered() {
|
||||
String text = "Centered Text";
|
||||
int totalWidth = 20;
|
||||
int padding = (totalWidth - text.length()) / 2;
|
||||
String centeredText = String.format("%" + padding + "s%s%" + padding + "s", "", text, "");
|
||||
String expectedCenteredText = " Centered Text ";
|
||||
Assert.assertEquals("Centered Text", expectedCenteredText, centeredText);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTextAndTotalWidth_whenCenterUsingStringBuilder_thenTextIsCentered() {
|
||||
String text = "Centered Text";
|
||||
int width = 20;
|
||||
int padding = (width - text.length()) / 2;
|
||||
StringBuilder centeredText = new StringBuilder();
|
||||
for (int i = 0; i < padding; i++) {
|
||||
centeredText.append(" ");
|
||||
}
|
||||
centeredText.append(text);
|
||||
for (int i = 0; i < padding; i++) {
|
||||
centeredText.append(" ");
|
||||
}
|
||||
String centeredTextString = centeredText.toString();
|
||||
String expectedCenteredText = " Centered Text ";
|
||||
Assert.assertEquals("Centered Text", expectedCenteredText, centeredTextString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTextAndTotalWidth_whenUsingStringUtilsCenterMethod_thenTextIsCentered() {
|
||||
String text = "Centered Text";
|
||||
int width = 20;
|
||||
String centeredText = StringUtils.center(text, width);
|
||||
String expectedCenteredText = StringUtils.center("Centered Text", width);
|
||||
assertEquals("Centered Text", expectedCenteredText, centeredText);
|
||||
}
|
||||
|
||||
}
|
|
@ -178,6 +178,7 @@
|
|||
<module>core-java-string-operations</module>
|
||||
<module>core-java-string-operations-2</module>
|
||||
<module>core-java-string-operations-6</module>
|
||||
<module>core-java-string-operations-7</module>
|
||||
<module>core-java-regex</module>
|
||||
<module>core-java-regex-2</module>
|
||||
<module>core-java-uuid</module>
|
||||
|
|
Loading…
Reference in New Issue