Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -169,14 +172,23 @@ public abstract class BaseConfigurator<T> implements Configurator<T> {
rawAttribute.setter((targetInstance, value) -> {
Object finalValue = value;

if (value instanceof Collection<?> collection && finalType.rawType.isArray()) {
Object array = newInstance(rawAttribute.getType(), collection.size());
int i = 0;
for (Object item : collection) {
set(array, i++, item);
if (value instanceof Collection<?> collection) {
if (finalType.rawType.isArray()) {
Object array = newInstance(rawAttribute.getType(), collection.size());
int i = 0;
for (Object item : collection) {
set(array, i++, item);
}
finalValue = array;

} else if (SortedSet.class.isAssignableFrom(finalType.rawType)) {
finalValue = new TreeSet<>(collection);

} else if (Set.class.isAssignableFrom(finalType.rawType)) {
finalValue = new LinkedHashSet<>(collection);
}
finalValue = array;
}

bestMethod.invoke(targetInstance, finalValue);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
import io.jenkins.plugins.casc.model.Mapping;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.stream.Collectors;
import org.junit.Test;
import org.kohsuke.accmod.Restricted;
Expand Down Expand Up @@ -58,6 +61,8 @@ public static class Z_Class {}
public static class DummyTarget {

private String[] items;
private Set<String> stringSet;
private SortedSet<String> sortedStringSet;

public String getStandard() {
return null;
Expand Down Expand Up @@ -109,6 +114,22 @@ public void setItems(String[] items) {
this.items = items;
}

public Set<String> getStringSet() {
return stringSet;
}

public void setStringSet(Set<String> stringSet) {
this.stringSet = stringSet;
}

public SortedSet<String> getSortedStringSet() {
return sortedStringSet;
}

public void setSortedStringSet(SortedSet<String> sortedStringSet) {
this.sortedStringSet = sortedStringSet;
}

public List<String> getArrayFallback() {
return null;
}
Expand Down Expand Up @@ -269,7 +290,7 @@ public void testDescribeResolvesBestSetters() {
Map<String, Class<?>> resolvedAttributes =
attributes.stream().collect(Collectors.toMap(Attribute::getName, attr -> (Class<?>) attr.getType()));

assertEquals("Should discover exactly 22 configurable properties", 22, resolvedAttributes.size());
assertEquals("Should discover exactly 24 configurable properties", 24, resolvedAttributes.size());

assertEquals("Standard setter should resolve to String", String.class, resolvedAttributes.get("standard"));

Expand Down Expand Up @@ -363,6 +384,8 @@ public void testDescribeResolvesBestSetters() {
"ambiguous",
"mismatchedToken",
"items",
"stringSet",
"sortedStringSet",
"arrayFallback",
"shapeSubtype",
"concreteWins",
Expand Down Expand Up @@ -405,6 +428,40 @@ public void testCollectionToArrayConversion() throws Exception {
assertArrayEquals(new String[] {"foo", "bar", "baz"}, result);
}

@Test
@SuppressWarnings({"unchecked", "rawtypes"})
public void testCollectionToSetConversions() throws Exception {
DummyConfigurator configurator = new DummyConfigurator();
Set<Attribute<DummyTarget, ?>> attributes = configurator.describe();

Attribute<DummyTarget, ?> setAttr = attributes.stream()
.filter(a -> a.getName().equals("stringSet"))
.findFirst()
.orElseThrow(() -> new AssertionError("stringSet attribute not found"));

Attribute<DummyTarget, ?> sortedSetAttr = attributes.stream()
.filter(a -> a.getName().equals("sortedStringSet"))
.findFirst()
.orElseThrow(() -> new AssertionError("sortedStringSet attribute not found"));

DummyTarget target = new DummyTarget();

List<String> inputCollection = Arrays.asList("foo", "bar", "baz");

((Attribute) setAttr).setValue(target, inputCollection);
((Attribute) sortedSetAttr).setValue(target, inputCollection);

Set<String> setResult = target.getStringSet();
assertNotNull("Set should have been populated", setResult);
assertEquals("Set should be converted to LinkedHashSet", LinkedHashSet.class, setResult.getClass());
assertEquals("Set should have 3 items", 3, setResult.size());

SortedSet<String> sortedSetResult = target.getSortedStringSet();
assertNotNull("SortedSet should have been populated", sortedSetResult);
assertEquals("SortedSet should be converted to TreeSet", TreeSet.class, sortedSetResult.getClass());
assertEquals("SortedSet should have 3 items", 3, sortedSetResult.size());
}

@Test
public void testFindGetterReturnsNullForMissingOrInvalidGetters() {
NoGetterConfigurator configurator = new NoGetterConfigurator();
Expand Down
Loading