From 34fe13b04e8522c203089431d684bce923897ec2 Mon Sep 17 00:00:00 2001 From: Ulli Hafner Date: Fri, 10 Aug 2018 23:15:11 +0200 Subject: [PATCH] Make all parsers based on files. - Provided actual report fileName as parameter for parsers. - Make tests read a file rather then an input stream - Replaced reference by actual file name of Jenkins console log --- .../hm/hafner/analysis/AbstractParser.java | 19 ++++--- .../edu/hm/hafner/analysis/IssueParser.java | 23 -------- .../analysis/parser/MavenConsoleParser.java | 2 +- .../hafner/analysis/AbstractParserTest.java | 56 ++++++++----------- .../analysis/parser/GnuMakeGccParserTest.java | 3 +- .../parser/MavenConsoleParserTest.java | 1 + .../parser/dry/cpd/CpdParserTest.java | 4 +- .../dry/dupfinder/DupFinderParserTest.java | 8 +-- .../parser/dry/simian/SimianParserTest.java | 6 +- .../java/edu/hm/hafner/util/ResourceTest.java | 21 +++++++ 10 files changed, 68 insertions(+), 75 deletions(-) diff --git a/src/main/java/edu/hm/hafner/analysis/AbstractParser.java b/src/main/java/edu/hm/hafner/analysis/AbstractParser.java index 43b3691a9..dad21cf22 100644 --- a/src/main/java/edu/hm/hafner/analysis/AbstractParser.java +++ b/src/main/java/edu/hm/hafner/analysis/AbstractParser.java @@ -40,29 +40,34 @@ public abstract class AbstractParser extends IssueParser { public static final String DEPRECATION = "Deprecation"; /** Category for warnings due to the usage of proprietary API. */ public static final String PROPRIETARY_API = "Proprietary API"; + + private String fileName; + + public String getFileName() { + return fileName; + } @Override public Report parse(final File file, final Charset charset, final Function preProcessor) throws ParsingException, ParsingCanceledException { - String absolutePath = file.getAbsolutePath(); + fileName = file.getAbsolutePath(); try (InputStream inputStream = new FileInputStream(file)) { - return parse(inputStream, charset, preProcessor, absolutePath); + return parse(inputStream, charset, preProcessor); } catch (FileNotFoundException exception) { - throw new ParsingException(exception, "Can't find file: " + absolutePath); + throw new ParsingException(exception, "Can't find file: " + fileName); } catch (IOException exception) { - throw new ParsingException(exception, "Can't scan file for issues: " + absolutePath); + throw new ParsingException(exception, "Can't scan file for issues: " + fileName); } } - private Report parse(final InputStream inputStream, final Charset charset, - final Function preProcessor, final String absolutePath) { + private Report parse(final InputStream inputStream, final Charset charset, final Function preProcessor) { try (Reader input = createReader(inputStream, charset)) { return parse(input, preProcessor); } catch (IOException exception) { - throw new ParsingException(exception, "Can't scan file for issues: " + absolutePath); + throw new ParsingException(exception, "Can't scan file for issues: " + fileName); } } diff --git a/src/main/java/edu/hm/hafner/analysis/IssueParser.java b/src/main/java/edu/hm/hafner/analysis/IssueParser.java index 3a29a2589..65ec88b76 100644 --- a/src/main/java/edu/hm/hafner/analysis/IssueParser.java +++ b/src/main/java/edu/hm/hafner/analysis/IssueParser.java @@ -5,8 +5,6 @@ import java.nio.charset.Charset; import java.util.function.Function; -import edu.hm.hafner.analysis.parser.MavenConsoleParser; - /** * Parses a file and returns the issues reported in this file. * @@ -15,27 +13,6 @@ public abstract class IssueParser implements Serializable { private static final long serialVersionUID = 200992696185460268L; - /** - * References an issue that has an affected file that is the file that has been parsed. E.g., the {@link - * MavenConsoleParser} creates issues from an input file that has references to itself. There are no other affected - * files involved. - */ - public static final String SELF = ""; - - /** - * Returns whether this issue has an affected file that is the file that has been parsed. - * - * @param issue - * the issue to check the affected file - * - * @return {@code true} if this issue has an affected file that is the file that has been parsed, {@code false} - * otherwise - * @see #SELF - */ - public static boolean isSelfReference(final Issue issue) { - return SELF.equals(issue.getFileName()); - } - /** * Parses the specified file for issues. * diff --git a/src/main/java/edu/hm/hafner/analysis/parser/MavenConsoleParser.java b/src/main/java/edu/hm/hafner/analysis/parser/MavenConsoleParser.java index 7fc6eb844..30a525287 100644 --- a/src/main/java/edu/hm/hafner/analysis/parser/MavenConsoleParser.java +++ b/src/main/java/edu/hm/hafner/analysis/parser/MavenConsoleParser.java @@ -68,7 +68,7 @@ protected Issue createIssue(final Matcher matcher, final IssueBuilder builder) { priority = Priority.NORMAL; category = "Warning"; } - return builder.setFileName(SELF).setLineStart(getCurrentLine()).setCategory(category) + return builder.setFileName(getFileName()).setLineStart(getCurrentLine()).setCategory(category) .setMessage(errorOrWarningMessage).setPriority(priority).build(); } diff --git a/src/test/java/edu/hm/hafner/analysis/AbstractParserTest.java b/src/test/java/edu/hm/hafner/analysis/AbstractParserTest.java index d13329d08..6d5b14349 100644 --- a/src/test/java/edu/hm/hafner/analysis/AbstractParserTest.java +++ b/src/test/java/edu/hm/hafner/analysis/AbstractParserTest.java @@ -1,24 +1,19 @@ package edu.hm.hafner.analysis; import java.io.ByteArrayOutputStream; +import java.io.File; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; import java.io.NotSerializableException; import java.io.ObjectOutputStream; -import java.io.Reader; import java.io.Serializable; import java.nio.charset.StandardCharsets; -import java.util.function.Function; -import org.apache.commons.io.input.BOMInputStream; import org.junit.jupiter.api.Test; -import static edu.hm.hafner.analysis.assertj.SoftAssertions.*; -import static org.assertj.core.api.Assertions.*; - import edu.hm.hafner.analysis.assertj.SoftAssertions; +import static edu.hm.hafner.analysis.assertj.SoftAssertions.*; import edu.hm.hafner.util.ResourceTest; +import static org.assertj.core.api.Assertions.*; /** * Base class for tests of {@link AbstractParser} instances. @@ -43,6 +38,15 @@ protected AbstractParserTest(final String fileWithIssuesName) { this.fileWithIssuesName = fileWithIssuesName; } + /** + * Returns the name of the file that should contain some issues. + * + * @return the file name + */ + protected String getFileWithIssuesName() { + return fileWithIssuesName; + } + /** * Parses the default file that must contain issues. Verification of the issues is delegated to method {@link * #assertThatIssuesArePresent(Report, SoftAssertions)} that needs to be implemented by sub classes. @@ -59,7 +63,7 @@ void shouldParseAllIssues() { * @return the issues in the default file */ protected Report parseDefaultFile() { - return createParser().parse(openFile(), Function.identity()); + return createParser().parse(getDefaultFile(), StandardCharsets.UTF_8); } /** @@ -68,7 +72,7 @@ protected Report parseDefaultFile() { */ @Test void shouldBeSerializable() throws IOException { - AbstractParser parser = createParser(); + IssueParser parser = createParser(); ByteArrayOutputStream out = new ByteArrayOutputStream(); try (ObjectOutputStream stream = new ObjectOutputStream(out)) { @@ -87,13 +91,14 @@ void shouldBeSerializable() throws IOException { * @return the found issues */ protected Report parse(final String fileName) { - return createParser().parse(openFile(fileName), Function.identity()); + return createParser().parse(getResourceAsFile(fileName), StandardCharsets.UTF_8); } /** * Verifies that the provided default file has been parsed correctly. I.e., a concrete test case needs to verify * that the number of issues is correct and that each issue contains the correct properties. - * @param report + * + * @param report * the issues that have been created while parsing the default file * @param softly * The soft assertions instance you can use for all {@link SoftAssertions#assertThat assertThat} calls. Note @@ -106,31 +111,14 @@ protected Report parse(final String fileName) { * * @return the new parser instance */ - protected abstract AbstractParser createParser(); + protected abstract IssueParser createParser(); /** - * Returns an input stream for the default file. + * Returns the default {@link File} that should be scanned for issues. * - * @return an input stream + * @return default file with issues */ - protected Reader openFile() { - return openFile(fileWithIssuesName); - } - - /** - * Returns an input stream for the specified resource. The file name must be relative to this {@link - * AbstractParserTest} class. - * - * @param fileName - * the file to read (relative this {@link AbstractParserTest} class - * - * @return an {@link BOMInputStream input stream} using character set UTF-8 - */ - protected Reader openFile(final String fileName) { - return asReader(asInputStream(fileName)); - } - - private Reader asReader(final InputStream stream) { - return new InputStreamReader(new BOMInputStream(stream), StandardCharsets.UTF_8); + protected File getDefaultFile() { + return getResourceAsFile(fileWithIssuesName); } } diff --git a/src/test/java/edu/hm/hafner/analysis/parser/GnuMakeGccParserTest.java b/src/test/java/edu/hm/hafner/analysis/parser/GnuMakeGccParserTest.java index 1726a35c8..c769b6cd5 100644 --- a/src/test/java/edu/hm/hafner/analysis/parser/GnuMakeGccParserTest.java +++ b/src/test/java/edu/hm/hafner/analysis/parser/GnuMakeGccParserTest.java @@ -1,5 +1,6 @@ package edu.hm.hafner.analysis.parser; +import java.nio.charset.StandardCharsets; import java.util.Iterator; import org.junit.jupiter.api.Test; @@ -45,7 +46,7 @@ void checkCorrectPathWindows() { * Checks that paths of the type "/c/anything" are changed to "c:/anything" on windows but no other OS. */ private void checkOsSpecificPath(final String os, final String rootDir) { - Report warnings = new GnuMakeGccParser(os).parse(openFile()); + Report warnings = new GnuMakeGccParser(os).parse(getDefaultFile(), StandardCharsets.UTF_8); assertSoftly(softly -> { softly.assertThat(warnings.get(14)) diff --git a/src/test/java/edu/hm/hafner/analysis/parser/MavenConsoleParserTest.java b/src/test/java/edu/hm/hafner/analysis/parser/MavenConsoleParserTest.java index f4c288257..b565db4a3 100644 --- a/src/test/java/edu/hm/hafner/analysis/parser/MavenConsoleParserTest.java +++ b/src/test/java/edu/hm/hafner/analysis/parser/MavenConsoleParserTest.java @@ -51,6 +51,7 @@ protected void assertThatIssuesArePresent(final Report report, final SoftAsserti softly.assertThat(report) .hasSize(5) .hasPriorities(2, 3, 0); + report.stream().forEach(issue -> assertThat(issue.getFileName()).endsWith(getFileWithIssuesName())); } @Override diff --git a/src/test/java/edu/hm/hafner/analysis/parser/dry/cpd/CpdParserTest.java b/src/test/java/edu/hm/hafner/analysis/parser/dry/cpd/CpdParserTest.java index d892fba55..423ef99ea 100644 --- a/src/test/java/edu/hm/hafner/analysis/parser/dry/cpd/CpdParserTest.java +++ b/src/test/java/edu/hm/hafner/analysis/parser/dry/cpd/CpdParserTest.java @@ -1,7 +1,7 @@ package edu.hm.hafner.analysis.parser.dry.cpd; import java.io.Serializable; -import java.util.function.Function; +import java.nio.charset.StandardCharsets; import org.junit.jupiter.api.Test; @@ -117,7 +117,7 @@ void shouldAssignPriority() { private Report parse(final int highThreshold, final int normalThreshold) { return new CpdParser(highThreshold, normalThreshold) - .parse(openFile("issue12516.xml"), Function.identity()); + .parse(getResourceAsFile("issue12516.xml"), StandardCharsets.UTF_8); } /** diff --git a/src/test/java/edu/hm/hafner/analysis/parser/dry/dupfinder/DupFinderParserTest.java b/src/test/java/edu/hm/hafner/analysis/parser/dry/dupfinder/DupFinderParserTest.java index bff618e1f..c1e443466 100644 --- a/src/test/java/edu/hm/hafner/analysis/parser/dry/dupfinder/DupFinderParserTest.java +++ b/src/test/java/edu/hm/hafner/analysis/parser/dry/dupfinder/DupFinderParserTest.java @@ -1,15 +1,15 @@ package edu.hm.hafner.analysis.parser.dry.dupfinder; import java.io.Serializable; -import java.util.function.Function; +import java.nio.charset.StandardCharsets; import org.junit.jupiter.api.Test; import edu.hm.hafner.analysis.AbstractParserTest; import edu.hm.hafner.analysis.Issue; -import edu.hm.hafner.analysis.Report; import edu.hm.hafner.analysis.Priority; -import static edu.hm.hafner.analysis.assertj.Assertions.assertThat; +import edu.hm.hafner.analysis.Report; +import static edu.hm.hafner.analysis.assertj.Assertions.*; import edu.hm.hafner.analysis.assertj.SoftAssertions; import edu.hm.hafner.analysis.parser.dry.DuplicationGroup; @@ -118,7 +118,7 @@ void shouldAssignPriority() { private Report parse(final int highThreshold, final int normalThreshold) { return new DupFinderParser(highThreshold, normalThreshold) - .parse(openFile("without-sourcecode.xml"), Function.identity()); + .parse(getResourceAsFile("without-sourcecode.xml"), StandardCharsets.UTF_8); } } diff --git a/src/test/java/edu/hm/hafner/analysis/parser/dry/simian/SimianParserTest.java b/src/test/java/edu/hm/hafner/analysis/parser/dry/simian/SimianParserTest.java index 5d9559fbb..4a613e2c5 100644 --- a/src/test/java/edu/hm/hafner/analysis/parser/dry/simian/SimianParserTest.java +++ b/src/test/java/edu/hm/hafner/analysis/parser/dry/simian/SimianParserTest.java @@ -1,13 +1,13 @@ package edu.hm.hafner.analysis.parser.dry.simian; -import java.util.function.Function; +import java.nio.charset.StandardCharsets; import org.junit.jupiter.api.Test; import edu.hm.hafner.analysis.AbstractParserTest; import edu.hm.hafner.analysis.Issue; -import edu.hm.hafner.analysis.Report; import edu.hm.hafner.analysis.Priority; +import edu.hm.hafner.analysis.Report; import static edu.hm.hafner.analysis.assertj.Assertions.*; import edu.hm.hafner.analysis.assertj.SoftAssertions; @@ -143,6 +143,6 @@ void shouldAssignPriority() { private Report parse(final int highThreshold, final int normalThreshold) { return new SimianParser(highThreshold, normalThreshold) - .parse(openFile("twofile.xml"), Function.identity()); + .parse(getResourceAsFile("twofile.xml"), StandardCharsets.UTF_8); } } diff --git a/src/test/java/edu/hm/hafner/util/ResourceTest.java b/src/test/java/edu/hm/hafner/util/ResourceTest.java index 04ac62250..a2af2629c 100644 --- a/src/test/java/edu/hm/hafner/util/ResourceTest.java +++ b/src/test/java/edu/hm/hafner/util/ResourceTest.java @@ -14,8 +14,11 @@ import java.nio.file.Paths; import java.util.stream.Stream; +import org.apache.commons.io.input.BOMInputStream; + import com.google.errorprone.annotations.MustBeClosed; +import edu.hm.hafner.analysis.AbstractParserTest; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; /** @@ -207,4 +210,22 @@ private String createString(final byte[] bytes) { protected Stream getTextLinesAsStream(final String text) { return new BufferedReader(new StringReader(text)).lines(); } + + /** + * Returns the {@link File} of the specified resource. The file name must be relative to the test class. + * + * @param fileName + * the file to read (relative this {@link AbstractParserTest} class + * + * @return an {@link BOMInputStream input stream} using character set UTF-8 + * @see #getTestResourceClass() + */ + protected File getResourceAsFile(final String fileName) { + try { + return Paths.get(getTestResourceClass().getResource(fileName).toURI()).toFile(); + } + catch (URISyntaxException e) { + throw new AssertionError("Can't open file " + fileName, e); + } + } }