/******************************************************************************************************************* * * ******************************************************************************************************************/ public ContextSampler (final @Nonnull Object owner) { contexts = Collections.unmodifiableList(contextManager.getContexts()); if (log.isTraceEnabled()) { log.trace(">>>> contexts for {} at construction time: {}", shortId(owner), shortIds(contexts)); } }
/******************************************************************************************************************* * * Returns the type of an object, taking care of mocks created by Mockito, for which the implemented interface is * returned. * * @param object the object * @return the object type * ******************************************************************************************************************/ @Nonnull /* VisibleForTesting */ static <T> Class<T> findTypeOf (final @Nonnull T object) { Class<?> ownerClass = object.getClass(); if (ownerClass.toString().contains("EnhancerByMockito")) { ownerClass = ownerClass.getInterfaces()[0]; // 1st is the original class, 2nd is CGLIB proxy if (log.isTraceEnabled()) { log.trace(">>>> owner is a mock {} implementing {}", shortName(ownerClass), shortNames(Arrays.asList(ownerClass.getInterfaces()))); log.trace(">>>> owner class replaced with {}", shortName(ownerClass)); } } return (Class<T>)ownerClass; } }
final @Nonnull Class<ROLE_TYPE> roleType) log.trace("findRoles({}, {})", shortId(datum), shortName(roleType)); final Class<?> datumType = findTypeOf(datum); final List<ROLE_TYPE> roles = new ArrayList<>(); log.trace(">>>> contexts: {}", shortIds(contextManager.getContexts())); shortName(roleImplementationType), shortName(contextType)); continue outer; log.trace(">>>> findRoles() returning: {}", shortIds((Collection)roles));
/******************************************************************************************************************* * ******************************************************************************************************************/ @Test(dataProvider = "ownersAndRoleImplementations") public void must_correctly_find_roles (final @Nonnull Object owner, final @Nonnull Class<?> roleClass, final @Nonnull List<?> expectedRoles) throws NotFoundException { // given when(contextManager.findContextOfType(eq(Context1.class))).thenThrow(new NotFoundException()); when(contextManager.findContextOfType(eq(Context2.class))).thenReturn(context2); final UnderTest underTest = new UnderTest(); registerMockRoles(underTest); underTest.registerBean(bean1); underTest.registerBean(bean2); underTest.scan(asList(RI1A.class, RI1B.class, RI1C.class, RI2A.class, RI2B.class, RI2C.class, RI3A.class, RI3B.class, RI3C.class)); // when final List<?> actualRoles = underTest.findRoles(owner, roleClass); // then final String s = String.format("owner: %s role: %s", shortId(owner), shortName(roleClass)); assertListEquals(s, actualRoles, expectedRoles); }
@Nonnull public static String shortIds (final @Nonnull Iterable<Object> objects) { final StringBuilder result = new StringBuilder(); String separator = ""; for (final Object object : objects) { result.append(separator).append(shortId(object)); separator = ", "; } return "[" + result.toString() + "]"; } }
/******************************************************************************************************************* * * Scans all the given role implementation classes and build a map of roles by owner class. * * @param roleImplementationTypes the types of role implementations to scan * ******************************************************************************************************************/ protected void scan (final @Nonnull Collection<Class<?>> roleImplementationTypes) { log.debug("scan({})", shortNames(roleImplementationTypes)); for (final Class<?> roleImplementationType : roleImplementationTypes) { for (final Class<?> datumType : findDatumTypesForRole(roleImplementationType)) { for (final Class<?> roleType : findAllImplementedInterfacesOf(roleImplementationType)) { if (!roleType.getName().equals("org.springframework.beans.factory.aspectj.ConfigurableObject")) { roleMapByDatumAndRole.add(new DatumAndRole(datumType, roleType), roleImplementationType); } } } } logRoles(); }
@Nonnull public static String shortNames (final @Nonnull Iterable<Class<?>> classes) { final StringBuilder result = new StringBuilder(); String separator = ""; for (final Class<?> clazz : classes) { result.append(separator).append(shortName(clazz)); separator = ", "; } return "[" + result.toString() + "]"; }
@Override @Nonnull public PresentationModel run() { final List<Object> roles = resolveRoles(datum, rolesOrFactories); roles.add(new DefaultSimpleComposite<>(pmFinder)); log.trace(">>>> roles for {}: {}", shortId(datum), shortIds(roles)); return defaultPresentationModelFactory.createPresentationModel(datum, roles.toArray()); } });
new Object[] { shortName(entry.getKey().getDatumClass()), shortName(entry.getKey().getRoleClass()), shortNames(entry.getValue())});
@Nonnull public static String shortId (final @Nullable Object object) { if (object == null) { return "null"; } String s = String.format("%s@%x", shortName(object.getClass()), System.identityHashCode(object)); if (object instanceof Identifiable) { s += "/" + ((Identifiable)object).getId(); } return s; }