-
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.
- Loading branch information
Showing
6 changed files
with
239 additions
and
5 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
86 changes: 86 additions & 0 deletions
86
src/main/java/com/github/checkstyle/regression/extract/ExtractInfoProcessor.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,86 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// 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<ModuleExtractInfo>}. */ | ||
private static final Type TYPE_EXTRACT_INFO_LIST = new TypeToken<List<ModuleExtractInfo>>() { | ||
}.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 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( | ||
Reader reader) { | ||
final List<ModuleExtractInfo> 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)); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/github/checkstyle/regression/extract/package-info.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,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; |
92 changes: 92 additions & 0 deletions
92
src/test/java/com/github/checkstyle/regression/extract/ExtractInfoProcessorTest.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,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<String, ModuleExtractInfo> 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()); | ||
} | ||
} | ||
} |
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,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" | ||
] | ||
} | ||
] |