BAEL-7105 Add code for StringBuilder comparison
This commit is contained in:
parent
6e25eb35cc
commit
3d561e24df
|
@ -33,6 +33,16 @@
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<finalName>core-java-string-apis-2</finalName>
|
<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>
|
<resources>
|
||||||
<resource>
|
<resource>
|
||||||
<directory>src/main/resources</directory>
|
<directory>src/main/resources</directory>
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue