Skip to content

Commit 849d446

Browse files
Refactor merging list of arguments to MethodMatchers
1 parent 7abf62e commit 849d446

3 files changed

Lines changed: 70 additions & 9 deletions

File tree

java-checks/src/main/java/org/sonar/java/checks/helpers/StringUtils.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
package org.sonar.java.checks.helpers;
1818

1919
import javax.annotation.Nullable;
20+
import java.util.ArrayList;
21+
import java.util.Arrays;
22+
import java.util.Collection;
23+
import java.util.Collections;
24+
import java.util.List;
2025

2126
public class StringUtils {
2227
private StringUtils() {}
@@ -41,4 +46,30 @@ public static int countMatches(@Nullable String string, @Nullable String pattern
4146

4247
return count;
4348
}
49+
50+
/**
51+
* Build String[] by concatenating Strings, arrays of Strings, and Collections of Strings.
52+
* Nested collections and arrays are not supported, and will throw an IllegalArgumentException if encountered.
53+
*/
54+
public static String[] stringArgs(Object ... args) {
55+
List<String> result = new ArrayList<>();
56+
for (Object arg : args) {
57+
if (arg instanceof String s) {
58+
result.add(s);
59+
} else if (arg instanceof String[] arr) {
60+
Collections.addAll(result, arr);
61+
} else if (arg instanceof Collection<?> col) {
62+
for (Object o : col) {
63+
if (o instanceof String s) {
64+
result.add(s);
65+
} else {
66+
throw new IllegalArgumentException("Unsupported collection element type: " + o.getClass());
67+
}
68+
}
69+
} else {
70+
throw new IllegalArgumentException("Unsupported argument type: " + arg.getClass());
71+
}
72+
}
73+
return result.toArray(new String[0]);
74+
}
4475
}

java-checks/src/main/java/org/sonar/java/checks/helpers/UnitTestUtils.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.Set;
2424
import java.util.function.Predicate;
2525
import java.util.regex.Pattern;
26-
import java.util.stream.Stream;
2726
import javax.annotation.Nullable;
2827

2928
import org.sonar.java.annotations.VisibleForTesting;
@@ -42,6 +41,7 @@
4241
import org.sonar.plugins.java.api.tree.Tree;
4342

4443
import static java.util.Arrays.asList;
44+
import static org.sonar.java.checks.helpers.StringUtils.stringArgs;
4545

4646
public final class UnitTestUtils {
4747
private static final List<String> ORG_ASSERTJ_CORE_API_ASSERTIONS = List.of(
@@ -103,8 +103,7 @@ public final class UnitTestUtils {
103103

104104
public static final MethodMatchers FAIL_METHOD_MATCHER = MethodMatchers.or(
105105
MethodMatchers.create().ofTypes(
106-
Stream.concat(
107-
Stream.of(
106+
stringArgs(
108107
// JUnit 5
109108
"org.junit.jupiter.api.Assertions",
110109
// JUnit 4
@@ -114,14 +113,13 @@ public final class UnitTestUtils {
114113
// Fest assert
115114
"org.fest.assertions.Fail",
116115
// AssertJ
117-
"org.assertj.core.api.Fail"
118-
),
119-
ORG_ASSERTJ_CORE_API_ASSERTIONS.stream()
120-
).toArray(String[]::new))
116+
"org.assertj.core.api.Fail",
117+
ORG_ASSERTJ_CORE_API_ASSERTIONS
118+
))
121119
.names("fail").withAnyParameters().build(),
122120
MethodMatchers.create().ofTypes(
123121
// AssertJ
124-
ORG_ASSERTJ_CORE_API_ASSERTIONS.toArray(String[]::new)
122+
stringArgs(ORG_ASSERTJ_CORE_API_ASSERTIONS)
125123
)
126124
.names("failBecauseExceptionWasNotThrown").withAnyParameters().build());
127125

@@ -134,7 +132,7 @@ public final class UnitTestUtils {
134132
.build(),
135133
// Fest assert and AssertJ
136134
MethodMatchers.create()
137-
.ofTypes(Stream.concat(ORG_ASSERTJ_CORE_API_ASSERTIONS.stream(), Stream.of("org.fest.assertions.Assertions")).toArray(String[]::new))
135+
.ofTypes(stringArgs(ORG_ASSERTJ_CORE_API_ASSERTIONS, "org.fest.assertions.Assertions"))
138136
.names("assertThat")
139137
.withAnyParameters()
140138
.build());

java-checks/src/test/java/org/sonar/java/checks/helpers/StringUtilsTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818

1919
import org.junit.jupiter.api.Test;
2020

21+
import java.util.List;
22+
2123
import static org.assertj.core.api.Assertions.assertThat;
24+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
25+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2226

2327
class StringUtilsTest {
2428
@Test
@@ -42,4 +46,32 @@ void testCountMatches() {
4246
assertThat(StringUtils.countMatches("abaTaba", "aba")).isEqualTo(2);
4347
assertThat(StringUtils.countMatches("abababa", "aba")).isEqualTo(2);
4448
}
49+
50+
@Test
51+
void testStringArgs() {
52+
assertThat(StringUtils.stringArgs()).isEmpty();
53+
assertThat(StringUtils.stringArgs("a", "b", "c")).containsExactly("a", "b", "c");
54+
55+
assertThat(StringUtils.stringArgs(new String[] {"a", "b"}, "c")).containsExactly("a", "b", "c");
56+
assertThat(StringUtils.stringArgs("a", new String[] {"b", "c"})).containsExactly("a", "b", "c");
57+
58+
assertThat(StringUtils.stringArgs(List.of("A", "B"), "a", new String[] {"b", "c"}))
59+
.containsExactly("A", "B", "a", "b", "c");
60+
}
61+
62+
@Test
63+
void testStringArgs_exceptions() {
64+
assertThatIllegalArgumentException()
65+
.isThrownBy(() -> StringUtils.stringArgs("a", 2))
66+
.withMessageContaining("Unsupported argument type:");
67+
68+
assertThatIllegalArgumentException()
69+
.isThrownBy(() -> StringUtils.stringArgs(new int[]{4, 5}))
70+
.withMessageContaining("Unsupported argument type:");
71+
72+
var list = List.of("b", List.of("c", "d"));
73+
assertThatIllegalArgumentException()
74+
.isThrownBy(() -> StringUtils.stringArgs("a", list))
75+
.withMessageContaining("Unsupported collection element type:");
76+
}
4577
}

0 commit comments

Comments
 (0)