Skip to content

Commit 5de16e2

Browse files
authored
Merge pull request #23 from aboutbits/add-missing-types
add support for charsequences and maps
2 parents dd9cd7e + 4d62198 commit 5de16e2

4 files changed

Lines changed: 188 additions & 3 deletions

File tree

.idea/checkstyle-idea.xml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@
88
import java.util.Collection;
99
import java.util.HashMap;
1010
import java.util.HashSet;
11+
import java.util.LinkedHashMap;
12+
import java.util.LinkedHashSet;
1113
import java.util.LinkedList;
1214
import java.util.List;
1315
import java.util.Map;
1416
import java.util.Random;
1517
import java.util.Set;
18+
import java.util.TreeMap;
1619
import java.util.TreeSet;
20+
import java.util.concurrent.ConcurrentHashMap;
21+
import java.util.concurrent.ConcurrentMap;
1722
import java.util.function.LongFunction;
1823
import java.util.stream.Stream;
1924

@@ -23,13 +28,21 @@ public class SizeGreaterThanValueSource implements ValueSource {
2328

2429
static {
2530
TYPE_SOURCES.put(String.class, SizeGreaterThanValueSource::getStringStream);
31+
TYPE_SOURCES.put(CharSequence.class, SizeGreaterThanValueSource::getStringStream);
2632
TYPE_SOURCES.put(Collection.class, SizeGreaterThanValueSource::getArrayListStream);
2733
TYPE_SOURCES.put(List.class, SizeGreaterThanValueSource::getArrayListStream);
2834
TYPE_SOURCES.put(ArrayList.class, SizeGreaterThanValueSource::getArrayListStream);
2935
TYPE_SOURCES.put(LinkedList.class, SizeGreaterThanValueSource::getLinkedListStream);
3036
TYPE_SOURCES.put(Set.class, SizeGreaterThanValueSource::getHashSetStream);
3137
TYPE_SOURCES.put(HashSet.class, SizeGreaterThanValueSource::getHashSetStream);
38+
TYPE_SOURCES.put(LinkedHashSet.class, SizeGreaterThanValueSource::getLinkedHashSetStream);
3239
TYPE_SOURCES.put(TreeSet.class, SizeGreaterThanValueSource::getTreeSetStream);
40+
TYPE_SOURCES.put(Map.class, SizeGreaterThanValueSource::getHashMapStream);
41+
TYPE_SOURCES.put(HashMap.class, SizeGreaterThanValueSource::getHashMapStream);
42+
TYPE_SOURCES.put(LinkedHashMap.class, SizeGreaterThanValueSource::getLinkedHashMapStream);
43+
TYPE_SOURCES.put(TreeMap.class, SizeGreaterThanValueSource::getTreeMapStream);
44+
TYPE_SOURCES.put(ConcurrentMap.class, SizeGreaterThanValueSource::getConcurrentHashMapStream);
45+
TYPE_SOURCES.put(ConcurrentHashMap.class, SizeGreaterThanValueSource::getConcurrentHashMapStream);
3346
}
3447

3548
@SuppressWarnings("unused")
@@ -82,6 +95,16 @@ private static Stream<Collection<?>> getHashSetStream(long value) {
8295
));
8396
}
8497

98+
@NonNull
99+
private static Stream<Collection<?>> getLinkedHashSetStream(long value) {
100+
return getTestSizes(value)
101+
.stream()
102+
.map(size -> generateCollection(
103+
Math.toIntExact(size),
104+
new LinkedHashSet<>()
105+
));
106+
}
107+
85108
@NonNull
86109
private static Stream<Collection<?>> getTreeSetStream(long value) {
87110
return getTestSizes(value)
@@ -112,6 +135,46 @@ private static Stream<Collection<?>> getLinkedListStream(long value) {
112135
));
113136
}
114137

138+
@NonNull
139+
private static Stream<Map<?, ?>> getHashMapStream(long value) {
140+
return getTestSizes(value)
141+
.stream()
142+
.map(size -> generateMap(
143+
Math.toIntExact(size),
144+
new HashMap<>()
145+
));
146+
}
147+
148+
@NonNull
149+
private static Stream<Map<?, ?>> getLinkedHashMapStream(long value) {
150+
return getTestSizes(value)
151+
.stream()
152+
.map(size -> generateMap(
153+
Math.toIntExact(size),
154+
new LinkedHashMap<>()
155+
));
156+
}
157+
158+
@NonNull
159+
private static Stream<Map<?, ?>> getTreeMapStream(long value) {
160+
return getTestSizes(value)
161+
.stream()
162+
.map(size -> generateMap(
163+
Math.toIntExact(size),
164+
new TreeMap<>()
165+
));
166+
}
167+
168+
@NonNull
169+
private static Stream<Map<?, ?>> getConcurrentHashMapStream(long value) {
170+
return getTestSizes(value)
171+
.stream()
172+
.map(size -> generateMap(
173+
Math.toIntExact(size),
174+
new ConcurrentHashMap<>()
175+
));
176+
}
177+
115178
private static List<Long> getTestSizes(long value) {
116179
var sizes = new ArrayList<Long>();
117180

@@ -136,6 +199,13 @@ private static Collection<Object> generateCollection(int size, Collection<Object
136199
return collection;
137200
}
138201

202+
private static Map<Object, Object> generateMap(int size, Map<Object, Object> map) {
203+
for (int i = 0; i < size; i++) {
204+
map.put(i, "dummy_" + i); // Add dummy elements, content doesn't matter
205+
}
206+
return map;
207+
}
208+
139209
private static Object generateArray(int size, Class<?> arrayClass) {
140210
var componentType = arrayClass.getComponentType();
141211
return Array.newInstance(componentType, size);

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

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@
88
import java.util.Collection;
99
import java.util.HashMap;
1010
import java.util.HashSet;
11+
import java.util.LinkedHashMap;
12+
import java.util.LinkedHashSet;
1113
import java.util.LinkedList;
1214
import java.util.List;
1315
import java.util.Map;
1416
import java.util.Random;
1517
import java.util.Set;
18+
import java.util.TreeMap;
1619
import java.util.TreeSet;
20+
import java.util.concurrent.ConcurrentHashMap;
21+
import java.util.concurrent.ConcurrentMap;
1722
import java.util.function.LongFunction;
1823
import java.util.stream.Stream;
1924

@@ -23,13 +28,21 @@ public class SizeLessThanValueSource implements ValueSource {
2328

2429
static {
2530
TYPE_SOURCES.put(String.class, SizeLessThanValueSource::getStringStream);
31+
TYPE_SOURCES.put(CharSequence.class, SizeLessThanValueSource::getStringStream);
2632
TYPE_SOURCES.put(Collection.class, SizeLessThanValueSource::getArrayListStream);
2733
TYPE_SOURCES.put(List.class, SizeLessThanValueSource::getArrayListStream);
2834
TYPE_SOURCES.put(ArrayList.class, SizeLessThanValueSource::getArrayListStream);
2935
TYPE_SOURCES.put(LinkedList.class, SizeLessThanValueSource::getLinkedListStream);
3036
TYPE_SOURCES.put(Set.class, SizeLessThanValueSource::getHashSetStream);
3137
TYPE_SOURCES.put(HashSet.class, SizeLessThanValueSource::getHashSetStream);
38+
TYPE_SOURCES.put(LinkedHashSet.class, SizeLessThanValueSource::getLinkedHashSetStream);
3239
TYPE_SOURCES.put(TreeSet.class, SizeLessThanValueSource::getTreeSetStream);
40+
TYPE_SOURCES.put(Map.class, SizeLessThanValueSource::getHashMapStream);
41+
TYPE_SOURCES.put(HashMap.class, SizeLessThanValueSource::getHashMapStream);
42+
TYPE_SOURCES.put(LinkedHashMap.class, SizeLessThanValueSource::getLinkedHashMapStream);
43+
TYPE_SOURCES.put(TreeMap.class, SizeLessThanValueSource::getTreeMapStream);
44+
TYPE_SOURCES.put(ConcurrentMap.class, SizeLessThanValueSource::getConcurrentHashMapStream);
45+
TYPE_SOURCES.put(ConcurrentHashMap.class, SizeLessThanValueSource::getConcurrentHashMapStream);
3346
}
3447

3548
@SuppressWarnings("unused")
@@ -118,6 +131,18 @@ private static Stream<Collection<?>> getHashSetStream(long value) {
118131
);
119132
}
120133

134+
@NonNull
135+
private static Stream<Collection<?>> getLinkedHashSetStream(long value) {
136+
return Stream.concat(
137+
Stream.of(new LinkedHashSet<>()),
138+
Stream.iterate(1L, i -> i < value, i -> i + 1)
139+
.map(size -> generateCollection(
140+
Math.toIntExact(size),
141+
new LinkedHashSet<>()
142+
))
143+
);
144+
}
145+
121146
@NonNull
122147
private static Stream<Collection<?>> getTreeSetStream(long value) {
123148
return Stream.concat(
@@ -130,6 +155,54 @@ private static Stream<Collection<?>> getTreeSetStream(long value) {
130155
);
131156
}
132157

158+
@NonNull
159+
private static Stream<Map<?, ?>> getHashMapStream(long value) {
160+
return Stream.concat(
161+
Stream.of(new HashMap<>()),
162+
Stream.iterate(1L, i -> i < value, i -> i + 1)
163+
.map(size -> generateMap(
164+
Math.toIntExact(size),
165+
new HashMap<>()
166+
))
167+
);
168+
}
169+
170+
@NonNull
171+
private static Stream<Map<?, ?>> getLinkedHashMapStream(long value) {
172+
return Stream.concat(
173+
Stream.of(new LinkedHashMap<>()),
174+
Stream.iterate(1L, i -> i < value, i -> i + 1)
175+
.map(size -> generateMap(
176+
Math.toIntExact(size),
177+
new LinkedHashMap<>()
178+
))
179+
);
180+
}
181+
182+
@NonNull
183+
private static Stream<Map<?, ?>> getTreeMapStream(long value) {
184+
return Stream.concat(
185+
Stream.of(new TreeMap<>()),
186+
Stream.iterate(1L, i -> i < value, i -> i + 1)
187+
.map(size -> generateMap(
188+
Math.toIntExact(size),
189+
new TreeMap<>()
190+
))
191+
);
192+
}
193+
194+
@NonNull
195+
private static Stream<Map<?, ?>> getConcurrentHashMapStream(long value) {
196+
return Stream.concat(
197+
Stream.of(new ConcurrentHashMap<>()),
198+
Stream.iterate(1L, i -> i < value, i -> i + 1)
199+
.map(size -> generateMap(
200+
Math.toIntExact(size),
201+
new ConcurrentHashMap<>()
202+
))
203+
);
204+
}
205+
133206
private static String generateRandomString(int length) {
134207
// Include printable ASCII characters (32-126) which includes space and common characters
135208
return RANDOM.ints(length, 32, 127)
@@ -144,6 +217,13 @@ private static Collection<Object> generateCollection(int size, Collection<Object
144217
return collection;
145218
}
146219

220+
private static Map<Object, Object> generateMap(int size, Map<Object, Object> map) {
221+
for (int i = 0; i < size; i++) {
222+
map.put(i, "dummy_" + i); // Add dummy elements, content doesn't matter
223+
}
224+
return map;
225+
}
226+
147227
private static Object generateArray(int size, Class<?> arrayClass) {
148228
var componentType = arrayClass.getComponentType();
149229
return Array.newInstance(componentType, size);

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

Lines changed: 36 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")
@@ -728,6 +740,7 @@ void usingRuleRegistrarShouldWork() {
728740
}
729741

730742

743+
@SuppressWarnings("checkstyle:MethodLength")
731744
private static SomeValidParameter getSomeValidParameter() {
732745
return new SomeValidParameter(
733746
// NotNull
@@ -852,16 +865,38 @@ private static SomeValidParameter getSomeValidParameter() {
852865
UUID.randomUUID(),
853866
UUID.randomUUID()
854867
}, // 4 elements, > 3 min
868+
Map.of(
869+
"ka", "a",
870+
"kb", "b",
871+
"kc", "c",
872+
"kd", "d"
873+
),
855874

856875
// Size - Max
857876
"small", // 5 characters, < 10 max
858877
List.of("a", "b", "c"), // 3 elements, < 10 max
859878
new String[]{"a", "b", "c"}, // 3 elements, < 10 max
879+
new HashMap<>(
880+
Map.of(
881+
"ka", "a",
882+
"kb", "b",
883+
"kc", "c",
884+
"kd", "d"
885+
)
886+
),
860887

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

866901
// Nullable
867902
null,

0 commit comments

Comments
 (0)