001package org.junit.experimental.runners; 002 003import java.lang.reflect.Modifier; 004import java.util.ArrayList; 005import java.util.List; 006 007import org.junit.runners.Suite; 008import org.junit.runners.model.RunnerBuilder; 009 010/** 011 * If you put tests in inner classes, Ant, for example, won't find them. By running the outer class 012 * with Enclosed, the tests in the inner classes will be run. You might put tests in inner classes 013 * to group them for convenience or to share constants. Abstract inner classes are ignored. 014 * 015 * So, for example: 016 * <pre> 017 * \@RunWith(Enclosed.class) 018 * public class ListTests { 019 * ...useful shared stuff... 020 * public static class OneKindOfListTest {...} 021 * public static class AnotherKind {...} 022 * abstract public static class Ignored {...} 023 * } 024 * </pre> 025 * 026 * For a real example, @see org.junit.tests.manipulation.SortableTest. 027 * 028 */ 029public class Enclosed extends Suite { 030 /** 031 * Only called reflectively. Do not use programmatically. 032 */ 033 public Enclosed(Class<?> klass, RunnerBuilder builder) throws Throwable { 034 super(builder, klass, filterAbstractClasses(klass.getClasses())); 035 } 036 037 private static Class<?>[] filterAbstractClasses(final Class<?>[] classes) { 038 final List<Class<?>> filteredList= new ArrayList<Class<?>>(classes.length); 039 040 for (final Class<?> clazz : classes) { 041 if (!Modifier.isAbstract(clazz.getModifiers())) { 042 filteredList.add(clazz); 043 } 044 } 045 046 return filteredList.toArray(new Class<?>[filteredList.size()]); 047 } 048}