Skip to content

Commit

Permalink
Add tests for String.lines() (jdk11 only)
Browse files Browse the repository at this point in the history
This change adds test cases for String.lines().

This change only affects Java 11.

Signed-off-by: Mohammad Ali Nikseresht <anikser@ibm.com>
  • Loading branch information
Mohammad Ali Nikseresht committed Jul 24, 2018
1 parent d6d3224 commit 1230c9c
Showing 1 changed file with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
import org.testng.annotations.Test;
import org.testng.log4testng.Logger;

import java.util.Iterator;
import java.util.stream.Stream;

/**
* This test Java.lang.String API added in Java 11 and later version.
*
Expand All @@ -42,6 +45,11 @@ public class Test_String {
String allWhiteSpace = " ";
String latin1 = "abc123";
String nonLatin1 = "abc\u0153";
String latin1WithTerm = "one\ntwo\rthree\r\nfour";
String[] latin1LinesArray = {"one", "two", "three", "four"};
String nonLatin1WithTerm = "\u0153\n\u0154\r\u0155\r\n\u0156";
String[] nonLatin1LinesArray = {"\u0153", "\u0154", "\u0155", "\u0156"};
String emptyWithTerm = "\n";

/*
* Test Java 11 API String.valueOfCodePoint(codePoint)
Expand Down Expand Up @@ -213,5 +221,50 @@ public void testIsblank() {
"isBlank: failed to return false on non-blank latin1 string with leading white space.");
Assert.assertFalse((allWhiteSpace + nonLatin1).isBlank(),
"isBlank: failed to return false on non-blank non-latin1 string with leading white space.");

/*
* Test Java 11 API String.lines
*/
@Test(groups = { "level.sanity" })
public void testLines() {
Stream<String> ss;
Iterator<String> iter;
String s;
int i;

// ensure latin1 and nonLatin1 properly breaks apart strings by line terminators
ss = latin1WithTerm.lines();
iter = ss.iterator();
i = 0;
while (iter.hasNext()) {
s = iter.next();
Assert.assertEquals(s, latin1LinesArray[i],
"lines: Failed to break apart latin1 string by line terminators - failed on " + latin1LinesArray[i]);
i++;
}

ss = nonLatin1WithTerm.lines();
iter = ss.iterator();
i = 0;
while (iter.hasNext()) {
s = iter.next();
Assert.assertEquals(s, nonLatin1LinesArray[i],
"lines: Failed to break apart latin1 string by line terminators - failed on " + nonLatin1LinesArray[i]);
i++;
}

// pass in string with only one line terminator - should return empty string
ss = emptyWithTerm.lines();
Assert.assertEquals(ss.findAny().get(), "",
"lines: Failed to get empty string from string with a single line terminator.");

// pass in empty string
try {
ss = empty.lines();
s = ss.findAny().get();
Assert.fail("Failed to throw exception on get non-existent value - empty string.");
} catch (java.util.NoSuchElementException e) {
// Expected exception
}
}
}

0 comments on commit 1230c9c

Please sign in to comment.