From cba5476a1cdf8d0c9612917fb49deb0f47ba693a Mon Sep 17 00:00:00 2001 From: Andrei Solntsev Date: Sun, 21 Jul 2024 18:17:21 +0300 Subject: [PATCH] #1306 add debug logs for FakerIntegrationTest --- .../datafaker/service/FakeValuesService.java | 20 +++++++++++-------- .../integration/FakerIntegrationTest.java | 16 ++++++++++----- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/main/java/net/datafaker/service/FakeValuesService.java b/src/main/java/net/datafaker/service/FakeValuesService.java index 1b16d6416..175b626f6 100644 --- a/src/main/java/net/datafaker/service/FakeValuesService.java +++ b/src/main/java/net/datafaker/service/FakeValuesService.java @@ -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); @@ -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); @@ -1004,9 +1005,7 @@ 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; } @@ -1014,13 +1013,13 @@ private MethodAndCoercedArgs retrieveMethodAccessor(Object object, String method /** * 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 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> methodMap = @@ -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) { @@ -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; } diff --git a/src/test/java/net/datafaker/integration/FakerIntegrationTest.java b/src/test/java/net/datafaker/integration/FakerIntegrationTest.java index f77fd8be0..2a2543478 100644 --- a/src/test/java/net/datafaker/integration/FakerIntegrationTest.java +++ b/src/test/java/net/datafaker/integration/FakerIntegrationTest.java @@ -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; @@ -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. @@ -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); } } @@ -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 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(); } }