BAEL-1171 java.lang.String API (#2732)

* Evaluation article: Different Types of Bean Injection in Spring

* added tests & changed configuration to Java-based config

* removed xml config files

* rename unit tests

* BAEL-972 - Apache Commons Text

* remove code from evaluation article

* remove code from evaluation article

* BAEL-972 - Apache Commons Text - added another example

* BAEL-972 - Apache Commons Text - just indentation

* BAEL-994 - TemporalAdjuster in Java

* BAEL-994 - TemporalAdjuster in Java

* BAEL-994 - TemporalAdjuster in Java

* BAEL-994 - TemporalAdjuster in Java

* BAEL-994 - TemporalAdjuster in Java - fix problems

* BAEL-1033 Introduction to StreamUtils

* BAEL-1033 Introduction to StreamUtils

* BAEL-1033 Introduction to StreamUtils

* fix formatting

* BAEL-1033 minor refactor

* BAEL-1035 Introduction to Eclipse Collections

* format

* BAEL-1035 Introduction to Eclipse Collections

* BAEL-1035 Introduction to Eclipse Collections

* BAEL-1035 Introduction to Eclipse Collections

* cleanup

* cleanup

* BAEL-1109 Introduction to JCache

* BAEL-1109 Introduction to JCache

* remove unneeded property in pom.xml

* fix formatting

* close cache instances properly

* remove latest commit

* BAEL-1057 Introduction to rxjava-jdbc

* refactor rxjava-jdbc

* Refactor rxjava-jdbc

* Refactoring rxjava-jdbc

* BAEL-1171 java.lang.String API

* refactor rxjava-jdbc

* refactor String

* String API - move multiple classes into a single class

* move class into test package

* BAEL-1171 String.lang.String API
This commit is contained in:
Ahmed Tawila 2017-10-15 10:31:46 +02:00 committed by Grzegorz Piwowarek
parent cccd72d451
commit b85bb1ae1e
1 changed files with 186 additions and 0 deletions

View File

@ -2,6 +2,13 @@ package com.baeldung.string;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.IllegalFormatException;
import java.util.regex.PatternSyntaxException;
import org.junit.Test; import org.junit.Test;
@ -12,6 +19,11 @@ public class StringTest {
assertEquals(97, "abcd".codePointAt(0)); assertEquals(97, "abcd".codePointAt(0));
} }
@Test(expected = StringIndexOutOfBoundsException.class)
public void whenPassNonExistingIndex_thenExceptionThrown() {
int a = "abcd".codePointAt(4);
}
@Test @Test
public void whenCallConcat_thenCorrect() { public void whenCallConcat_thenCorrect() {
assertEquals("elephant", "elep".concat("hant")); assertEquals("elephant", "elep".concat("hant"));
@ -21,6 +33,180 @@ public class StringTest {
public void whenGetBytes_thenCorrect() { public void whenGetBytes_thenCorrect() {
byte[] byteArray = "abcd".getBytes(); byte[] byteArray = "abcd".getBytes();
byte[] expected = new byte[] { 97, 98, 99, 100 }; byte[] expected = new byte[] { 97, 98, 99, 100 };
assertArrayEquals(expected, byteArray); assertArrayEquals(expected, byteArray);
} }
@Test
public void whenGetBytesUsingUTF16_thenCorrect() {
byte[] byteArray = "efgh".getBytes(StandardCharsets.US_ASCII);
byte[] expected = new byte[] { 101, 102, 103, 104 };
assertArrayEquals(expected, byteArray);
}
@Test
public void whenCreateStringUsingByteArray_thenCorrect() {
byte[] array = new byte[] { 97, 98, 99, 100 };
String s = new String(array);
assertEquals("abcd", s);
}
@Test
public void whenCallCharAt_thenCorrect() {
assertEquals('P', "Paul".charAt(0));
}
@Test(expected = IndexOutOfBoundsException.class)
public void whenCharAtOnNonExistingIndex_thenIndexOutOfBoundsExceptionThrown() {
char character = "Paul".charAt(4);
}
@Test
public void whenCallCodePointCount_thenCorrect() {
assertEquals(2, "abcd".codePointCount(0, 2));
}
@Test(expected = IndexOutOfBoundsException.class)
public void whenSecondIndexEqualToLengthOfString_thenIndexOutOfBoundsExceptionThrown() {
char character = "Paul".charAt(4);
}
@Test
public void whenCallContains_thenCorrect() {
String s = "abcd";
assertTrue(s.contains("abc"));
assertFalse(s.contains("cde"));
}
@Test
public void whenCallCopyValueOf_thenStringConstructed() {
char[] array = new char[] { 'a', 'b', 'c', 'd' };
assertEquals("abcd", String.copyValueOf(array));
}
@Test
public void whenCallEndsWith_thenCorrect() {
String s1 = "test";
assertTrue(s1.endsWith("t"));
}
@Test
public void whenFormat_thenCorrect() {
String value = "Baeldung";
String formatted = String.format("Welcome to %s!", value);
assertEquals("Welcome to Baeldung!", formatted);
}
@Test(expected = IllegalFormatException.class)
public void whenInvalidFormatSyntax_thenIllegalFormatExceptionThrown() {
String value = "Baeldung";
String formatted = String.format("Welcome to %x!", value);
}
@Test
public void whenCallIndexOf_thenCorrect() {
assertEquals(1, "foo".indexOf("o"));
}
@Test
public void whenCallIsEmpty_thenCorrect() {
String s1 = "";
assertTrue(s1.isEmpty());
}
@Test
public void whenCallLastIndexOf_thenCorrect() {
assertEquals(2, "foo".lastIndexOf("o"));
}
@Test
public void whenCallRegionMatches_thenCorrect() {
assertTrue("welcome to baeldung".regionMatches(false, 11, "baeldung", 0, 8));
}
@Test
public void whenCallStartsWith_thenCorrect() {
assertTrue("foo".startsWith("f"));
}
@Test
public void whenTrim_thenCorrect() {
assertEquals("foo", " foo ".trim());
}
@Test
public void whenSplit_thenCorrect() {
String s = "Welcome to Baeldung";
String[] array = new String[] { "Welcome", "to", "Baeldung" };
assertArrayEquals(array, s.split(" "));
}
@Test(expected = PatternSyntaxException.class)
public void whenPassInvalidParameterToSplit_thenPatternSyntaxExceptionThrown() {
String s = "Welcome*to Baeldung";
String[] result = s.split("*");
}
@Test
public void whenCallSubstring_thenCorrect() {
String s = "Welcome to Baeldung";
assertEquals("Welcome", s.substring(0, 7));
}
@Test(expected = IndexOutOfBoundsException.class)
public void whenSecondIndexEqualToLengthOfString_thenCorrect() {
String s = "Welcome to Baeldung";
String sub = s.substring(0, 20);
}
@Test
public void whenConvertToUpperCase_thenCorrect() {
String s = "Welcome to Baeldung!";
assertEquals("WELCOME TO BAELDUNG!", s.toUpperCase());
}
@Test
public void whenConvertToLowerCase_thenCorrect() {
String s = "WELCOME to BAELDUNG!";
assertEquals("welcome to baeldung!", s.toLowerCase());
}
@Test
public void whenCallReplace_thenCorrect() {
String s = "I learn Spanish";
assertEquals("I learn French", s.replaceAll("Spanish", "French"));
}
@Test
public void whenIntern_thenCorrect() {
String s1 = "abc";
String s2 = new String("abc");
String s3 = new String("foo");
String s4 = s1.intern();
String s5 = s2.intern();
assertFalse(s3 == s4);
assertTrue(s1 == s5);
}
@Test
public void whenCallValueOf_thenCorrect() {
long l = 200L;
assertEquals("200", String.valueOf(l));
}
} }