-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue #37: json file processor #40
Merged
+239
−5
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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" | ||
] | ||
} | ||
] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Throwing as a runtime exception is fine, but we will never be able to catch it in
Main
if we wish to add any more information to it. Please confirm this is your intent.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only way to go into catch block is
reader.close()
throwing an exception. I think it is rare. Instead of this,GSON.fromJson
is more likely to throwing exception sometimes (wrong format, etc.). Json exception is not caught here so we could use them in main if needed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Method has no
throws Exception
declaration so then all throwable exceptions, if any, are runtime.We can't do anything about
GSON
but if we know an error is possible, we should not use runtime exceptions.We can ignore this for now.