Skip to content

Commit

Permalink
Make all parsers based on files.
Browse files Browse the repository at this point in the history
- Provided actual report fileName as parameter for parsers.
- Make tests read a file rather then an input stream
- Replaced <SELF> reference by actual file name of Jenkins console log
  • Loading branch information
uhafner committed Aug 10, 2018
1 parent 922b103 commit 34fe13b
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 75 deletions.
19 changes: 12 additions & 7 deletions src/main/java/edu/hm/hafner/analysis/AbstractParser.java
Expand Up @@ -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<String, String> 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<String, String> preProcessor, final String absolutePath) {
private Report parse(final InputStream inputStream, final Charset charset, final Function<String, String> 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);

This comment has been minimized.

Copy link
@uhafner

uhafner Oct 27, 2018

Author Member

Check if it makes sense to use a runtime file exception.

}
}

Expand Down
23 changes: 0 additions & 23 deletions src/main/java/edu/hm/hafner/analysis/IssueParser.java
Expand Up @@ -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.
*
Expand All @@ -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 = "<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.
*
Expand Down
Expand Up @@ -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();
}

Expand Down
56 changes: 22 additions & 34 deletions 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.
Expand All @@ -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.
Expand All @@ -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);
}

/**
Expand All @@ -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)) {
Expand All @@ -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
Expand All @@ -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);
}
}
@@ -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;
Expand Down Expand Up @@ -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))
Expand Down
Expand Up @@ -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
Expand Down
@@ -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;

Expand Down Expand Up @@ -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);
}

/**
Expand Down
@@ -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;

Expand Down Expand Up @@ -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);
}
}

@@ -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;

Expand Down Expand Up @@ -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);
}
}
21 changes: 21 additions & 0 deletions src/test/java/edu/hm/hafner/util/ResourceTest.java
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -207,4 +210,22 @@ private String createString(final byte[] bytes) {
protected Stream<String> 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);
}
}
}

0 comments on commit 34fe13b

Please sign in to comment.