-
Notifications
You must be signed in to change notification settings - Fork 40
Tutorial 3. Simple Bulk Validation, Multiple Packages
So suppose you have your classes under one top level and you'd like to write one test to test them all.
public class MultiPackageTest {
// The top level package for all classes to be tested
private String packageName = "com.mycompany";
// [...]
@Before
public void setup() {
// Get all classes recursively under package
pojoClasses = PojoClassFactory.getPojoClassesRecursively(packageName, null);
// [...]
}
@Test
public void validate() {
for (PojoClass pojoClass : pojoClasses)
pojoValidator.runValidation(pojoClass);
}
}
The above call will return all classes recursively under the given packageName.
If you don't want all classes, say you don't want your package-info interfaces, you can do:
// Filter out the package-info.java classes //
PojoClassFactory.getPojoClassesRecursively(packageName,
new FilterPackageInfo());
This will skip all package-info classes because the second parameter in getPojoClassesRecursively is the filtering parameter.
This filter enables you to get all classes that implement or extend a given class.
For Example, get all classes that implement or extend public interface Persistable
PojoClassFactory.getPojoClassesRecursively(packageName,
new FilterBasedOnInheritance(Persistable.class));
**Note: ** This will skip the Persistable interface itself.
This filter enables you to get all classes based on a regular expression in their name.
For Example, get all classes that have end with "Test" in their name
This filter enables you to get all classes that are concrete, (i.e. not Interface, abstract or Enum)
PojoClassFactory.getPojoClassesRecursively(packageName,
new FilterNonConcrete());
This filter enables you to get all classes that are not synthetic.
Synthetic classes are classes created in your code by the compiler.
PojoClassFactory.getPojoClassesRecursively(packageName,
new FilterSyntheticClasses());
If you have an anonymous inner class, Java will create a synthetic nested class for your class which will be marked as synthetic.
for example:
Runnable myrunnable = new Runnable() {
public void run() {
// do something
}
}
PojoClassFactory.getPojoClassesRecursively(packageName,
new FilterNestedClasses());
Sometimes you want to combine more than one filter in one, FilterChain enables exactly that.
FilterChain filterSyntheticAndNonConcrete =
new FilterChain(new FilterSyntheticClasses(), new FilterNonConcrete());
PojoClassFactory.getPojoClassesRecursively(packageName, filterSyntheticAndNonConcrete);