use of jakarta.enterprise.util.TypeLiteral in project core by weld.
the class CovariantTypesTest method testWildcardAssignableFromWildcard2.
@Test
@SuppressWarnings("serial")
public <A extends Throwable, B extends A, C extends B, D extends Exception> void testWildcardAssignableFromWildcard2() {
// (both) wildcards bounded by a type variable
final int count = 4;
Type[] typeVariables = new Type[count];
final Type a = typeVariables[0] = new TypeLiteral<A>() {
}.getType();
final Type b = typeVariables[1] = new TypeLiteral<B>() {
}.getType();
final Type c = typeVariables[2] = new TypeLiteral<C>() {
}.getType();
final Type d = typeVariables[3] = new TypeLiteral<D>() {
}.getType();
for (int i = 0; i < count; i++) {
// unbounded wildcard should be assignable from anything
assertTrue(CovariantTypes.isAssignableFrom(WildcardTypeImpl.defaultInstance(), WildcardTypeImpl.withUpperBound(typeVariables[i])));
assertTrue(CovariantTypes.isAssignableFrom(WildcardTypeImpl.defaultInstance(), WildcardTypeImpl.withLowerBound(typeVariables[i])));
for (int j = 0; j < count; j++) {
// wildcard with an upper bound is UNassignable from a wildcard with a lower bound and vice versa
assertFalse(CovariantTypes.isAssignableFrom(WildcardTypeImpl.withUpperBound(typeVariables[i]), WildcardTypeImpl.withLowerBound(typeVariables[j])));
assertFalse(CovariantTypes.isAssignableFrom(WildcardTypeImpl.withLowerBound(typeVariables[i]), WildcardTypeImpl.withUpperBound(typeVariables[j])));
if (i == j || (i < j && j != 3)) {
assertTrue(CovariantTypes.isAssignableFrom(WildcardTypeImpl.withUpperBound(typeVariables[i]), WildcardTypeImpl.withUpperBound(typeVariables[j])));
assertTrue(CovariantTypes.isAssignableFrom(WildcardTypeImpl.withLowerBound(typeVariables[j]), WildcardTypeImpl.withLowerBound(typeVariables[i])));
} else {
assertFalse(CovariantTypes.isAssignableFrom(WildcardTypeImpl.withUpperBound(typeVariables[i]), WildcardTypeImpl.withUpperBound(typeVariables[j])));
assertFalse(CovariantTypes.isAssignableFrom(WildcardTypeImpl.withLowerBound(typeVariables[j]), WildcardTypeImpl.withLowerBound(typeVariables[i])));
}
}
}
// one wildcard bounded by a class and the other bounded by a type variable
assertTrue(CovariantTypes.isAssignableFrom(WildcardTypeImpl.withUpperBound(Exception.class), WildcardTypeImpl.withUpperBound(d)));
assertFalse(CovariantTypes.isAssignableFrom(WildcardTypeImpl.withUpperBound(RuntimeException.class), WildcardTypeImpl.withUpperBound(d)));
assertTrue(CovariantTypes.isAssignableFrom(WildcardTypeImpl.withUpperBound(Serializable.class), WildcardTypeImpl.withUpperBound(b)));
assertTrue(CovariantTypes.isAssignableFrom(WildcardTypeImpl.withUpperBound(Throwable.class), WildcardTypeImpl.withUpperBound(b)));
assertFalse(CovariantTypes.isAssignableFrom(WildcardTypeImpl.withUpperBound(Exception.class), WildcardTypeImpl.withUpperBound(b)));
assertTrue(CovariantTypes.isAssignableFrom(WildcardTypeImpl.withLowerBound(a), WildcardTypeImpl.withLowerBound(Throwable.class)));
assertTrue(CovariantTypes.isAssignableFrom(WildcardTypeImpl.withLowerBound(c), WildcardTypeImpl.withLowerBound(Throwable.class)));
assertFalse(CovariantTypes.isAssignableFrom(WildcardTypeImpl.withLowerBound(a), WildcardTypeImpl.withLowerBound(Exception.class)));
assertFalse(CovariantTypes.isAssignableFrom(WildcardTypeImpl.withLowerBound(c), WildcardTypeImpl.withLowerBound(Exception.class)));
assertTrue(CovariantTypes.isAssignableFrom(WildcardTypeImpl.withLowerBound(d), WildcardTypeImpl.withLowerBound(Throwable.class)));
assertTrue(CovariantTypes.isAssignableFrom(WildcardTypeImpl.withLowerBound(d), WildcardTypeImpl.withLowerBound(Exception.class)));
assertFalse(CovariantTypes.isAssignableFrom(WildcardTypeImpl.withLowerBound(d), WildcardTypeImpl.withLowerBound(RuntimeException.class)));
assertFalse(CovariantTypes.isAssignableFrom(WildcardTypeImpl.withLowerBound(RuntimeException.class), WildcardTypeImpl.withLowerBound(a)));
assertFalse(CovariantTypes.isAssignableFrom(WildcardTypeImpl.withLowerBound(Throwable.class), WildcardTypeImpl.withLowerBound(a)));
assertFalse(CovariantTypes.isAssignableFrom(WildcardTypeImpl.withLowerBound(RuntimeException.class), WildcardTypeImpl.withLowerBound(d)));
assertFalse(CovariantTypes.isAssignableFrom(WildcardTypeImpl.withLowerBound(Throwable.class), WildcardTypeImpl.withLowerBound(d)));
// mix upper bounds with lower bounds
assertFalse(CovariantTypes.isAssignableFrom(WildcardTypeImpl.withUpperBound(Exception.class), WildcardTypeImpl.withLowerBound(d)));
assertFalse(CovariantTypes.isAssignableFrom(WildcardTypeImpl.withLowerBound(Exception.class), WildcardTypeImpl.withUpperBound(d)));
assertFalse(CovariantTypes.isAssignableFrom(WildcardTypeImpl.withLowerBound(d), WildcardTypeImpl.withUpperBound(Exception.class)));
assertFalse(CovariantTypes.isAssignableFrom(WildcardTypeImpl.withUpperBound(d), WildcardTypeImpl.withLowerBound(Exception.class)));
}
use of jakarta.enterprise.util.TypeLiteral in project core by weld.
the class EventTypeResolutionTest method testWithInterfaces4.
@Test
public void testWithInterfaces4() {
Type selectedType = new TypeLiteral<Interface4<Set<Integer>>>() {
}.getType();
Type type = resolveType(selectedType, Charlie.class);
assertEquals(new TypeLiteral<Charlie<Integer>>() {
}.getType(), type);
}
use of jakarta.enterprise.util.TypeLiteral in project core by weld.
the class EventTypeResolutionTest method testWithInterfaces6.
@Test
public void testWithInterfaces6() {
Type selectedType = new TypeLiteral<Interface5<Map<Double, Set<Long>>>>() {
}.getType();
Type type = resolveType(selectedType, Charlie.class);
assertEquals(new TypeLiteral<Charlie<Long>>() {
}.getType(), type);
}
use of jakarta.enterprise.util.TypeLiteral in project core by weld.
the class ContainerLifecyleObserverTest method testExtensionBuilder.
@SuppressWarnings({ "serial" })
@Test
public void testExtensionBuilder() {
ActionSequence.reset();
Extension myExtension = ContainerLifecycleObserver.extensionBuilder().add(beforeBeanDiscovery((e) -> addAction(BeforeBeanDiscovery.class.getSimpleName()))).add(afterTypeDiscovery().notify((e, b) -> {
addAction(AfterTypeDiscovery.class.getSimpleName());
e.addAnnotatedType(b.createAnnotatedType(Charlie.class), Charlie.class.getName());
})).add(afterBeanDiscovery((e) -> {
addAction(AfterBeanDiscovery.class.getSimpleName());
e.addObserverMethod().beanClass(Foo.class).observedType(Foo.class).notifyWith((ctx) -> {
});
e.addBean().beanClass(Integer.class).addType(Integer.class).addQualifier(Juicy.Literal.INSTANCE).createWith((ctx) -> Integer.valueOf(10));
})).add(afterDeploymentValidation((e) -> addAction(AfterDeploymentValidation.class.getSimpleName()))).add(beforeShutdown((e) -> addAction(BeforeShutdown.class.getSimpleName()))).build();
Extension myExtension2 = ContainerLifecycleObserver.extensionBuilder().add(processAnnotatedType().withAnnotations(RequestScoped.class).notify((e) -> e.veto())).add(processBeanAttributes().notify((e) -> addAction(ProcessBeanAttributes.class.getSimpleName()))).add(processSyntheticAnnotatedType(new TypeLiteral<ProcessSyntheticAnnotatedType<?>>() {
}.getType()).notify((e) -> addAction(ProcessSyntheticAnnotatedType.class.getSimpleName()))).add(processInjectionPoint().notify((e) -> addAction(ProcessInjectionPoint.class.getSimpleName()))).add(processProducer().notify((e) -> addAction(ProcessProducer.class.getSimpleName()))).add(processBean().notify((e) -> addAction(ProcessBean.class.getSimpleName()))).add(processManagedBean().notify((e) -> addAction(ProcessManagedBean.class.getSimpleName()))).add(processProducerField().notify((e) -> addAction(ProcessProducerField.class.getSimpleName()))).add(processProducerMethod().notify((e) -> {
// Weld SE defines some producer methods, e.g. ParametersFactory
addAction(ProcessProducerMethod.class.getSimpleName());
})).add(processBeanAttributes().notify((e) -> addAction(ProcessBeanAttributes.class.getSimpleName()))).add(processObserverMethod().notify((e) -> addAction(ProcessObserverMethod.class.getSimpleName()))).add(processObserverMethod(new TypeLiteral<ProcessObserverMethod<String, ?>>() {
}.getType()).notify((e) -> addAction(ProcessObserverMethod.class.getSimpleName() + String.class.getSimpleName()))).add(processSyntheticObserverMethod(new TypeLiteral<ProcessSyntheticObserverMethod<Foo, ?>>() {
}.getType()).notify((e) -> addAction(ProcessSyntheticObserverMethod.class.getSimpleName() + Foo.class.getSimpleName()))).add(processSyntheticBean(new TypeLiteral<ProcessSyntheticBean<Integer>>() {
}.getType()).notify((e) -> addAction(ProcessSyntheticBean.class.getSimpleName() + Integer.class.getSimpleName()))).build();
try (WeldContainer container = new Weld().disableDiscovery().beanClasses(Foo.class, Bravo.class).addExtension(myExtension).addExtension(myExtension2).initialize()) {
assertTrue(container.select(Foo.class).isUnsatisfied());
assertFalse(container.select(Bravo.class).isUnsatisfied());
Assert.assertEquals(Integer.valueOf(10), container.select(Integer.class, Juicy.Literal.INSTANCE).get());
}
ActionSequence.assertSequenceDataContainsAll(BeforeBeanDiscovery.class, AfterTypeDiscovery.class, AfterBeanDiscovery.class, AfterDeploymentValidation.class, BeforeShutdown.class);
ActionSequence.assertSequenceDataContainsAll(ProcessBeanAttributes.class, ProcessSyntheticAnnotatedType.class, ProcessInjectionPoint.class, ProcessObserverMethod.class, ProcessBeanAttributes.class, ProcessProducer.class);
ActionSequence.assertSequenceDataContainsAll(ProcessObserverMethod.class.getSimpleName() + String.class.getSimpleName(), ProcessSyntheticObserverMethod.class.getSimpleName() + Foo.class.getSimpleName(), ProcessSyntheticBean.class.getSimpleName() + Integer.class.getSimpleName());
ActionSequence.assertSequenceDataContainsAll(ProcessBean.class, ProcessManagedBean.class, ProcessProducerMethod.class, ProcessProducerField.class);
}
use of jakarta.enterprise.util.TypeLiteral in project core by weld.
the class ContainerLifecyleObserverTest method testAddContainerLifecycleObserver.
@SuppressWarnings("serial")
@Test
public void testAddContainerLifecycleObserver() {
final AtomicBoolean called = new AtomicBoolean(false);
try (WeldContainer container = new Weld().disableDiscovery().beanClasses(Foo.class).addContainerLifecycleObserver(processAnnotatedType(new TypeLiteral<ProcessAnnotatedType<Foo>>() {
}.getType()).notify((e) -> e.veto())).addContainerLifecycleObserver(afterBeanDiscovery((e) -> called.set(true))).initialize()) {
assertTrue(called.get());
assertTrue(container.select(Foo.class).isUnsatisfied());
}
}
Aggregations