forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-2368 convert array to string and back (eugenp#5689)
* BAEL-2368 convert string array to string * BAEL-2368 Convert array to string code * BAEL-2368 Change package * Fix for test
- Loading branch information
Showing
1 changed file
with
136 additions
and
0 deletions.
There are no files selected for viewing
136 changes: 136 additions & 0 deletions
136
...ns-conversions/src/test/java/org/baeldung/convertarraytostring/ArrayToStringUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package org.baeldung.convertarraytostring; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.junit.Test; | ||
|
||
import com.google.common.base.Joiner; | ||
import com.google.common.base.Splitter; | ||
|
||
import static org.hamcrest.CoreMatchers.instanceOf; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
public class ArrayToStringUnitTest { | ||
|
||
// convert with Java | ||
|
||
@Test | ||
public void givenAStringArray_whenConvertBeforeJava8_thenReturnString() { | ||
|
||
String[] strArray = { "Convert", "Array", "With", "Java" }; | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
|
||
for (int i = 0; i < strArray.length; i++) { | ||
stringBuilder.append(strArray[i]); | ||
} | ||
String joinedString = stringBuilder.toString(); | ||
|
||
assertThat(joinedString, instanceOf(String.class)); | ||
assertEquals("ConvertArrayWithJava", joinedString); | ||
} | ||
|
||
@Test | ||
public void givenAString_whenConvertBeforeJava8_thenReturnStringArray() { | ||
|
||
String input = "lorem ipsum dolor sit amet"; | ||
String[] strArray = input.split(" "); | ||
|
||
assertThat(strArray, instanceOf(String[].class)); | ||
assertEquals(5, strArray.length); | ||
|
||
input = "loremipsum"; | ||
strArray = input.split(""); | ||
assertThat(strArray, instanceOf(String[].class)); | ||
assertEquals(10, strArray.length); | ||
} | ||
|
||
@Test | ||
public void givenAnIntArray_whenConvertBeforeJava8_thenReturnString() { | ||
|
||
int[] strArray = { 1, 2, 3, 4, 5 }; | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
|
||
for (int i = 0; i < strArray.length; i++) { | ||
stringBuilder.append(Integer.valueOf(strArray[i])); | ||
} | ||
String joinedString = stringBuilder.toString(); | ||
|
||
assertThat(joinedString, instanceOf(String.class)); | ||
assertEquals("12345", joinedString); | ||
} | ||
|
||
// convert with Java Stream API | ||
|
||
@Test | ||
public void givenAStringArray_whenConvertWithJavaStream_thenReturnString() { | ||
|
||
String[] strArray = { "Convert", "With", "Java", "Streams" }; | ||
String joinedString = Arrays.stream(strArray) | ||
.collect(Collectors.joining()); | ||
assertThat(joinedString, instanceOf(String.class)); | ||
assertEquals("ConvertWithJavaStreams", joinedString); | ||
|
||
joinedString = Arrays.stream(strArray) | ||
.collect(Collectors.joining(",")); | ||
assertThat(joinedString, instanceOf(String.class)); | ||
assertEquals("Convert,With,Java,Streams", joinedString); | ||
} | ||
|
||
|
||
// convert with Apache Commons | ||
|
||
@Test | ||
public void givenAStringArray_whenConvertWithApacheCommons_thenReturnString() { | ||
|
||
String[] strArray = { "Convert", "With", "Apache", "Commons" }; | ||
String joinedString = StringUtils.join(strArray); | ||
|
||
assertThat(joinedString, instanceOf(String.class)); | ||
assertEquals("ConvertWithApacheCommons", joinedString); | ||
} | ||
|
||
@Test | ||
public void givenAString_whenConvertWithApacheCommons_thenReturnStringArray() { | ||
|
||
String input = "lorem ipsum dolor sit amet"; | ||
String[] strArray = StringUtils.split(input, " "); | ||
|
||
assertThat(strArray, instanceOf(String[].class)); | ||
assertEquals(5, strArray.length); | ||
} | ||
|
||
|
||
// convert with Guava | ||
|
||
@Test | ||
public void givenAStringArray_whenConvertWithGuava_thenReturnString() { | ||
|
||
String[] strArray = { "Convert", "With", "Guava", null }; | ||
String joinedString = Joiner.on("") | ||
.skipNulls() | ||
.join(strArray); | ||
|
||
assertThat(joinedString, instanceOf(String.class)); | ||
assertEquals("ConvertWithGuava", joinedString); | ||
} | ||
|
||
|
||
@Test | ||
public void givenAString_whenConvertWithGuava_thenReturnStringArray() { | ||
|
||
String input = "lorem ipsum dolor sit amet"; | ||
|
||
List<String> resultList = Splitter.on(' ') | ||
.trimResults() | ||
.omitEmptyStrings() | ||
.splitToList(input); | ||
String[] strArray = resultList.toArray(new String[0]); | ||
|
||
assertThat(strArray, instanceOf(String[].class)); | ||
assertEquals(5, strArray.length); | ||
} | ||
} |