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 edu.hm.hafner.analysis.assertj.IssueAssert.*;

/**
 * Base class for parser tests. Provides an assertion test for warnings.
 * <p>
 * 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) {
        assertThat(warning).hasPriority(priority);
        assertThat(warning).hasCategory(category);
        assertThat(warning).hasLineStart(lineNumber);
        assertThat(warning).hasLineEnd(lineNumber);
        assertThat(warning).hasMessage(message);
        assertThat(warning).hasFileName(fileName);
    }

    /**
     * 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);
        assertThat(warning).hasColumnStart(column);
    }

    /**
     * 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);
        assertThat(warning).hasType(type);
    }

    /**
     * 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);
        assertThat(warning).hasType(type);
    }

    /**
     * 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,50 +389,40 @@
 tic 
-edu.hm.hafner.analysis.assertj.Issue
+org.junit.jupiter.api.
 Assert
+ions
 .*;%0A
@@ -505,12 +505,8 @@
 .%0A *
- %3Cp%3E
 %0A * 
@@ -848,35 +848,32 @@
  @param warning 
-   
 the warning to c
@@ -1306,33 +1306,44 @@
   assert
-That(
+Equals(priority, 
 warning
-).has
+.get
 Priority
@@ -1347,50 +1347,83 @@
 ity(
-priority);%0A        assertThat(
+), %22Wrong priority detected: %22);%0A        assertEquals(category, 
 warning
-).has
+.get
 Cate
@@ -1431,50 +1431,96 @@
 ory(
-category);%0A        assertThat(
+), %22Wrong category of warning detected: %22);%0A        assertEquals(lineNumber, 
 warning
-).has
+.get
 Line
@@ -1529,60 +1529,95 @@
 art(
-lineNumber);%0A        assertThat(
+), %22Wrong line start detected: %22);%0A        assertEquals(lineNumber, 
 warning
-).has
+.get
 LineEnd(
 line
@@ -1612,26 +1612,46 @@
 LineEnd(
-lineNumber
+), %22Wrong line end detected: %22
 );%0A     
@@ -1663,33 +1663,43 @@
 sert
-That(
+Equals(message, 
 warning
-).has
+.get
 Message(
 mess
@@ -1698,49 +1698,82 @@
 age(
-message);%0A        assertThat(
+), %22Wrong message detected: %22);%0A        assertEquals(fileName, 
 warning
-).has
+.get
 File
@@ -1777,24 +1777,46 @@
 ileName(
+), %22Wrong 
 file
-N
+n
 ame
+ detected: %22
 );%0A    %7D
@@ -1901,35 +1901,32 @@
  @param warning 
-   
 the warning to c
@@ -2504,33 +2504,42 @@
   assert
-That(
+Equals(column, 
 warning
-).has
+.get
 ColumnSt
@@ -2542,22 +2542,50 @@
 mnStart(
-column
+), %22Wrong column start detected: %22
 );%0A    %7D
@@ -2670,35 +2670,32 @@
  @param warning 
-   
 the warning to c
@@ -3276,34 +3276,74 @@
 sert
-That(warning).hasType(type
+Equals(type, warning.getType(), %22Wrong type of warning detected: %22
 );%0A 
@@ -3440,19 +3440,16 @@
 warning 
-   
 the warn
@@ -4125,34 +4125,74 @@
 sert
-That(warning).hasType(type
+Equals(type, warning.getType(), %22Wrong type of warning detected: %22
 );%0A 

Revert "Use AssertJ assertions in ParserTest."