From 14f518107e88a757f4cd7b2692568d41db6f320a Mon Sep 17 00:00:00 2001 From: Mohd Farid Date: Fri, 28 Apr 2017 16:38:13 +0530 Subject: [PATCH] String split tests --- .../java/com/baeldung/string/SplitTest.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 core-java/src/test/java/com/baeldung/string/SplitTest.java diff --git a/core-java/src/test/java/com/baeldung/string/SplitTest.java b/core-java/src/test/java/com/baeldung/string/SplitTest.java new file mode 100644 index 0000000000..d2e307e68d --- /dev/null +++ b/core-java/src/test/java/com/baeldung/string/SplitTest.java @@ -0,0 +1,32 @@ +package com.baeldung.string; + +import org.apache.commons.lang.StringUtils; +import org.junit.Test; + +import java.util.Arrays; + +import static org.junit.Assert.assertArrayEquals; + +public class SplitTest { + + @Test + public void givenString_whenSplit_thenRetrunsArray_through_JavaLangString() { + assertArrayEquals("split by comma", Arrays.asList("peter", "james", "thomas").toArray(), "peter,james,thomas".split(",")); + assertArrayEquals("split by whitespace", Arrays.asList("car", "jeep", "scooter").toArray(), "car jeep scooter".split(" ")); + assertArrayEquals("split by hyphen", Arrays.asList("1", "120", "232323").toArray(), "1-120-232323".split("-")); + assertArrayEquals("split by dot", Arrays.asList("192", "168", "1", "178").toArray(), "192.168.1.178".split("\\.")); + assertArrayEquals("split by a regex", Arrays.asList("b", "a", "e", "l", "d", "u", "n", "g").toArray(), + "b a, e, l.d u, n g".split("\\s+|,\\s*|\\.\\s*")); + } + + @Test + public void givenString_whenSplit_thenRetrunsArray_through_StringUtils() { + StringUtils.split("car jeep scooter"); + + assertArrayEquals("split by whitespace", Arrays.asList("car", "jeep", "scooter").toArray(), StringUtils.split("car jeep scooter")); + assertArrayEquals("split by space, extra spaces ignored", Arrays.asList("car", "jeep", "scooter").toArray(), + StringUtils.split("car jeep scooter")); + assertArrayEquals("split by colon", Arrays.asList("car", "jeep", "scooter").toArray(), StringUtils.split("car:jeep:scooter", ":")); + assertArrayEquals("split by dot", Arrays.asList("car", "jeep", "scooter").toArray(), StringUtils.split("car.jeep.scooter", ".")); + } +}