|
| 1 | +//////////////////////////////////////////////////////////////////////////////// |
| 2 | +// checkstyle: Checks Java source code for adherence to a set of rules. |
| 3 | +// Copyright (C) 2001-2017 the original author or authors. |
| 4 | +// |
| 5 | +// This library is free software; you can redistribute it and/or |
| 6 | +// modify it under the terms of the GNU Lesser General Public |
| 7 | +// License as published by the Free Software Foundation; either |
| 8 | +// version 2.1 of the License, or (at your option) any later version. |
| 9 | +// |
| 10 | +// This library is distributed in the hope that it will be useful, |
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | +// Lesser General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU Lesser General Public |
| 16 | +// License along with this library; if not, write to the Free Software |
| 17 | +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | +//////////////////////////////////////////////////////////////////////////////// |
| 19 | + |
| 20 | +package com.github.checkstyle.regression.module; |
| 21 | + |
| 22 | +import java.io.File; |
| 23 | +import java.lang.reflect.Field; |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.Set; |
| 28 | + |
| 29 | +import com.github.checkstyle.regression.data.ModuleInfo; |
| 30 | +import com.puppycrawl.tools.checkstyle.Checker; |
| 31 | +import com.puppycrawl.tools.checkstyle.DefaultConfiguration; |
| 32 | +import com.puppycrawl.tools.checkstyle.TreeWalker; |
| 33 | +import com.puppycrawl.tools.checkstyle.api.AbstractCheck; |
| 34 | +import com.puppycrawl.tools.checkstyle.api.CheckstyleException; |
| 35 | +import com.puppycrawl.tools.checkstyle.api.Configuration; |
| 36 | +import com.puppycrawl.tools.checkstyle.api.FileSetCheck; |
| 37 | + |
| 38 | +/** |
| 39 | + * Processes the unit test class of a checkstyle module. |
| 40 | + * This utility class would run {@link UnitTestProcessorCheck} on the file with |
| 41 | + * the given path and return the collected property info. |
| 42 | + * @author LuoLiangchen |
| 43 | + */ |
| 44 | +public final class UnitTestProcessor { |
| 45 | + /** Prevents instantiation. */ |
| 46 | + private UnitTestProcessor() { |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Processes the unit test class with the given path. |
| 51 | + * @param path the path of the unit test class |
| 52 | + * @return the unit test method name to properties map |
| 53 | + * @throws CheckstyleException failure when running the check |
| 54 | + * @throws ReflectiveOperationException failure of reflection on checker and tree walker |
| 55 | + */ |
| 56 | + public static Map<String, Set<ModuleInfo.Property>> process(String path) |
| 57 | + throws CheckstyleException, ReflectiveOperationException { |
| 58 | + final DefaultConfiguration moduleConfig = createModuleConfig(UnitTestProcessorCheck.class); |
| 59 | + final Configuration dc = createTreeWalkerConfig(moduleConfig); |
| 60 | + final Checker checker = new Checker(); |
| 61 | + checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader()); |
| 62 | + checker.configure(dc); |
| 63 | + final List<File> processedFiles = Collections.singletonList(new File(path)); |
| 64 | + checker.process(processedFiles); |
| 65 | + |
| 66 | + final UnitTestProcessorCheck check = getCheckInstance(checker); |
| 67 | + return check.getUnitTestToPropertiesMap(); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Gets the instance of {@link UnitTestProcessorCheck} from the checker. |
| 72 | + * @param checker the checker which run the check |
| 73 | + * @return the instance of {@link UnitTestProcessorCheck} |
| 74 | + * @throws ReflectiveOperationException failure of reflection |
| 75 | + */ |
| 76 | + @SuppressWarnings("unchecked") |
| 77 | + private static UnitTestProcessorCheck getCheckInstance(Checker checker) |
| 78 | + throws ReflectiveOperationException { |
| 79 | + final Field fileSetChecks = checker.getClass().getDeclaredField("fileSetChecks"); |
| 80 | + fileSetChecks.setAccessible(true); |
| 81 | + final TreeWalker treeWalker = |
| 82 | + (TreeWalker) ((List<FileSetCheck>) fileSetChecks.get(checker)).get(0); |
| 83 | + final Field ordinaryChecks = treeWalker.getClass().getDeclaredField("ordinaryChecks"); |
| 84 | + ordinaryChecks.setAccessible(true); |
| 85 | + final Set<AbstractCheck> checks = (Set<AbstractCheck>) ordinaryChecks.get(treeWalker); |
| 86 | + return (UnitTestProcessorCheck) checks.iterator().next(); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Creates {@link DefaultConfiguration} for the {@link TreeWalker} |
| 91 | + * based on the given {@link Configuration} instance. |
| 92 | + * @param config {@link Configuration} instance. |
| 93 | + * @return {@link DefaultConfiguration} for the {@link TreeWalker} |
| 94 | + * based on the given {@link Configuration} instance. |
| 95 | + */ |
| 96 | + private static DefaultConfiguration createTreeWalkerConfig(Configuration config) { |
| 97 | + final DefaultConfiguration dc = |
| 98 | + new DefaultConfiguration("configuration"); |
| 99 | + final DefaultConfiguration twConf = createModuleConfig(TreeWalker.class); |
| 100 | + // make sure that the tests always run with this charset |
| 101 | + dc.addAttribute("charset", "UTF-8"); |
| 102 | + dc.addChild(twConf); |
| 103 | + twConf.addChild(config); |
| 104 | + return dc; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Creates {@link DefaultConfiguration} for the given class. |
| 109 | + * @param clazz the class of module |
| 110 | + * @return the {@link DefaultConfiguration} of the module class |
| 111 | + */ |
| 112 | + private static DefaultConfiguration createModuleConfig(Class<?> clazz) { |
| 113 | + return new DefaultConfiguration(clazz.getName()); |
| 114 | + } |
| 115 | +} |
0 commit comments