Skip to content

Commit

Permalink
fix issue #149; make method parameter injection work for parametrized…
Browse files Browse the repository at this point in the history
… types
  • Loading branch information
Johannes Hahn authored and manovotn committed Dec 5, 2022
1 parent 503db2b commit 91b7b3d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte
if (getContainerFromStore(extensionContext) != null) {
List<Annotation> qualifiers = resolveQualifiers(parameterContext, getContainerFromStore(extensionContext).getBeanManager());
return getContainerFromStore(extensionContext)
.select(parameterContext.getParameter().getType(), qualifiers.toArray(new Annotation[qualifiers.size()])).get();
.select(parameterContext.getParameter().getParameterizedType(),
qualifiers.toArray(new Annotation[qualifiers.size()])).get();
}
return null;
}
Expand All @@ -167,7 +168,9 @@ public boolean supportsParameter(ParameterContext parameterContext, ExtensionCon
} else {
// attempt to resolve the bean; at this point we know it should be a CDI bean since it has CDI qualifiers
// if resolution fails, throw an exception
WeldInstance<?> select = getContainerFromStore(extensionContext).select(parameterContext.getParameter().getType(), qualifiers.toArray(new Annotation[qualifiers.size()]));
WeldInstance<?> select = getContainerFromStore(extensionContext).select(
parameterContext.getParameter().getParameterizedType(),
qualifiers.toArray(new Annotation[qualifiers.size()]));
if (!select.isResolvable()) {
throw new ParameterResolutionException(String.format("Weld has failed to resolve test parameter [%s] in method [%s].%n" +
"%s dependency has type %s and qualifiers %s.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@
*/
package org.jboss.weld.junit5.extensionInjection;

import jakarta.enterprise.event.Event;
import jakarta.enterprise.inject.Instance;
import jakarta.inject.Inject;

import org.jboss.weld.junit5.WeldJunit5Extension;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.util.Map;

/**
* Basic test for JUnit 5 injection into parameter/field handled by Weld
* @author <a href="mailto:manovotn@redhat.com">Matej Novotny</a>
Expand Down Expand Up @@ -54,4 +58,26 @@ public void testparamInjectionWithQualifier(@MyQualifier BarBean bar) {
Assertions.assertNotNull(bar);
bar.ping();
}
@Nested
class TestParameterInjectionWithParametrizedTypes {

@Test
public void testparamInjectionWithEvent(Event<String> stringEvent) {
// assert param injection with built-in Event bean works
Assertions.assertNotNull(stringEvent);
}

@Test
public void testparamInjectionWithInstance(Instance<String> stringInstance) {
// assert param injection with built-in Instance works
Assertions.assertNotNull(stringInstance);
}

@Test
public void testparamInjectionWithOtherParametrizedType(Map<String, Integer> map) {
// assert param injection with ordinary bean with parametrized type works
Assertions.assertNotNull(map);
Assertions.assertTrue(map.containsKey("java.util.Map<java.lang.String, java.lang.Integer>"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.jboss.weld.junit5.extensionInjection;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.context.Dependent;
import jakarta.enterprise.inject.Produces;
import jakarta.enterprise.inject.spi.InjectionPoint;

import java.util.HashMap;
import java.util.Map;

@ApplicationScoped
public class MapProducer {

@Produces
@Dependent
public <T> Map<String,T> produceMap(InjectionPoint ip) {
Map<String, T> map = new HashMap<>();
map.put(ip.getType().getTypeName(), null);
return map;
}
}

0 comments on commit 91b7b3d

Please sign in to comment.