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
4 changes: 2 additions & 2 deletions .idea/checkstyle-idea.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.LongFunction;
import java.util.stream.Stream;

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

static {
TYPE_SOURCES.put(String.class, SizeGreaterThanValueSource::getStringStream);
TYPE_SOURCES.put(CharSequence.class, SizeGreaterThanValueSource::getStringStream);
TYPE_SOURCES.put(Collection.class, SizeGreaterThanValueSource::getArrayListStream);
TYPE_SOURCES.put(List.class, SizeGreaterThanValueSource::getArrayListStream);
TYPE_SOURCES.put(ArrayList.class, SizeGreaterThanValueSource::getArrayListStream);
TYPE_SOURCES.put(LinkedList.class, SizeGreaterThanValueSource::getLinkedListStream);
TYPE_SOURCES.put(Set.class, SizeGreaterThanValueSource::getHashSetStream);
TYPE_SOURCES.put(HashSet.class, SizeGreaterThanValueSource::getHashSetStream);
TYPE_SOURCES.put(LinkedHashSet.class, SizeGreaterThanValueSource::getLinkedHashSetStream);
TYPE_SOURCES.put(TreeSet.class, SizeGreaterThanValueSource::getTreeSetStream);
TYPE_SOURCES.put(Map.class, SizeGreaterThanValueSource::getHashMapStream);
TYPE_SOURCES.put(HashMap.class, SizeGreaterThanValueSource::getHashMapStream);
TYPE_SOURCES.put(LinkedHashMap.class, SizeGreaterThanValueSource::getLinkedHashMapStream);
TYPE_SOURCES.put(TreeMap.class, SizeGreaterThanValueSource::getTreeMapStream);
TYPE_SOURCES.put(ConcurrentMap.class, SizeGreaterThanValueSource::getConcurrentHashMapStream);
TYPE_SOURCES.put(ConcurrentHashMap.class, SizeGreaterThanValueSource::getConcurrentHashMapStream);
}

@SuppressWarnings("unused")
Expand Down Expand Up @@ -82,6 +95,16 @@ private static Stream<Collection<?>> getHashSetStream(long value) {
));
}

@NonNull
private static Stream<Collection<?>> getLinkedHashSetStream(long value) {
return getTestSizes(value)
.stream()
.map(size -> generateCollection(
Math.toIntExact(size),
new LinkedHashSet<>()
));
}

@NonNull
private static Stream<Collection<?>> getTreeSetStream(long value) {
return getTestSizes(value)
Expand Down Expand Up @@ -112,6 +135,46 @@ private static Stream<Collection<?>> getLinkedListStream(long value) {
));
}

@NonNull
private static Stream<Map<?, ?>> getHashMapStream(long value) {
return getTestSizes(value)
.stream()
.map(size -> generateMap(
Math.toIntExact(size),
new HashMap<>()
));
}

@NonNull
private static Stream<Map<?, ?>> getLinkedHashMapStream(long value) {
return getTestSizes(value)
.stream()
.map(size -> generateMap(
Math.toIntExact(size),
new LinkedHashMap<>()
));
}

@NonNull
private static Stream<Map<?, ?>> getTreeMapStream(long value) {
return getTestSizes(value)
.stream()
.map(size -> generateMap(
Math.toIntExact(size),
new TreeMap<>()
));
}

@NonNull
private static Stream<Map<?, ?>> getConcurrentHashMapStream(long value) {
return getTestSizes(value)
.stream()
.map(size -> generateMap(
Math.toIntExact(size),
new ConcurrentHashMap<>()
));
}

private static List<Long> getTestSizes(long value) {
var sizes = new ArrayList<Long>();

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

private static Map<Object, Object> generateMap(int size, Map<Object, Object> map) {
for (int i = 0; i < size; i++) {
map.put(i, "dummy_" + i); // Add dummy elements, content doesn't matter
}
return map;
}

private static Object generateArray(int size, Class<?> arrayClass) {
var componentType = arrayClass.getComponentType();
return Array.newInstance(componentType, size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.LongFunction;
import java.util.stream.Stream;

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

static {
TYPE_SOURCES.put(String.class, SizeLessThanValueSource::getStringStream);
TYPE_SOURCES.put(CharSequence.class, SizeLessThanValueSource::getStringStream);
TYPE_SOURCES.put(Collection.class, SizeLessThanValueSource::getArrayListStream);
TYPE_SOURCES.put(List.class, SizeLessThanValueSource::getArrayListStream);
TYPE_SOURCES.put(ArrayList.class, SizeLessThanValueSource::getArrayListStream);
TYPE_SOURCES.put(LinkedList.class, SizeLessThanValueSource::getLinkedListStream);
TYPE_SOURCES.put(Set.class, SizeLessThanValueSource::getHashSetStream);
TYPE_SOURCES.put(HashSet.class, SizeLessThanValueSource::getHashSetStream);
TYPE_SOURCES.put(LinkedHashSet.class, SizeLessThanValueSource::getLinkedHashSetStream);
TYPE_SOURCES.put(TreeSet.class, SizeLessThanValueSource::getTreeSetStream);
TYPE_SOURCES.put(Map.class, SizeLessThanValueSource::getHashMapStream);
TYPE_SOURCES.put(HashMap.class, SizeLessThanValueSource::getHashMapStream);
TYPE_SOURCES.put(LinkedHashMap.class, SizeLessThanValueSource::getLinkedHashMapStream);
TYPE_SOURCES.put(TreeMap.class, SizeLessThanValueSource::getTreeMapStream);
TYPE_SOURCES.put(ConcurrentMap.class, SizeLessThanValueSource::getConcurrentHashMapStream);
TYPE_SOURCES.put(ConcurrentHashMap.class, SizeLessThanValueSource::getConcurrentHashMapStream);
}

@SuppressWarnings("unused")
Expand Down Expand Up @@ -118,6 +131,18 @@ private static Stream<Collection<?>> getHashSetStream(long value) {
);
}

@NonNull
private static Stream<Collection<?>> getLinkedHashSetStream(long value) {
return Stream.concat(
Stream.of(new LinkedHashSet<>()),
Stream.iterate(1L, i -> i < value, i -> i + 1)
.map(size -> generateCollection(
Math.toIntExact(size),
new LinkedHashSet<>()
))
);
}

@NonNull
private static Stream<Collection<?>> getTreeSetStream(long value) {
return Stream.concat(
Expand All @@ -130,6 +155,54 @@ private static Stream<Collection<?>> getTreeSetStream(long value) {
);
}

@NonNull
private static Stream<Map<?, ?>> getHashMapStream(long value) {
return Stream.concat(
Stream.of(new HashMap<>()),
Stream.iterate(1L, i -> i < value, i -> i + 1)
.map(size -> generateMap(
Math.toIntExact(size),
new HashMap<>()
))
);
}

@NonNull
private static Stream<Map<?, ?>> getLinkedHashMapStream(long value) {
return Stream.concat(
Stream.of(new LinkedHashMap<>()),
Stream.iterate(1L, i -> i < value, i -> i + 1)
.map(size -> generateMap(
Math.toIntExact(size),
new LinkedHashMap<>()
))
);
}

@NonNull
private static Stream<Map<?, ?>> getTreeMapStream(long value) {
return Stream.concat(
Stream.of(new TreeMap<>()),
Stream.iterate(1L, i -> i < value, i -> i + 1)
.map(size -> generateMap(
Math.toIntExact(size),
new TreeMap<>()
))
);
}

@NonNull
private static Stream<Map<?, ?>> getConcurrentHashMapStream(long value) {
return Stream.concat(
Stream.of(new ConcurrentHashMap<>()),
Stream.iterate(1L, i -> i < value, i -> i + 1)
.map(size -> generateMap(
Math.toIntExact(size),
new ConcurrentHashMap<>()
))
);
}

private static String generateRandomString(int length) {
// Include printable ASCII characters (32-126) which includes space and common characters
return RANDOM.ints(length, 32, 127)
Expand All @@ -144,6 +217,13 @@ private static Collection<Object> generateCollection(int size, Collection<Object
return collection;
}

private static Map<Object, Object> generateMap(int size, Map<Object, Object> map) {
for (int i = 0; i < size; i++) {
map.put(i, "dummy_" + i); // Add dummy elements, content doesn't matter
}
return map;
}

private static Object generateArray(int size, Class<?> arrayClass) {
var componentType = arrayClass.getComponentType();
return Array.newInstance(componentType, size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.UUID;
import java.util.function.Consumer;

Expand Down Expand Up @@ -158,16 +161,19 @@ public record SomeValidParameter(
@Size(min = 3) String minSizeString,
@Size(min = 3) Set<UUID> minSizeCollection,
@Size(min = 3) UUID[] minSizeArray,
@Size(min = 3) Map<String, String> minSizeMap,

// Size - Max
@Size(max = 10) String maxSizeString,
@Size(max = 10) CharSequence maxSizeString,
@Size(max = 10) Collection<String> maxSizeCollection,
@Size(max = 10) String[] maxSizeArray,
@Size(max = 10) HashMap<String, String> maxSizeMap,

// Size - Min/Max
@Size(min = 2, max = 8) String minMaxSizeString,
@Size(min = 2, max = 8) Collection<String> minMaxSizeCollection,
@Size(min = 2, max = 8) String[] minMaxSizeArray,
@Size(min = 2, max = 8) TreeMap<String, UUID> minMaxSizeMap,

// Nullable
@Nullable Object nullable,
Expand Down Expand Up @@ -297,16 +303,19 @@ void testWithBeanValidation() {
.size("minSizeString").min(3)
.size("minSizeCollection").min(3)
.size("minSizeArray").min(3)
.size("minSizeMap").min(3)

// Size - Max
.size("maxSizeString").max(10)
.size("maxSizeCollection").max(10)
.size("maxSizeArray").max(10)
.size("maxSizeMap").max(10)

// Size - Min/Max
.size("minMaxSizeString").minMax(2, 8)
.size("minMaxSizeCollection").minMax(2, 8)
.size("minMaxSizeArray").minMax(2, 8)
.size("minMaxSizeMap").minMax(2, 8)

// Nullable
.nullable("nullable")
Expand Down Expand Up @@ -575,16 +584,19 @@ void propertyMissingRule_shouldFail() {
.size("minSizeString").min(3)
.size("minSizeCollection").min(3)
.size("minSizeArray").min(3)
.size("minSizeMap").min(3)

// Size - Max
.size("maxSizeString").max(10)
.size("maxSizeCollection").max(10)
.size("maxSizeArray").max(10)
.size("maxSizeMap").max(10)

// Size - Min/Max
.size("minMaxSizeString").minMax(2, 8)
.size("minMaxSizeCollection").minMax(2, 8)
.size("minMaxSizeArray").minMax(2, 8)
.size("minMaxSizeMap").minMax(2, 8)

// Nullable
.nullable("nullable")
Expand Down Expand Up @@ -728,6 +740,7 @@ void usingRuleRegistrarShouldWork() {
}


@SuppressWarnings("checkstyle:MethodLength")
private static SomeValidParameter getSomeValidParameter() {
return new SomeValidParameter(
// NotNull
Expand Down Expand Up @@ -852,16 +865,38 @@ private static SomeValidParameter getSomeValidParameter() {
UUID.randomUUID(),
UUID.randomUUID()
}, // 4 elements, > 3 min
Map.of(
"ka", "a",
"kb", "b",
"kc", "c",
"kd", "d"
),

// Size - Max
"small", // 5 characters, < 10 max
List.of("a", "b", "c"), // 3 elements, < 10 max
new String[]{"a", "b", "c"}, // 3 elements, < 10 max
new HashMap<>(
Map.of(
"ka", "a",
"kb", "b",
"kc", "c",
"kd", "d"
)
),

// Size - Min/Max
"medium", // 6 characters, between 2-8
List.of("a", "b", "c", "d", "e"), // 5 elements, between 2-8
new String[]{"a", "b", "c", "d", "e"}, // 5 elements, between 2-8
new TreeMap<>(
Map.of(
"ka", UUID.randomUUID(),
"kb", UUID.randomUUID(),
"kc", UUID.randomUUID(),
"kd", UUID.randomUUID()
)
),

// Nullable
null,
Expand Down