Search in sources :

Example 1 with AlfrescoPublicApi

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);
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) AlfrescoPublicApi(org.alfresco.api.AlfrescoPublicApi) TypeVariable(java.lang.reflect.TypeVariable) WildcardType(java.lang.reflect.WildcardType) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) Reflections(org.reflections.Reflections) Field(java.lang.reflect.Field) Constructor(java.lang.reflect.Constructor) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) SetMultimap(com.google.common.collect.SetMultimap) Sets(com.google.common.collect.Sets) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) HashMultimap(com.google.common.collect.HashMultimap) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Modifier(java.lang.reflect.Modifier) Executable(java.lang.reflect.Executable) Method(java.lang.reflect.Method) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ArrayList(java.util.ArrayList) Reflections(org.reflections.Reflections) HashSet(java.util.HashSet)

Aggregations

HashMultimap (com.google.common.collect.HashMultimap)1 SetMultimap (com.google.common.collect.SetMultimap)1 Sets (com.google.common.collect.Sets)1 Constructor (java.lang.reflect.Constructor)1 Executable (java.lang.reflect.Executable)1 Field (java.lang.reflect.Field)1 GenericArrayType (java.lang.reflect.GenericArrayType)1 Method (java.lang.reflect.Method)1 Modifier (java.lang.reflect.Modifier)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 TypeVariable (java.lang.reflect.TypeVariable)1 WildcardType (java.lang.reflect.WildcardType)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 AlfrescoPublicApi (org.alfresco.api.AlfrescoPublicApi)1