Skip to content
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
merged 1 commit into from
Jun 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions config/import-control.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
<allow pkg="org.immutables.gson"/>
</subpackage>

<subpackage name="extract">
<allow pkg="com.google.gson"/>
<allow class="com.sun.tools.javac.util.ServiceLoader"/>
<allow class="java.lang.reflect.Type"/>
</subpackage>

<subpackage name="git">
<allow pkg="org.eclipse.jgit"/>
</subpackage>
Expand Down
5 changes: 0 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,6 @@
<branchRate>0</branchRate>
<lineRate>0</lineRate>
</regex>
<regex>
<pattern>com.github.checkstyle.regression.data.ModuleExtractInfo</pattern>
<branchRate>0</branchRate>
<lineRate>0</lineRate>
</regex>
<regex>
<pattern>com.github.checkstyle.regression.data.ModuleInfo</pattern>
<branchRate>0</branchRate>
Expand Down
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(
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Json exception is not caught here so we could use them in main

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.

"Failed when loaing hardcoded checkstyle module information", ex);
}

return modules.stream().collect(Collectors.toMap(
ModuleExtractInfo::fullName, extractInfo -> extractInfo));
}
}
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;
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());
}
}
}
31 changes: 31 additions & 0 deletions src/test/resources/checkstyle_modules.json
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"
]
}
]