Skip to content

Tutorial 3. Simple Bulk Validation, Multiple Packages

Osman Shoukry edited this page Apr 12, 2015 · 8 revisions

Tutorial 3. Simple Bulk Validation, Multiple Package

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() {
    for (PojoClass pojoClass : pojoClasses)
      pojoValidator.runValidation(pojoClass);
  }
}

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

``` Java PojoClassFactory.getPojoClassesRecursively(packageName, new FilterClassName("\\w*Test\\w*$")); ``` #### 3. FilterEnum This filter enables you to get all classes that are _not_ of type Enum. ``` Java 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);