|
| 1 | +/* |
| 2 | + * Copyright 2017-2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.spring.javaformat.checkstyle.check; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.LinkedHashMap; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.stream.Collectors; |
| 26 | + |
| 27 | +import com.puppycrawl.tools.checkstyle.api.DetailAST; |
| 28 | +import com.puppycrawl.tools.checkstyle.api.FullIdent; |
| 29 | +import com.puppycrawl.tools.checkstyle.api.TokenTypes; |
| 30 | +import com.puppycrawl.tools.checkstyle.utils.AnnotationUtil; |
| 31 | + |
| 32 | +/** |
| 33 | + * Checks that JUnit 5 conventions are followed and that JUnit 4 is not accidentally used. |
| 34 | + * |
| 35 | + * @author Phillip Webb |
| 36 | + */ |
| 37 | +public class SpringJUnit5Check extends AbstractSpringCheck { |
| 38 | + |
| 39 | + private static final String JUNIT4_TEST_ANNOTATION = "org.junit.Test"; |
| 40 | + |
| 41 | + private static final String JUNIT5_TEST_ANNOTATION = "org.junit.jupiter.api.Test"; |
| 42 | + |
| 43 | + private static final List<String> TEST_ANNOTATIONS = Collections |
| 44 | + .unmodifiableList(Arrays.asList("Test", JUNIT4_TEST_ANNOTATION, JUNIT5_TEST_ANNOTATION)); |
| 45 | + |
| 46 | + private static final List<String> BANNED_IMPORTS; |
| 47 | + static { |
| 48 | + List<String> bannedImports = new ArrayList<>(); |
| 49 | + bannedImports.add(JUNIT4_TEST_ANNOTATION); |
| 50 | + bannedImports.add("org.junit.After"); |
| 51 | + bannedImports.add("org.junit.AfterClass"); |
| 52 | + bannedImports.add("org.junit.Before"); |
| 53 | + bannedImports.add("org.junit.BeforeClass"); |
| 54 | + bannedImports.add("org.junit.Rule"); |
| 55 | + bannedImports.add("org.junit.ClassRule"); |
| 56 | + BANNED_IMPORTS = Collections.unmodifiableList(bannedImports); |
| 57 | + } |
| 58 | + |
| 59 | + private List<String> unlessImports = new ArrayList<>(); |
| 60 | + |
| 61 | + private final List<DetailAST> testMethods = new ArrayList<>(); |
| 62 | + |
| 63 | + private final Map<String, FullIdent> imports = new LinkedHashMap<>(); |
| 64 | + |
| 65 | + @Override |
| 66 | + public int[] getAcceptableTokens() { |
| 67 | + return new int[] { TokenTypes.METHOD_DEF, TokenTypes.IMPORT }; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void beginTree(DetailAST rootAST) { |
| 72 | + this.testMethods.clear(); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void visitToken(DetailAST ast) { |
| 77 | + switch (ast.getType()) { |
| 78 | + case TokenTypes.METHOD_DEF: |
| 79 | + visitMethodDef(ast); |
| 80 | + case TokenTypes.IMPORT: |
| 81 | + visitImport(ast); |
| 82 | + break; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private void visitMethodDef(DetailAST ast) { |
| 87 | + if (AnnotationUtil.containsAnnotation(ast, TEST_ANNOTATIONS)) { |
| 88 | + this.testMethods.add(ast); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private void visitImport(DetailAST ast) { |
| 93 | + FullIdent ident = FullIdent.createFullIdentBelow(ast); |
| 94 | + this.imports.put(ident.getText(), ident); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void finishTree(DetailAST rootAST) { |
| 99 | + if (shouldCheck()) { |
| 100 | + check(); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private boolean shouldCheck() { |
| 105 | + if (this.testMethods.isEmpty()) { |
| 106 | + return false; |
| 107 | + } |
| 108 | + for (String unlessImport : this.unlessImports) { |
| 109 | + if (this.imports.containsKey(unlessImport)) { |
| 110 | + return false; |
| 111 | + } |
| 112 | + } |
| 113 | + return true; |
| 114 | + } |
| 115 | + |
| 116 | + private void check() { |
| 117 | + for (String bannedImport : BANNED_IMPORTS) { |
| 118 | + FullIdent ident = this.imports.get(bannedImport); |
| 119 | + if (ident != null) { |
| 120 | + log(ident.getLineNo(), ident.getColumnNo(), "junit5.bannedImport", bannedImport); |
| 121 | + } |
| 122 | + } |
| 123 | + for (DetailAST testMethod : this.testMethods) { |
| 124 | + if (AnnotationUtil.containsAnnotation(testMethod, JUNIT4_TEST_ANNOTATION)) { |
| 125 | + log(testMethod, "junit5.bannedTestAnnotation"); |
| 126 | + } |
| 127 | + } |
| 128 | + for (DetailAST testMethod : this.testMethods) { |
| 129 | + DetailAST modifiers = testMethod.findFirstToken(TokenTypes.MODIFIERS); |
| 130 | + if (modifiers.findFirstToken(TokenTypes.LITERAL_PUBLIC) != null) { |
| 131 | + log(testMethod, "junit5.publicMethod"); |
| 132 | + } |
| 133 | + if (modifiers.findFirstToken(TokenTypes.LITERAL_PRIVATE) != null) { |
| 134 | + log(testMethod, "junit5.privateMethod"); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + private void log(DetailAST method, String key) { |
| 140 | + String name = method.findFirstToken(TokenTypes.IDENT).getText(); |
| 141 | + log(method.getLineNo(), method.getColumnNo(), key, name); |
| 142 | + } |
| 143 | + |
| 144 | + public void setUnlessImports(String unlessImports) { |
| 145 | + this.unlessImports = Collections.unmodifiableList( |
| 146 | + Arrays.stream(unlessImports.split(",")).map(String::trim).collect(Collectors.toList())); |
| 147 | + } |
| 148 | + |
| 149 | +} |
0 commit comments