Class AbstractTest<R extends IModel>

  • Type Parameters:
    R - the top-level result model class

    public abstract class AbstractTest<R extends IModel>
    extends java.lang.Object
    An abstract test support class for reading, parsing and comparing models. Due to architectural conventions, we avoid any jUnit dependencies in this bundle.
    Author:
    Holger Eichelberger
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      protected static class  AbstractTest.EqualitySetup<R extends IModel>
      Setup data needed to compare test results for equality.
    • Constructor Summary

      Constructors 
      Constructor Description
      AbstractTest()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected java.lang.String assertNamingAndVersion​(AbstractTest.EqualitySetup<R> data, TranslationResult<R> result)
      Asserts the naming and the version in result.
      protected java.lang.String checkEqualsAndPrepareMessage​(java.lang.String fileAsString, java.io.Writer model)
      Asserts the equality of two strings containing models and, if needed, emits the location where the strings are not equal.
      protected java.lang.String checkEqualsAndPrepareMessage​(java.lang.String fileAsString, java.io.Writer model, boolean trimModel)
      Asserts the equality of two strings containing models and, if needed, emits the location where the strings are not equal.
      protected boolean checkEqualsAndPrint​(java.lang.String fileAsString, java.lang.String modelAsString)
      Asserts the equality of two strings containing models and, if needed, prints the location where the strings are not equal.
      protected boolean checkEqualsAndPrint​(java.lang.String fileAsString, java.lang.String modelAsString, java.io.PrintWriter writer)
      Asserts the equality of two strings containing models and, if needed, prints the location where the strings are not equal.
      protected java.lang.String checkErrorCodes​(java.util.List<Message> messages, java.util.List<Message> warnings, int... expectedErrorCodes)
      Asserts that the expected error codes match the errors stored in result.
      protected java.lang.String checkErrorCodes​(TranslationResult<R> result, java.util.List<Message> warnings, int... expectedErrorCodes)
      Asserts that the expected error codes match the errors stored in result.
      protected static java.lang.String checkWarnings​(java.util.List<Message> warnings, int... expectedWarnings)
      Returns the message for missing expected warnings.
      static java.io.File determineTestDataDir​(java.lang.String property)
      Determines the actual directory with the test IVML files depending on the JVM system specified property which may contain a specific directory (or be empty -> default directory testdata).
      protected java.lang.String file2String​(java.io.File file)
      Turns a file into a string for comparison.
      private boolean handleReplacementComment​(StringWithPosition expected, StringWithPosition actual)
      Handles a replacement comment, i.e.
      private boolean handleWindowsLinuxLineEnd​(StringWithPosition s1, StringWithPosition s2)
      Compare and handle possible line ends for equality over windows and linux conventions.
      protected int isEqual​(java.lang.String fileAsString, java.lang.String modelAsString)
      Compares two strings containing models and, if needed, returns the location where the strings are not equal.
      private static boolean isLineEnd​(java.lang.Character ch)
      Returns whether the given character is a line end.
      private static boolean isWhitespaceButNotLineEnd​(java.lang.Character ch)
      Returns whether the given character is a whitespace but not a line end.
      protected static void resourceInitialization()
      Performs the resource initialization.
      static java.lang.String toString​(java.util.List<Message> messages)
      Turns a list of messages into a readable string.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • AbstractTest

        public AbstractTest()
    • Method Detail

      • resourceInitialization

        protected static void resourceInitialization()
        Performs the resource initialization.
      • determineTestDataDir

        public static java.io.File determineTestDataDir​(java.lang.String property)
        Determines the actual directory with the test IVML files depending on the JVM system specified property which may contain a specific directory (or be empty -> default directory testdata).
        Parameters:
        property - the JVM property to read out
        Returns:
        the actual directory as file
      • checkErrorCodes

        protected java.lang.String checkErrorCodes​(TranslationResult<R> result,
                                                   java.util.List<Message> warnings,
                                                   int... expectedErrorCodes)
        Asserts that the expected error codes match the errors stored in result.
        Parameters:
        result - the result from parsing and analyzing a project
        warnings - the warnings that occurred, to be modified as a side effect, may be null
        expectedErrorCodes - the allowed / expected error codes
        Returns:
        the error message if the codes do not match, null else
      • checkErrorCodes

        protected java.lang.String checkErrorCodes​(java.util.List<Message> messages,
                                                   java.util.List<Message> warnings,
                                                   int... expectedErrorCodes)
        Asserts that the expected error codes match the errors stored in result.
        Parameters:
        messages - the actual messages to be analyzed
        warnings - the warnings that occurred, to be modified as a side effect, may be null
        expectedErrorCodes - the allowed / expected error codes
        Returns:
        the error message if the codes do not match, null else
      • file2String

        protected java.lang.String file2String​(java.io.File file)
                                        throws java.io.IOException
        Turns a file into a string for comparison. If file is somewhat temporary and you want to delete it, call System.gc(); after this method. We do not call it by default as it affects the performance of the tests.
        Parameters:
        file - the file to be turned into a string
        Returns:
        the resulting string
        Throws:
        java.io.IOException - in case that file cannot be found/read
      • isLineEnd

        private static final boolean isLineEnd​(java.lang.Character ch)
        Returns whether the given character is a line end.
        Parameters:
        ch - the character to test
        Returns:
        true if it is a line end, false else
      • isWhitespaceButNotLineEnd

        private static final boolean isWhitespaceButNotLineEnd​(java.lang.Character ch)
        Returns whether the given character is a whitespace but not a line end.
        Parameters:
        ch - the character to test
        Returns:
        true if it is a whitespace but not a line end, false else
      • isEqual

        protected int isEqual​(java.lang.String fileAsString,
                              java.lang.String modelAsString)
        Compares two strings containing models and, if needed, returns the location where the strings are not equal.
        Parameters:
        fileAsString - the file as a string
        modelAsString - the model as a string
        Returns:
        the position in fileAsString where the inequality occurs, a negative value if the model contains more data, no problem if return value same/larger than length of fileAsString
      • handleWindowsLinuxLineEnd

        private boolean handleWindowsLinuxLineEnd​(StringWithPosition s1,
                                                  StringWithPosition s2)
        Compare and handle possible line ends for equality over windows and linux conventions. If s1 and s2 are at line ends, advance the positions over line ends
        Parameters:
        s1 - the first string to be compared
        s2 - the second string to be compared
        Returns:
        true if s1 and s2 are at line ends and both were advanced, false if nothing changed
      • handleReplacementComment

        private boolean handleReplacementComment​(StringWithPosition expected,
                                                 StringWithPosition actual)
        Handles a replacement comment, i.e. //* occurred in expected. The current position in actual will be compared to expected and if equal, the current and the next line in expected as well as the current line in actual will be consumed
        Parameters:
        expected - the expected string
        actual - the actual string
        Returns:
        true if the comment is handled, false if a comparison problem occurred and the lines are not considered to be equal.
      • checkEqualsAndPrint

        protected boolean checkEqualsAndPrint​(java.lang.String fileAsString,
                                              java.lang.String modelAsString)
        Asserts the equality of two strings containing models and, if needed, prints the location where the strings are not equal.
        Parameters:
        fileAsString - the file as a string
        modelAsString - the model as a string
        Returns:
        true if both are considered as equal, false else
        See Also:
        checkEqualsAndPrint(String, String, PrintWriter)
      • checkEqualsAndPrint

        protected boolean checkEqualsAndPrint​(java.lang.String fileAsString,
                                              java.lang.String modelAsString,
                                              java.io.PrintWriter writer)
        Asserts the equality of two strings containing models and, if needed, prints the location where the strings are not equal. This also adds the error cause to the given writer instead of printing it to System.err.println().
        Parameters:
        fileAsString - the file as a string
        modelAsString - the model as a string
        writer - A writer, which should be used for printing out a detailed error message
        Returns:
        true if both are considered as equal, false else
      • checkEqualsAndPrepareMessage

        protected java.lang.String checkEqualsAndPrepareMessage​(java.lang.String fileAsString,
                                                                java.io.Writer model)
                                                         throws java.io.IOException
        Asserts the equality of two strings containing models and, if needed, emits the location where the strings are not equal. The model string will be trimmed before comparison.
        Parameters:
        fileAsString - the file as a string
        model - the model as a string in the writer
        Returns:
        null if no error occurred, the formatted error string else
        Throws:
        java.io.IOException - in case of (internal) IO problems
        See Also:
        checkEqualsAndPrint(String, String, PrintWriter)
      • checkEqualsAndPrepareMessage

        protected java.lang.String checkEqualsAndPrepareMessage​(java.lang.String fileAsString,
                                                                java.io.Writer model,
                                                                boolean trimModel)
                                                         throws java.io.IOException
        Asserts the equality of two strings containing models and, if needed, emits the location where the strings are not equal.
        Parameters:
        fileAsString - the file as a string
        model - the model as a string in the writer
        trimModel - trim the model (string) on both sides before comparing it or only at the end
        Returns:
        null if no error occurred, the formatted error string else
        Throws:
        java.io.IOException - in case of (internal) IO problems
        See Also:
        checkEqualsAndPrint(String, String, PrintWriter)
      • checkWarnings

        protected static java.lang.String checkWarnings​(java.util.List<Message> warnings,
                                                        int... expectedWarnings)
        Returns the message for missing expected warnings.
        Parameters:
        warnings - the warnings to check
        expectedWarnings - the expected warnings
        Returns:
        null in case of no message, the message text else
      • assertNamingAndVersion

        protected java.lang.String assertNamingAndVersion​(AbstractTest.EqualitySetup<R> data,
                                                          TranslationResult<R> result)
        Asserts the naming and the version in result.
        Parameters:
        data - the data describing the setup
        result - the data obtained from parsing
        Returns:
        the message to be used in assertions
      • toString

        public static java.lang.String toString​(java.util.List<Message> messages)
        Turns a list of messages into a readable string.
        Parameters:
        messages - the messages to be turned into a string
        Returns:
        the textual representation of the messages