Skip to content

Commit 4b8099c

Browse files
authored
fix: add null input validation to AlternativeStringArrange.arrange() (#7425)
* fix: add null input validation to AlternativeStringArrange.arrange() The arrange() method previously threw a NullPointerException when either input string was null. This change explicitly validates the inputs and throws IllegalArgumentException with a clear message, matching the fail-fast pattern used by other utility classes in this package (e.g. HammingDistance). - Add null guard at the start of arrange() - Update Javadoc with @throws and contract notes - Add parameterized test covering all three null-input combinations * fix: remove unused JUnit @test import (Checkstyle violation)
1 parent 8848ed1 commit 4b8099c

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

src/main/java/com/thealgorithms/strings/AlternativeStringArrange.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,19 @@ private AlternativeStringArrange() {
2121

2222
/**
2323
* Arranges two strings by alternating their characters.
24+
* If one string is longer than the other, the remaining characters of the longer string
25+
* are appended at the end of the result.
2426
*
25-
* @param firstString the first input string
26-
* @param secondString the second input string
27+
* @param firstString the first input string, must not be {@code null}
28+
* @param secondString the second input string, must not be {@code null}
2729
* @return a new string with characters from both strings arranged alternately
30+
* @throws IllegalArgumentException if {@code firstString} or {@code secondString} is {@code null}
2831
*/
2932
public static String arrange(String firstString, String secondString) {
33+
if (firstString == null || secondString == null) {
34+
throw new IllegalArgumentException("Input strings must not be null");
35+
}
36+
3037
StringBuilder result = new StringBuilder();
3138
int length1 = firstString.length();
3239
int length2 = secondString.length();

src/test/java/com/thealgorithms/strings/AlternativeStringArrangeTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.thealgorithms.strings;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45

56
import java.util.stream.Stream;
67
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
79
import org.junit.jupiter.params.provider.MethodSource;
810

911
class AlternativeStringArrangeTest {
@@ -20,4 +22,15 @@ private static Stream<Object[]> provideTestData() {
2022
void arrangeTest(String input1, String input2, String expected) {
2123
assertEquals(expected, AlternativeStringArrange.arrange(input1, input2));
2224
}
25+
26+
@ParameterizedTest(name = "null input ({0}, {1}) should throw IllegalArgumentException")
27+
@MethodSource("provideNullInputs")
28+
void arrangeThrowsOnNullInput(String input1, String input2) {
29+
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> AlternativeStringArrange.arrange(input1, input2));
30+
assertEquals("Input strings must not be null", ex.getMessage());
31+
}
32+
33+
private static Stream<Arguments> provideNullInputs() {
34+
return Stream.of(Arguments.of(null, "abc"), Arguments.of("abc", null), Arguments.of(null, null));
35+
}
2336
}

0 commit comments

Comments
 (0)