use of org.alfresco.api.AlfrescoPublicApi in project records-management by Alfresco.
the class PublicAPITestUtil method testPublicAPIConsistency.
/**
* Check the consistency of the public API exposed from the given package. For each class in the package that is
* annotated {@link AlfrescoPublicApi}, check that no exposed methods (or fields, constructors, etc.) use
* non-public-API classes from Alfresco.
*
* @param basePackageName The package to check classes within.
* @param knownBadReferences Any references that would cause this test to fail, but which we don't want to change.
* The keys should be public API classes within our code and the values should be the non-public-API
* class that is being referenced.
*/
public static void testPublicAPIConsistency(String basePackageName, SetMultimap<Class<?>, Class<?>> knownBadReferences) {
Reflections reflections = new Reflections(basePackageName);
Set<Class<?>> publicAPIClasses = reflections.getTypesAnnotatedWith(AlfrescoPublicApi.class, true);
SetMultimap<Class<?>, Class<?>> referencedFrom = HashMultimap.create();
Set<Class<?>> referencedClasses = new HashSet<>();
for (Class<?> publicAPIClass : publicAPIClasses) {
Set<Class<?>> referencedClassesFromClass = getReferencedClassesFromClass(publicAPIClass, new HashSet<>());
referencedClassesFromClass.forEach(clazz -> referencedFrom.put(clazz, publicAPIClass));
// Remove any references in knownBadReferences and error if an expected reference wasn't found.
if (knownBadReferences.containsKey(publicAPIClass)) {
for (Class<?> clazz : knownBadReferences.get(publicAPIClass)) {
assertTrue("Supplied knownBadReferences expects " + clazz + " to be referenced by " + publicAPIClass + ", but no such error was found", referencedClassesFromClass.remove(clazz));
}
}
referencedClasses.addAll(referencedClassesFromClass);
}
List<String> errorMessages = new ArrayList<>();
for (Class<?> referencedClass : referencedClasses) {
if (isInAlfresco(referencedClass) && !isPartOfPublicApi(referencedClass)) {
Set<String> referencerNames = referencedFrom.get(referencedClass).stream().map(c -> c.getName()).collect(Collectors.toSet());
errorMessages.add(referencedClass.getName() + " <- " + StringUtils.join(referencerNames, ", "));
}
}
if (!errorMessages.isEmpty()) {
System.out.println("Errors found:");
System.out.println(StringUtils.join(errorMessages, "\n"));
}
assertEquals("Found references to non-public API classes from public API classes.", Collections.emptyList(), errorMessages);
}
Aggregations