This repository has been archived by the owner on Sep 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Supporting 3-rd party detectors (rules)
- multiple messages.xml are read and merged - Java SPI mechanism for discovering 3-rd party detectors - default detector package is still needs to be used - huntbugs maven plugin is not affected (to configure it standard tag 'dependencies' inside tag 'plugin' could be used) - removed redundant public modifiers at interface nested classes (public by default) - plugin currently supports single package for detectors - introducing helper class to test detectors (to be reused) - custom detectors can be located in custom packages - minor refactoring - test coverage improved - added license agreement headers - demonstrating development of custom detector - sample project for custom detectors - detectors are automatically tested with sample code in test scope - sample project with HuntBugs maven plugin with custom detectors - production code in sample project is not annotated with @AssertWarning - sample project might be used for demonstration of project configuration without custom detectors as well
- Loading branch information
Showing
18 changed files
with
628 additions
and
94 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
78 changes: 78 additions & 0 deletions
78
huntbugs/src/main/java/one/util/huntbugs/spi/DataTests.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,78 @@ | ||
/* | ||
* Copyright 2016 HuntBugs contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package one.util.huntbugs.spi; | ||
|
||
import one.util.huntbugs.analysis.AnalysisOptions; | ||
import one.util.huntbugs.analysis.Context; | ||
import one.util.huntbugs.analysis.ErrorMessage; | ||
import one.util.huntbugs.analysis.HuntBugsResult; | ||
import one.util.huntbugs.input.XmlReportReader; | ||
import one.util.huntbugs.output.Reports; | ||
import one.util.huntbugs.repo.CompositeRepository; | ||
import one.util.huntbugs.repo.Repository; | ||
|
||
import java.io.PrintStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.ServiceLoader; | ||
import java.util.stream.Collectors; | ||
|
||
import static java.lang.String.format; | ||
|
||
/** | ||
* @author Tagir Valeev | ||
* | ||
*/ | ||
public abstract class DataTests { | ||
|
||
public static void test(String packageToAnalyze) throws Exception { | ||
|
||
// creating built-in and plugins repositories | ||
List<Repository> repositories = new ArrayList<>(); | ||
repositories.add(Repository.createSelfRepository()); | ||
for (HuntBugsPlugin huntBugsPlugin : ServiceLoader.load(HuntBugsPlugin.class)) { | ||
repositories.add(Repository.createPluginRepository(huntBugsPlugin)); | ||
} | ||
CompositeRepository repository = new CompositeRepository(repositories); | ||
|
||
Context ctx = new Context(repository, new AnalysisOptions()); | ||
ctx.analyzePackage(packageToAnalyze); | ||
ctx.reportStats(System.out); | ||
ctx.reportErrors(System.err); | ||
ctx.reportWarnings(new PrintStream("target/testWarnings.out")); | ||
Path xmlReport = Paths.get("target/testWarnings.xml"); | ||
Reports.write(xmlReport, Paths.get("target/testWarnings.html"), ctx); | ||
System.out.println("Analyzed " + ctx.getClassesCount() + " classes"); | ||
if (ctx.getErrorCount() > 0) { | ||
List<ErrorMessage> errorMessages = ctx.errors().collect(Collectors.toList()); | ||
throw new AssertionError(format("Analysis finished with %s errors: %s", ctx.getErrorCount(), errorMessages)); | ||
} | ||
HuntBugsResult result = XmlReportReader.read(ctx, xmlReport); | ||
Path rereadReport = Paths.get("target/testWarnings_reread.xml"); | ||
Reports.write(rereadReport, null, result); | ||
byte[] expectedReport = Files.readAllBytes(xmlReport); | ||
byte[] actualReport = Files.readAllBytes(rereadReport); | ||
if (!Arrays.equals(expectedReport, actualReport)) { | ||
String errorMessage = format("Expected: \n%s\n\nActual: \n%s\n\n", new String(expectedReport), new String(actualReport)); | ||
throw new AssertionError(errorMessage); | ||
} | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
huntbugs/src/main/java/one/util/huntbugs/spi/HuntBugsPlugin.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,30 @@ | ||
/* | ||
* Copyright 2016 HuntBugs contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package one.util.huntbugs.spi; | ||
|
||
/** | ||
* This is extension point for 3-rd party detector providers. | ||
* | ||
* @author Mihails Volkovs | ||
* | ||
*/ | ||
public interface HuntBugsPlugin { | ||
|
||
String name(); | ||
|
||
String detectorPackage(); | ||
|
||
} |
Oops, something went wrong.