|
| 1 | +package com.itextpdf.io.util; |
| 2 | + |
| 3 | +import com.itextpdf.test.annotations.type.UnitTest; |
| 4 | +import org.junit.Assert; |
| 5 | +import org.junit.Test; |
| 6 | +import org.junit.experimental.categories.Category; |
| 7 | + |
| 8 | +import java.util.regex.Pattern; |
| 9 | + |
| 10 | +/** |
| 11 | + * At the moment there is no StringUtil class in Java, but there is one in C# and we are testing |
| 12 | + */ |
| 13 | +@Category(UnitTest.class) |
| 14 | +public class StringUtilTest { |
| 15 | + |
| 16 | + @Test |
| 17 | + public void patternSplitTest01() { |
| 18 | + // Pattern.split in Java works differently compared to Regex.Split in C# |
| 19 | + // In C#, empty strings are possible at the beginning of the resultant array for non-capturing groups in split regex |
| 20 | + // Thus, in C# we use a separate utility for splitting to align the implementation with Java |
| 21 | + // This test verifies that the resultant behavior is the same |
| 22 | + Pattern pattern = Pattern.compile("(?=[ab])"); |
| 23 | + String source = "a01aa78ab89b"; |
| 24 | + String[] expected = new String[] {"a01", "a", "a78", "a", "b89", "b"}; |
| 25 | + String[] result = pattern.split(source); |
| 26 | + Assert.assertArrayEquals(expected, result); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void patternSplitTest02() { |
| 31 | + Pattern pattern = Pattern.compile("(?=[ab])"); |
| 32 | + String source = ""; |
| 33 | + String[] expected = new String[] {""}; |
| 34 | + String[] result = pattern.split(source); |
| 35 | + Assert.assertArrayEquals(expected, result); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void stringSplitTest01() { |
| 40 | + String source = "a01aa78ab89b"; |
| 41 | + String[] expected = new String[] {"a01", "a", "a78", "a", "b89", "b"}; |
| 42 | + String[] result = source.split("(?=[ab])"); |
| 43 | + Assert.assertArrayEquals(expected, result); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void stringSplitTest02() { |
| 48 | + String source = ""; |
| 49 | + String[] expected = new String[] {""}; |
| 50 | + String[] result = source.split("(?=[ab])"); |
| 51 | + Assert.assertArrayEquals(expected, result); |
| 52 | + } |
| 53 | + |
| 54 | +} |
0 commit comments