Class MethodWrapper<T,R>

java.lang.Object
io.github.valentinherrmann.levenshtein.Wrapper<T>
io.github.valentinherrmann.levenshtein.MethodWrapper<T,R>
Type Parameters:
T - the type of the class containing the method
R - the expected return type of the method

public class MethodWrapper<T,R> extends Wrapper<T>
Wrapper for class methods that verifies their existence, name, return type, parameter types, and modifiers using Levenshtein distance for fuzzy name matching.
  • Constructor Details

    • MethodWrapper

      public MethodWrapper(ClassWrapper<T> parentClass, String expectedName, Class<R> expectedReturnType, Class<?>[] paramTypes, String... modifiers)
      Constructs a new MethodWrapper for verifying a class method.
      Parameters:
      parentClass - the class wrapper containing this method
      expectedName - the expected name of the method
      expectedReturnType - the expected return type of the method
      paramTypes - the expected parameter types for the method
      modifiers - the expected modifiers (e.g., "public", "static")
    • MethodWrapper

      public MethodWrapper(ClassWrapper<T> parentClass, String expectedName, Class<R> expectedReturnType, Object[] paramTypes, String... modifiers)
      Constructs a new MethodWrapper whose parameter types may include student classes. Each parameter type is a Class, a ClassWrapper (e.g. ClassWrapper.reference("Guitarist", "band")) or a Supplier of a class wrapper (needs a cast in an array: (Supplier<ClassWrapper<?>>) () -> x). Student classes are only looked up when the method is checked; if one is missing, only the parameters count as missing.
      Parameters:
      parentClass - the class wrapper containing this method
      expectedName - the expected name of the method
      expectedReturnType - the expected return type of the method
      paramTypes - the expected parameter types for the method
      modifiers - the expected modifiers (e.g., "public", "static")
      Throws:
      IllegalArgumentException - if a parameter type is null or of another kind
    • MethodWrapper

      public MethodWrapper(ClassWrapper<T> parentClass, String expectedName, ClassWrapper<? extends R> expectedReturnType, Object[] paramTypes, String... modifiers)
      Constructs a new MethodWrapper whose return type is a student class, e.g. Drummer getDrummer(). The return type is only looked up when the method is checked; if the student class is missing, only the return type counts as missing.
      Parameters:
      parentClass - the class wrapper containing this method
      expectedName - the expected name of the method
      expectedReturnType - the wrapper of the expected return type
      paramTypes - the expected parameter types, see MethodWrapper(ClassWrapper, String, Class, Object[], String...)
      modifiers - the expected modifiers (e.g., "public", "static")
    • MethodWrapper

      public MethodWrapper(ClassWrapper<T> parentClass, String expectedName, ClassWrapper<? extends R> expectedReturnType, String... modifiers)
      Parameters:
      parentClass - the class wrapper containing this method
      expectedName - the expected name of the method
      expectedReturnType - the wrapper of the expected return type
      modifiers - the expected modifiers (e.g., "public", "static")
    • MethodWrapper

      public MethodWrapper(ClassWrapper<T> parentClass, String expectedName, Supplier<? extends ClassWrapper<? extends R>> expectedReturnType, Object[] paramTypes, String... modifiers)
      Like MethodWrapper(ClassWrapper, String, ClassWrapper, Object[], String...), but the wrapper of the return type is only taken from the supplier when the method is checked, e.g. () -> TestManager.drummer.
      Parameters:
      parentClass - the class wrapper containing this method
      expectedName - the expected name of the method
      expectedReturnType - supplies the wrapper of the expected return type
      paramTypes - the expected parameter types, see MethodWrapper(ClassWrapper, String, Class, Object[], String...)
      modifiers - the expected modifiers (e.g., "public", "static")
    • MethodWrapper

      public MethodWrapper(ClassWrapper<T> parentClass, String expectedName, Supplier<? extends ClassWrapper<? extends R>> expectedReturnType, String... modifiers)
      Parameters:
      parentClass - the class wrapper containing this method
      expectedName - the expected name of the method
      expectedReturnType - supplies the wrapper of the expected return type
      modifiers - the expected modifiers (e.g., "public", "static")
    • MethodWrapper

      public MethodWrapper(ClassWrapper<T> parentClass, String expectedName, Class<R> expectedReturnType, String... modifiers)
      Constructs a new MethodWrapper for a method with no parameters.
      Parameters:
      parentClass - the class wrapper containing this method
      expectedName - the expected name of the method
      expectedReturnType - the expected return type of the method
      modifiers - the expected modifiers (e.g., "public", "static")
  • Method Details

    • verifyExistence

      public void verifyExistence(boolean throwAssertion)
      Description copied from class: Wrapper
      Verifies that the wrapped element exists in the actual class. Subclasses must implement this to perform element-specific existence checks.
      Specified by:
      verifyExistence in class Wrapper<T>
      Parameters:
      throwAssertion - if true, throws an AssertJ assertion failure when the element is missing. Usually set to true for behavioral tests, and false for structural tests to allow further deviation analysis.
    • legacyReportsUnexpectedStatic

      protected boolean legacyReportsUnexpectedStatic()
      Description copied from class: Wrapper
      Whether an unexpected static is reported with LevenshteinSettings.UnexpectedModifiers.IGNORE (the behaviour before unexpected modifiers were reported in general). Attributes and methods turn it on.
      Overrides:
      legacyReportsUnexpectedStatic in class Wrapper<T>
      Returns:
      true if an unexpected static is reported in the legacy mode
    • relevantModifierMask

      protected int relevantModifierMask()
      Description copied from class: Wrapper
      The modifier bits that are meaningful for this kind of element. Other bits of the bitmask are ignored: for methods, the bits of volatile and transient mean bridge and varargs. The default fits classes; methods, attributes and constructors override it.
      Overrides:
      relevantModifierMask in class Wrapper<T>
      Returns:
      the mask of relevant modifier bits
    • implicitModifiers

      protected int implicitModifiers()
      Abstract methods of interfaces are implicitly abstract.
      Overrides:
      implicitModifiers in class Wrapper<T>
      Returns:
      the mask of implicit modifier bits
    • findWithDeviation

      protected void findWithDeviation()
      Description copied from class: Wrapper
      Searches for the wrapped element using deviation detection (e.g., Levenshtein distance). Subclasses must implement this to perform element-specific searches and update the property states. Called at most once per wrapper, see Wrapper.ensureChecked().
      Specified by:
      findWithDeviation in class Wrapper<T>
    • parseExistence

      protected void parseExistence()
      Description copied from class: Wrapper
      Parses and updates the existence state based on all properties. Subclasses must implement this to define how to aggregate property states.
      Specified by:
      parseExistence in class Wrapper<T>
    • invoke

      public R invoke(Object... params)
      Invokes the method on the class's default instance (or null for static methods). An exception thrown by the student code fails the test with the exception type, its message and the argument values.

      Pass a single array argument as (Object) array: otherwise Java spreads an object array (e.g. String[]) into several arguments. A wrong number of arguments is reported as IllegalArgumentException.

      Parameters:
      params - values for the method's parameters
      Returns:
      the return value of the method, safely cast to R
      See Also:
    • invokeWithContext

      public R invokeWithContext(Supplier<String> context, Object... params)
      Like invoke(Object...), but if the student code throws, the text of context is put in front of the failure message, e.g. the scenario or the seed of generated inputs:
      car.calculateCost().invokeWithContext(() -> "Scenario " + i + ", seed " + seed, years);
      
      The supplier is only evaluated if the call fails.

      Pass a single array argument as (Object) array: otherwise Java spreads an object array into several arguments.

      Parameters:
      context - supplies the text shown in front of the failure message (may be null)
      params - values for the method's parameters
      Returns:
      the return value of the method, safely cast to R
    • invokeOnSpecificObject

      public R invokeOnSpecificObject(Object obj, Object... params)
      Invokes the method on a specific object instance. An exception thrown by the student code fails the test with the exception type, its message and the argument values; use invokeExpectingException(Class, Object, Object...) if an exception is the expected outcome.

      Pass a single array argument as (Object) array: otherwise Java spreads an object array (e.g. String[]) into several arguments. A wrong number of arguments is reported as IllegalArgumentException.

      Parameters:
      obj - the object to invoke the method on (null uses default instance or null for static methods)
      params - values for the method's parameters
      Returns:
      the return value of the method, safely cast to R
      See Also:
    • invokeOnSpecificObjectWithContext

      public R invokeOnSpecificObjectWithContext(Supplier<String> context, Object obj, Object... params)
      Like invokeOnSpecificObject(Object, Object...), but if the student code throws, the text of context is put in front of the failure message (e.g. the scenario or the seed of generated inputs). The supplier is only evaluated if the call fails.

      Pass a single array argument as (Object) array: otherwise Java spreads an object array into several arguments.

      Parameters:
      context - supplies the text shown in front of the failure message (may be null)
      obj - the object to invoke the method on (null uses default instance or null for static methods)
      params - values for the method's parameters
      Returns:
      the return value of the method, safely cast to R
    • invokeExpectingException

      public <X extends Throwable> X invokeExpectingException(Class<X> expected, Object obj, Object... params)
      Invokes the method and expects the student code to throw an exception of the given type.

      Pass a single array argument as (Object) array: otherwise Java spreads an object array into several arguments.

      Type Parameters:
      X - the exception type
      Parameters:
      expected - the expected exception type (subclasses are accepted)
      obj - the object to invoke the method on (null uses default instance or null for static methods)
      params - values for the method's parameters
      Returns:
      the thrown exception, e.g. for further assertions on its message
    • getParamTypes

      public Class<?>[] getParamTypes()
      Resolves student classes given as parameter types.
      Returns:
      the expected parameter types; a student class that is missing is null
    • getExpectedReturnType

      public Class<?> getExpectedReturnType()
      Resolves a student class given as return type.
      Returns:
      the expected return type, or null if none was given or the student class is missing
    • getActualParamTypes

      public Class<?>[] getActualParamTypes()
      Returns:
      the parameter types of the student's method, or the expected ones if the method is missing
    • expectedToString

      public String expectedToString()
      Description copied from class: Wrapper
      Returns a string representation of the expected element signature. Subclasses must implement this to provide element-specific formatting.
      Specified by:
      expectedToString in class Wrapper<T>
      Returns:
      a string showing the expected element details
    • actualToString

      public String actualToString()
      Description copied from class: Wrapper
      Returns a string representation of the actual element signature found. Subclasses must implement this to provide element-specific formatting.
      Specified by:
      actualToString in class Wrapper<T>
      Returns:
      a string showing the actual element details