From cf49235b4ee49b75f9e0bfae503eb0fccb9cdb51 Mon Sep 17 00:00:00 2001 From: Mainak Majumder Date: Mon, 3 Jan 2022 02:59:00 +0100 Subject: [PATCH] Equals() Vs Contentequals() Method in Java (#11598) * Application source code for the Baeldung article "HTTP PUT vs POST method in REST API" * update indention in pom file, update code in Address class * update indention * rename application * update pom * source code for article "Connection timeout vs read timeout" * Source code for Baeldung article BAEL-4896 * update code * code for article "String Equals() Vs Contentequals() Method in Java" * update code with tests * code update * update test methods * remove upper case and wrong sequence test * remove wrong sequence and upper case string variables --- .../StringEqualsVsContentEqualsUnitTest.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 core-java-modules/core-java-string-operations-4/src/test/java/com/baeldung/equalsvscontentequals/StringEqualsVsContentEqualsUnitTest.java diff --git a/core-java-modules/core-java-string-operations-4/src/test/java/com/baeldung/equalsvscontentequals/StringEqualsVsContentEqualsUnitTest.java b/core-java-modules/core-java-string-operations-4/src/test/java/com/baeldung/equalsvscontentequals/StringEqualsVsContentEqualsUnitTest.java new file mode 100644 index 0000000000..1b8c97d024 --- /dev/null +++ b/core-java-modules/core-java-string-operations-4/src/test/java/com/baeldung/equalsvscontentequals/StringEqualsVsContentEqualsUnitTest.java @@ -0,0 +1,30 @@ +package com.baeldung.equalsvscontentequals; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class StringEqualsVsContentEqualsUnitTest { + + String actualString = "baeldung"; + String identicalString = "baeldung"; + CharSequence identicalStringInstance = "baeldung"; + CharSequence identicalStringBufferInstance = new StringBuffer("baeldung"); + + @Test + public void whenIdenticalTestString_thenBothTrue() { + assertTrue(actualString.equals(identicalString)); + assertTrue(actualString.contentEquals(identicalString)); + } + + @Test + public void whenSameContentButDifferentType_thenEqualsIsFalseAndContentEqualsIsTrue() { + assertTrue(actualString.equals(identicalStringInstance)); + assertTrue(actualString.contentEquals(identicalStringInstance)); + + assertFalse(actualString.equals(identicalStringBufferInstance)); + assertTrue(actualString.contentEquals(identicalStringBufferInstance)); + } + +}