Skip to content

Commit d0a49bd

Browse files
SCANGRADLE-249 Release version 6.3.0.5676 fails with ClassCastException: String cannot be cast to class Collection
1 parent 06cdc47 commit d0a49bd

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed

src/main/java/org/sonarqube/gradle/SonarUtils.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,17 @@ static List<File> exists(Collection<File> files) {
184184
}
185185

186186
static void appendProps(Map<String, Object> properties, String key, Iterable<?> valuesToAppend) {
187-
properties.putIfAbsent(key, new LinkedHashSet<String>());
188-
StreamSupport.stream(valuesToAppend.spliterator(), false)
189-
.forEach(((Collection<Object>) properties.get(key))::add);
187+
Set<Object> newList = new LinkedHashSet<>();
188+
Object previousValue = properties.get(key);
189+
if (previousValue instanceof Collection) {
190+
newList.addAll((Collection<Object>) previousValue);
191+
} else if (previousValue != null) {
192+
newList.add(previousValue);
193+
}
194+
for (Object value : valuesToAppend) {
195+
newList.add(value);
196+
}
197+
properties.put(key, newList);
190198
}
191199

192200
static void appendSourcesProp(Map<String, Object> properties, Iterable<File> filesToAppend, boolean testSources) {

src/test/groovy/org/sonarqube/gradle/SonarUtilsTest.java renamed to src/test/groovy/org/sonarqube/gradle/SonarUtilsGroovyTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
import static org.sonarqube.gradle.SonarUtils.findProjectFileType;
3939
import static org.sonarqube.gradle.SonarUtils.isCompatibleWithJavaPluginExtension;
4040

41-
class SonarUtilsTest {
41+
class SonarUtilsGroovyTest {
4242

4343
@Test
4444
void get_project_base_dir() {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* SonarQube Scanner for Gradle
3+
* Copyright (C) 2015-2025 SonarSource
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public
17+
* License along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
19+
*/
20+
package org.sonarqube.gradle;
21+
22+
import java.io.File;
23+
import java.util.Collection;
24+
import java.util.HashMap;
25+
import java.util.List;
26+
import java.util.Map;
27+
import org.junit.jupiter.api.Test;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
31+
class SonarUtilsTest {
32+
33+
@Test
34+
void append_props_to_without_previous_value() {
35+
Map<String, Object> properties = new HashMap<>();
36+
SonarUtils.appendProps(properties, "my-key", List.of("a", "b", "c", "d"));
37+
assertThat((Collection<Object>) properties.get("my-key")).containsExactly("a", "b", "c", "d");
38+
}
39+
40+
@Test
41+
void append_props_to_a_previous_file_value() {
42+
Map<String, Object> properties = new HashMap<>();
43+
properties.put("my-key", new File("a"));
44+
SonarUtils.appendProps(properties, "my-key", List.of("b", "c", "d"));
45+
assertThat((Collection<Object>) properties.get("my-key")).containsExactly(new File("a"), "b", "c", "d");
46+
}
47+
48+
@Test
49+
void append_props_to_a_previous_collection_value() {
50+
Map<String, Object> properties = new HashMap<>();
51+
properties.put("my-key", List.of("a", "b"));
52+
SonarUtils.appendProps(properties, "my-key", List.of("c", "d"));
53+
assertThat((Collection<Object>) properties.get("my-key")).containsExactly("a", "b", "c", "d");
54+
}
55+
56+
}

0 commit comments

Comments
 (0)