package org.infinispan.checkstyle.filters;

import java.io.File;

import com.puppycrawl.tools.checkstyle.api.AuditEvent;
import com.puppycrawl.tools.checkstyle.api.Filter;

/**
 * Allows disabling some rules for the test suite source.
 *
 * Any violation will be suppressed if it's generated by a source file having {@code /src/test/java}
 * included in its path and if the violation message contains the keyword
 * "[not required for tests]".
 * <p>
 * A SuppressionFilter is too generic, and requires per-module configuration.
 *
 * @author Sanne Grinovero &lt;sanne@hibernate.org&gt;
 */
public class ExcludeTestPackages implements Filter {

   private static final String SUB_PATH = File.separator + "src" + File.separator + "test" + File.separator + "java";
   private static final String MESSAGE_DISABLE_KEYWORD = "[not required for tests]";

   @Override
   public boolean accept(AuditEvent aEvent) {
      final String fileName = aEvent.getFileName();
      if (fileName != null && fileName.contains(SUB_PATH)) {
         return acceptTestfileEvent(aEvent);
      }
      return true;
   }

   private boolean acceptTestfileEvent(AuditEvent aEvent) {
      final String message = aEvent.getMessage();
      if (message != null && message.contains(MESSAGE_DISABLE_KEYWORD)) {
         return false;
      }
      return true;
   }
}
