BAEL-5277 generate string of repeated characters (#11611)
* BAEL-5277 generate string of repeated characters * BAEL-5277 fixed test * BAEL-5277 fixing tests * removed public from class and methods, removed unnecessary test examples, extracted contants * renamed constants * renamed a test * fixed the string repeat test * removed public from class and method * modified the test * reset the java version * tests added * added apache dependency * removed repeat method for string * updated tests * fixed tests * added two additional tests * formatted code * renaming variables * 1.0.0 | FIX removed unnecessary annotation * removing EMPTY_STRING constant Co-authored-by: Matea Pejčinović <matea.pejcinovic@intellexi.hr>
This commit is contained in:
parent
ccecd19d6a
commit
7a5d817a1d
|
@ -24,6 +24,11 @@
|
||||||
<artifactId>commons-validator</artifactId>
|
<artifactId>commons-validator</artifactId>
|
||||||
<version>${validator.version}</version>
|
<version>${validator.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>${apache-commons-lang3.version}</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -51,6 +56,7 @@
|
||||||
<properties>
|
<properties>
|
||||||
<guava.version>31.0.1-jre</guava.version>
|
<guava.version>31.0.1-jre</guava.version>
|
||||||
<validator.version>1.7</validator.version>
|
<validator.version>1.7</validator.version>
|
||||||
|
<apache-commons-lang3.version>3.12.0</apache-commons-lang3.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -0,0 +1,92 @@
|
||||||
|
package com.baeldung.repeatedcharstring;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.RandomStringUtils;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import com.google.common.base.Joiner;
|
||||||
|
import com.google.common.base.Strings;
|
||||||
|
|
||||||
|
import static java.util.stream.Stream.generate;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
class RepeatedCharacterStringUnitTest {
|
||||||
|
|
||||||
|
private static final String EXPECTED_STRING = "aaaaaaa";
|
||||||
|
private static final int N = 7;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenString_whenStringBuilderUsed_thenStringCreated() {
|
||||||
|
StringBuilder builder = new StringBuilder(N);
|
||||||
|
for (int i = 0; i < N; i++) {
|
||||||
|
builder.append("a");
|
||||||
|
}
|
||||||
|
String newString = builder.toString();
|
||||||
|
assertEquals(EXPECTED_STRING, newString);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenString_whenCharArrayUsed_thenStringCreated() {
|
||||||
|
char[] charArray = new char[N];
|
||||||
|
for (int i = 0; i < N; i++) {
|
||||||
|
charArray[i] = 'a';
|
||||||
|
}
|
||||||
|
String newString = new String(charArray);
|
||||||
|
assertEquals(EXPECTED_STRING, newString);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenString_whenApacheStringUtilsUsed_thenStringCreated() {
|
||||||
|
char charToAppend = 'a';
|
||||||
|
String newString = StringUtils.repeat(charToAppend, N);
|
||||||
|
assertEquals(EXPECTED_STRING, newString);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenString_whenGuavaRepeatUsed_thenStringCreated() {
|
||||||
|
String charToAppend = "a";
|
||||||
|
String newString = Strings.repeat(charToAppend, N);
|
||||||
|
assertEquals(EXPECTED_STRING, newString);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenString_whenArraysFillUsed_thenStringCreated() {
|
||||||
|
char charToAppend = 'a';
|
||||||
|
char[] charArray = new char[N];
|
||||||
|
Arrays.fill(charArray, charToAppend);
|
||||||
|
String newString = new String(charArray);
|
||||||
|
assertEquals(EXPECTED_STRING, newString);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenString_whenGenerateAndJoiningUsed_thenStringCreated() {
|
||||||
|
String charToAppend = "a";
|
||||||
|
String newString = generate(() -> charToAppend).limit(N).collect(Collectors.joining());
|
||||||
|
assertEquals(EXPECTED_STRING, newString);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenString_whenStringJoinUsed_thenStringCreated() {
|
||||||
|
String charToAppend = "a";
|
||||||
|
String newString = String.join("", Collections.nCopies(N, charToAppend));
|
||||||
|
assertEquals(EXPECTED_STRING, newString);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenString_whenGuavaJoinerUsed_thenStringCreated() {
|
||||||
|
String charToAppend = "a";
|
||||||
|
String newString = Joiner.on("").join(Collections.nCopies(N, charToAppend));
|
||||||
|
assertEquals(EXPECTED_STRING, newString);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenString_whenRandomStringUtilsUsed_thenStringCreated() {
|
||||||
|
String charToAppend = "a";
|
||||||
|
String newString = RandomStringUtils.random(N, charToAppend);
|
||||||
|
assertEquals(EXPECTED_STRING, newString);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue