diff --git a/core-java-modules/core-java-string-apis-2/pom.xml b/core-java-modules/core-java-string-apis-2/pom.xml index 814d301532..0289a18e85 100644 --- a/core-java-modules/core-java-string-apis-2/pom.xml +++ b/core-java-modules/core-java-string-apis-2/pom.xml @@ -33,6 +33,16 @@ core-java-string-apis-2 + + + org.apache.maven.plugins + maven-compiler-plugin + + 11 + 11 + + + src/main/resources diff --git a/core-java-modules/core-java-string-apis-2/src/main/java/stringbuildercomparison/StringBuilderCompare.java b/core-java-modules/core-java-string-apis-2/src/main/java/stringbuildercomparison/StringBuilderCompare.java new file mode 100644 index 0000000000..2d2dc04b83 --- /dev/null +++ b/core-java-modules/core-java-string-apis-2/src/main/java/stringbuildercomparison/StringBuilderCompare.java @@ -0,0 +1,17 @@ +package stringbuildercomparison; + +public class StringBuilderCompare { + + public static boolean compare(StringBuilder one, StringBuilder two){ + if(one.length() != two.length()){ + return false; + } + for(int i = 0; i < one.length(); i++){ + if(one.charAt(i) != two.charAt(i)){ + return false; + } + } + return true; + } + +} diff --git a/core-java-modules/core-java-string-apis-2/src/test/java/com/baeldung/stringbuildercomparison/StringBuilderComparisonUnitTest.java b/core-java-modules/core-java-string-apis-2/src/test/java/com/baeldung/stringbuildercomparison/StringBuilderComparisonUnitTest.java new file mode 100644 index 0000000000..b1a5a83770 --- /dev/null +++ b/core-java-modules/core-java-string-apis-2/src/test/java/com/baeldung/stringbuildercomparison/StringBuilderComparisonUnitTest.java @@ -0,0 +1,43 @@ +package com.baeldung.stringbuildercomparison; + +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertNotSame; + +import org.junit.Test; + +import stringbuildercomparison.StringBuilderCompare; + +public class StringBuilderComparisonUnitTest { + + @Test + public void whenUsingJavaEight_givenTwoIdenticalStringBuilders_thenCorrectlyMatch(){ + StringBuilder one = new StringBuilder("Hello"); + StringBuilder two = new StringBuilder("Hello"); + boolean result = StringBuilderCompare.compare(one, two); + assertEquals(true, result); + } + + @Test + public void whenUsingJavaEight_givenTwoDifferentStringBuilders_thenCorrectlyIdentifyDifference(){ + StringBuilder one = new StringBuilder("Hello"); + StringBuilder two = new StringBuilder("World"); + boolean result = StringBuilderCompare.compare(one, two); + assertEquals(false, result); + } + + @Test + public void whenUsingJavaEleven_givenTwoIdenticalStringBuilders_thenCorrectlyMatch(){ + StringBuilder one = new StringBuilder("Hello"); + StringBuilder two = new StringBuilder("Hello"); + assertEquals(0, one.compareTo(two)); + } + + @Test + public void whenUsingJavaEleven_givenTwoDifferentStringBuilders_thenCorrectlyIdentifyDifference(){ + StringBuilder one = new StringBuilder("Hello"); + StringBuilder two = new StringBuilder("World"); + assertNotSame(0, one.compareTo(two)); + } + + +}