View Javadoc
1   /**
2    * Powerunit - A JDK1.8 test framework
3    * Copyright (C) 2014 Mathieu Boretti.
4    *
5    * This file is part of Powerunit
6    *
7    * Powerunit is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation, either version 3 of the License, or
10   * (at your option) any later version.
11   *
12   * Powerunit is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with Powerunit. If not, see <http://www.gnu.org/licenses/>.
19   */
20  package ch.powerunit.impl.validator;
21  
22  import javax.annotation.processing.RoundEnvironment;
23  import javax.lang.model.element.Element;
24  import javax.lang.model.element.ElementKind;
25  import javax.lang.model.element.ExecutableElement;
26  import javax.lang.model.element.Modifier;
27  import javax.lang.model.type.TypeKind;
28  
29  import ch.powerunit.Test;
30  
31  public interface TestProcessorValidator extends ProcessValidator {
32      default void testAnnotationValidation(RoundEnvironment roundEnv) {
33          roundEnv.getElementsAnnotatedWith(Test.class).forEach(
34                  this::testOneAnnotationValidation);
35      }
36  
37      default void testOneAnnotationValidation(Element element) {
38          if (element.getKind() != ElementKind.METHOD) {
39              error("@Test must prefix a method -- " + element
40                      + " is not a method");
41              return;
42          }
43          ExecutableElement ee = (ExecutableElement) element;
44          if (ee.getModifiers().contains(Modifier.STATIC)) {
45              warn("Method "
46                      + elementAsString(ee)
47                      + "\n\tis prefixed with @Test and is static\n\tA test method can't be static");
48          }
49          if (!ee.getModifiers().contains(Modifier.PUBLIC)) {
50              warn("Method "
51                      + elementAsString(ee)
52                      + "\n\tis prefixed with @Test and is not public \n\tA test method must be public");
53          }
54          if (!TypeKind.VOID.equals(ee.getReturnType().getKind())) {
55              warn("Method "
56                      + elementAsString(ee)
57                      + "\n\tis prefixed with @Test and is not void\n\tA test method must be void");
58          }
59          if (!ee.getParameters().isEmpty()) {
60              warn("Method"
61                      + elementAsString(ee)
62                      + "\n\tis prefixed with @Test and is not 0-args\n\tA test method must be 0-args");
63          }
64      }
65  }