Skip to content

Commit b85bb1a

Browse files
Ahmed Tawilapivovarit
authored andcommitted
BAEL-1171 java.lang.String API (eugenp#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
1 parent cccd72d commit b85bb1a

1 file changed

Lines changed: 186 additions & 0 deletions

File tree

‎core-java/src/test/java/com/baeldung/string/StringTest.java‎

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
import static org.junit.Assert.assertArrayEquals;
44
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertTrue;
6+
import static org.junit.Assert.assertFalse;
7+
8+
import java.nio.charset.Charset;
9+
import java.nio.charset.StandardCharsets;
10+
import java.util.IllegalFormatException;
11+
import java.util.regex.PatternSyntaxException;
512

613
import org.junit.Test;
714

@@ -12,6 +19,11 @@ public void whenCallCodePointAt_thenDecimalUnicodeReturned() {
1219
assertEquals(97, "abcd".codePointAt(0));
1320
}
1421

22+
@Test(expected = StringIndexOutOfBoundsException.class)
23+
public void whenPassNonExistingIndex_thenExceptionThrown() {
24+
int a = "abcd".codePointAt(4);
25+
}
26+
1527
@Test
1628
public void whenCallConcat_thenCorrect() {
1729
assertEquals("elephant", "elep".concat("hant"));
@@ -21,6 +33,180 @@ public void whenCallConcat_thenCorrect() {
2133
public void whenGetBytes_thenCorrect() {
2234
byte[] byteArray = "abcd".getBytes();
2335
byte[] expected = new byte[] { 97, 98, 99, 100 };
36+
37+
assertArrayEquals(expected, byteArray);
38+
}
39+
40+
@Test
41+
public void whenGetBytesUsingUTF16_thenCorrect() {
42+
byte[] byteArray = "efgh".getBytes(StandardCharsets.US_ASCII);
43+
byte[] expected = new byte[] { 101, 102, 103, 104 };
44+
2445
assertArrayEquals(expected, byteArray);
2546
}
47+
48+
@Test
49+
public void whenCreateStringUsingByteArray_thenCorrect() {
50+
byte[] array = new byte[] { 97, 98, 99, 100 };
51+
String s = new String(array);
52+
53+
assertEquals("abcd", s);
54+
}
55+
56+
@Test
57+
public void whenCallCharAt_thenCorrect() {
58+
assertEquals('P', "Paul".charAt(0));
59+
}
60+
61+
@Test(expected = IndexOutOfBoundsException.class)
62+
public void whenCharAtOnNonExistingIndex_thenIndexOutOfBoundsExceptionThrown() {
63+
char character = "Paul".charAt(4);
64+
}
65+
66+
@Test
67+
public void whenCallCodePointCount_thenCorrect() {
68+
assertEquals(2, "abcd".codePointCount(0, 2));
69+
}
70+
71+
@Test(expected = IndexOutOfBoundsException.class)
72+
public void whenSecondIndexEqualToLengthOfString_thenIndexOutOfBoundsExceptionThrown() {
73+
char character = "Paul".charAt(4);
74+
}
75+
76+
@Test
77+
public void whenCallContains_thenCorrect() {
78+
String s = "abcd";
79+
80+
assertTrue(s.contains("abc"));
81+
assertFalse(s.contains("cde"));
82+
}
83+
84+
@Test
85+
public void whenCallCopyValueOf_thenStringConstructed() {
86+
char[] array = new char[] { 'a', 'b', 'c', 'd' };
87+
88+
assertEquals("abcd", String.copyValueOf(array));
89+
}
90+
91+
@Test
92+
public void whenCallEndsWith_thenCorrect() {
93+
String s1 = "test";
94+
95+
assertTrue(s1.endsWith("t"));
96+
}
97+
98+
@Test
99+
public void whenFormat_thenCorrect() {
100+
String value = "Baeldung";
101+
String formatted = String.format("Welcome to %s!", value);
102+
103+
assertEquals("Welcome to Baeldung!", formatted);
104+
}
105+
106+
@Test(expected = IllegalFormatException.class)
107+
public void whenInvalidFormatSyntax_thenIllegalFormatExceptionThrown() {
108+
String value = "Baeldung";
109+
String formatted = String.format("Welcome to %x!", value);
110+
}
111+
112+
@Test
113+
public void whenCallIndexOf_thenCorrect() {
114+
assertEquals(1, "foo".indexOf("o"));
115+
}
116+
117+
@Test
118+
public void whenCallIsEmpty_thenCorrect() {
119+
String s1 = "";
120+
121+
assertTrue(s1.isEmpty());
122+
}
123+
124+
@Test
125+
public void whenCallLastIndexOf_thenCorrect() {
126+
assertEquals(2, "foo".lastIndexOf("o"));
127+
}
128+
129+
@Test
130+
public void whenCallRegionMatches_thenCorrect() {
131+
assertTrue("welcome to baeldung".regionMatches(false, 11, "baeldung", 0, 8));
132+
}
133+
134+
@Test
135+
public void whenCallStartsWith_thenCorrect() {
136+
assertTrue("foo".startsWith("f"));
137+
}
138+
139+
@Test
140+
public void whenTrim_thenCorrect() {
141+
assertEquals("foo", " foo ".trim());
142+
}
143+
144+
@Test
145+
public void whenSplit_thenCorrect() {
146+
String s = "Welcome to Baeldung";
147+
String[] array = new String[] { "Welcome", "to", "Baeldung" };
148+
149+
assertArrayEquals(array, s.split(" "));
150+
}
151+
152+
@Test(expected = PatternSyntaxException.class)
153+
public void whenPassInvalidParameterToSplit_thenPatternSyntaxExceptionThrown() {
154+
String s = "Welcome*to Baeldung";
155+
156+
String[] result = s.split("*");
157+
}
158+
159+
@Test
160+
public void whenCallSubstring_thenCorrect() {
161+
String s = "Welcome to Baeldung";
162+
163+
assertEquals("Welcome", s.substring(0, 7));
164+
}
165+
166+
@Test(expected = IndexOutOfBoundsException.class)
167+
public void whenSecondIndexEqualToLengthOfString_thenCorrect() {
168+
String s = "Welcome to Baeldung";
169+
170+
String sub = s.substring(0, 20);
171+
}
172+
173+
@Test
174+
public void whenConvertToUpperCase_thenCorrect() {
175+
String s = "Welcome to Baeldung!";
176+
177+
assertEquals("WELCOME TO BAELDUNG!", s.toUpperCase());
178+
}
179+
180+
@Test
181+
public void whenConvertToLowerCase_thenCorrect() {
182+
String s = "WELCOME to BAELDUNG!";
183+
184+
assertEquals("welcome to baeldung!", s.toLowerCase());
185+
}
186+
187+
@Test
188+
public void whenCallReplace_thenCorrect() {
189+
String s = "I learn Spanish";
190+
191+
assertEquals("I learn French", s.replaceAll("Spanish", "French"));
192+
}
193+
194+
@Test
195+
public void whenIntern_thenCorrect() {
196+
String s1 = "abc";
197+
String s2 = new String("abc");
198+
String s3 = new String("foo");
199+
String s4 = s1.intern();
200+
String s5 = s2.intern();
201+
202+
assertFalse(s3 == s4);
203+
assertTrue(s1 == s5);
204+
}
205+
206+
@Test
207+
public void whenCallValueOf_thenCorrect() {
208+
long l = 200L;
209+
210+
assertEquals("200", String.valueOf(l));
211+
}
26212
}

0 commit comments

Comments
 (0)