Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.googlecode.junittoolbox;
  
  
 import java.util.List;
 
 class CategoriesFilter extends Filter {
 
     static CategoriesFilter forTestSuite(Class<?> testSuiteClass) {
         List<Class<?>> includedCategories = new ArrayList<Class<?>>();
         List<Class<?>> excludedCategories = new ArrayList<Class<?>>();
         IncludeCategory includeCategoryAnnotationtestSuiteClass.getAnnotation(IncludeCategory.class);
         if (includeCategoryAnnotation != null) { includedCategories.add(includeCategoryAnnotation.value()); }
         IncludeCategories includeCategoriesAnnotationtestSuiteClass.getAnnotation(IncludeCategories.class);
         if (includeCategoriesAnnotation != null) { includedCategories.addAll(Arrays.asList(includeCategoriesAnnotation.value())); }
         ExcludeCategory excludeCategoryAnnotationtestSuiteClass.getAnnotation(ExcludeCategory.class);
         if (excludeCategoryAnnotation != null) { excludedCategories.add(excludeCategoryAnnotation.value()); }
         ExcludeCategories excludeCategoriesAnnotationtestSuiteClass.getAnnotation(ExcludeCategories.class);
         if (excludeCategoriesAnnotation != null) { excludedCategories.addAll(Arrays.asList(excludeCategoriesAnnotation.value())); }
         return (includedCategories.isEmpty() && excludedCategories.isEmpty() ? null : new CategoriesFilter(includedCategoriesexcludedCategories));
     }
 
     private final List<Class<?>> includedCategories = new ArrayList<Class<?>>();
     private final List<Class<?>> excludedCategories = new ArrayList<Class<?>>();
 
     private CategoriesFilter(Collection<Class<?>> includedCategoriesCollection<Class<?>> excludedCategories) {
         this..addAll(includedCategories);
         this..addAll(excludedCategories);
     }
 
     @Override
     public String describe() {
         StringBuilder sb = new StringBuilder();
         String separator = "include categories: ";
         for (Class<?> categoryClass : ) {
             sb.append(separator);
             sb.append(categoryClass.getSimpleName());
             separator = ", ";
         }
         separator = (sb.length() == 0 ? "" : "; ") + "exclude categories: ";
         for (Class<?> categoryClass : ) {
             sb.append(separator);
             sb.append(categoryClass.getSimpleName());
             separator = ", ";
         }
         return sb.toString();
     }
 
     @Override
     public boolean shouldRun(Description description) {
         if (hasCorrectCategoryAnnotation(description)) {
             return true;
         }
         for (Description childDescription : description.getChildren()) {
             if (shouldRun(childDescription)) {
                 return true;
             }
         }
         return false;
     }
 
     private boolean hasCorrectCategoryAnnotation(Description description) {
         List<Class<?>> categories = categories(description);
         if (categories.isEmpty()) {
             // Test has no @Category annotation, is match if no included categories have been specified ...
             return .isEmpty();
         }
         if (!.isEmpty()) {
             for (Class<?> category : categories) {
                 for (Class<?> excludedCategory : ) {
                     if (excludedCategory.isAssignableFrom(category)) {
                         // Test has an excluded category ...
                         return false;
                     }
                 }
             }
         }
         if (.isEmpty()) {
             // Test is not excluded and no included categories have been specified ...
             return true;
         }
         for (Class<?> category : categories) {
             for (Class<?> includedCategory : ) {
                 if (includedCategory.isAssignableFrom(category)) {
                     // Test has an included category ...
                     return true;
                 }
             }
         }
         // Test has no included category ...
         return false;
     }
    private List<Class<?>> categories(Description description) {
        ArrayList<Class<?>> categories = new ArrayList<Class<?>>();
        categories.addAll(Arrays.asList(directCategories(description)));
        categories.addAll(Arrays.asList(directCategories(parentDescription(description))));
        return categories;
    }
    private Description parentDescription(Description description) {
        Class<?> testClass = description.getTestClass();
        if (testClass == null) {
            return null;
        }
        return Description.createSuiteDescription(testClass);
    }
    private Class<?>[] directCategories(Description description) {
        if (description == null) {
            return new Class<?>[0];
        }
        Category annotation = description.getAnnotation(Category.class);
        if (annotation == null) {
            return new Class<?>[0];
        }
        return annotation.value();
    }
New to GrepCode? Check out our FAQ X