public Statement apply()

in src/main/java/org/apache/sling/testing/junit/rules/FilterRule.java [43:138]


    public Statement apply(Statement base, Description description) {

        // get the annotations in order and skip the tests accordingly

        // IgnoreIf
        IgnoreIf ignoreIf = description.getAnnotation(IgnoreIf.class);
        if (null != ignoreIf) {
            for (Class condClass : Arrays.asList(ignoreIf.value())) {
                try {
                    Constructor<? extends Condition> constructor = condClass.getConstructor();
                    Condition cond = constructor.newInstance();
                    if (cond.satisfy()) {
                        return emptyStatement("IgnoreIf condition met to skip: " + cond.description());
                    }
                } catch (Exception e) {
                    LOG.error("Error getting condition object", e);
                }
            }
        }

        // IgnoreIfProperties
        List<IgnoreIfProperty> ignoreIfPropertyList = new ArrayList<IgnoreIfProperty>();
        IgnoreIfProperties ignoreIfProperties = description.getAnnotation(IgnoreIfProperties.class);
        if (null != ignoreIfProperties) {
            ignoreIfPropertyList.addAll(Arrays.asList(ignoreIfProperties.value()));
        }

        // IgnoreIfProperty
        IgnoreIfProperty ignoreIfProperty = description.getAnnotation(IgnoreIfProperty.class);
        if (null != ignoreIfProperty) {
            ignoreIfPropertyList.add(ignoreIfProperty);
        }
        // Process the ignoreIfProperties
        for (IgnoreIfProperty ignoreIfProp : ignoreIfPropertyList) {
            if (null != System.getProperty(ignoreIfProp.name())
                    && System.getProperty(ignoreIfProp.name()).equals(ignoreIfProp.value())) {
                return emptyStatement("IgnoreIfProperty condition met to skip: system property '" + ignoreIfProp.name()
                        + "' is equal to '" + ignoreIfProp.value() + "'.");
            }
        }

        // Filter using IgnoreTestsConfig
        String fqdn = description.getClassName();
        String methodName = description.getMethodName();
        if (null != methodName) {
            fqdn += "#" + methodName;
        }

        Match match = IgnoreTestsConfig.get().match(fqdn);
        if (match.isIgnored()) {
            return emptyStatement(match.getReason());
        }

       // filterCategory processing
        List<String> filteredCategories;
        if (System.getProperty(CATEGORY_PROPERTY) != null) {
            filteredCategories = Arrays.asList(System.getProperty(CATEGORY_PROPERTY).split(","));
        } else {
            filteredCategories = defaultCategories;
        }

        Category testCategory = description.getAnnotation(Category.class);
        Class[] testCategories = new Class[0];
        if (testCategory != null) {
            testCategories = testCategory.value();
        }
        /*
         * Category annotation exists and the -DfilterCategory property is also set. If test category exists
         * in -DfilterCategory list & -DrunOnlyFilteredCategories is NOT set, then the test is skipped If test
         * category exists in -DfilterCategory & -DrunOnlyFilteredCategories is set, then the test is included
         */
        if ((System.getProperty(INCLUDE_CATEGORY_PROPERTY) == null)) {
            // Skip Tests from CATEGORY_PROPERTY
            for (Class<?> category : testCategories) {
                if (filteredCategories.contains(category.getSimpleName())) {
                    return emptyStatement("Excluding category: " + category.getSimpleName());
                }
            }
        } else {
            // Run only Tests from CATEGORY_PROPERTY
            boolean categorySelected = false;
            for (Class<?> category : testCategories) {
                if ((filteredCategories.contains(category.getSimpleName()))) {
                    categorySelected = true;
                }
            }

            if (!categorySelected) {
                // No @Category from Test is in CATEGORY_PROPERTY (which should be executed), so skip
                return emptyStatement("Test has no category in (" + INCLUDE_CATEGORY_PROPERTY + "=true): '" + filteredCategories + "'");
            }
        }

        // No Filter excluded this test, so execute
        return base;
    }