Skip to content

Commit

Permalink
Issue #19: Module Component
Browse files Browse the repository at this point in the history
  • Loading branch information
Luolc committed Jun 28, 2017
1 parent 567d5b7 commit a26ba49
Show file tree
Hide file tree
Showing 6 changed files with 484 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,9 @@
<lineRate>0</lineRate>
</regex>
<regex>
<pattern>com.github.checkstyle.regression.data.ModuleInfo</pattern>
<branchRate>0</branchRate>
<lineRate>0</lineRate>
<pattern>com.github.checkstyle.regression.module.ModuleUtils</pattern>
<branchRate>33</branchRate>
<lineRate>57</lineRate>
</regex>
</regexes>
</check>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
////////////////////////////////////////////////////////////////////////////////
// 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.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.github.checkstyle.regression.data.GitChange;
import com.github.checkstyle.regression.data.ModifiableModuleInfo;
import com.github.checkstyle.regression.data.ModuleExtractInfo;
import com.github.checkstyle.regression.data.ModuleInfo;

/**
* Collects all the necessary information for the generation, in module level.
* @author LuoLiangchen
*/
public final class ModuleCollector {
/** Prevents instantiation. */
private ModuleCollector() {
}

/**
* Generates the module information from a list of changes.
* @param changes the changes source
* @return the module information generated from the given changes
*/
public static List<ModuleInfo> generate(List<GitChange> changes) {
final Map<String, ModifiableModuleInfo> moduleInfos = new LinkedHashMap<>();
for (GitChange change : changes) {
if (ModuleUtils.isCheckstyleModule(change)) {
final ModuleExtractInfo extractInfo =
ModuleUtils.convertModuleChangeToExtractInfo(change);
final ModifiableModuleInfo moduleInfo = ModifiableModuleInfo.create()
.setModuleExtractInfo(extractInfo);
moduleInfos.put(extractInfo.fullName(), moduleInfo);
}
}

return moduleInfos.values().stream()
.map(ModifiableModuleInfo::toImmutable)
.collect(Collectors.toList());
}
}
172 changes: 172 additions & 0 deletions src/main/java/com/github/checkstyle/regression/module/ModuleUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
////////////////////////////////////////////////////////////////////////////////
// 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.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import com.github.checkstyle.regression.data.GitChange;
import com.github.checkstyle.regression.data.ModuleExtractInfo;

/**
* Contains utility methods related to checkstyle module.
* @author LuoLiangchen
*/
public final class ModuleUtils {
/** The compiled regex pattern of the path of Java source files. */
private static final Pattern JAVA_SOURCE_PARTTEN =
Pattern.compile("src/(main|test)/java/(.+)\\.java");

/** The postfix of a test of a checkstyle module. */
private static final String TEST_POSTFIX = "Test";

/** The map of full qualified name to module extract info. */
private static final Map<String, ModuleExtractInfo> NAME_TO_MODULE_EXTRAT_INFO =
new HashMap<>();

/** Prevents instantiation. */
private ModuleUtils() {
}

/**
* Sets the map of full qualified name to module extract info with the given map.
* @param map the given map
*/
public static void setNameToModuleExtratInfo(Map<String, ModuleExtractInfo> map) {
NAME_TO_MODULE_EXTRAT_INFO.clear();
NAME_TO_MODULE_EXTRAT_INFO.putAll(map);
}

/**
* Checks whether the corresponding file of a change may be considered as
* a checkstyle module.
* @param change change to check
* @return true if the corresponding file of a change may be considered as
* a checkstyle module
*/
public static boolean isCheckstyleModule(GitChange change) {
final boolean returnValue;
if (isJavaMainSource(change)) {
final String fullName = convertJavaSourcePathToFullName(change.getPath());
returnValue = NAME_TO_MODULE_EXTRAT_INFO.containsKey(fullName);
}
else {
returnValue = false;
}
return returnValue;
}

/**
* Checks whether the corresponding file of a change may be considered as
* a checkstyle utility class.
* @param change change to check
* @return true if the corresponding file of a change may be considered as
* a checkstyle utility class.
*/
public static boolean isCheckstyleUtility(GitChange change) {
final boolean returnValue;
if (isJavaMainSource(change)) {
final String fullName = convertJavaSourcePathToFullName(change.getPath());
returnValue = !NAME_TO_MODULE_EXTRAT_INFO.containsKey(fullName);
}
else {
returnValue = false;
}
return returnValue;
}

/**
* Checks whether the corresponding file of a change may be considered as
* a Java main source file.
* @param change change to check
* @return true if the corresponding file of a change may be considered as
* a Java main source file.
*/
private static boolean isJavaMainSource(GitChange change) {
final boolean returnValue;
final Matcher matcher = JAVA_SOURCE_PARTTEN.matcher(change.getPath());
if (matcher.find()) {
returnValue = "main".equals(matcher.group(1));
}
else {
returnValue = false;
}
return returnValue;
}

/**
* Checks whether the corresponding file of a change may be considered as
* a test of checkstyle module.
* @param change change to check
* @return true if the corresponding file of a change may be considered as
* a test of checkstyle module
*/
public static boolean isCheckstyleModuleTest(GitChange change) {
final boolean returnValue;
if (JAVA_SOURCE_PARTTEN.matcher(change.getPath()).find()) {
final String fullName = convertJavaSourcePathToFullName(change.getPath());
if (fullName.endsWith(TEST_POSTFIX)) {
returnValue = NAME_TO_MODULE_EXTRAT_INFO.containsKey(
fullName.substring(0, fullName.length() - TEST_POSTFIX.length()));
}
else {
returnValue = false;
}
}
else {
returnValue = false;
}
return returnValue;
}

/**
* Converts a {@link GitChange} of checkstyle module to a {@link ModuleExtractInfo}.
* @param change the change to convert
* @return the module info converted from the given change
*/
public static ModuleExtractInfo convertModuleChangeToExtractInfo(GitChange change) {
final String fullName = convertJavaSourcePathToFullName(change.getPath());
return NAME_TO_MODULE_EXTRAT_INFO.get(fullName);
}

/**
* Converts a {@link GitChange} of test to the full name of corresponding checkstyle module.
* @param change the change to convert
* @return the full name of corresponding checkstyle module
*/
public static String convertModuleTestChangeToModuleFullName(GitChange change) {
final String testName = convertJavaSourcePathToFullName(change.getPath());
return testName.substring(0, testName.length() - TEST_POSTFIX.length());
}

/**
* Converts a path of Java source file to its full qualified name.
* @param path the path of Java source file
* @return the corresponding full qualified name
*/
private static String convertJavaSourcePathToFullName(String path) {
return Arrays.stream(JAVA_SOURCE_PARTTEN.matcher(path).replaceAll("$2").split("/"))
.collect(Collectors.joining("."));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
////////////////////////////////////////////////////////////////////////////////
// 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
////////////////////////////////////////////////////////////////////////////////

/**
* Contains the module information generation and utility classes.
*/
package com.github.checkstyle.regression.module;
Loading

0 comments on commit a26ba49

Please sign in to comment.