BAEL-7105 Add code for StringBuilder comparison

This commit is contained in:
Sam Gardner 2023-11-03 12:43:44 +00:00
parent 6e25eb35cc
commit 3d561e24df
3 changed files with 70 additions and 0 deletions

View File

@ -33,6 +33,16 @@
<build>
<finalName>core-java-string-apis-2</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>

View File

@ -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;
}
}

View File

@ -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));
}
}