Move articles out of java-strings part5
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
package com.baeldung.chararraypassword;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class PasswordStoreExamplesUnitTest {
|
||||
|
||||
String stringPassword = "password";
|
||||
char[] charPassword = new char[]{'p', 'a', 's', 's', 'w', 'o', 'r', 'd'};
|
||||
|
||||
@Test
|
||||
public void givenStringHashCode_WhenStringValueChanged_ThenHashCodesNotEqualAndValesNotEqual() {
|
||||
String originalHashCode = Integer.toHexString(stringPassword.hashCode());
|
||||
|
||||
stringPassword = "********";
|
||||
String changedHashCode = Integer.toHexString(stringPassword.hashCode());
|
||||
|
||||
assertThat(originalHashCode).isNotEqualTo(changedHashCode);
|
||||
assertThat(stringPassword).isNotEqualTo("password");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringHashCode_WhenStringValueChangedAndStringValueReassigned_ThenHashCodesEqualAndValesEqual() {
|
||||
String originalHashCode = Integer.toHexString(stringPassword.hashCode());
|
||||
|
||||
stringPassword = "********";
|
||||
stringPassword = "password";
|
||||
String reassignedHashCode = Integer.toHexString(stringPassword.hashCode());
|
||||
|
||||
assertThat(originalHashCode).isEqualTo(reassignedHashCode);
|
||||
assertThat(stringPassword).isEqualTo("password");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringHashCode_WhenStringValueReplaced_ThenHashCodesEqualAndValesEqual() {
|
||||
String originalHashCode = Integer.toHexString(stringPassword.hashCode());
|
||||
|
||||
String newString = "********";
|
||||
stringPassword.replace(stringPassword, newString);
|
||||
|
||||
String hashCodeAfterReplace = Integer.toHexString(stringPassword.hashCode());
|
||||
|
||||
assertThat(originalHashCode).isEqualTo(hashCodeAfterReplace);
|
||||
assertThat(stringPassword).isEqualTo("password");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCharArrayHashCode_WhenArrayElementsValueChanged_ThenHashCodesEqualAndValesNotEqual() {
|
||||
String originalHashCode = Integer.toHexString(charPassword.hashCode());
|
||||
|
||||
Arrays.fill(charPassword, '*');
|
||||
String changedHashCode = Integer.toHexString(charPassword.hashCode());
|
||||
|
||||
assertThat(originalHashCode).isEqualTo(changedHashCode);
|
||||
assertThat(charPassword).isNotEqualTo(new char[]{'p', 'a', 's', 's', 'w', 'o', 'r', 'd'});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallingToStringOfString_ThenValuesEqual() {
|
||||
assertThat(stringPassword.toString()).isEqualTo("password");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallingToStringOfCharArray_ThenValuesNotEqual() {
|
||||
assertThat(charPassword.toString()).isNotEqualTo("password");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.text.NumberFormat;
|
||||
import java.util.Locale;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class LocaleUnitTest {
|
||||
@Test
|
||||
public void whenUsingLocal_thenCorrectResultsForDifferentLocale() {
|
||||
Locale usLocale = Locale.US;
|
||||
BigDecimal number = new BigDecimal(102_300.456d);
|
||||
|
||||
NumberFormat usNumberFormat = NumberFormat.getCurrencyInstance(usLocale);
|
||||
assertEquals(usNumberFormat.format(number), "$102,300.46");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class StringAnagramUnitTest {
|
||||
public boolean isAnagram(String s1, String s2) {
|
||||
if(s1.length() != s2.length())
|
||||
return false;
|
||||
|
||||
char[] arr1 = s1.toCharArray();
|
||||
char[] arr2 = s2.toCharArray();
|
||||
|
||||
Arrays.sort(arr1);
|
||||
Arrays.sort(arr2);
|
||||
|
||||
return Arrays.equals(arr1, arr2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenTestAnagrams_thenTestingCorrectly() {
|
||||
assertThat(isAnagram("car", "arc")).isTrue();
|
||||
assertThat(isAnagram("west", "stew")).isTrue();
|
||||
assertThat(isAnagram("west", "east")).isFalse();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class StringChangeCaseUnitTest {
|
||||
@Test
|
||||
public void givenString_whenChangingToUppercase_thenCaseChanged() {
|
||||
String s = "Welcome to Baeldung!";
|
||||
assertEquals("WELCOME TO BAELDUNG!", s.toUpperCase());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenString_whenChangingToLowerrcase_thenCaseChanged() {
|
||||
String s = "Welcome to Baeldung!";
|
||||
assertEquals("welcome to baeldung!", s.toLowerCase());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class StringCountOccurrencesUnitTest {
|
||||
public int countOccurrences(String s, char c) {
|
||||
int count = 0;
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
if (s.charAt(i) == c) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenCountingFrequencyOfChar_thenCountCorrect() {
|
||||
assertEquals(3, countOccurrences("united states", 't'));
|
||||
}
|
||||
|
||||
public void givenString_whenUsingJava8_thenCountingOfCharCorrect() {
|
||||
String str = "united states";
|
||||
long count = str.chars().filter(ch -> (char)ch == 't').count();
|
||||
assertEquals(3, count);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class StringFormatUnitTest {
|
||||
@Test
|
||||
public void givenString_whenUsingStringFormat_thenStringFormatted() {
|
||||
String title = "Baeldung";
|
||||
String formatted = String.format("Title is %s", title);
|
||||
assertEquals(formatted, "Title is Baeldung");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class StringInternUnitTest {
|
||||
@Test
|
||||
public void whenCallingStringIntern_thenStringsInterned() {
|
||||
String s1 = "Baeldung";
|
||||
String s2 = new String("Baeldung");
|
||||
String s3 = new String("Baeldung").intern();
|
||||
|
||||
assertThat(s1 == s2).isFalse();
|
||||
assertThat(s1 == s3).isTrue();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.StringJoiner;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class StringJoinerUnitTest {
|
||||
@Test
|
||||
public void whenUsingStringJoiner_thenStringsJoined() {
|
||||
StringJoiner joiner = new StringJoiner(",", "[", "]");
|
||||
joiner.add("Red")
|
||||
.add("Green")
|
||||
.add("Blue");
|
||||
|
||||
assertEquals(joiner.toString(), "[Red,Green,Blue]");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class StringPalindromeUnitTest {
|
||||
|
||||
public boolean isPalindrome(String text) {
|
||||
int forward = 0;
|
||||
int backward = text.length() - 1;
|
||||
while (backward > forward) {
|
||||
char forwardChar = text.charAt(forward++);
|
||||
char backwardChar = text.charAt(backward--);
|
||||
if (forwardChar != backwardChar)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIsPalindromeMethod_whenCheckingString_thenFindIfPalindrome() {
|
||||
assertThat(isPalindrome("madam")).isTrue();
|
||||
assertThat(isPalindrome("radar")).isTrue();
|
||||
assertThat(isPalindrome("level")).isTrue();
|
||||
|
||||
assertThat(isPalindrome("baeldung")).isFalse();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class StringReverseUnitTest {
|
||||
@Test
|
||||
public void whenUsingInbuildMethods_thenStringReversed() {
|
||||
String reversed = new StringBuilder("baeldung").reverse().toString();
|
||||
assertEquals("gnudleab", reversed);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.interview;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
public class StringSplitUnitTest {
|
||||
@Test
|
||||
public void givenCoreJava_whenSplittingStrings_thenSplitted() {
|
||||
String expected[] = {
|
||||
"john",
|
||||
"peter",
|
||||
"mary"
|
||||
};
|
||||
|
||||
String[] splitted = "john,peter,mary".split(",");
|
||||
assertArrayEquals( expected, splitted );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenApacheCommons_whenSplittingStrings_thenSplitted() {
|
||||
String expected[] = {
|
||||
"john",
|
||||
"peter",
|
||||
"mary"
|
||||
};
|
||||
String[] splitted = StringUtils.split("john peter mary");
|
||||
assertArrayEquals( expected, splitted );
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
public class StringToByteArrayUnitTest {
|
||||
@Test
|
||||
public void whenGetBytes_thenCorrect() throws UnsupportedEncodingException {
|
||||
byte[] byteArray1 = "abcd".getBytes();
|
||||
byte[] byteArray2 = "efgh".getBytes(StandardCharsets.US_ASCII);
|
||||
byte[] byteArray3 = "ijkl".getBytes("UTF-8");
|
||||
byte[] expected1 = new byte[] { 97, 98, 99, 100 };
|
||||
byte[] expected2 = new byte[] { 101, 102, 103, 104 };
|
||||
byte[] expected3 = new byte[] { 105, 106, 107, 108 };
|
||||
|
||||
assertArrayEquals(expected1, byteArray1);
|
||||
assertArrayEquals(expected2, byteArray2);
|
||||
assertArrayEquals(expected3, byteArray3);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class StringToCharArrayUnitTest {
|
||||
@Test
|
||||
public void whenConvertingStringToCharArray_thenConversionSuccessful() {
|
||||
String beforeConvStr = "hello";
|
||||
char[] afterConvCharArr = { 'h', 'e', 'l', 'l', 'o' };
|
||||
|
||||
assertEquals(Arrays.equals(beforeConvStr.toCharArray(), afterConvCharArr), true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.interview;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class StringToIntegerUnitTest {
|
||||
@Test
|
||||
public void givenString_whenParsingInt_shouldConvertToInt() {
|
||||
String givenString = "42";
|
||||
|
||||
int result = Integer.parseInt(givenString);
|
||||
|
||||
assertThat(result).isEqualTo(42);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.baeldung.localization;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ICUFormatUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenInUK_whenAliceSendsNothing_thenCorrectMessage() {
|
||||
assertEquals("Alice has sent you no messages.", ICUFormat.getLabel(Locale.UK, new Object[] { "Alice", "female", 0 }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInUK_whenAliceSendsOneMessage_thenCorrectMessage() {
|
||||
assertEquals("Alice has sent you a message.", ICUFormat.getLabel(Locale.UK, new Object[] { "Alice", "female", 1 }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInUK_whenBobSendsSixMessages_thenCorrectMessage() {
|
||||
assertEquals("Bob has sent you 6 messages.", ICUFormat.getLabel(Locale.UK, new Object[] { "Bob", "male", 6 }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInItaly_whenAliceSendsNothing_thenCorrectMessage() {
|
||||
assertEquals("Alice non ti ha inviato nessun messaggio.", ICUFormat.getLabel(Locale.ITALY, new Object[] { "Alice", "female", 0 }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInItaly_whenAliceSendsOneMessage_thenCorrectMessage() {
|
||||
assertEquals("Alice ti ha inviato un messaggio.", ICUFormat.getLabel(Locale.ITALY, new Object[] { "Alice", "female", 1 }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInItaly_whenBobSendsSixMessages_thenCorrectMessage() {
|
||||
assertEquals("Bob ti ha inviato 6 messaggi.", ICUFormat.getLabel(Locale.ITALY, new Object[] { "Bob", "male", 6 }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInFrance_whenAliceSendsNothing_thenCorrectMessage() {
|
||||
assertEquals("Alice ne vous a envoyé aucun message.", ICUFormat.getLabel(Locale.FRANCE, new Object[] { "Alice", "female", 0 }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInFrance_whenAliceSendsOneMessage_thenCorrectMessage() {
|
||||
assertEquals("Alice vous a envoyé un message.", ICUFormat.getLabel(Locale.FRANCE, new Object[] { "Alice", "female", 1 }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInFrance_whenBobSendsSixMessages_thenCorrectMessage() {
|
||||
assertEquals("Bob vous a envoyé 6 messages.", ICUFormat.getLabel(Locale.FRANCE, new Object[] { "Bob", "male", 6 }));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenInPoland_whenAliceSendsNothing_thenCorrectMessage() {
|
||||
assertEquals("Alice nie wysłała ci żadnej wiadomości.", ICUFormat.getLabel(Locale.forLanguageTag("pl-PL"), new Object[] { "Alice", "female", 0 }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInPoland_whenAliceSendsOneMessage_thenCorrectMessage() {
|
||||
assertEquals("Alice wysłała ci wiadomość.", ICUFormat.getLabel(Locale.forLanguageTag("pl-PL"), new Object[] { "Alice", "female", 1 }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInPoland_whenBobSendsSixMessages_thenCorrectMessage() {
|
||||
assertEquals("Bob wysłał ci 6 wiadomości.", ICUFormat.getLabel(Locale.forLanguageTag("pl-PL"), new Object[] { "Bob", "male", 6 }));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.multiline;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class MultiLineStringUnitTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void whenCompareMultiLineStrings_thenTheyAreAllTheSame() throws IOException {
|
||||
MultiLineString ms = new MultiLineString();
|
||||
assertEquals(ms.stringConcatenation(), ms.stringJoin());
|
||||
assertEquals(ms.stringJoin(), ms.stringBuilder());
|
||||
assertEquals(ms.stringBuilder(), ms.guavaJoiner());
|
||||
assertEquals(ms.guavaJoiner(), ms.loadFromFile());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.baeldung.randomstrings;
|
||||
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Random;
|
||||
|
||||
public class RandomStringsUnitTest {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(RandomStringsUnitTest.class);
|
||||
|
||||
@Test
|
||||
public void givenUsingPlainJava_whenGeneratingRandomStringUnbounded_thenCorrect() {
|
||||
final byte[] array = new byte[7]; // length is bounded by 7
|
||||
new Random().nextBytes(array);
|
||||
final String generatedString = new String(array, Charset.forName("UTF-8"));
|
||||
|
||||
LOG.debug(generatedString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingPlainJava_whenGeneratingRandomStringBounded_thenCorrect() {
|
||||
final int leftLimit = 97; // letter 'a'
|
||||
final int rightLimit = 122; // letter 'z'
|
||||
final int targetStringLength = 10;
|
||||
final Random random = new Random();
|
||||
final StringBuilder buffer = new StringBuilder(targetStringLength);
|
||||
|
||||
for (int i = 0; i < targetStringLength; i++) {
|
||||
final int randomLimitedInt = leftLimit + (int) (random.nextFloat() * (rightLimit - leftLimit + 1));
|
||||
buffer.append((char) randomLimitedInt);
|
||||
}
|
||||
final String generatedString = buffer.toString();
|
||||
|
||||
LOG.debug(generatedString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingApache_whenGeneratingRandomString_thenCorrect() {
|
||||
final String generatedString = RandomStringUtils.random(10);
|
||||
|
||||
LOG.debug(generatedString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingApache_whenGeneratingRandomAlphabeticString_thenCorrect() {
|
||||
final String generatedString = RandomStringUtils.randomAlphabetic(10);
|
||||
|
||||
LOG.debug(generatedString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingApache_whenGeneratingRandomAlphanumericString_thenCorrect() {
|
||||
final String generatedString = RandomStringUtils.randomAlphanumeric(10);
|
||||
|
||||
LOG.debug(generatedString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingApache_whenGeneratingRandomStringBounded_thenCorrect() {
|
||||
final int length = 10;
|
||||
final boolean useLetters = true;
|
||||
final boolean useNumbers = false;
|
||||
final String generatedString = RandomStringUtils.random(length, useLetters, useNumbers);
|
||||
|
||||
LOG.debug(generatedString);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.stringnotempty;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.not;
|
||||
import static org.hamcrest.text.IsEmptyString.isEmptyOrNullString;
|
||||
import static org.hamcrest.text.IsEmptyString.isEmptyString;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class StringNotEmptyUnitTest {
|
||||
|
||||
private String text = "baeldung";
|
||||
|
||||
@Test
|
||||
public void givenAString_whenCheckedForEmptyUsingJunit_shouldAssertSuccessfully() {
|
||||
assertTrue(!text.isEmpty());
|
||||
assertFalse(text.isEmpty());
|
||||
assertNotEquals("", text);
|
||||
assertNotSame("", text);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAString_whenCheckedForEmptyUsingHamcrest_shouldAssertSuccessfully() {
|
||||
assertThat(text, not(isEmptyString()));
|
||||
assertThat(text, not(isEmptyOrNullString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAString_whenCheckedForEmptyUsingCommonsLang_shouldAssertSuccessfully() {
|
||||
assertTrue(StringUtils.isNotBlank(text));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAString_whenCheckedForEmptyUsingAssertJ_shouldAssertSuccessfully() {
|
||||
Assertions.assertThat(text).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAString_whenCheckedForEmptyUsingGuava_shouldAssertSuccessfully() {
|
||||
assertFalse(Strings.isNullOrEmpty(text));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.stringpool;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class StringPoolUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenCreatingConstantStrings_thenTheirAddressesAreEqual() {
|
||||
String constantString1 = "Baeldung";
|
||||
String constantString2 = "Baeldung";
|
||||
|
||||
assertThat(constantString1).isSameAs(constantString2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreatingStringsWithTheNewOperator_thenTheirAddressesAreDifferent() {
|
||||
String newString1 = new String("Baeldung");
|
||||
String newString2 = new String("Baeldung");
|
||||
|
||||
assertThat(newString1).isNotSameAs(newString2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenComparingConstantAndNewStrings_thenTheirAddressesAreDifferent() {
|
||||
String constantString = "Baeldung";
|
||||
String newString = new String("Baeldung");
|
||||
|
||||
assertThat(constantString).isNotSameAs(newString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInterningAStringWithIdenticalValueToAnother_thenTheirAddressesAreEqual() {
|
||||
String constantString = "interned Baeldung";
|
||||
String newString = new String("interned Baeldung");
|
||||
|
||||
assertThat(constantString).isNotSameAs(newString);
|
||||
|
||||
String internedString = newString.intern();
|
||||
|
||||
assertThat(constantString).isSameAs(internedString);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user