
org.apache.openwebbeans.junit5.internal.CdiParametersResolverExtension Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.openwebbeans.junit5.internal;
import org.junit.jupiter.api.extension.AfterEachCallback;
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;
import jakarta.enterprise.context.spi.CreationalContext;
import jakarta.enterprise.inject.spi.Bean;
import jakarta.enterprise.inject.spi.BeanManager;
import jakarta.enterprise.inject.spi.CDI;
import java.lang.annotation.Annotation;
import java.lang.reflect.Parameter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class CdiParametersResolverExtension implements ParameterResolver, AfterEachCallback
{
private static final ExtensionContext.Namespace NAMESPACE =
ExtensionContext.Namespace.create(CdiParametersResolverExtension.class.getName());
@Override
public boolean supportsParameter(final ParameterContext parameterContext, final ExtensionContext extensionContext)
throws ParameterResolutionException
{
final Instances instances = extensionContext.getStore(NAMESPACE).getOrComputeIfAbsent(
Instances.class, k -> new Instances(), Instances.class);
if (instances != null && instances.instances != null &&
instances.instances.containsKey(parameterContext.getParameter()))
{
return false; // already handled
}
try
{
final Parameter parameter = parameterContext.getParameter();
final BeanManager bm = CDI.current().getBeanManager();
final Bean> bean = resolveParameterBean(bm, parameter);
if (bean == null)
{
return false;
}
final CreationalContext
© 2015 - 2025 Weber Informatics LLC | Privacy Policy