001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase;
019
020import org.junit.jupiter.api.extension.ExtensionContext;
021import org.junit.jupiter.api.extension.ParameterContext;
022import org.junit.jupiter.api.extension.ParameterResolutionException;
023import org.junit.jupiter.api.extension.ParameterResolver;
024import org.junit.jupiter.params.provider.Arguments;
025
026import org.apache.hbase.thirdparty.com.google.common.primitives.Primitives;
027
028/**
029 * @see HBaseParameterizedTestTemplate
030 */
031public class HBaseParameterizedParameterResolver implements ParameterResolver {
032
033  private final Object[] values;
034
035  HBaseParameterizedParameterResolver(Arguments arguments) {
036    this.values = arguments.get();
037  }
038
039  @Override
040  public boolean supportsParameter(ParameterContext pc, ExtensionContext ec)
041    throws ParameterResolutionException {
042    int index = pc.getIndex();
043    if (index >= values.length) {
044      return false;
045    }
046    Object value = values[index];
047    Class<?> expectedType = pc.getParameter().getType();
048    if (expectedType.isPrimitive()) {
049      // primitive type can not accept null value
050      if (value == null) {
051        return false;
052      }
053      // test with wrapper type, otherwise it will always return false
054      return Primitives.wrap(expectedType).isAssignableFrom(value.getClass());
055    }
056    return expectedType.isAssignableFrom(value.getClass());
057  }
058
059  @Override
060  public Object resolveParameter(ParameterContext pc, ExtensionContext ec)
061    throws ParameterResolutionException {
062    return values[pc.getIndex()];
063  }
064}