diff --git a/config/import-control.xml b/config/import-control.xml index 43fa3db..fb22fab 100644 --- a/config/import-control.xml +++ b/config/import-control.xml @@ -14,6 +14,12 @@ + + + + + + diff --git a/pom.xml b/pom.xml index 83a1a7f..763bd26 100644 --- a/pom.xml +++ b/pom.xml @@ -285,11 +285,6 @@ 0 0 - - com.github.checkstyle.regression.data.ModuleExtractInfo - 0 - 0 - com.github.checkstyle.regression.data.ModuleInfo 0 diff --git a/src/main/java/com/github/checkstyle/regression/extract/ExtractInfoProcessor.java b/src/main/java/com/github/checkstyle/regression/extract/ExtractInfoProcessor.java new file mode 100644 index 0000000..9e51a28 --- /dev/null +++ b/src/main/java/com/github/checkstyle/regression/extract/ExtractInfoProcessor.java @@ -0,0 +1,85 @@ +//////////////////////////////////////////////////////////////////////////////// +// 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.IOException; +import java.io.Reader; +import java.lang.reflect.Type; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import com.github.checkstyle.regression.data.ModuleExtractInfo; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.TypeAdapterFactory; +import com.google.gson.reflect.TypeToken; +import com.sun.tools.javac.util.ServiceLoader; + +/** + * Processes the module extract info grabbed from checkstyle project. + * @author LuoLiangchen + */ +public final class ExtractInfoProcessor { + /** The {@link Gson} instance. */ + private static final Gson GSON; + + /** The type of {@code List}. */ + private static final Type TYPE_EXTRACT_INFO_LIST = new TypeToken>() { + }.getType(); + + /** Prevents instantiation. */ + private ExtractInfoProcessor() { + } + + static { + final GsonBuilder gsonBuilder = new GsonBuilder(); + for (TypeAdapterFactory factory : ServiceLoader.load(TypeAdapterFactory.class)) { + gsonBuilder.registerTypeAdapterFactory(factory); + } + GSON = gsonBuilder.create(); + } + + /** + * Gets the full qualified name to module extract info map from the given reader. + * @param reader the given reader + * @return the full qualified name to module extract info map + */ + public static Map getModuleExtractInfosFromReader( + Reader reader) { + final List modules; + + try { + try { + modules = GSON.fromJson(reader, TYPE_EXTRACT_INFO_LIST); + } + finally { + reader.close(); + } + } + catch (IOException ex) { + throw new IllegalStateException( + "Failed when loaing hardcoded checkstyle module information", ex); + } + + return modules.stream().collect(Collectors.toMap( + ModuleExtractInfo::fullName, extractInfo -> extractInfo)); + } +} diff --git a/src/main/java/com/github/checkstyle/regression/extract/package-info.java b/src/main/java/com/github/checkstyle/regression/extract/package-info.java new file mode 100644 index 0000000..72b5e7c --- /dev/null +++ b/src/main/java/com/github/checkstyle/regression/extract/package-info.java @@ -0,0 +1,24 @@ +//////////////////////////////////////////////////////////////////////////////// +// 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 execution of checkstyle maven extract process and extract file processor. + * @author LuoLiangchen + */ +package com.github.checkstyle.regression.extract; diff --git a/src/test/java/com/github/checkstyle/regression/extract/ExtractInfoProcessorTest.java b/src/test/java/com/github/checkstyle/regression/extract/ExtractInfoProcessorTest.java new file mode 100644 index 0000000..e3da393 --- /dev/null +++ b/src/test/java/com/github/checkstyle/regression/extract/ExtractInfoProcessorTest.java @@ -0,0 +1,92 @@ +//////////////////////////////////////////////////////////////////////////////// +// 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 static com.github.checkstyle.regression.internal.TestUtils.assertUtilsClassHasPrivateConstructor; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.spy; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.Charset; +import java.util.Map; + +import org.junit.Test; + +import com.github.checkstyle.regression.data.ImmutableModuleExtractInfo; +import com.github.checkstyle.regression.data.ModuleExtractInfo; + +public class ExtractInfoProcessorTest { + private static final String BASE_PACKAGE = "com.puppycrawl.tools.checkstyle"; + + @Test + public void testIsProperUtilsClass() throws Exception { + assertUtilsClassHasPrivateConstructor(ExtractInfoProcessor.class); + } + + @Test + public void testGetModuleExtractInfosFromReader() { + final InputStream is = ExtractInfoProcessor.class.getClassLoader() + .getResourceAsStream("checkstyle_modules.json"); + final InputStreamReader reader = new InputStreamReader(is, Charset.forName("UTF-8")); + final Map map = + ExtractInfoProcessor.getModuleExtractInfosFromReader(reader); + + final String module1 = "NewlineAtEndOfFileCheck"; + final ModuleExtractInfo extractInfo1 = ImmutableModuleExtractInfo.builder() + .name(module1) + .packageName(BASE_PACKAGE + ".checks") + .parent("Checker") + .build(); + final String module2 = "EmptyStatementCheck"; + final ModuleExtractInfo extractInfo2 = ImmutableModuleExtractInfo.builder() + .name(module2) + .packageName(BASE_PACKAGE + ".checks.coding") + .parent("TreeWalker") + .build(); + assertEquals("There should be 2 entries in the extract info map.", 2, map.size()); + assertEquals("The extract info of NewlineAtEndOfFileCheck is wrong.", + extractInfo1, map.get(BASE_PACKAGE + ".checks." + module1)); + assertEquals("The extract info of EmptyStatementCheck is wrong.", + extractInfo2, map.get(BASE_PACKAGE + ".checks.coding." + module2)); + } + + @Test + public void testGetNameToModuleInfoFromInputStreamWithException() throws Exception { + final InputStream is = spy(ExtractInfoProcessor.class.getClassLoader() + .getResourceAsStream("checkstyle_modules.json")); + final InputStreamReader reader = new InputStreamReader(is, Charset.forName("UTF-8")); + doThrow(IOException.class).when(is).close(); + + try { + ExtractInfoProcessor.getModuleExtractInfosFromReader(reader); + fail("Exception is expected"); + } + catch (IllegalStateException ex) { + assertEquals( + "Exception message is wrong", + "Failed when loaing hardcoded checkstyle module information", + ex.getMessage()); + } + } +} diff --git a/src/test/resources/checkstyle_modules.json b/src/test/resources/checkstyle_modules.json new file mode 100644 index 0000000..8a28051 --- /dev/null +++ b/src/test/resources/checkstyle_modules.json @@ -0,0 +1,31 @@ +[ + { + "name": "NewlineAtEndOfFileCheck", + "packageName": "com.puppycrawl.tools.checkstyle.checks", + "parent": "Checker", + "hierarchies": [ + "com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck", + "com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter", + "com.puppycrawl.tools.checkstyle.api.AutomaticBean" + ], + "interfaces": [ + "com.puppycrawl.tools.checkstyle.api.FileSetCheck", + "com.puppycrawl.tools.checkstyle.api.Configurable", + "com.puppycrawl.tools.checkstyle.api.Contextualizable" + ] + }, + { + "name": "EmptyStatementCheck", + "packageName": "com.puppycrawl.tools.checkstyle.checks.coding", + "parent": "TreeWalker", + "hierarchies": [ + "com.puppycrawl.tools.checkstyle.api.AbstractCheck", + "com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter", + "com.puppycrawl.tools.checkstyle.api.AutomaticBean" + ], + "interfaces": [ + "com.puppycrawl.tools.checkstyle.api.Configurable", + "com.puppycrawl.tools.checkstyle.api.Contextualizable" + ] + } +]