Skip to content

Tutorial 3. Simple Bulk Validation, Multiple Packages

Osman Shoukry edited this page Jul 4, 2017 · 8 revisions

All my Pojos are under the package top level com.mycompany.*

So suppose you have your classes under one top level and you'd like to write one test to test them all.

Enumerate all classes given a package name

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() {
    // [...]
  }
}

The above call will return all classes recursively under the given packageName.


What if I don't want every class?

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.

What other filters can I use?

1. FilterBasedOnInheritance

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.

2. FilterClassName

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

  PojoClassFactory.getPojoClassesRecursively(packageName,
        new FilterClassName("\\w*Test\\w*$"));

3. FilterEnum

This filter enables you to get all classes that are not of type Enum.

  PojoClassFactory.getPojoClassesRecursively(packageName,
        new FilterEnum());

4. FilterNonConcrete

This filter enables you to get all classes that are concrete, (i.e. not Interface, abstract or Enum)

  PojoClassFactory.getPojoClassesRecursively(packageName,
        new FilterNonConcrete());

5. FilterSyntheticClasses

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());
How are synthetic classes created?

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
    }
  }

6. FilterNestedClasses

  PojoClassFactory.getPojoClassesRecursively(packageName,
        new FilterNestedClasses());

7. FilterChain

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);

I don't like those filters, I want to use my own

Sure, just implement PojoClassFilter and write your logic in the include method.

For example, include only final classes (i.e. filter out any non-final classes):

public class FilterNonFinalClasses implements PojoClassFilter {

  // true includes this class, false excludes it.
  public boolean include(final PojoClass pojoClass) {
    return pojoClass.isFinal();
  }
If you have implemented a cool filter and would like to have it added to the library open an issue and let me know, or better yet, send me a pull request.