All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.aoreshin.junit5.extensions.GenericParameterResolver Maven / Gradle / Ivy

There is a newer version: 83
Show newest version
package com.github.aoreshin.junit5.extensions;

import java.util.Objects;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;

/**
 * Generic parameter resolver implementation
 *
 * @param  - type of parameter to be resolved
 */
public class GenericParameterResolver implements ParameterResolver {
  private final T value;
  private final Class parameterType;
  private final String name;

  GenericParameterResolver(Class parameterType, T value) {
    this.parameterType = parameterType;
    this.value = value;
    this.name = null;
  }

  GenericParameterResolver(Class parameterType, T value, String name) {
    this.parameterType = parameterType;
    this.value = value;
    this.name = name;
  }

  @Override
  public boolean supportsParameter(
      ParameterContext parameterContext, ExtensionContext extensionContext)
      throws ParameterResolutionException {
    if (parameterContext.getParameter().getType() == parameterType) {
      if (name != null) {
        return parameterContext.getParameter().getName().equals(name);
      }

      return true;
    }

    return false;
  }

  @Override
  public Object resolveParameter(
      ParameterContext parameterContext, ExtensionContext extensionContext)
      throws ParameterResolutionException {
    return value;
  }

  @Generated
  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    GenericParameterResolver that = (GenericParameterResolver) o;
    return Objects.equals(value, that.value)
        && Objects.equals(parameterType, that.parameterType)
        && Objects.equals(name, that.name);
  }

  @Generated
  @Override
  public int hashCode() {
    return Objects.hash(value, parameterType, name);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy