Skip to content

Commit

Permalink
Issue checkstyle#37: inject, invoke maven command, read file and clea…
Browse files Browse the repository at this point in the history
…n up
  • Loading branch information
Luolc committed Jul 10, 2017
1 parent f924b17 commit 639e730
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 14 deletions.
5 changes: 4 additions & 1 deletion config/findbugs-exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
<Match>
<!-- This is a false positive. It is no problem to do that cast. In fact the cast is
in JGit library. There is no reason to raise this warning. -->
<Class name="com.github.checkstyle.regression.git.DiffParser"/>
<Or>
<Class name="com.github.checkstyle.regression.git.DiffParser"/>
<Class name="com.github.checkstyle.regression.extract.CheckstyleInjector"/>
</Or>
<Or>
<Bug pattern="BC_UNCONFIRMED_CAST"/>
<Bug pattern="BC_UNCONFIRMED_CAST_OF_RETURN_VALUE"/>
Expand Down
5 changes: 4 additions & 1 deletion config/import-control.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

<import-control pkg="com.github.checkstyle.regression">
<allow pkg="java.io"/>
<allow pkg="java.nio"/>
<allow pkg="org.apache.commons.io"/>
<allow pkg="java.util"/>
<allow pkg="com.github.checkstyle.regression.data"/>
<allow pkg="org.immutables.value"/>
Expand All @@ -13,7 +15,6 @@
<allow pkg="javax.xml"/>
<allow pkg="org.w3c.dom"/>
<allow pkg="org.xml"/>
<allow pkg="java.nio"/>
</subpackage>

<subpackage name="data">
Expand All @@ -25,6 +26,8 @@
<allow pkg="com.google.gson"/>
<allow class="com.sun.tools.javac.util.ServiceLoader"/>
<allow class="java.lang.reflect.Type"/>
<!-- we need to run Git checkout command. -->
<allow pkg="org.eclipse.jgit"/>
</subpackage>

<subpackage name="git">
Expand Down
12 changes: 11 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,17 @@
<regex>
<pattern>com.github.checkstyle.regression.configuration.ConfigGenerator</pattern>
<branchRate>80</branchRate>
<lineRate>91</lineRate>
<lineRate>92</lineRate>
</regex>
<regex>
<pattern>com.github.checkstyle.regression.extract.ExtractInfoProcessor</pattern>
<branchRate>100</branchRate>
<lineRate>66</lineRate>
</regex>
<regex>
<pattern>com.github.checkstyle.regression.extract.CheckstyleInjector</pattern>
<branchRate>0</branchRate>
<lineRate>0</lineRate>
</regex>
<regex>
<pattern>com.github.checkstyle.regression.module.ModuleUtils</pattern>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
////////////////////////////////////////////////////////////////////////////////
// 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.extract;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;

/**
* Injects files to checkstyle repository, which would be invoked by maven command
* to generate module extract info.
* @author LuoLiangchen
*/
final class CheckstyleInjector implements Closeable {
/** The path to checkstyle repository. */
private final String repoPath;

/** The name of PR branch. */
private final String branch;

/** The checkstyle repository. */
private final Repository repository;

/**
* Creates a new instance of CheckstyleInjector.
* @param repoPath the path to checkstyle repository
* @param branch the name of PR branch
*/
CheckstyleInjector(String repoPath, String branch) {
this.repoPath = repoPath;
this.branch = branch;

final File gitDir = new File(repoPath, ".git");
final Repository repo;
try {
repo = new FileRepositoryBuilder().setGitDir(gitDir)
.readEnvironment().findGitDir().build();
}
catch (IOException ex) {
throw new IllegalStateException(ex);
}
repository = repo;
}

/**
* Generates the module extract info file.
* @return the module extract info file
* @throws IOException failure of generation
* @throws GitAPIException JGit library exception
*/
public File generateExtractInfoFile() throws IOException, GitAPIException {
checkoutToPrBranch();
copyInjectFilesToCheckstyleRepo();
invokeMavenCommand();
return null;
}

/**
* Clears the injected files and the generated info file in checkstyle repository.
* @throws GitAPIException JGit library exception
*/
public void clearInjections() throws GitAPIException {
final Git git = new Git(repository);

try {
git.checkout().addPath(".").call();
}
finally {
git.close();
}
}

/** Closes the repository resource. */
@Override
public void close() {
repository.close();
}

/**
* Invokes Maven command to generate the extract info file in checkstyle repository.
*/
private static void invokeMavenCommand() {
// invoke maven command to generate the extract info file
}

/**
* Copies the injection files to checkstyle repository.
* @throws IOException failure of copying
*/
private void copyInjectFilesToCheckstyleRepo() throws IOException {
final File srcDir =
new File("src/main/resources/com/github/checkstyle/regression/extract/");
final File destDir = new File(repoPath, "src/test/java/com/puppycrawl/tools/checkstyle/");
final String[] injections = {"ExtractInfoGeneratorTest.java", "Json.java"};
for (String injection : injections) {
final File srcFile = new File(srcDir, injection);
final File destFile = new File(destDir, injection);
FileUtils.copyFile(srcFile, destFile);
}
}

/**
* Checkouts to the PR branch in the given repository.
* @throws GitAPIException JGit library exception
*/
private void checkoutToPrBranch()
throws GitAPIException {
final Git git = new Git(repository);

try {
git.checkout().setName(branch).call();
}
finally {
git.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@

package com.github.checkstyle.regression.extract;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.eclipse.jgit.api.errors.GitAPIException;

import com.github.checkstyle.regression.data.ModuleExtractInfo;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
Expand Down Expand Up @@ -57,13 +63,40 @@ private ExtractInfoProcessor() {
GSON = gsonBuilder.create();
}

/**
* Gets the module extract info map from the given branch of checkstyle repository.
* @param repoPath the path of checkstyle repository
* @param branch the given branch on which to generate the extract info
* @return the full qualified name to module extract info map
* @throws IOException failure when creating or reading the info file
* @throws GitAPIException failure when running Git command
*/
public static Map<String, ModuleExtractInfo> getModuleExtractInfos(
String repoPath, String branch) throws IOException, GitAPIException {
final Map<String, ModuleExtractInfo> returnValue;
final CheckstyleInjector injector = new CheckstyleInjector(repoPath, branch);

try {
final File file = injector.generateExtractInfoFile();
final Reader reader = new InputStreamReader(
new FileInputStream(file), Charset.forName("UTF-8"));
returnValue = getModuleExtractInfosFromReader(reader);
}
finally {
injector.clearInjections();
injector.close();
}

return returnValue;
}

/**
* Gets the module extract info map from the given reader. Map key is the
* fully qualified module name.
* @param reader the given reader
* @return the full qualified name to module extract info map
*/
public static Map<String, ModuleExtractInfo> getModuleExtractInfosFromReader(
private static Map<String, ModuleExtractInfo> getModuleExtractInfosFromReader(
Reader reader) {
final List<ModuleExtractInfo> modules;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;

import com.github.checkstyle.regression.data.ImmutableModuleExtractInfo;
Expand All @@ -39,18 +43,26 @@
public class ExtractInfoProcessorTest {
private static final String BASE_PACKAGE = "com.puppycrawl.tools.checkstyle";

private Method getModuleExtractInfosFromReaderMethod;

@Before
public void setUp() throws Exception {
getModuleExtractInfosFromReaderMethod = ExtractInfoProcessor.class
.getDeclaredMethod("getModuleExtractInfosFromReader", Reader.class);
getModuleExtractInfosFromReaderMethod.setAccessible(true);
}

@Test
public void testIsProperUtilsClass() throws Exception {
assertUtilsClassHasPrivateConstructor(ExtractInfoProcessor.class);
}

@Test
public void testGetModuleExtractInfosFromReader() {
public void testGetModuleExtractInfosFromReader() throws Exception {
final InputStream is = ExtractInfoProcessor.class.getClassLoader()
.getResourceAsStream("checkstyle_modules.json");
final InputStreamReader reader = new InputStreamReader(is, Charset.forName("UTF-8"));
final Map<String, ModuleExtractInfo> map =
ExtractInfoProcessor.getModuleExtractInfosFromReader(reader);
final Map<String, ModuleExtractInfo> map = getModuleExtractInfosFromReader(reader);

final String module1 = "NewlineAtEndOfFileCheck";
final ModuleExtractInfo extractInfo1 = ImmutableModuleExtractInfo.builder()
Expand Down Expand Up @@ -79,14 +91,21 @@ public void testGetNameToModuleInfoFromInputStreamWithException() throws Excepti
doThrow(IOException.class).when(is).close();

try {
ExtractInfoProcessor.getModuleExtractInfosFromReader(reader);
getModuleExtractInfosFromReader(reader);
fail("Exception is expected");
}
catch (IllegalStateException ex) {
catch (InvocationTargetException ex) {
assertEquals(
"Exception message is wrong",
"Failed when loaing hardcoded checkstyle module information",
ex.getMessage());
ex.getTargetException().getMessage());
}
}

@SuppressWarnings("unchecked")
private Map<String, ModuleExtractInfo> getModuleExtractInfosFromReader(Reader reader)
throws Exception {
return (Map<String, ModuleExtractInfo>) getModuleExtractInfosFromReaderMethod
.invoke(ExtractInfoProcessor.class, reader);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.LinkedList;
Expand Down Expand Up @@ -51,13 +53,17 @@ public class ModuleInfoCollectorTest {
private static final String JAVA_TEST_SOURCE_PREFIX =
"src/test/java/com/puppycrawl/tools/checkstyle/";

@SuppressWarnings("unchecked")
@Before
public void setUp() {
public void setUp() throws Exception {
final InputStream is = ExtractInfoProcessor.class.getClassLoader()
.getResourceAsStream("checkstyle_modules.json");
final InputStreamReader reader = new InputStreamReader(is, Charset.forName("UTF-8"));
final Method method = ExtractInfoProcessor.class
.getDeclaredMethod("getModuleExtractInfosFromReader", Reader.class);
method.setAccessible(true);
final Map<String, ModuleExtractInfo> map =
ExtractInfoProcessor.getModuleExtractInfosFromReader(reader);
(Map<String, ModuleExtractInfo>) method.invoke(ExtractInfoProcessor.class, reader);
ModuleUtils.setNameToModuleExtractInfo(map);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.util.Map;

Expand All @@ -44,13 +46,17 @@ public class ModuleUtilsTest {
private static final String JAVA_MAIN_SOURCE_PREFIX =
"src/main/java/com/puppycrawl/tools/checkstyle/";

@SuppressWarnings("unchecked")
@Before
public void setUp() {
public void setUp() throws Exception {
final InputStream is = ExtractInfoProcessor.class.getClassLoader()
.getResourceAsStream("checkstyle_modules.json");
final InputStreamReader reader = new InputStreamReader(is, Charset.forName("UTF-8"));
final Method method = ExtractInfoProcessor.class
.getDeclaredMethod("getModuleExtractInfosFromReader", Reader.class);
method.setAccessible(true);
final Map<String, ModuleExtractInfo> map =
ExtractInfoProcessor.getModuleExtractInfosFromReader(reader);
(Map<String, ModuleExtractInfo>) method.invoke(ExtractInfoProcessor.class, reader);
ModuleUtils.setNameToModuleExtractInfo(map);
}

Expand Down

0 comments on commit 639e730

Please sign in to comment.