package edu.hm.hafner.analysis.parser;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;

import org.apache.commons.io.input.BOMInputStream;

import edu.hm.hafner.analysis.Issue;
import edu.hm.hafner.analysis.IssueBuilder;
import edu.hm.hafner.analysis.Priority;
import edu.hm.hafner.util.Ensure;
import static org.junit.jupiter.api.Assertions.*;

/**
 * Base class for parser tests. Provides an assertion test for warnings.
 *
 * FIXME: close files
 */
public abstract class ParserTester {
    public static final String WRONG_NUMBER_OF_WARNINGS_DETECTED = "Wrong number of warnings detected: ";
    public static final String DEFAULT_CATEGORY = new IssueBuilder().build().getCategory();

    /**
     * Checks the properties of the specified warning.
     *
     * @param warning the warning to check
     * @param lineNumber the expected line number
     * @param message    the expected message
     * @param fileName   the expected filename
     * @param category   the expected category
     * @param priority   the expected priority
     */
    protected void checkWarning(final Issue warning, final int lineNumber, final String message, final String fileName, final String category, final Priority priority) {
        assertEquals(priority, warning.getPriority(), "Wrong priority detected: ");
        assertEquals(category, warning.getCategory(), "Wrong category of warning detected: ");
        assertEquals(lineNumber, warning.getLineStart(), "Wrong line start detected: ");
        assertEquals(lineNumber, warning.getLineEnd(), "Wrong line end detected: ");
        assertEquals(message, warning.getMessage(), "Wrong message detected: ");
        assertEquals(fileName, warning.getFileName(), "Wrong filename detected: ");
    }

    /**
     * Checks the properties of the specified warning.
     *
     * @param warning the warning to check
     * @param lineNumber the expected line number
     * @param column     the expected column
     * @param message    the expected message
     * @param fileName   the expected filename
     * @param category   the expected category
     * @param priority   the expected priority
     */
    protected void checkWarning(final Issue warning, final int lineNumber, final int column, final String message, final String fileName, final String category, final Priority priority) {
        checkWarning(warning, lineNumber, message, fileName, category, priority);
        assertEquals(column, warning.getColumnStart(), "Wrong column start detected: ");
    }

    /**
     * Checks the properties of the specified warning.
     *
     * @param warning the warning to check
     * @param lineNumber the expected line number
     * @param message    the expected message
     * @param fileName   the expected filename
     * @param type       the expected type
     * @param category   the expected category
     * @param priority   the expected priority
     */
    protected void checkWarning(final Issue warning, final int lineNumber, final String message, final String fileName, final String type, final String category, final Priority priority) {
        checkWarning(warning, lineNumber, message, fileName, category, priority);
        assertEquals(type, warning.getType(), "Wrong type of warning detected: ");
    }

    /**
     * Checks the properties of the specified warning.
     *
     * @param warning the warning to check
     * @param lineNumber the expected line number
     * @param column     the expected column
     * @param message    the expected message
     * @param fileName   the expected filename
     * @param type       the expected type
     * @param category   the expected category
     * @param priority   the expected priority
     */
    protected void checkWarning(final Issue warning, final int lineNumber, final int column, final String message, final String fileName, final String type, final String category, final Priority priority) { // NOCHECKSTYLE
        checkWarning(warning, lineNumber, column, message, fileName, category, priority);
        assertEquals(type, warning.getType(), "Wrong type of warning detected: ");
    }

    /**
     * Returns an input stream with the warnings.
     *
     * @return an input stream
     */
    protected Reader openFile() {
        return openFile(getWarningsFile());
    }

    /**
     * Returns an input stream with the warnings.
     *
     * @param fileName the file to read
     * @return an input stream
     */
    protected Reader openFile(final String fileName) {
        try {
            return new InputStreamReader(new BOMInputStream(asStream(fileName)), "UTF-8");
        }
        catch (UnsupportedEncodingException exception) {
            return new InputStreamReader(asStream(fileName));
        }
    }

    private InputStream asStream(final String fileName) {
        InputStream resourceAsStream = ParserTester.class.getResourceAsStream(fileName);

        Ensure.that(resourceAsStream).isNotNull("File %s not found!", fileName);

        return resourceAsStream;
    }

    /**
     * Returns the file name of the warnings file.
     *
     * @return the warnings file name
     */
    protected abstract String getWarningsFile();
}

@@ -389,40 +389,50 @@
 tic 
-org.junit.jupiter.api.
+edu.hm.hafner.analysis.assertj.Issue
 Assert
-ions
 .*;%0A
@@ -511,16 +511,20 @@
 ings.%0A *
+ %3Cp%3E
 %0A * FIXM
@@ -862,32 +862,35 @@
  @param warning 
+   
 the warning to c
@@ -1327,36 +1327,25 @@
 sert
-Equals(priority, 
+That(
 warning
-.get
+).has
 Prio
@@ -1353,83 +1353,50 @@
 ity(
-), %22Wrong priority detected: %22);%0A        assertEquals(category, 
+priority);%0A        assertThat(
 warning
-.get
+).has
 Cate
@@ -1404,96 +1404,50 @@
 ory(
-), %22Wrong category of warning detected: %22);%0A        assertEquals(lineNumber, 
+category);%0A        assertThat(
 warning
-.get
+).has
 Line
@@ -1456,95 +1456,60 @@
 art(
-), %22Wrong line start detected: %22);%0A        assertEquals(lineNumber, 
+lineNumber);%0A        assertThat(
 warning
-.get
+).has
 LineEnd(
 ), %22
@@ -1508,38 +1508,18 @@
 End(
-), %22Wrong line end detected: %22
+lineNumber
 );%0A 
@@ -1535,43 +1535,33 @@
 sert
-Equals(message, 
+That(
 warning
-.get
+).has
 Message(
 ), %22
@@ -1560,82 +1560,49 @@
 age(
-), %22Wrong message detected: %22);%0A        assertEquals(fileName, 
+message);%0A        assertThat(
 warning
-.get
+).has
 File
@@ -1610,38 +1610,16 @@
 ame(
-), %22Wrong 
 file
-n
+N
 ame
- detected: %22
 );%0A 
@@ -1708,32 +1708,35 @@
  @param warning 
+   
 the warning to c
@@ -2318,34 +2318,25 @@
 sert
-Equals(column, 
+That(
 warning
-.get
+).has
 Colu
@@ -2347,42 +2347,14 @@
 art(
-), %22Wrong column start detected: %22
+column
 );%0A 
@@ -2443,32 +2443,35 @@
  @param warning 
+   
 the warning to c
@@ -3052,74 +3052,34 @@
 sert
-Equals(type, warning.getType(), %22Wrong type of warning detected: %22
+That(warning).hasType(type
 );%0A 
@@ -3176,16 +3176,19 @@
 warning 
+   
 the warn
@@ -3864,74 +3864,34 @@
 sert
-Equals(type, warning.getType(), %22Wrong type of warning detected: %22
+That(warning).hasType(type
 );%0A 

Use AssertJ assertions in ParserTest.