Skip to content

Commit

Permalink
#1306 add debug logs for FakerIntegrationTest
Browse files Browse the repository at this point in the history
  • Loading branch information
asolntsev committed Jul 21, 2024
1 parent d18f2d9 commit 946fa37
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
20 changes: 12 additions & 8 deletions src/main/java/net/datafaker/service/FakeValuesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,7 @@ private String[] splitExpressions(String expression, int length) {

private Object resExp(String directive, String[] args, Object current, ProviderRegistration root, FakerContext context, RegExpContext regExpContext) {
Object res = resolveExpression(directive, args, current, root, context);
LOG.fine(() -> "resExp(%s [%s]) current: %s, root: %s, context: %s, regExpContext: %s -> res: %s".formatted(directive, Arrays.toString(args), current, root, context, regExpContext, res));
if (res instanceof CharSequence) {
if (((CharSequence) res).isEmpty()) {
REGEXP2SUPPLIER_MAP.put(regExpContext, EMPTY_STRING);
Expand Down Expand Up @@ -971,7 +972,7 @@ private ValueResolver resolveFakerObjectAndMethod(ProviderRegistration faker, St
String fakerMethodName = removeUnderscoreChars(classAndMethod[0]);
final MethodAndCoercedArgs fakerAccessor = retrieveMethodAccessor(faker, fakerMethodName, EMPTY_ARRAY);
if (fakerAccessor == null) {
LOG.fine("Can't find top level faker object named " + fakerMethodName + ".");
LOG.fine(() -> "Can't find top level faker object named " + fakerMethodName + ".");
return null;
}
Object objectWithMethodToInvoke = fakerAccessor.invoke(faker);
Expand Down Expand Up @@ -1004,23 +1005,21 @@ private MethodAndCoercedArgs retrieveMethodAccessor(Object object, String method
stringMapMap.putIfAbsent(methodName, new CopyOnWriteMap<>(WeakHashMap::new));
stringMapMap.get(methodName).putIfAbsent(args, accessor);
if (accessor == null) {
LOG.fine("Can't find method on "
+ object.getClass().getSimpleName()
+ " called " + methodName + ".");
LOG.fine(() -> "Can't find method on %s called %s.".formatted(object.getClass().getSimpleName(), methodName));
}
return accessor;
}

/**
* Find an accessor by name ignoring case.
*/
private MethodAndCoercedArgs accessor(Class<?> clazz, String name, String[] args) {
final String finalName = name;
LOG.fine(() -> "Find accessor named " + finalName + " on " + clazz.getSimpleName() + " with args " + Arrays.toString(args));
name = removeUnderscoreChars(name);
private MethodAndCoercedArgs accessor(Class<?> clazz, final String accessorName, String[] args) {
LOG.fine(() -> "Find accessor named %s on %s with args %s".formatted(accessorName, clazz.getSimpleName(), Arrays.toString(args)));
String name = removeUnderscoreChars(accessorName);
final Collection<Method> methods;
if (CLASS_2_METHODS_CACHE.containsKey(clazz)) {
methods = CLASS_2_METHODS_CACHE.get(clazz).getOrDefault(name, Collections.emptyList());
LOG.fine(() -> "Found accessor named %s on %s in cache: %s".formatted(accessorName, clazz.getSimpleName(), methods));
} else {
Method[] classMethods = clazz.getMethods();
Map<String, Collection<Method>> methodMap =
Expand All @@ -1031,9 +1030,13 @@ private MethodAndCoercedArgs accessor(Class<?> clazz, String name, String[] args
methodMap.get(key).add(m);
}
CLASS_2_METHODS_CACHE.putIfAbsent(clazz, methodMap);
LOG.fine(() -> "Detected accessor named %s on %s, stored to cache: %s".formatted(accessorName, clazz.getSimpleName(), methodMap));

methods = methodMap.get(name);
LOG.fine(() -> "Detected accessor named %s on %s and taken from cache: %s".formatted(accessorName, clazz.getSimpleName(), methods));
}
if (methods == null) {
LOG.fine(() -> "Didn't accessor named %s on %s with args %s (methods=%s)".formatted(accessorName, clazz.getSimpleName(), Arrays.toString(args), null));
return null;
}
for (Method m : methods) {
Expand All @@ -1044,6 +1047,7 @@ private MethodAndCoercedArgs accessor(Class<?> clazz, String name, String[] args
}
}
}
LOG.fine(() -> "Didn't accessor named %s on %s with args %s (methods=%s)".formatted(accessorName, clazz.getSimpleName(), Arrays.toString(args), methods));
return null;
}

Expand Down
16 changes: 11 additions & 5 deletions src/test/java/net/datafaker/integration/FakerIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.function.Supplier;
import java.util.logging.Logger;
import java.util.stream.Stream;

import static java.lang.Thread.currentThread;
Expand All @@ -37,6 +39,7 @@
* are correct. These tests just ensure that the methods can be invoked.
*/
class FakerIntegrationTest {
private static final Logger log = Logger.getLogger(FakerIntegrationTest.class.getName());

/**
* a collection of Locales -> Exceptions.
Expand Down Expand Up @@ -85,13 +88,15 @@ private Faker init(Locale locale, Random random) {
@ParameterizedTest
@MethodSource("dataParameters")
void testAllFakerMethodsThatReturnStrings(Locale locale, Random random) throws Exception {
log.fine(() -> " (%s, %s)".formatted(locale, random));
final Faker faker = init(locale, random);

Method[] methods = faker.getClass().getMethods();
for (Method provider : methods) {
if (AbstractProvider.class.isAssignableFrom(provider.getReturnType()) && provider.getParameterCount() == 0) {
AbstractProvider<?> providerImpl = (AbstractProvider<?>) provider.invoke(faker);
log.fine(() -> " (%s), method: %s.%s()".formatted(locale, provider.getDeclaringClass().getSimpleName(), provider.getName()));

AbstractProvider<?> providerImpl = (AbstractProvider<?>) provider.invoke(faker);
testAllMethodsThatReturnStringsActuallyReturnStrings(providerImpl);
}
}
Expand All @@ -111,14 +116,15 @@ private void testAllMethodsThatReturnStringsActuallyReturnStrings(AbstractProvid
}
final Object returnValue;
try {
returnValue = method.invoke(provider);
log.fine(() -> " (%s), method: %s.%s()".formatted(locale, method.getDeclaringClass(), method.getName()));
returnValue = method.invoke(provider);
} catch (Exception e) {
throw new RuntimeException("Test for method %s and object %s was failed for locale %s [thread: %s]".formatted(
method, provider, locale, currentThread().getName()), e);
}
assertThat(returnValue).as("For method " + provider.getClass() + "#" + method.getName() + "value is '" + returnValue + "'").isInstanceOf(String.class);
final String returnValueAsString = (String) returnValue;
assertThat(returnValueAsString).as("For method " + provider.getClass() + "#" + method.getName()).isNotEmpty();
Supplier<String> description = () -> "For method %s.%s(), value is '%s'".formatted(provider.getClass().getSimpleName(), method.getName(), returnValue);
assertThat(returnValue).as(description).isInstanceOf(String.class);
assertThat((String) returnValue).as(description).isNotEmpty();
}
}

Expand Down

0 comments on commit 946fa37

Please sign in to comment.