|
11 | 11 | import io.jenkins.plugins.casc.model.Mapping; |
12 | 12 | import java.lang.reflect.Method; |
13 | 13 | import java.util.Arrays; |
| 14 | +import java.util.LinkedHashSet; |
14 | 15 | import java.util.List; |
15 | 16 | import java.util.Map; |
16 | 17 | import java.util.Set; |
| 18 | +import java.util.SortedSet; |
| 19 | +import java.util.TreeSet; |
17 | 20 | import java.util.stream.Collectors; |
18 | 21 | import org.junit.Test; |
19 | 22 | import org.kohsuke.accmod.Restricted; |
@@ -58,6 +61,8 @@ public static class Z_Class {} |
58 | 61 | public static class DummyTarget { |
59 | 62 |
|
60 | 63 | private String[] items; |
| 64 | + private Set<String> stringSet; |
| 65 | + private SortedSet<String> sortedStringSet; |
61 | 66 |
|
62 | 67 | public String getStandard() { |
63 | 68 | return null; |
@@ -109,6 +114,22 @@ public void setItems(String[] items) { |
109 | 114 | this.items = items; |
110 | 115 | } |
111 | 116 |
|
| 117 | + public Set<String> getStringSet() { |
| 118 | + return stringSet; |
| 119 | + } |
| 120 | + |
| 121 | + public void setStringSet(Set<String> stringSet) { |
| 122 | + this.stringSet = stringSet; |
| 123 | + } |
| 124 | + |
| 125 | + public SortedSet<String> getSortedStringSet() { |
| 126 | + return sortedStringSet; |
| 127 | + } |
| 128 | + |
| 129 | + public void setSortedStringSet(SortedSet<String> sortedStringSet) { |
| 130 | + this.sortedStringSet = sortedStringSet; |
| 131 | + } |
| 132 | + |
112 | 133 | public List<String> getArrayFallback() { |
113 | 134 | return null; |
114 | 135 | } |
@@ -269,7 +290,7 @@ public void testDescribeResolvesBestSetters() { |
269 | 290 | Map<String, Class<?>> resolvedAttributes = |
270 | 291 | attributes.stream().collect(Collectors.toMap(Attribute::getName, attr -> (Class<?>) attr.getType())); |
271 | 292 |
|
272 | | - assertEquals("Should discover exactly 22 configurable properties", 22, resolvedAttributes.size()); |
| 293 | + assertEquals("Should discover exactly 24 configurable properties", 24, resolvedAttributes.size()); |
273 | 294 |
|
274 | 295 | assertEquals("Standard setter should resolve to String", String.class, resolvedAttributes.get("standard")); |
275 | 296 |
|
@@ -363,6 +384,8 @@ public void testDescribeResolvesBestSetters() { |
363 | 384 | "ambiguous", |
364 | 385 | "mismatchedToken", |
365 | 386 | "items", |
| 387 | + "stringSet", |
| 388 | + "sortedStringSet", |
366 | 389 | "arrayFallback", |
367 | 390 | "shapeSubtype", |
368 | 391 | "concreteWins", |
@@ -405,6 +428,40 @@ public void testCollectionToArrayConversion() throws Exception { |
405 | 428 | assertArrayEquals(new String[] {"foo", "bar", "baz"}, result); |
406 | 429 | } |
407 | 430 |
|
| 431 | + @Test |
| 432 | + @SuppressWarnings({"unchecked", "rawtypes"}) |
| 433 | + public void testCollectionToSetConversions() throws Exception { |
| 434 | + DummyConfigurator configurator = new DummyConfigurator(); |
| 435 | + Set<Attribute<DummyTarget, ?>> attributes = configurator.describe(); |
| 436 | + |
| 437 | + Attribute<DummyTarget, ?> setAttr = attributes.stream() |
| 438 | + .filter(a -> a.getName().equals("stringSet")) |
| 439 | + .findFirst() |
| 440 | + .orElseThrow(() -> new AssertionError("stringSet attribute not found")); |
| 441 | + |
| 442 | + Attribute<DummyTarget, ?> sortedSetAttr = attributes.stream() |
| 443 | + .filter(a -> a.getName().equals("sortedStringSet")) |
| 444 | + .findFirst() |
| 445 | + .orElseThrow(() -> new AssertionError("sortedStringSet attribute not found")); |
| 446 | + |
| 447 | + DummyTarget target = new DummyTarget(); |
| 448 | + |
| 449 | + List<String> inputCollection = Arrays.asList("foo", "bar", "baz"); |
| 450 | + |
| 451 | + ((Attribute) setAttr).setValue(target, inputCollection); |
| 452 | + ((Attribute) sortedSetAttr).setValue(target, inputCollection); |
| 453 | + |
| 454 | + Set<String> setResult = target.getStringSet(); |
| 455 | + assertNotNull("Set should have been populated", setResult); |
| 456 | + assertEquals("Set should be converted to LinkedHashSet", LinkedHashSet.class, setResult.getClass()); |
| 457 | + assertEquals("Set should have 3 items", 3, setResult.size()); |
| 458 | + |
| 459 | + SortedSet<String> sortedSetResult = target.getSortedStringSet(); |
| 460 | + assertNotNull("SortedSet should have been populated", sortedSetResult); |
| 461 | + assertEquals("SortedSet should be converted to TreeSet", TreeSet.class, sortedSetResult.getClass()); |
| 462 | + assertEquals("SortedSet should have 3 items", 3, sortedSetResult.size()); |
| 463 | + } |
| 464 | + |
408 | 465 | @Test |
409 | 466 | public void testFindGetterReturnsNullForMissingOrInvalidGetters() { |
410 | 467 | NoGetterConfigurator configurator = new NoGetterConfigurator(); |
|
0 commit comments