Condition.java

  1. package com.github.dakusui.jcunitx.runners.junit4.annotations;

  2. import org.junit.runners.model.FrameworkMethod;
  3. import org.junit.validator.AnnotationValidator;
  4. import org.junit.validator.ValidateWith;

  5. import java.lang.annotation.ElementType;
  6. import java.lang.annotation.Retention;
  7. import java.lang.annotation.RetentionPolicy;
  8. import java.lang.annotation.Target;
  9. import java.util.LinkedList;
  10. import java.util.List;

  11. @Retention(RetentionPolicy.RUNTIME)
  12. @Target(ElementType.METHOD)
  13. @ValidateWith(Condition.Validator.class)
  14. public @interface Condition {
  15.   /**
  16.    * Conditions cannot be overloaded.
  17.    */

  18.   class Validator extends AnnotationValidator {
  19.     /**
  20.      * Validates a precondition method.
  21.      * A precondition method is a method annotated with {@literal @}{@code Precondition} or referred to by an annotation {@literal @}Given.
  22.      * It is mainly used to determine if a test method (or methods) should be executed in advance.
  23.      * <p>
  24.      * It must be public, non-static, returning a boolean value, and must have no parameter.
  25.      * In case it is not valid, this method add a string message which describes the failure to {@code errors} list.
  26.      */
  27.     @Override
  28.     public List<Exception> validateAnnotatedMethod(FrameworkMethod method) {
  29.       List<Exception> errors = new LinkedList<Exception>();
  30.       Class testClass = method.getDeclaringClass();
  31.       if (!method.isPublic()) {
  32.         errors.add(new Exception(String.format(
  33.             "The method '%s' must be public. (in %s)", method.getName(), testClass.getCanonicalName()
  34.         )));
  35.       }
  36.       if (method.isStatic()) {
  37.         errors.add(new Exception(String.format(
  38.             "The method '%s' must not be static. (in %s)", method.getName(), testClass.getCanonicalName()
  39.         )));
  40.       }
  41.       if (!Boolean.TYPE.equals(method.getReturnType())) {
  42.         errors.add(new Exception(String.format(
  43.             "The method '%s' must return a boolean value, but '%s' is returned. (in %s)",
  44.             method.getName(),
  45.             method.getReturnType().getName(),
  46.             testClass.getCanonicalName()
  47.         )));
  48.       }
  49.       // TODO
  50.       /*
  51.       Class<?>[] parameterTypes = method.getMethod().getParameterTypes();
  52.       if (parameterTypes.length != 0) {
  53.         errors.add(new Exception(String.format(
  54.             "The method '%s' must not have any parameter. (in %s)",
  55.             method.getName(),
  56.             testClass.getCanonicalName()
  57.         )));
  58.       }
  59.       */
  60.       /*
  61.       if (method.getAnnotation(Condition.class) != null && method.getAnnotation(Condition.class).constraint()) {
  62.         if (method.getDeclaringClass().getAnnotation(GenerateCoveringArrayWith.class) == null
  63.             || method.getDeclaringClass().getAnnotation(GenerateCoveringArrayWith.class).checker() == null
  64.             || !SmartConstraintCheckerImpl.class.isAssignableFrom(method.getDeclaringClass().getAnnotation(GenerateCoveringArrayWith.class).checker().value())) {
  65.           errors.add(new Exception(String.format(
  66.               "'constraint' attribute of @Condition is set to true for %s#%s, but SmartConstraintChecker isn't present.",
  67.               method.getDeclaringClass().getCanonicalName(),
  68.               method.getName()
  69.               )));
  70.         }
  71.       }
  72.       */
  73.       return errors;
  74.     }
  75.   }

  76.   /**
  77.    * Returns if this condition is a constraint or not
  78.    *
  79.    * @return tells if this condition is a constraint or not.
  80.    */
  81.   boolean constraint() default false;
  82. }