-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #78: extract: grab property info
- Loading branch information
Showing
15 changed files
with
804 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
src/main/java/com/github/checkstyle/regression/module/UnitTestProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// checkstyle: Checks Java source code for adherence to a set of rules. | ||
// Copyright (C) 2001-2017 the original author or authors. | ||
// | ||
// This library is free software; you can redistribute it and/or | ||
// modify it under the terms of the GNU Lesser General Public | ||
// License as published by the Free Software Foundation; either | ||
// version 2.1 of the License, or (at your option) any later version. | ||
// | ||
// This library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public | ||
// License along with this library; if not, write to the Free Software | ||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
package com.github.checkstyle.regression.module; | ||
|
||
import java.io.File; | ||
import java.lang.reflect.Field; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import com.github.checkstyle.regression.data.ModuleInfo; | ||
import com.puppycrawl.tools.checkstyle.Checker; | ||
import com.puppycrawl.tools.checkstyle.DefaultConfiguration; | ||
import com.puppycrawl.tools.checkstyle.TreeWalker; | ||
import com.puppycrawl.tools.checkstyle.api.AbstractCheck; | ||
import com.puppycrawl.tools.checkstyle.api.CheckstyleException; | ||
import com.puppycrawl.tools.checkstyle.api.Configuration; | ||
import com.puppycrawl.tools.checkstyle.api.FileSetCheck; | ||
|
||
/** | ||
* Processes the unit test class of a checkstyle module. | ||
* This utility class would run {@link UnitTestProcessorCheck} on the file with | ||
* the given path and return the collected property info. | ||
* @author LuoLiangchen | ||
*/ | ||
public final class UnitTestProcessor { | ||
/** Prevents instantiation. */ | ||
private UnitTestProcessor() { | ||
} | ||
|
||
/** | ||
* Processes the unit test class with the given path. | ||
* @param path the path of the unit test class | ||
* @return the unit test method name to properties map | ||
* @throws CheckstyleException failure when running the check | ||
* @throws ReflectiveOperationException failure of reflection on checker and tree walker | ||
*/ | ||
public static Map<String, Set<ModuleInfo.Property>> process(String path) | ||
throws CheckstyleException, ReflectiveOperationException { | ||
final DefaultConfiguration moduleConfig = createModuleConfig(UnitTestProcessorCheck.class); | ||
final Configuration dc = createTreeWalkerConfig(moduleConfig); | ||
final Checker checker = new Checker(); | ||
checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader()); | ||
checker.configure(dc); | ||
final List<File> processedFiles = Collections.singletonList(new File(path)); | ||
checker.process(processedFiles); | ||
|
||
final UnitTestProcessorCheck check = getCheckInstance(checker); | ||
return check.getUnitTestToPropertiesMap(); | ||
} | ||
|
||
/** | ||
* Gets the instance of {@link UnitTestProcessorCheck} from the checker. | ||
* @param checker the checker which run the check | ||
* @return the instance of {@link UnitTestProcessorCheck} | ||
* @throws ReflectiveOperationException failure of reflection | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
private static UnitTestProcessorCheck getCheckInstance(Checker checker) | ||
throws ReflectiveOperationException { | ||
final Field fileSetChecks = checker.getClass().getDeclaredField("fileSetChecks"); | ||
fileSetChecks.setAccessible(true); | ||
final TreeWalker treeWalker = | ||
(TreeWalker) ((List<FileSetCheck>) fileSetChecks.get(checker)).get(0); | ||
final Field ordinaryChecks = treeWalker.getClass().getDeclaredField("ordinaryChecks"); | ||
ordinaryChecks.setAccessible(true); | ||
final Set<AbstractCheck> checks = (Set<AbstractCheck>) ordinaryChecks.get(treeWalker); | ||
return (UnitTestProcessorCheck) checks.iterator().next(); | ||
} | ||
|
||
/** | ||
* Creates {@link DefaultConfiguration} for the {@link TreeWalker} | ||
* based on the given {@link Configuration} instance. | ||
* @param config {@link Configuration} instance. | ||
* @return {@link DefaultConfiguration} for the {@link TreeWalker} | ||
* based on the given {@link Configuration} instance. | ||
*/ | ||
private static DefaultConfiguration createTreeWalkerConfig(Configuration config) { | ||
final DefaultConfiguration dc = | ||
new DefaultConfiguration("configuration"); | ||
final DefaultConfiguration twConf = createModuleConfig(TreeWalker.class); | ||
// make sure that the tests always run with this charset | ||
dc.addAttribute("charset", "UTF-8"); | ||
dc.addChild(twConf); | ||
twConf.addChild(config); | ||
return dc; | ||
} | ||
|
||
/** | ||
* Creates {@link DefaultConfiguration} for the given class. | ||
* @param clazz the class of module | ||
* @return the {@link DefaultConfiguration} of the module class | ||
*/ | ||
private static DefaultConfiguration createModuleConfig(Class<?> clazz) { | ||
return new DefaultConfiguration(clazz.getName()); | ||
} | ||
} |
Oops, something went wrong.