Skip to content

Commit ec2f2e3

Browse files
committed
add support for charsequences and maps
1 parent f049488 commit ec2f2e3

3 files changed

Lines changed: 103 additions & 1 deletion

File tree

src/main/java/it/aboutbits/springboot/testing/validation/source/SizeGreaterThanValueSource.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.Map;
1414
import java.util.Random;
1515
import java.util.Set;
16+
import java.util.TreeMap;
1617
import java.util.TreeSet;
1718
import java.util.function.LongFunction;
1819
import java.util.stream.Stream;
@@ -23,13 +24,17 @@ public class SizeGreaterThanValueSource implements ValueSource {
2324

2425
static {
2526
TYPE_SOURCES.put(String.class, SizeGreaterThanValueSource::getStringStream);
27+
TYPE_SOURCES.put(CharSequence.class, SizeGreaterThanValueSource::getStringStream);
2628
TYPE_SOURCES.put(Collection.class, SizeGreaterThanValueSource::getArrayListStream);
2729
TYPE_SOURCES.put(List.class, SizeGreaterThanValueSource::getArrayListStream);
2830
TYPE_SOURCES.put(ArrayList.class, SizeGreaterThanValueSource::getArrayListStream);
2931
TYPE_SOURCES.put(LinkedList.class, SizeGreaterThanValueSource::getLinkedListStream);
3032
TYPE_SOURCES.put(Set.class, SizeGreaterThanValueSource::getHashSetStream);
3133
TYPE_SOURCES.put(HashSet.class, SizeGreaterThanValueSource::getHashSetStream);
3234
TYPE_SOURCES.put(TreeSet.class, SizeGreaterThanValueSource::getTreeSetStream);
35+
TYPE_SOURCES.put(Map.class, SizeGreaterThanValueSource::getHashMapStream);
36+
TYPE_SOURCES.put(HashMap.class, SizeGreaterThanValueSource::getHashMapStream);
37+
TYPE_SOURCES.put(TreeMap.class, SizeGreaterThanValueSource::getTreeMapStream);
3338
}
3439

3540
@SuppressWarnings("unused")
@@ -112,6 +117,26 @@ private static Stream<Collection<?>> getLinkedListStream(long value) {
112117
));
113118
}
114119

120+
@NonNull
121+
private static Stream<Map<?, ?>> getHashMapStream(long value) {
122+
return getTestSizes(value)
123+
.stream()
124+
.map(size -> generateMap(
125+
Math.toIntExact(size),
126+
new HashMap<>()
127+
));
128+
}
129+
130+
@NonNull
131+
private static Stream<Map<?, ?>> getTreeMapStream(long value) {
132+
return getTestSizes(value)
133+
.stream()
134+
.map(size -> generateMap(
135+
Math.toIntExact(size),
136+
new TreeMap<>()
137+
));
138+
}
139+
115140
private static List<Long> getTestSizes(long value) {
116141
var sizes = new ArrayList<Long>();
117142

@@ -136,6 +161,13 @@ private static Collection<Object> generateCollection(int size, Collection<Object
136161
return collection;
137162
}
138163

164+
private static Map<Object, Object> generateMap(int size, Map<Object, Object> map) {
165+
for (int i = 0; i < size; i++) {
166+
map.put(i, "dummy_" + i); // Add dummy elements, content doesn't matter
167+
}
168+
return map;
169+
}
170+
139171
private static Object generateArray(int size, Class<?> arrayClass) {
140172
var componentType = arrayClass.getComponentType();
141173
return Array.newInstance(componentType, size);

src/main/java/it/aboutbits/springboot/testing/validation/source/SizeLessThanValueSource.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.Map;
1414
import java.util.Random;
1515
import java.util.Set;
16+
import java.util.TreeMap;
1617
import java.util.TreeSet;
1718
import java.util.function.LongFunction;
1819
import java.util.stream.Stream;
@@ -23,13 +24,17 @@ public class SizeLessThanValueSource implements ValueSource {
2324

2425
static {
2526
TYPE_SOURCES.put(String.class, SizeLessThanValueSource::getStringStream);
27+
TYPE_SOURCES.put(CharSequence.class, SizeLessThanValueSource::getStringStream);
2628
TYPE_SOURCES.put(Collection.class, SizeLessThanValueSource::getArrayListStream);
2729
TYPE_SOURCES.put(List.class, SizeLessThanValueSource::getArrayListStream);
2830
TYPE_SOURCES.put(ArrayList.class, SizeLessThanValueSource::getArrayListStream);
2931
TYPE_SOURCES.put(LinkedList.class, SizeLessThanValueSource::getLinkedListStream);
3032
TYPE_SOURCES.put(Set.class, SizeLessThanValueSource::getHashSetStream);
3133
TYPE_SOURCES.put(HashSet.class, SizeLessThanValueSource::getHashSetStream);
3234
TYPE_SOURCES.put(TreeSet.class, SizeLessThanValueSource::getTreeSetStream);
35+
TYPE_SOURCES.put(Map.class, SizeLessThanValueSource::getHashMapStream);
36+
TYPE_SOURCES.put(HashMap.class, SizeLessThanValueSource::getHashMapStream);
37+
TYPE_SOURCES.put(TreeMap.class, SizeLessThanValueSource::getTreeMapStream);
3338
}
3439

3540
@SuppressWarnings("unused")
@@ -130,6 +135,30 @@ private static Stream<Collection<?>> getTreeSetStream(long value) {
130135
);
131136
}
132137

138+
@NonNull
139+
private static Stream<Map<?, ?>> getHashMapStream(long value) {
140+
return Stream.concat(
141+
Stream.of(new HashMap<>()),
142+
Stream.iterate(1L, i -> i < value, i -> i + 1)
143+
.map(size -> generateMap(
144+
Math.toIntExact(size),
145+
new HashMap<>()
146+
))
147+
);
148+
}
149+
150+
@NonNull
151+
private static Stream<Map<?, ?>> getTreeMapStream(long value) {
152+
return Stream.concat(
153+
Stream.of(new TreeMap<>()),
154+
Stream.iterate(1L, i -> i < value, i -> i + 1)
155+
.map(size -> generateMap(
156+
Math.toIntExact(size),
157+
new TreeMap<>()
158+
))
159+
);
160+
}
161+
133162
private static String generateRandomString(int length) {
134163
// Include printable ASCII characters (32-126) which includes space and common characters
135164
return RANDOM.ints(length, 32, 127)
@@ -144,6 +173,13 @@ private static Collection<Object> generateCollection(int size, Collection<Object
144173
return collection;
145174
}
146175

176+
private static Map<Object, Object> generateMap(int size, Map<Object, Object> map) {
177+
for (int i = 0; i < size; i++) {
178+
map.put(i, "dummy_" + i); // Add dummy elements, content doesn't matter
179+
}
180+
return map;
181+
}
182+
147183
private static Object generateArray(int size, Class<?> arrayClass) {
148184
var componentType = arrayClass.getComponentType();
149185
return Array.newInstance(componentType, size);

src/test/java/it/aboutbits/springboot/testing/validation/ValidationAssertTest.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@
3434
import java.time.ZonedDateTime;
3535
import java.time.temporal.ChronoUnit;
3636
import java.util.Collection;
37+
import java.util.HashMap;
3738
import java.util.List;
39+
import java.util.Map;
3840
import java.util.Set;
41+
import java.util.TreeMap;
3942
import java.util.UUID;
4043
import java.util.function.Consumer;
4144

@@ -158,16 +161,19 @@ public record SomeValidParameter(
158161
@Size(min = 3) String minSizeString,
159162
@Size(min = 3) Set<UUID> minSizeCollection,
160163
@Size(min = 3) UUID[] minSizeArray,
164+
@Size(min = 3) Map<String, String> minSizeMap,
161165

162166
// Size - Max
163-
@Size(max = 10) String maxSizeString,
167+
@Size(max = 10) CharSequence maxSizeString,
164168
@Size(max = 10) Collection<String> maxSizeCollection,
165169
@Size(max = 10) String[] maxSizeArray,
170+
@Size(max = 10) HashMap<String, String> maxSizeMap,
166171

167172
// Size - Min/Max
168173
@Size(min = 2, max = 8) String minMaxSizeString,
169174
@Size(min = 2, max = 8) Collection<String> minMaxSizeCollection,
170175
@Size(min = 2, max = 8) String[] minMaxSizeArray,
176+
@Size(min = 2, max = 8) TreeMap<String, UUID> minMaxSizeMap,
171177

172178
// Nullable
173179
@Nullable Object nullable,
@@ -297,16 +303,19 @@ void testWithBeanValidation() {
297303
.size("minSizeString").min(3)
298304
.size("minSizeCollection").min(3)
299305
.size("minSizeArray").min(3)
306+
.size("minSizeMap").min(3)
300307

301308
// Size - Max
302309
.size("maxSizeString").max(10)
303310
.size("maxSizeCollection").max(10)
304311
.size("maxSizeArray").max(10)
312+
.size("maxSizeMap").max(10)
305313

306314
// Size - Min/Max
307315
.size("minMaxSizeString").minMax(2, 8)
308316
.size("minMaxSizeCollection").minMax(2, 8)
309317
.size("minMaxSizeArray").minMax(2, 8)
318+
.size("minMaxSizeMap").minMax(2, 8)
310319

311320
// Nullable
312321
.nullable("nullable")
@@ -575,16 +584,19 @@ void propertyMissingRule_shouldFail() {
575584
.size("minSizeString").min(3)
576585
.size("minSizeCollection").min(3)
577586
.size("minSizeArray").min(3)
587+
.size("minSizeMap").min(3)
578588

579589
// Size - Max
580590
.size("maxSizeString").max(10)
581591
.size("maxSizeCollection").max(10)
582592
.size("maxSizeArray").max(10)
593+
.size("maxSizeMap").max(10)
583594

584595
// Size - Min/Max
585596
.size("minMaxSizeString").minMax(2, 8)
586597
.size("minMaxSizeCollection").minMax(2, 8)
587598
.size("minMaxSizeArray").minMax(2, 8)
599+
.size("minMaxSizeMap").minMax(2, 8)
588600

589601
// Nullable
590602
.nullable("nullable")
@@ -852,16 +864,38 @@ private static SomeValidParameter getSomeValidParameter() {
852864
UUID.randomUUID(),
853865
UUID.randomUUID()
854866
}, // 4 elements, > 3 min
867+
Map.of(
868+
"ka", "a",
869+
"kb", "b",
870+
"kc", "c",
871+
"kd", "d"
872+
),
855873

856874
// Size - Max
857875
"small", // 5 characters, < 10 max
858876
List.of("a", "b", "c"), // 3 elements, < 10 max
859877
new String[]{"a", "b", "c"}, // 3 elements, < 10 max
878+
new HashMap<>(
879+
Map.of(
880+
"ka", "a",
881+
"kb", "b",
882+
"kc", "c",
883+
"kd", "d"
884+
)
885+
),
860886

861887
// Size - Min/Max
862888
"medium", // 6 characters, between 2-8
863889
List.of("a", "b", "c", "d", "e"), // 5 elements, between 2-8
864890
new String[]{"a", "b", "c", "d", "e"}, // 5 elements, between 2-8
891+
new TreeMap<>(
892+
Map.of(
893+
"ka", UUID.randomUUID(),
894+
"kb", UUID.randomUUID(),
895+
"kc", UUID.randomUUID(),
896+
"kd", UUID.randomUUID()
897+
)
898+
),
865899

866900
// Nullable
867901
null,

0 commit comments

Comments
 (0)