[JAVA-18123] Upgraded algorithms-modules to java 11 + Upgraded unit t… (#13437)

* [JAVA-18123] Upgraded algorithms-modules to java 11 + Upgraded unit tests to junit5

* [JAVA-18123] Clean up properties

---------

Co-authored-by: panagiotiskakos <panagiotis.kakos@libra-is.com>
This commit is contained in:
panos-kakos
2023-02-07 14:42:21 +00:00
committed by GitHub
parent 2c0c3b269e
commit d47969ce2d
82 changed files with 740 additions and 721 deletions

View File

@@ -34,6 +34,23 @@
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<!-- API, java.xml.bind module -->
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>${xml-bind-api.version}</version>
</dependency>
<!-- Runtime, com.sun.xml.bind module -->
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>${jaxb-runtime.version}</version>
</dependency>
</dependencies>
<properties>
<xml-bind-api.version>4.0.0</xml-bind-api.version>
<jaxb-runtime.version>4.0.0</jaxb-runtime.version>
</properties>
</project>

View File

@@ -2,13 +2,15 @@ package com.baeldung.algorithms.conversion;
import java.math.BigInteger;
import javax.xml.bind.DatatypeConverter;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import com.google.common.io.BaseEncoding;
import jakarta.xml.bind.DatatypeConverter;
public class HexStringConverter {
/**
@@ -90,7 +92,7 @@ public class HexStringConverter {
return DatatypeConverter.parseHexBinary(hexString);
}
public String encodeUsingApacheCommons(byte[] bytes) throws DecoderException {
public String encodeUsingApacheCommons(byte[] bytes) {
return Hex.encodeHexString(bytes);
}

View File

@@ -1,22 +1,23 @@
package com.baeldung.algorithms.balancedbinarytree;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class BalancedBinaryTreeUnitTest extends BinaryTreeDataProvider {
import org.junit.jupiter.api.Test;
class BalancedBinaryTreeUnitTest extends BinaryTreeDataProvider {
@Test
public void givenBalancedTrees_whenCallingIsBalanced_ShouldReturnTrue() {
void givenBalancedTrees_whenCallingIsBalanced_ShouldReturnTrue() {
for (Tree tree : balancedTrees()) {
assertTrue(toString(tree) + " should be balanced", BalancedBinaryTree.isBalanced(tree));
assertTrue(BalancedBinaryTree.isBalanced(tree), toString(tree) + " should be balanced");
}
}
@Test
public void givenUnbalancedTrees_whenCallingIsBalanced_ShouldReturnFalse() {
void givenUnbalancedTrees_whenCallingIsBalanced_ShouldReturnFalse() {
for (Tree tree : unbalancedTrees()) {
assertFalse(toString(tree) + " should not be balanced", BalancedBinaryTree.isBalanced(tree));
assertFalse(BalancedBinaryTree.isBalanced(tree), toString(tree) + " should not be balanced");
}
}

View File

@@ -5,27 +5,31 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class BinaryGapUnitTest {
class BinaryGapUnitTest {
@Test public void givenNoOccurrenceOfBoundedZeros_whenCalculateBinaryGap_thenOutputCorrectResult() {
@Test
void givenNoOccurrenceOfBoundedZeros_whenCalculateBinaryGap_thenOutputCorrectResult() {
int result = calculateBinaryGap(63);
assertEquals(0, result);
}
@Test public void givenTrailingZeros_whenCalculateBinaryGap_thenOutputCorrectResult() {
@Test
void givenTrailingZeros_whenCalculateBinaryGap_thenOutputCorrectResult() {
int result = calculateBinaryGap(40);
assertEquals(1, result);
}
@Test public void givenSingleOccurrenceOfBoundedZeros_whenCalculateBinaryGap_thenOutputCorrectResult() {
@Test
void givenSingleOccurrenceOfBoundedZeros_whenCalculateBinaryGap_thenOutputCorrectResult() {
int result = calculateBinaryGap(9);
assertEquals(2, result);
}
@Test public void givenMultipleOccurrenceOfBoundedZeros_whenCalculateBinaryGap_thenOutputCorrectResult() {
@Test
void givenMultipleOccurrenceOfBoundedZeros_whenCalculateBinaryGap_thenOutputCorrectResult() {
int result = calculateBinaryGap(145);
assertEquals(3, result);

View File

@@ -1,17 +1,18 @@
package com.baeldung.algorithms.combinatorics;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import org.junit.jupiter.api.Test;
public class CombinatoricsUnitTest {
class CombinatoricsUnitTest {
@Test
public void givenEmptySequence_whenCallingPermutations_ShouldReturnEmptyList() {
void givenEmptySequence_whenCallingPermutations_ShouldReturnEmptyList() {
List<Integer> sequence = Arrays.asList();
List<List<Integer>> permutations = Combinatorics.permutations(sequence);
@@ -20,7 +21,7 @@ public class CombinatoricsUnitTest {
}
@Test
public void givenOneElementSequence_whenCallingPermutations_ShouldReturnPermutations() {
void givenOneElementSequence_whenCallingPermutations_ShouldReturnPermutations() {
List<Integer> sequence = Arrays.asList(1);
List<List<Integer>> permutations = Combinatorics.permutations(sequence);
@@ -31,7 +32,7 @@ public class CombinatoricsUnitTest {
}
@Test
public void givenFourElementsSequence_whenCallingPermutations_ShouldReturnPermutations() {
void givenFourElementsSequence_whenCallingPermutations_ShouldReturnPermutations() {
List<Integer> sequence = Arrays.asList(1, 2, 3, 4);
List<List<Integer>> permutations = Combinatorics.permutations(sequence);
@@ -41,7 +42,7 @@ public class CombinatoricsUnitTest {
}
@Test
public void givenTwoElements_whenCalling3Combinations_ShouldReturnEmptyList() {
void givenTwoElements_whenCalling3Combinations_ShouldReturnEmptyList() {
List<Integer> set = Arrays.asList(1, 2);
List<List<Integer>> combinations = Combinatorics.combinations(set, 3);
@@ -50,7 +51,7 @@ public class CombinatoricsUnitTest {
}
@Test
public void givenThreeElements_whenCalling3Combinations_ShouldReturnOneCombination() {
void givenThreeElements_whenCalling3Combinations_ShouldReturnOneCombination() {
List<Integer> set = Arrays.asList(1, 2, 3);
List<List<Integer>> combinations = Combinatorics.combinations(set, 3);
@@ -60,7 +61,7 @@ public class CombinatoricsUnitTest {
}
@Test
public void givenFourElements_whenCalling2Combinations_ShouldReturnCombinations() {
void givenFourElements_whenCalling2Combinations_ShouldReturnCombinations() {
List<Integer> set = Arrays.asList(1, 2, 3, 4);
List<List<Integer>> combinations = Combinatorics.combinations(set, 2);
@@ -70,7 +71,7 @@ public class CombinatoricsUnitTest {
}
@Test
public void givenFourElements_whenCallingPowerSet_ShouldReturn15Sets() {
void givenFourElements_whenCallingPowerSet_ShouldReturn15Sets() {
List<Character> sequence = Arrays.asList('a', 'b', 'c', 'd');
List<List<Character>> combinations = Combinatorics.powerSet(sequence);

View File

@@ -1,27 +1,27 @@
package com.baeldung.algorithms.conversion;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.apache.commons.codec.DecoderException;
import org.hamcrest.text.IsEqualIgnoringCase;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.baeldung.algorithms.conversion.HexStringConverter;
public class ByteArrayConverterUnitTest {
class ByteArrayConverterUnitTest {
private HexStringConverter hexStringConverter;
@Before
@BeforeEach
public void setup() {
hexStringConverter = new HexStringConverter();
}
@Test
public void shouldEncodeByteArrayToHexStringUsingBigIntegerToString() {
void shouldEncodeByteArrayToHexStringUsingBigIntegerToString() {
byte[] bytes = getSampleBytes();
String hexString = getSampleHexString();
if(hexString.charAt(0) == '0') {
@@ -32,7 +32,7 @@ public class ByteArrayConverterUnitTest {
}
@Test
public void shouldEncodeByteArrayToHexStringUsingBigIntegerStringFormat() {
void shouldEncodeByteArrayToHexStringUsingBigIntegerStringFormat() {
byte[] bytes = getSampleBytes();
String hexString = getSampleHexString();
String output = hexStringConverter.encodeUsingBigIntegerStringFormat(bytes);
@@ -40,7 +40,7 @@ public class ByteArrayConverterUnitTest {
}
@Test
public void shouldDecodeHexStringToByteArrayUsingBigInteger() {
void shouldDecodeHexStringToByteArrayUsingBigInteger() {
byte[] bytes = getSampleBytes();
String hexString = getSampleHexString();
byte[] output = hexStringConverter.decodeUsingBigInteger(hexString);
@@ -48,7 +48,7 @@ public class ByteArrayConverterUnitTest {
}
@Test
public void shouldEncodeByteArrayToHexStringUsingCharacterConversion() {
void shouldEncodeByteArrayToHexStringUsingCharacterConversion() {
byte[] bytes = getSampleBytes();
String hexString = getSampleHexString();
String output = hexStringConverter.encodeHexString(bytes);
@@ -56,20 +56,22 @@ public class ByteArrayConverterUnitTest {
}
@Test
public void shouldDecodeHexStringToByteArrayUsingCharacterConversion() {
void shouldDecodeHexStringToByteArrayUsingCharacterConversion() {
byte[] bytes = getSampleBytes();
String hexString = getSampleHexString();
byte[] output = hexStringConverter.decodeHexString(hexString);
assertArrayEquals(bytes, output);
}
@Test(expected=IllegalArgumentException.class)
public void shouldDecodeHexToByteWithInvalidHexCharacter() {
hexStringConverter.hexToByte("fg");
@Test
void shouldDecodeHexToByteWithInvalidHexCharacter() {
assertThrows(IllegalArgumentException.class, () -> {
hexStringConverter.hexToByte("fg");
});
}
@Test
public void shouldEncodeByteArrayToHexStringDataTypeConverter() {
void shouldEncodeByteArrayToHexStringDataTypeConverter() {
byte[] bytes = getSampleBytes();
String hexString = getSampleHexString();
String output = hexStringConverter.encodeUsingDataTypeConverter(bytes);
@@ -77,7 +79,7 @@ public class ByteArrayConverterUnitTest {
}
@Test
public void shouldDecodeHexStringToByteArrayUsingDataTypeConverter() {
void shouldDecodeHexStringToByteArrayUsingDataTypeConverter() {
byte[] bytes = getSampleBytes();
String hexString = getSampleHexString();
byte[] output = hexStringConverter.decodeUsingDataTypeConverter(hexString);
@@ -85,7 +87,7 @@ public class ByteArrayConverterUnitTest {
}
@Test
public void shouldEncodeByteArrayToHexStringUsingGuava() {
void shouldEncodeByteArrayToHexStringUsingGuava() {
byte[] bytes = getSampleBytes();
String hexString = getSampleHexString();
String output = hexStringConverter.encodeUsingGuava(bytes);
@@ -93,7 +95,7 @@ public class ByteArrayConverterUnitTest {
}
@Test
public void shouldDecodeHexStringToByteArrayUsingGuava() {
void shouldDecodeHexStringToByteArrayUsingGuava() {
byte[] bytes = getSampleBytes();
String hexString = getSampleHexString();
byte[] output = hexStringConverter.decodeUsingGuava(hexString);
@@ -101,7 +103,7 @@ public class ByteArrayConverterUnitTest {
}
@Test
public void shouldEncodeByteArrayToHexStringUsingApacheCommons() throws DecoderException {
void shouldEncodeByteArrayToHexStringUsingApacheCommons() throws DecoderException {
byte[] bytes = getSampleBytes();
String hexString = getSampleHexString();
String output = hexStringConverter.encodeUsingApacheCommons(bytes);
@@ -109,7 +111,7 @@ public class ByteArrayConverterUnitTest {
}
@Test
public void shouldDecodeHexStringToByteArrayUsingApacheCommons() throws DecoderException {
void shouldDecodeHexStringToByteArrayUsingApacheCommons() throws DecoderException {
byte[] bytes = getSampleBytes();
String hexString = getSampleHexString();
byte[] output = hexStringConverter.decodeUsingApacheCommons(hexString);

View File

@@ -1,16 +1,18 @@
package com.baeldung.algorithms.integerstreammedian;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.LinkedHashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import org.junit.jupiter.api.Test;
public class MedianOfIntegerStreamUnitTest {
class MedianOfIntegerStreamUnitTest {
@Test
public void givenStreamOfIntegers_whenAnElementIsRead_thenMedianChangesWithApproach1() {
void givenStreamOfIntegers_whenAnElementIsRead_thenMedianChangesWithApproach1() {
MedianOfIntegerStream mis = new MedianOfIntegerStream();
for (Map.Entry<Integer, Double> e : testcaseFixture().entrySet()) {
mis.add(e.getKey());
@@ -19,7 +21,7 @@ public class MedianOfIntegerStreamUnitTest {
}
@Test
public void givenStreamOfIntegers_whenAnElementIsRead_thenMedianChangesWithApproach2() {
void givenStreamOfIntegers_whenAnElementIsRead_thenMedianChangesWithApproach2() {
MedianOfIntegerStream2 mis = new MedianOfIntegerStream2();
for (Map.Entry<Integer, Double> e : testcaseFixture().entrySet()) {
mis.add(e.getKey());

View File

@@ -4,10 +4,10 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class KnapsackUnitTest {
class KnapsackUnitTest {
@Test
public void givenWeightsandValues_whenCalculateMax_thenOutputCorrectResult() {
void givenWeightsandValues_whenCalculateMax_thenOutputCorrectResult() {
final int[] w = new int[] { 23, 26, 20, 18, 32, 27, 29, 26, 30, 27 };
final int[] v = new int[] { 505, 352, 458, 220, 354, 414, 498, 545, 473, 543 };
final int n = 10;
@@ -19,7 +19,7 @@ public class KnapsackUnitTest {
}
@Test
public void givenZeroItems_whenCalculateMax_thenOutputZero() {
void givenZeroItems_whenCalculateMax_thenOutputZero() {
final int[] w = new int[] {};
final int[] v = new int[] {};
final int n = 0;
@@ -31,7 +31,7 @@ public class KnapsackUnitTest {
}
@Test
public void givenZeroWeightLimit_whenCalculateMax_thenOutputZero() {
void givenZeroWeightLimit_whenCalculateMax_thenOutputZero() {
final int[] w = new int[] { 23, 26, 20, 18, 32, 27, 29, 26, 30, 27 };
final int[] v = new int[] { 505, 352, 458, 220, 354, 414, 498, 545, 473, 543 };
final int n = 10;

View File

@@ -5,10 +5,10 @@ import org.junit.jupiter.api.Test;
import com.baeldung.algorithms.mergesortedarrays.SortedArrays;
public class SortedArraysUnitTest {
class SortedArraysUnitTest {
@Test
public void givenTwoSortedArrays_whenMerged_thenReturnMergedSortedArray() {
void givenTwoSortedArrays_whenMerged_thenReturnMergedSortedArray() {
int[] foo = { 3, 7 };
int[] bar = { 4, 8, 11 };
@@ -18,7 +18,7 @@ public class SortedArraysUnitTest {
}
@Test
public void givenTwoSortedArraysWithDuplicates_whenMerged_thenReturnMergedSortedArray() {
void givenTwoSortedArraysWithDuplicates_whenMerged_thenReturnMergedSortedArray() {
int[] foo = { 3, 3, 7 };
int[] bar = { 4, 8, 8, 11 };

View File

@@ -1,14 +1,15 @@
package com.baeldung.algorithms.prim;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class PrimUnitTest {
import org.junit.jupiter.api.Test;
class PrimUnitTest {
@Test
public void givenAGraph_whenPrimRuns_thenPrintMST() {
void givenAGraph_whenPrimRuns_thenPrintMST() {
Prim prim = new Prim(createGraph());
System.out.println(prim.originalGraphToString());
System.out.println("----------------");

View File

@@ -1,49 +1,49 @@
package com.baeldung.algorithms.relativelyprime;
import org.junit.Test;
import static com.baeldung.algorithms.relativelyprime.RelativelyPrime.*;
import static org.assertj.core.api.Assertions.assertThat;
public class RelativelyPrimeUnitTest {
import org.junit.jupiter.api.Test;
class RelativelyPrimeUnitTest {
@Test
public void givenNonRelativelyPrimeNumbers_whenCheckingIteratively_shouldReturnFalse() {
void givenNonRelativelyPrimeNumbers_whenCheckingIteratively_shouldReturnFalse() {
boolean result = iterativeRelativelyPrime(45, 35);
assertThat(result).isFalse();
}
@Test
public void givenRelativelyPrimeNumbers_whenCheckingIteratively_shouldReturnTrue() {
void givenRelativelyPrimeNumbers_whenCheckingIteratively_shouldReturnTrue() {
boolean result = iterativeRelativelyPrime(500, 501);
assertThat(result).isTrue();
}
@Test
public void givenNonRelativelyPrimeNumbers_whenCheckingRecursively_shouldReturnFalse() {
void givenNonRelativelyPrimeNumbers_whenCheckingRecursively_shouldReturnFalse() {
boolean result = recursiveRelativelyPrime(45, 35);
assertThat(result).isFalse();
}
@Test
public void givenRelativelyPrimeNumbers_whenCheckingRecursively_shouldReturnTrue() {
void givenRelativelyPrimeNumbers_whenCheckingRecursively_shouldReturnTrue() {
boolean result = recursiveRelativelyPrime(500, 501);
assertThat(result).isTrue();
}
@Test
public void givenNonRelativelyPrimeNumbers_whenCheckingUsingBigIntegers_shouldReturnFalse() {
void givenNonRelativelyPrimeNumbers_whenCheckingUsingBigIntegers_shouldReturnFalse() {
boolean result = bigIntegerRelativelyPrime(45, 35);
assertThat(result).isFalse();
}
@Test
public void givenRelativelyPrimeNumbers_whenCheckingBigIntegers_shouldReturnTrue() {
void givenRelativelyPrimeNumbers_whenCheckingBigIntegers_shouldReturnTrue() {
boolean result = bigIntegerRelativelyPrime(500, 501);
assertThat(result).isTrue();

View File

@@ -4,10 +4,10 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class TreeReverserUnitTest {
class TreeReverserUnitTest {
@Test
public void givenTreeWhenReversingRecursivelyThenReversed() {
void givenTreeWhenReversingRecursivelyThenReversed() {
TreeReverser reverser = new TreeReverser();
TreeNode treeNode = createBinaryTree();
@@ -19,7 +19,7 @@ public class TreeReverserUnitTest {
}
@Test
public void givenTreeWhenReversingIterativelyThenReversed() {
void givenTreeWhenReversingIterativelyThenReversed() {
TreeReverser reverser = new TreeReverser();
TreeNode treeNode = createBinaryTree();