|
| 1 | +package it.aboutbits.springboot.testing.validation.source; |
| 2 | + |
| 3 | +import it.aboutbits.springboot.testing.validation.core.ValueSource; |
| 4 | +import org.jspecify.annotations.NullMarked; |
| 5 | +import org.jspecify.annotations.Nullable; |
| 6 | + |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.function.Function; |
| 10 | +import java.util.stream.Stream; |
| 11 | + |
| 12 | +@NullMarked |
| 13 | +public class InvalidEmailValueSource implements ValueSource { |
| 14 | + private static final Map<Class<?>, Function<Object[], Stream<?>>> TYPE_SOURCES = new HashMap<>(); |
| 15 | + |
| 16 | + static { |
| 17 | + TYPE_SOURCES.put( |
| 18 | + String.class, |
| 19 | + (Object[] args) -> Stream.of( |
| 20 | + "plainaddress", |
| 21 | + "@example.com", |
| 22 | + "someone@", |
| 23 | + "someone@@example.com", |
| 24 | + "some one@example.com", |
| 25 | + "someone@exam ple.com", |
| 26 | + "someone@example..com", |
| 27 | + "someone@.example.com" |
| 28 | + ) |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + @SuppressWarnings("unused") |
| 33 | + public static void registerType(Class<?> type, Function<Object[], Stream<?>> source) { |
| 34 | + TYPE_SOURCES.put(type, source); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + @SuppressWarnings("unchecked") |
| 39 | + public <T> Stream<@Nullable T> values(Class<T> propertyClass, Object... args) { |
| 40 | + var sourceFunction = TYPE_SOURCES.get(propertyClass); |
| 41 | + if (sourceFunction != null) { |
| 42 | + return (Stream<@Nullable T>) sourceFunction.apply(args); |
| 43 | + } |
| 44 | + |
| 45 | + throw new IllegalArgumentException("Property class not supported!"); |
| 46 | + } |
| 47 | +} |
0 commit comments