martin-g commented on code in PR #1313:
URL: https://github.com/apache/wicket/pull/1313#discussion_r2580009255
##########
wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/AnnotProxyFieldValueFactoryTest.java:
##########
@@ -16,150 +16,310 @@
*/
package org.apache.wicket.spring.injection.annot;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+
+import java.lang.reflect.Field;
+import java.util.stream.Stream;
+
import org.apache.wicket.proxy.ILazyInitProxy;
-import org.apache.wicket.spring.ISpringContextLocator;
-import org.apache.wicket.spring.SpringBeanLocator;
-import org.apache.wicket.spring.injection.util.Bean;
-import org.apache.wicket.spring.injection.util.Bean2;
-import org.apache.wicket.spring.injection.util.InjectableInterface;
+import org.apache.wicket.proxy.IProxyTargetLocator;
import org.apache.wicket.spring.test.ApplicationContextMock;
+import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
-import org.springframework.context.ApplicationContext;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.springframework.beans.factory.support.AbstractBeanDefinition;
-import java.lang.reflect.Field;
-
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertNull;
-import static org.junit.jupiter.api.Assertions.assertSame;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
+import jakarta.inject.Inject;
+import jakarta.inject.Named;
/**
- * Tests for BeanAnnotLocatorFactory
+ * Tests for AnnotProxyFieldValueFactory
*
* @author igor
+ * @author hosea
*/
-public abstract class AnnotProxyFieldValueFactoryTest
+public class AnnotProxyFieldValueFactoryTest
{
- ISpringContextLocator mockCtxLocator = new ISpringContextLocator()
+
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldCreateProxyForBeanName(final Object obj) throws
Exception
{
- private static final long serialVersionUID = 1L;
+ final Bean somebean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ // add two beans to make sure wiring by name is different by
wiring by class
+ applicationContext.putBean(new Bean());
+ applicationContext.putBean("somebean", somebean);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByName");
+ final Object beanByClassProxy =
factory.getFieldValue(beanByClassField, obj);
+ final ILazyInitProxy lazyInitProxy =
assertInstanceOf(ILazyInitProxy.class, beanByClassProxy);
+ final IProxyTargetLocator beanByClassLocator =
lazyInitProxy.getObjectLocator();
+ assertSame(somebean, beanByClassLocator.locateProxyTarget());
+ }
- @Override
- public ApplicationContext getSpringContext()
- {
- ApplicationContextMock mock = new
ApplicationContextMock();
- mock.putBean(new Bean());
- mock.putBean("somebean", new Bean2());
- return mock;
- }
- };
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldThrowExceptionIfBeanNameNotFound(final Object obj)
throws Exception
+ {
+ final Bean somebean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ // add two beans to make sure wiring by name is different by
wiring by class
+ applicationContext.putBean(new Bean());
+ applicationContext.putBean("wrongNameBean", somebean);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
- protected final InjectableInterface obj;
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByName");
+ Assertions.assertThrows(IllegalStateException.class, () ->
factory.getFieldValue(beanByClassField, obj));
+ }
- protected final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(mockCtxLocator);
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldCreateProxyForClass(final Object obj) throws Exception
+ {
+ final Bean bean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ applicationContext.putBean(bean);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ final Object proxy = factory.getFieldValue(beanByClassField,
obj);
+ final ILazyInitProxy lazyInitProxy =
assertInstanceOf(ILazyInitProxy.class, proxy);
+ final IProxyTargetLocator beanByClassLocator =
lazyInitProxy.getObjectLocator();
+ assertSame(bean, beanByClassLocator.locateProxyTarget());
+ }
- protected AnnotProxyFieldValueFactoryTest(InjectableInterface
injectable)
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldThrowException_beanNameAmbiguous(final Object obj)
throws Exception
{
- obj = injectable;
+ final Bean primaryBean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ // add two beans to make ambiguous
+ applicationContext.putBean("anyBean", new Bean());
+ applicationContext.putBean("primaryBean", primaryBean);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ Assertions.assertThrows(IllegalStateException.class, () ->
factory.getFieldValue(beanByClassField, obj));
+ }
+
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldCreateProxyForUniquePrimary_beanNameAmbiguous(final
Object obj) throws Exception
+ {
+ final Bean primaryBean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ // add two beans to make ambiguous
+ applicationContext.putBean("anyBean", new Bean());
+ applicationContext.putBean("primaryBean", primaryBean);
+
applicationContext.getBeanFactory().getBeanDefinition("primaryBean").setPrimary(true);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ final Object beanByClassProxy =
factory.getFieldValue(beanByClassField, obj);
+ final ILazyInitProxy lazyInitProxy =
assertInstanceOf(ILazyInitProxy.class, beanByClassProxy);
+ final IProxyTargetLocator beanByClassLocator =
lazyInitProxy.getObjectLocator();
+ assertSame(primaryBean, beanByClassLocator.locateProxyTarget());
+ }
+
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldCreateProxyForFieldname_beanNameAmbiguous(final
Object obj) throws Exception
+ {
+ final Bean bean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ applicationContext.putBean(new Bean());
+ applicationContext.putBean("beanByClass", bean);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ final Object proxy = factory.getFieldValue(beanByClassField,
obj);
+ final ILazyInitProxy lazyInitProxy =
assertInstanceOf(ILazyInitProxy.class, proxy);
+ final IProxyTargetLocator beanByClassLocator =
lazyInitProxy.getObjectLocator();
+ assertSame(bean, beanByClassLocator.locateProxyTarget());
+ }
+
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldIgnoreUnannotatedFields(final Object obj) throws
Exception
+ {
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(ApplicationContextMock::new);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("nobean");
+ final Object beanByClassProxy =
factory.getFieldValue(beanByClassField, obj);
+ assertNull(beanByClassProxy);
+ }
+
+ // https://issues.apache.org/jira/browse/WICKET-7170
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void
shouldCreateProxyForUniqueDefaultCandidate_beanNameAmbiguous(final Object obj)
throws Exception
+ {
+ final Bean defaultCandidate = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ // add two beans to make ambiguous
+ applicationContext.putBean("anyBean", new Bean());
+ applicationContext.putBean("primaryBean", defaultCandidate);
+ final AbstractBeanDefinition abstractBeanDefinition =
(AbstractBeanDefinition)
applicationContext.getBeanFactory().getBeanDefinition("anyBean");
+ abstractBeanDefinition.setDefaultCandidate(false);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ final Object beanByClassProxy =
factory.getFieldValue(beanByClassField, obj);
+ final ILazyInitProxy lazyInitProxy =
assertInstanceOf(ILazyInitProxy.class, beanByClassProxy);
+ final IProxyTargetLocator beanByClassLocator =
lazyInitProxy.getObjectLocator();
+ assertSame(defaultCandidate,
beanByClassLocator.locateProxyTarget());
}
/**
- * Test the factory
- *
- * @throws Exception
+ * test the cache, make sure the same proxy is returned for the same
dependency it represents
*/
- @Test
- public void testFactory() throws Exception
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void testCacheForBeanName(final Object obj) throws Exception
Review Comment:
```suggestion
public void testCacheForBeanByClass(final Object obj) throws Exception
```
##########
wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/AnnotProxyFieldValueFactoryTest.java:
##########
@@ -16,150 +16,310 @@
*/
package org.apache.wicket.spring.injection.annot;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+
+import java.lang.reflect.Field;
+import java.util.stream.Stream;
+
import org.apache.wicket.proxy.ILazyInitProxy;
-import org.apache.wicket.spring.ISpringContextLocator;
-import org.apache.wicket.spring.SpringBeanLocator;
-import org.apache.wicket.spring.injection.util.Bean;
-import org.apache.wicket.spring.injection.util.Bean2;
-import org.apache.wicket.spring.injection.util.InjectableInterface;
+import org.apache.wicket.proxy.IProxyTargetLocator;
import org.apache.wicket.spring.test.ApplicationContextMock;
+import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
-import org.springframework.context.ApplicationContext;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.springframework.beans.factory.support.AbstractBeanDefinition;
-import java.lang.reflect.Field;
-
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertNull;
-import static org.junit.jupiter.api.Assertions.assertSame;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
+import jakarta.inject.Inject;
+import jakarta.inject.Named;
/**
- * Tests for BeanAnnotLocatorFactory
+ * Tests for AnnotProxyFieldValueFactory
*
* @author igor
+ * @author hosea
*/
-public abstract class AnnotProxyFieldValueFactoryTest
+public class AnnotProxyFieldValueFactoryTest
{
- ISpringContextLocator mockCtxLocator = new ISpringContextLocator()
+
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldCreateProxyForBeanName(final Object obj) throws
Exception
{
- private static final long serialVersionUID = 1L;
+ final Bean somebean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ // add two beans to make sure wiring by name is different by
wiring by class
+ applicationContext.putBean(new Bean());
+ applicationContext.putBean("somebean", somebean);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByName");
+ final Object beanByClassProxy =
factory.getFieldValue(beanByClassField, obj);
+ final ILazyInitProxy lazyInitProxy =
assertInstanceOf(ILazyInitProxy.class, beanByClassProxy);
+ final IProxyTargetLocator beanByClassLocator =
lazyInitProxy.getObjectLocator();
+ assertSame(somebean, beanByClassLocator.locateProxyTarget());
+ }
- @Override
- public ApplicationContext getSpringContext()
- {
- ApplicationContextMock mock = new
ApplicationContextMock();
- mock.putBean(new Bean());
- mock.putBean("somebean", new Bean2());
- return mock;
- }
- };
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldThrowExceptionIfBeanNameNotFound(final Object obj)
throws Exception
+ {
+ final Bean somebean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ // add two beans to make sure wiring by name is different by
wiring by class
+ applicationContext.putBean(new Bean());
+ applicationContext.putBean("wrongNameBean", somebean);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
- protected final InjectableInterface obj;
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByName");
+ Assertions.assertThrows(IllegalStateException.class, () ->
factory.getFieldValue(beanByClassField, obj));
+ }
- protected final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(mockCtxLocator);
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldCreateProxyForClass(final Object obj) throws Exception
+ {
+ final Bean bean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ applicationContext.putBean(bean);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ final Object proxy = factory.getFieldValue(beanByClassField,
obj);
+ final ILazyInitProxy lazyInitProxy =
assertInstanceOf(ILazyInitProxy.class, proxy);
+ final IProxyTargetLocator beanByClassLocator =
lazyInitProxy.getObjectLocator();
+ assertSame(bean, beanByClassLocator.locateProxyTarget());
+ }
- protected AnnotProxyFieldValueFactoryTest(InjectableInterface
injectable)
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldThrowException_beanNameAmbiguous(final Object obj)
throws Exception
{
- obj = injectable;
+ final Bean primaryBean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ // add two beans to make ambiguous
+ applicationContext.putBean("anyBean", new Bean());
+ applicationContext.putBean("primaryBean", primaryBean);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ Assertions.assertThrows(IllegalStateException.class, () ->
factory.getFieldValue(beanByClassField, obj));
+ }
+
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldCreateProxyForUniquePrimary_beanNameAmbiguous(final
Object obj) throws Exception
+ {
+ final Bean primaryBean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ // add two beans to make ambiguous
+ applicationContext.putBean("anyBean", new Bean());
+ applicationContext.putBean("primaryBean", primaryBean);
+
applicationContext.getBeanFactory().getBeanDefinition("primaryBean").setPrimary(true);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ final Object beanByClassProxy =
factory.getFieldValue(beanByClassField, obj);
+ final ILazyInitProxy lazyInitProxy =
assertInstanceOf(ILazyInitProxy.class, beanByClassProxy);
+ final IProxyTargetLocator beanByClassLocator =
lazyInitProxy.getObjectLocator();
+ assertSame(primaryBean, beanByClassLocator.locateProxyTarget());
+ }
+
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldCreateProxyForFieldname_beanNameAmbiguous(final
Object obj) throws Exception
+ {
+ final Bean bean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ applicationContext.putBean(new Bean());
+ applicationContext.putBean("beanByClass", bean);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ final Object proxy = factory.getFieldValue(beanByClassField,
obj);
+ final ILazyInitProxy lazyInitProxy =
assertInstanceOf(ILazyInitProxy.class, proxy);
+ final IProxyTargetLocator beanByClassLocator =
lazyInitProxy.getObjectLocator();
+ assertSame(bean, beanByClassLocator.locateProxyTarget());
+ }
+
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldIgnoreUnannotatedFields(final Object obj) throws
Exception
+ {
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(ApplicationContextMock::new);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("nobean");
+ final Object beanByClassProxy =
factory.getFieldValue(beanByClassField, obj);
+ assertNull(beanByClassProxy);
+ }
+
+ // https://issues.apache.org/jira/browse/WICKET-7170
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void
shouldCreateProxyForUniqueDefaultCandidate_beanNameAmbiguous(final Object obj)
throws Exception
+ {
+ final Bean defaultCandidate = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ // add two beans to make ambiguous
+ applicationContext.putBean("anyBean", new Bean());
+ applicationContext.putBean("primaryBean", defaultCandidate);
+ final AbstractBeanDefinition abstractBeanDefinition =
(AbstractBeanDefinition)
applicationContext.getBeanFactory().getBeanDefinition("anyBean");
+ abstractBeanDefinition.setDefaultCandidate(false);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ final Object beanByClassProxy =
factory.getFieldValue(beanByClassField, obj);
+ final ILazyInitProxy lazyInitProxy =
assertInstanceOf(ILazyInitProxy.class, beanByClassProxy);
+ final IProxyTargetLocator beanByClassLocator =
lazyInitProxy.getObjectLocator();
+ assertSame(defaultCandidate,
beanByClassLocator.locateProxyTarget());
}
/**
- * Test the factory
- *
- * @throws Exception
+ * test the cache, make sure the same proxy is returned for the same
dependency it represents
*/
- @Test
- public void testFactory() throws Exception
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void testCacheForBeanName(final Object obj) throws Exception
{
- SpringBeanLocator locator;
- Object proxy;
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ applicationContext.putBean(new Bean());
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
- Field field = obj.getClass().getDeclaredField("nobean");
- proxy = factory.getFieldValue(field, obj);
- assertNull(proxy);
+ final Field field =
obj.getClass().getDeclaredField("beanByClass");
+ final Object proxy1 = factory.getFieldValue(field, obj);
+ final Object proxy2 = factory.getFieldValue(field, obj);
+ assertSame(proxy1, proxy2);
+ }
- field = obj.getClass().getDeclaredField("beanByClass");
- proxy = factory.getFieldValue(field, obj);
- locator =
(SpringBeanLocator)((ILazyInitProxy)proxy).getObjectLocator();
- assertEquals(Bean.class, locator.getBeanType());
- assertSame(locator.getSpringContextLocator(), mockCtxLocator);
- assertTrue(factory.getFieldValue(field, obj) instanceof
ILazyInitProxy);
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void testCacheForClass(final Object obj) throws Exception
+ {
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ applicationContext.putBean(new Bean());
+ applicationContext.putBean("somebean", new Bean());
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
- field = obj.getClass().getDeclaredField("beanByName");
- proxy = factory.getFieldValue(field, obj);
- locator =
(SpringBeanLocator)((ILazyInitProxy)proxy).getObjectLocator();
- assertEquals("somebean", locator.getBeanName());
- assertEquals(Bean2.class, locator.getBeanType());
- assertSame(locator.getSpringContextLocator(), mockCtxLocator);
- assertTrue(factory.getFieldValue(field, obj) instanceof
ILazyInitProxy);
+ final Field field =
obj.getClass().getDeclaredField("beanByName");
+ final Object proxy1 = factory.getFieldValue(field, obj);
+ final Object proxy2 = factory.getFieldValue(field, obj);
+ assertSame(proxy1, proxy2);
}
/**
- * test the cache, make sure the same proxy is returned for the same
dependency it represents
- *
+ * https://issues.apache.org/jira/browse/WICKET-5686
* @throws Exception
*/
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void required(final Object obj) throws Exception
+ {
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(ApplicationContextMock::new);
+ final Field field =
obj.getClass().getDeclaredField("beanByClass");
+ Assertions.assertThrows(IllegalStateException.class, () ->
factory.getFieldValue(field, obj));
+ }
+
@Test
- public void testCache() throws Exception
+ public void optional() throws Exception
{
- Field field = obj.getClass().getDeclaredField("beanByClass");
- Object proxy1 = factory.getFieldValue(field, obj);
- Object proxy2 = factory.getFieldValue(field, obj);
- assertSame(proxy1, proxy2);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(ApplicationContextMock::new);
+ final SpringBeanInjectable springBeanInjectable = new
SpringBeanInjectable();
+ final Field field =
springBeanInjectable.getClass().getDeclaredField("optional");
+ Assertions.assertNull(factory.getFieldValue(field,
springBeanInjectable));
+ }
- field = obj.getClass().getDeclaredField("beanByName");
- proxy1 = factory.getFieldValue(field, obj);
- proxy2 = factory.getFieldValue(field, obj);
- assertSame(proxy1, proxy2);
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void lookupNonProxy(final Object obj) throws Exception
+ {
+ final Bean bean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ applicationContext.putBean(bean);
+ final boolean wrapInProxies = false;
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext, wrapInProxies);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ final Object value = factory.getFieldValue(beanByClassField,
obj);
+ assertSame(bean, value);
+ }
+
+ private static Stream<Object> beans() {
+ return Stream.of(new SpringBeanInjectable(), new
JakartaInjectInjectable());
}
/**
* Test creation fails with null springcontextlocator
+ * Mock for an object with some Jakarta-Inject annotations
Review Comment:
This line does not seem related to the method below.
##########
wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/AnnotProxyFieldValueFactoryTest.java:
##########
@@ -16,150 +16,310 @@
*/
package org.apache.wicket.spring.injection.annot;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+
+import java.lang.reflect.Field;
+import java.util.stream.Stream;
+
import org.apache.wicket.proxy.ILazyInitProxy;
-import org.apache.wicket.spring.ISpringContextLocator;
-import org.apache.wicket.spring.SpringBeanLocator;
-import org.apache.wicket.spring.injection.util.Bean;
-import org.apache.wicket.spring.injection.util.Bean2;
-import org.apache.wicket.spring.injection.util.InjectableInterface;
+import org.apache.wicket.proxy.IProxyTargetLocator;
import org.apache.wicket.spring.test.ApplicationContextMock;
+import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
-import org.springframework.context.ApplicationContext;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.springframework.beans.factory.support.AbstractBeanDefinition;
-import java.lang.reflect.Field;
-
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertNull;
-import static org.junit.jupiter.api.Assertions.assertSame;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
+import jakarta.inject.Inject;
+import jakarta.inject.Named;
/**
- * Tests for BeanAnnotLocatorFactory
+ * Tests for AnnotProxyFieldValueFactory
*
* @author igor
+ * @author hosea
*/
-public abstract class AnnotProxyFieldValueFactoryTest
+public class AnnotProxyFieldValueFactoryTest
{
- ISpringContextLocator mockCtxLocator = new ISpringContextLocator()
+
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldCreateProxyForBeanName(final Object obj) throws
Exception
{
- private static final long serialVersionUID = 1L;
+ final Bean somebean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ // add two beans to make sure wiring by name is different by
wiring by class
+ applicationContext.putBean(new Bean());
+ applicationContext.putBean("somebean", somebean);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByName");
+ final Object beanByClassProxy =
factory.getFieldValue(beanByClassField, obj);
+ final ILazyInitProxy lazyInitProxy =
assertInstanceOf(ILazyInitProxy.class, beanByClassProxy);
+ final IProxyTargetLocator beanByClassLocator =
lazyInitProxy.getObjectLocator();
+ assertSame(somebean, beanByClassLocator.locateProxyTarget());
+ }
- @Override
- public ApplicationContext getSpringContext()
- {
- ApplicationContextMock mock = new
ApplicationContextMock();
- mock.putBean(new Bean());
- mock.putBean("somebean", new Bean2());
- return mock;
- }
- };
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldThrowExceptionIfBeanNameNotFound(final Object obj)
throws Exception
+ {
+ final Bean somebean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ // add two beans to make sure wiring by name is different by
wiring by class
+ applicationContext.putBean(new Bean());
+ applicationContext.putBean("wrongNameBean", somebean);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
- protected final InjectableInterface obj;
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByName");
+ Assertions.assertThrows(IllegalStateException.class, () ->
factory.getFieldValue(beanByClassField, obj));
+ }
- protected final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(mockCtxLocator);
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldCreateProxyForClass(final Object obj) throws Exception
+ {
+ final Bean bean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ applicationContext.putBean(bean);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ final Object proxy = factory.getFieldValue(beanByClassField,
obj);
+ final ILazyInitProxy lazyInitProxy =
assertInstanceOf(ILazyInitProxy.class, proxy);
+ final IProxyTargetLocator beanByClassLocator =
lazyInitProxy.getObjectLocator();
+ assertSame(bean, beanByClassLocator.locateProxyTarget());
+ }
- protected AnnotProxyFieldValueFactoryTest(InjectableInterface
injectable)
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldThrowException_beanNameAmbiguous(final Object obj)
throws Exception
{
- obj = injectable;
+ final Bean primaryBean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ // add two beans to make ambiguous
+ applicationContext.putBean("anyBean", new Bean());
+ applicationContext.putBean("primaryBean", primaryBean);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ Assertions.assertThrows(IllegalStateException.class, () ->
factory.getFieldValue(beanByClassField, obj));
+ }
+
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldCreateProxyForUniquePrimary_beanNameAmbiguous(final
Object obj) throws Exception
+ {
+ final Bean primaryBean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ // add two beans to make ambiguous
+ applicationContext.putBean("anyBean", new Bean());
+ applicationContext.putBean("primaryBean", primaryBean);
+
applicationContext.getBeanFactory().getBeanDefinition("primaryBean").setPrimary(true);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ final Object beanByClassProxy =
factory.getFieldValue(beanByClassField, obj);
+ final ILazyInitProxy lazyInitProxy =
assertInstanceOf(ILazyInitProxy.class, beanByClassProxy);
+ final IProxyTargetLocator beanByClassLocator =
lazyInitProxy.getObjectLocator();
+ assertSame(primaryBean, beanByClassLocator.locateProxyTarget());
+ }
+
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldCreateProxyForFieldname_beanNameAmbiguous(final
Object obj) throws Exception
+ {
+ final Bean bean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ applicationContext.putBean(new Bean());
+ applicationContext.putBean("beanByClass", bean);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ final Object proxy = factory.getFieldValue(beanByClassField,
obj);
+ final ILazyInitProxy lazyInitProxy =
assertInstanceOf(ILazyInitProxy.class, proxy);
+ final IProxyTargetLocator beanByClassLocator =
lazyInitProxy.getObjectLocator();
+ assertSame(bean, beanByClassLocator.locateProxyTarget());
+ }
+
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldIgnoreUnannotatedFields(final Object obj) throws
Exception
+ {
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(ApplicationContextMock::new);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("nobean");
+ final Object beanByClassProxy =
factory.getFieldValue(beanByClassField, obj);
+ assertNull(beanByClassProxy);
+ }
+
+ // https://issues.apache.org/jira/browse/WICKET-7170
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void
shouldCreateProxyForUniqueDefaultCandidate_beanNameAmbiguous(final Object obj)
throws Exception
+ {
+ final Bean defaultCandidate = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ // add two beans to make ambiguous
+ applicationContext.putBean("anyBean", new Bean());
+ applicationContext.putBean("primaryBean", defaultCandidate);
+ final AbstractBeanDefinition abstractBeanDefinition =
(AbstractBeanDefinition)
applicationContext.getBeanFactory().getBeanDefinition("anyBean");
+ abstractBeanDefinition.setDefaultCandidate(false);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ final Object beanByClassProxy =
factory.getFieldValue(beanByClassField, obj);
+ final ILazyInitProxy lazyInitProxy =
assertInstanceOf(ILazyInitProxy.class, beanByClassProxy);
+ final IProxyTargetLocator beanByClassLocator =
lazyInitProxy.getObjectLocator();
+ assertSame(defaultCandidate,
beanByClassLocator.locateProxyTarget());
}
/**
- * Test the factory
- *
- * @throws Exception
+ * test the cache, make sure the same proxy is returned for the same
dependency it represents
*/
- @Test
- public void testFactory() throws Exception
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void testCacheForBeanName(final Object obj) throws Exception
{
- SpringBeanLocator locator;
- Object proxy;
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ applicationContext.putBean(new Bean());
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
- Field field = obj.getClass().getDeclaredField("nobean");
- proxy = factory.getFieldValue(field, obj);
- assertNull(proxy);
+ final Field field =
obj.getClass().getDeclaredField("beanByClass");
+ final Object proxy1 = factory.getFieldValue(field, obj);
+ final Object proxy2 = factory.getFieldValue(field, obj);
+ assertSame(proxy1, proxy2);
+ }
- field = obj.getClass().getDeclaredField("beanByClass");
- proxy = factory.getFieldValue(field, obj);
- locator =
(SpringBeanLocator)((ILazyInitProxy)proxy).getObjectLocator();
- assertEquals(Bean.class, locator.getBeanType());
- assertSame(locator.getSpringContextLocator(), mockCtxLocator);
- assertTrue(factory.getFieldValue(field, obj) instanceof
ILazyInitProxy);
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void testCacheForClass(final Object obj) throws Exception
+ {
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ applicationContext.putBean(new Bean());
+ applicationContext.putBean("somebean", new Bean());
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
- field = obj.getClass().getDeclaredField("beanByName");
- proxy = factory.getFieldValue(field, obj);
- locator =
(SpringBeanLocator)((ILazyInitProxy)proxy).getObjectLocator();
- assertEquals("somebean", locator.getBeanName());
- assertEquals(Bean2.class, locator.getBeanType());
- assertSame(locator.getSpringContextLocator(), mockCtxLocator);
- assertTrue(factory.getFieldValue(field, obj) instanceof
ILazyInitProxy);
+ final Field field =
obj.getClass().getDeclaredField("beanByName");
+ final Object proxy1 = factory.getFieldValue(field, obj);
+ final Object proxy2 = factory.getFieldValue(field, obj);
+ assertSame(proxy1, proxy2);
}
/**
- * test the cache, make sure the same proxy is returned for the same
dependency it represents
- *
+ * https://issues.apache.org/jira/browse/WICKET-5686
* @throws Exception
*/
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void required(final Object obj) throws Exception
+ {
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(ApplicationContextMock::new);
+ final Field field =
obj.getClass().getDeclaredField("beanByClass");
+ Assertions.assertThrows(IllegalStateException.class, () ->
factory.getFieldValue(field, obj));
+ }
+
@Test
- public void testCache() throws Exception
+ public void optional() throws Exception
{
- Field field = obj.getClass().getDeclaredField("beanByClass");
- Object proxy1 = factory.getFieldValue(field, obj);
- Object proxy2 = factory.getFieldValue(field, obj);
- assertSame(proxy1, proxy2);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(ApplicationContextMock::new);
+ final SpringBeanInjectable springBeanInjectable = new
SpringBeanInjectable();
+ final Field field =
springBeanInjectable.getClass().getDeclaredField("optional");
+ Assertions.assertNull(factory.getFieldValue(field,
springBeanInjectable));
+ }
- field = obj.getClass().getDeclaredField("beanByName");
- proxy1 = factory.getFieldValue(field, obj);
- proxy2 = factory.getFieldValue(field, obj);
- assertSame(proxy1, proxy2);
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void lookupNonProxy(final Object obj) throws Exception
+ {
+ final Bean bean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ applicationContext.putBean(bean);
+ final boolean wrapInProxies = false;
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext, wrapInProxies);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ final Object value = factory.getFieldValue(beanByClassField,
obj);
+ assertSame(bean, value);
+ }
+
+ private static Stream<Object> beans() {
Review Comment:
```suggestion
private static Stream<Object> beans()
{
```
##########
wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/AnnotProxyFieldValueFactoryTest.java:
##########
@@ -16,150 +16,310 @@
*/
package org.apache.wicket.spring.injection.annot;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+
+import java.lang.reflect.Field;
+import java.util.stream.Stream;
+
import org.apache.wicket.proxy.ILazyInitProxy;
-import org.apache.wicket.spring.ISpringContextLocator;
-import org.apache.wicket.spring.SpringBeanLocator;
-import org.apache.wicket.spring.injection.util.Bean;
-import org.apache.wicket.spring.injection.util.Bean2;
-import org.apache.wicket.spring.injection.util.InjectableInterface;
+import org.apache.wicket.proxy.IProxyTargetLocator;
import org.apache.wicket.spring.test.ApplicationContextMock;
+import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
-import org.springframework.context.ApplicationContext;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.springframework.beans.factory.support.AbstractBeanDefinition;
-import java.lang.reflect.Field;
-
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertNull;
-import static org.junit.jupiter.api.Assertions.assertSame;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
+import jakarta.inject.Inject;
+import jakarta.inject.Named;
/**
- * Tests for BeanAnnotLocatorFactory
+ * Tests for AnnotProxyFieldValueFactory
*
* @author igor
+ * @author hosea
*/
-public abstract class AnnotProxyFieldValueFactoryTest
+public class AnnotProxyFieldValueFactoryTest
{
- ISpringContextLocator mockCtxLocator = new ISpringContextLocator()
+
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldCreateProxyForBeanName(final Object obj) throws
Exception
{
- private static final long serialVersionUID = 1L;
+ final Bean somebean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ // add two beans to make sure wiring by name is different by
wiring by class
+ applicationContext.putBean(new Bean());
+ applicationContext.putBean("somebean", somebean);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByName");
+ final Object beanByClassProxy =
factory.getFieldValue(beanByClassField, obj);
+ final ILazyInitProxy lazyInitProxy =
assertInstanceOf(ILazyInitProxy.class, beanByClassProxy);
+ final IProxyTargetLocator beanByClassLocator =
lazyInitProxy.getObjectLocator();
+ assertSame(somebean, beanByClassLocator.locateProxyTarget());
+ }
- @Override
- public ApplicationContext getSpringContext()
- {
- ApplicationContextMock mock = new
ApplicationContextMock();
- mock.putBean(new Bean());
- mock.putBean("somebean", new Bean2());
- return mock;
- }
- };
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldThrowExceptionIfBeanNameNotFound(final Object obj)
throws Exception
+ {
+ final Bean somebean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ // add two beans to make sure wiring by name is different by
wiring by class
+ applicationContext.putBean(new Bean());
+ applicationContext.putBean("wrongNameBean", somebean);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
- protected final InjectableInterface obj;
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByName");
+ Assertions.assertThrows(IllegalStateException.class, () ->
factory.getFieldValue(beanByClassField, obj));
+ }
- protected final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(mockCtxLocator);
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldCreateProxyForClass(final Object obj) throws Exception
+ {
+ final Bean bean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ applicationContext.putBean(bean);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ final Object proxy = factory.getFieldValue(beanByClassField,
obj);
+ final ILazyInitProxy lazyInitProxy =
assertInstanceOf(ILazyInitProxy.class, proxy);
+ final IProxyTargetLocator beanByClassLocator =
lazyInitProxy.getObjectLocator();
+ assertSame(bean, beanByClassLocator.locateProxyTarget());
+ }
- protected AnnotProxyFieldValueFactoryTest(InjectableInterface
injectable)
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldThrowException_beanNameAmbiguous(final Object obj)
throws Exception
{
- obj = injectable;
+ final Bean primaryBean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ // add two beans to make ambiguous
+ applicationContext.putBean("anyBean", new Bean());
+ applicationContext.putBean("primaryBean", primaryBean);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ Assertions.assertThrows(IllegalStateException.class, () ->
factory.getFieldValue(beanByClassField, obj));
+ }
+
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldCreateProxyForUniquePrimary_beanNameAmbiguous(final
Object obj) throws Exception
+ {
+ final Bean primaryBean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ // add two beans to make ambiguous
+ applicationContext.putBean("anyBean", new Bean());
+ applicationContext.putBean("primaryBean", primaryBean);
+
applicationContext.getBeanFactory().getBeanDefinition("primaryBean").setPrimary(true);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ final Object beanByClassProxy =
factory.getFieldValue(beanByClassField, obj);
+ final ILazyInitProxy lazyInitProxy =
assertInstanceOf(ILazyInitProxy.class, beanByClassProxy);
+ final IProxyTargetLocator beanByClassLocator =
lazyInitProxy.getObjectLocator();
+ assertSame(primaryBean, beanByClassLocator.locateProxyTarget());
+ }
+
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldCreateProxyForFieldname_beanNameAmbiguous(final
Object obj) throws Exception
+ {
+ final Bean bean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ applicationContext.putBean(new Bean());
+ applicationContext.putBean("beanByClass", bean);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ final Object proxy = factory.getFieldValue(beanByClassField,
obj);
+ final ILazyInitProxy lazyInitProxy =
assertInstanceOf(ILazyInitProxy.class, proxy);
+ final IProxyTargetLocator beanByClassLocator =
lazyInitProxy.getObjectLocator();
+ assertSame(bean, beanByClassLocator.locateProxyTarget());
+ }
+
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldIgnoreUnannotatedFields(final Object obj) throws
Exception
+ {
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(ApplicationContextMock::new);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("nobean");
+ final Object beanByClassProxy =
factory.getFieldValue(beanByClassField, obj);
+ assertNull(beanByClassProxy);
+ }
+
+ // https://issues.apache.org/jira/browse/WICKET-7170
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void
shouldCreateProxyForUniqueDefaultCandidate_beanNameAmbiguous(final Object obj)
throws Exception
+ {
+ final Bean defaultCandidate = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ // add two beans to make ambiguous
+ applicationContext.putBean("anyBean", new Bean());
+ applicationContext.putBean("primaryBean", defaultCandidate);
+ final AbstractBeanDefinition abstractBeanDefinition =
(AbstractBeanDefinition)
applicationContext.getBeanFactory().getBeanDefinition("anyBean");
+ abstractBeanDefinition.setDefaultCandidate(false);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ final Object beanByClassProxy =
factory.getFieldValue(beanByClassField, obj);
+ final ILazyInitProxy lazyInitProxy =
assertInstanceOf(ILazyInitProxy.class, beanByClassProxy);
+ final IProxyTargetLocator beanByClassLocator =
lazyInitProxy.getObjectLocator();
+ assertSame(defaultCandidate,
beanByClassLocator.locateProxyTarget());
}
/**
- * Test the factory
- *
- * @throws Exception
+ * test the cache, make sure the same proxy is returned for the same
dependency it represents
*/
- @Test
- public void testFactory() throws Exception
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void testCacheForBeanName(final Object obj) throws Exception
{
- SpringBeanLocator locator;
- Object proxy;
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ applicationContext.putBean(new Bean());
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
- Field field = obj.getClass().getDeclaredField("nobean");
- proxy = factory.getFieldValue(field, obj);
- assertNull(proxy);
+ final Field field =
obj.getClass().getDeclaredField("beanByClass");
+ final Object proxy1 = factory.getFieldValue(field, obj);
+ final Object proxy2 = factory.getFieldValue(field, obj);
+ assertSame(proxy1, proxy2);
+ }
- field = obj.getClass().getDeclaredField("beanByClass");
- proxy = factory.getFieldValue(field, obj);
- locator =
(SpringBeanLocator)((ILazyInitProxy)proxy).getObjectLocator();
- assertEquals(Bean.class, locator.getBeanType());
- assertSame(locator.getSpringContextLocator(), mockCtxLocator);
- assertTrue(factory.getFieldValue(field, obj) instanceof
ILazyInitProxy);
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void testCacheForClass(final Object obj) throws Exception
+ {
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ applicationContext.putBean(new Bean());
+ applicationContext.putBean("somebean", new Bean());
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
- field = obj.getClass().getDeclaredField("beanByName");
- proxy = factory.getFieldValue(field, obj);
- locator =
(SpringBeanLocator)((ILazyInitProxy)proxy).getObjectLocator();
- assertEquals("somebean", locator.getBeanName());
- assertEquals(Bean2.class, locator.getBeanType());
- assertSame(locator.getSpringContextLocator(), mockCtxLocator);
- assertTrue(factory.getFieldValue(field, obj) instanceof
ILazyInitProxy);
+ final Field field =
obj.getClass().getDeclaredField("beanByName");
+ final Object proxy1 = factory.getFieldValue(field, obj);
+ final Object proxy2 = factory.getFieldValue(field, obj);
+ assertSame(proxy1, proxy2);
}
/**
- * test the cache, make sure the same proxy is returned for the same
dependency it represents
- *
+ * https://issues.apache.org/jira/browse/WICKET-5686
* @throws Exception
*/
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void required(final Object obj) throws Exception
+ {
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(ApplicationContextMock::new);
+ final Field field =
obj.getClass().getDeclaredField("beanByClass");
+ Assertions.assertThrows(IllegalStateException.class, () ->
factory.getFieldValue(field, obj));
+ }
+
@Test
- public void testCache() throws Exception
+ public void optional() throws Exception
{
- Field field = obj.getClass().getDeclaredField("beanByClass");
- Object proxy1 = factory.getFieldValue(field, obj);
- Object proxy2 = factory.getFieldValue(field, obj);
- assertSame(proxy1, proxy2);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(ApplicationContextMock::new);
+ final SpringBeanInjectable springBeanInjectable = new
SpringBeanInjectable();
+ final Field field =
springBeanInjectable.getClass().getDeclaredField("optional");
+ Assertions.assertNull(factory.getFieldValue(field,
springBeanInjectable));
+ }
- field = obj.getClass().getDeclaredField("beanByName");
- proxy1 = factory.getFieldValue(field, obj);
- proxy2 = factory.getFieldValue(field, obj);
- assertSame(proxy1, proxy2);
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void lookupNonProxy(final Object obj) throws Exception
+ {
+ final Bean bean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ applicationContext.putBean(bean);
+ final boolean wrapInProxies = false;
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext, wrapInProxies);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ final Object value = factory.getFieldValue(beanByClassField,
obj);
+ assertSame(bean, value);
+ }
+
+ private static Stream<Object> beans() {
+ return Stream.of(new SpringBeanInjectable(), new
JakartaInjectInjectable());
}
/**
* Test creation fails with null springcontextlocator
+ * Mock for an object with some Jakarta-Inject annotations
*/
- @Test
- public void testNullContextLocator()
+ @Test
+ public void testNullContextLocator()
+ {
+ Assertions.assertThrows(IllegalArgumentException.class, () -> new
AnnotProxyFieldValueFactory(null));
+ }
+
+ /**
+ * Class with Jakarta-Inject annotations for several scenarios:
+ * <UL>
+ * <LI>@{@link Inject} inject by class</LI>
+ * <LI>@{@link Inject} inject by name / @{@link Named}</LI>
+ * <LI>no possible injection as not annotated</LI>
+ * </UL>
+ * Same property names for the same scenarios as in {@link
SpringBeanInjectable}
+ * so both beans can be used with the same test methods, this makes
@ParameterizedTest - Tests possible
+ */
+ public static class JakartaInjectInjectable
{
- try
- {
- new AnnotProxyFieldValueFactory(null);
- fail();
- }
- catch (IllegalArgumentException e)
- {
- // noop
+ private Bean nobean;
+
+ @Inject
+ private Bean beanByClass;
+
+ @Inject
+ @Named("somebean")
+ private Bean beanByName;
+
+ @Override
+ public String toString() {
Review Comment:
```suggestion
public String toString()
{
```
##########
wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/AnnotProxyFieldValueFactoryTest.java:
##########
@@ -16,150 +16,310 @@
*/
package org.apache.wicket.spring.injection.annot;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+
+import java.lang.reflect.Field;
+import java.util.stream.Stream;
+
import org.apache.wicket.proxy.ILazyInitProxy;
-import org.apache.wicket.spring.ISpringContextLocator;
-import org.apache.wicket.spring.SpringBeanLocator;
-import org.apache.wicket.spring.injection.util.Bean;
-import org.apache.wicket.spring.injection.util.Bean2;
-import org.apache.wicket.spring.injection.util.InjectableInterface;
+import org.apache.wicket.proxy.IProxyTargetLocator;
import org.apache.wicket.spring.test.ApplicationContextMock;
+import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
-import org.springframework.context.ApplicationContext;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.springframework.beans.factory.support.AbstractBeanDefinition;
-import java.lang.reflect.Field;
-
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertNull;
-import static org.junit.jupiter.api.Assertions.assertSame;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
+import jakarta.inject.Inject;
+import jakarta.inject.Named;
/**
- * Tests for BeanAnnotLocatorFactory
+ * Tests for AnnotProxyFieldValueFactory
*
* @author igor
+ * @author hosea
*/
-public abstract class AnnotProxyFieldValueFactoryTest
+public class AnnotProxyFieldValueFactoryTest
{
- ISpringContextLocator mockCtxLocator = new ISpringContextLocator()
+
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldCreateProxyForBeanName(final Object obj) throws
Exception
{
- private static final long serialVersionUID = 1L;
+ final Bean somebean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ // add two beans to make sure wiring by name is different by
wiring by class
+ applicationContext.putBean(new Bean());
+ applicationContext.putBean("somebean", somebean);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByName");
+ final Object beanByClassProxy =
factory.getFieldValue(beanByClassField, obj);
+ final ILazyInitProxy lazyInitProxy =
assertInstanceOf(ILazyInitProxy.class, beanByClassProxy);
+ final IProxyTargetLocator beanByClassLocator =
lazyInitProxy.getObjectLocator();
+ assertSame(somebean, beanByClassLocator.locateProxyTarget());
+ }
- @Override
- public ApplicationContext getSpringContext()
- {
- ApplicationContextMock mock = new
ApplicationContextMock();
- mock.putBean(new Bean());
- mock.putBean("somebean", new Bean2());
- return mock;
- }
- };
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldThrowExceptionIfBeanNameNotFound(final Object obj)
throws Exception
+ {
+ final Bean somebean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ // add two beans to make sure wiring by name is different by
wiring by class
+ applicationContext.putBean(new Bean());
+ applicationContext.putBean("wrongNameBean", somebean);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
- protected final InjectableInterface obj;
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByName");
+ Assertions.assertThrows(IllegalStateException.class, () ->
factory.getFieldValue(beanByClassField, obj));
+ }
- protected final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(mockCtxLocator);
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldCreateProxyForClass(final Object obj) throws Exception
+ {
+ final Bean bean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ applicationContext.putBean(bean);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ final Object proxy = factory.getFieldValue(beanByClassField,
obj);
+ final ILazyInitProxy lazyInitProxy =
assertInstanceOf(ILazyInitProxy.class, proxy);
+ final IProxyTargetLocator beanByClassLocator =
lazyInitProxy.getObjectLocator();
+ assertSame(bean, beanByClassLocator.locateProxyTarget());
+ }
- protected AnnotProxyFieldValueFactoryTest(InjectableInterface
injectable)
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldThrowException_beanNameAmbiguous(final Object obj)
throws Exception
{
- obj = injectable;
+ final Bean primaryBean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ // add two beans to make ambiguous
+ applicationContext.putBean("anyBean", new Bean());
+ applicationContext.putBean("primaryBean", primaryBean);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ Assertions.assertThrows(IllegalStateException.class, () ->
factory.getFieldValue(beanByClassField, obj));
+ }
+
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldCreateProxyForUniquePrimary_beanNameAmbiguous(final
Object obj) throws Exception
+ {
+ final Bean primaryBean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ // add two beans to make ambiguous
+ applicationContext.putBean("anyBean", new Bean());
+ applicationContext.putBean("primaryBean", primaryBean);
+
applicationContext.getBeanFactory().getBeanDefinition("primaryBean").setPrimary(true);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ final Object beanByClassProxy =
factory.getFieldValue(beanByClassField, obj);
+ final ILazyInitProxy lazyInitProxy =
assertInstanceOf(ILazyInitProxy.class, beanByClassProxy);
+ final IProxyTargetLocator beanByClassLocator =
lazyInitProxy.getObjectLocator();
+ assertSame(primaryBean, beanByClassLocator.locateProxyTarget());
+ }
+
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldCreateProxyForFieldname_beanNameAmbiguous(final
Object obj) throws Exception
+ {
+ final Bean bean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ applicationContext.putBean(new Bean());
+ applicationContext.putBean("beanByClass", bean);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ final Object proxy = factory.getFieldValue(beanByClassField,
obj);
+ final ILazyInitProxy lazyInitProxy =
assertInstanceOf(ILazyInitProxy.class, proxy);
+ final IProxyTargetLocator beanByClassLocator =
lazyInitProxy.getObjectLocator();
+ assertSame(bean, beanByClassLocator.locateProxyTarget());
+ }
+
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldIgnoreUnannotatedFields(final Object obj) throws
Exception
+ {
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(ApplicationContextMock::new);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("nobean");
+ final Object beanByClassProxy =
factory.getFieldValue(beanByClassField, obj);
+ assertNull(beanByClassProxy);
+ }
+
+ // https://issues.apache.org/jira/browse/WICKET-7170
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void
shouldCreateProxyForUniqueDefaultCandidate_beanNameAmbiguous(final Object obj)
throws Exception
+ {
+ final Bean defaultCandidate = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ // add two beans to make ambiguous
+ applicationContext.putBean("anyBean", new Bean());
+ applicationContext.putBean("primaryBean", defaultCandidate);
+ final AbstractBeanDefinition abstractBeanDefinition =
(AbstractBeanDefinition)
applicationContext.getBeanFactory().getBeanDefinition("anyBean");
+ abstractBeanDefinition.setDefaultCandidate(false);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ final Object beanByClassProxy =
factory.getFieldValue(beanByClassField, obj);
+ final ILazyInitProxy lazyInitProxy =
assertInstanceOf(ILazyInitProxy.class, beanByClassProxy);
+ final IProxyTargetLocator beanByClassLocator =
lazyInitProxy.getObjectLocator();
+ assertSame(defaultCandidate,
beanByClassLocator.locateProxyTarget());
}
/**
- * Test the factory
- *
- * @throws Exception
+ * test the cache, make sure the same proxy is returned for the same
dependency it represents
*/
- @Test
- public void testFactory() throws Exception
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void testCacheForBeanName(final Object obj) throws Exception
{
- SpringBeanLocator locator;
- Object proxy;
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ applicationContext.putBean(new Bean());
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
- Field field = obj.getClass().getDeclaredField("nobean");
- proxy = factory.getFieldValue(field, obj);
- assertNull(proxy);
+ final Field field =
obj.getClass().getDeclaredField("beanByClass");
+ final Object proxy1 = factory.getFieldValue(field, obj);
+ final Object proxy2 = factory.getFieldValue(field, obj);
+ assertSame(proxy1, proxy2);
+ }
- field = obj.getClass().getDeclaredField("beanByClass");
- proxy = factory.getFieldValue(field, obj);
- locator =
(SpringBeanLocator)((ILazyInitProxy)proxy).getObjectLocator();
- assertEquals(Bean.class, locator.getBeanType());
- assertSame(locator.getSpringContextLocator(), mockCtxLocator);
- assertTrue(factory.getFieldValue(field, obj) instanceof
ILazyInitProxy);
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void testCacheForClass(final Object obj) throws Exception
Review Comment:
```suggestion
public void testCacheForBeanByName(final Object obj) throws Exception
```
##########
wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/AnnotProxyFieldValueFactoryTest.java:
##########
@@ -16,150 +16,310 @@
*/
package org.apache.wicket.spring.injection.annot;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+
+import java.lang.reflect.Field;
+import java.util.stream.Stream;
+
import org.apache.wicket.proxy.ILazyInitProxy;
-import org.apache.wicket.spring.ISpringContextLocator;
-import org.apache.wicket.spring.SpringBeanLocator;
-import org.apache.wicket.spring.injection.util.Bean;
-import org.apache.wicket.spring.injection.util.Bean2;
-import org.apache.wicket.spring.injection.util.InjectableInterface;
+import org.apache.wicket.proxy.IProxyTargetLocator;
import org.apache.wicket.spring.test.ApplicationContextMock;
+import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
-import org.springframework.context.ApplicationContext;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.springframework.beans.factory.support.AbstractBeanDefinition;
-import java.lang.reflect.Field;
-
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertNull;
-import static org.junit.jupiter.api.Assertions.assertSame;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
+import jakarta.inject.Inject;
+import jakarta.inject.Named;
/**
- * Tests for BeanAnnotLocatorFactory
+ * Tests for AnnotProxyFieldValueFactory
*
* @author igor
+ * @author hosea
*/
-public abstract class AnnotProxyFieldValueFactoryTest
+public class AnnotProxyFieldValueFactoryTest
{
- ISpringContextLocator mockCtxLocator = new ISpringContextLocator()
+
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldCreateProxyForBeanName(final Object obj) throws
Exception
{
- private static final long serialVersionUID = 1L;
+ final Bean somebean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ // add two beans to make sure wiring by name is different by
wiring by class
+ applicationContext.putBean(new Bean());
+ applicationContext.putBean("somebean", somebean);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByName");
+ final Object beanByClassProxy =
factory.getFieldValue(beanByClassField, obj);
+ final ILazyInitProxy lazyInitProxy =
assertInstanceOf(ILazyInitProxy.class, beanByClassProxy);
+ final IProxyTargetLocator beanByClassLocator =
lazyInitProxy.getObjectLocator();
+ assertSame(somebean, beanByClassLocator.locateProxyTarget());
+ }
- @Override
- public ApplicationContext getSpringContext()
- {
- ApplicationContextMock mock = new
ApplicationContextMock();
- mock.putBean(new Bean());
- mock.putBean("somebean", new Bean2());
- return mock;
- }
- };
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldThrowExceptionIfBeanNameNotFound(final Object obj)
throws Exception
+ {
+ final Bean somebean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ // add two beans to make sure wiring by name is different by
wiring by class
+ applicationContext.putBean(new Bean());
+ applicationContext.putBean("wrongNameBean", somebean);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
- protected final InjectableInterface obj;
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByName");
+ Assertions.assertThrows(IllegalStateException.class, () ->
factory.getFieldValue(beanByClassField, obj));
+ }
- protected final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(mockCtxLocator);
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldCreateProxyForClass(final Object obj) throws Exception
+ {
+ final Bean bean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ applicationContext.putBean(bean);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ final Object proxy = factory.getFieldValue(beanByClassField,
obj);
+ final ILazyInitProxy lazyInitProxy =
assertInstanceOf(ILazyInitProxy.class, proxy);
+ final IProxyTargetLocator beanByClassLocator =
lazyInitProxy.getObjectLocator();
+ assertSame(bean, beanByClassLocator.locateProxyTarget());
+ }
- protected AnnotProxyFieldValueFactoryTest(InjectableInterface
injectable)
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldThrowException_beanNameAmbiguous(final Object obj)
throws Exception
{
- obj = injectable;
+ final Bean primaryBean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ // add two beans to make ambiguous
+ applicationContext.putBean("anyBean", new Bean());
+ applicationContext.putBean("primaryBean", primaryBean);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ Assertions.assertThrows(IllegalStateException.class, () ->
factory.getFieldValue(beanByClassField, obj));
+ }
+
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldCreateProxyForUniquePrimary_beanNameAmbiguous(final
Object obj) throws Exception
+ {
+ final Bean primaryBean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ // add two beans to make ambiguous
+ applicationContext.putBean("anyBean", new Bean());
+ applicationContext.putBean("primaryBean", primaryBean);
+
applicationContext.getBeanFactory().getBeanDefinition("primaryBean").setPrimary(true);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ final Object beanByClassProxy =
factory.getFieldValue(beanByClassField, obj);
+ final ILazyInitProxy lazyInitProxy =
assertInstanceOf(ILazyInitProxy.class, beanByClassProxy);
+ final IProxyTargetLocator beanByClassLocator =
lazyInitProxy.getObjectLocator();
+ assertSame(primaryBean, beanByClassLocator.locateProxyTarget());
+ }
+
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldCreateProxyForFieldname_beanNameAmbiguous(final
Object obj) throws Exception
+ {
+ final Bean bean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ applicationContext.putBean(new Bean());
+ applicationContext.putBean("beanByClass", bean);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ final Object proxy = factory.getFieldValue(beanByClassField,
obj);
+ final ILazyInitProxy lazyInitProxy =
assertInstanceOf(ILazyInitProxy.class, proxy);
+ final IProxyTargetLocator beanByClassLocator =
lazyInitProxy.getObjectLocator();
+ assertSame(bean, beanByClassLocator.locateProxyTarget());
+ }
+
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldIgnoreUnannotatedFields(final Object obj) throws
Exception
+ {
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(ApplicationContextMock::new);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("nobean");
+ final Object beanByClassProxy =
factory.getFieldValue(beanByClassField, obj);
+ assertNull(beanByClassProxy);
+ }
+
+ // https://issues.apache.org/jira/browse/WICKET-7170
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void
shouldCreateProxyForUniqueDefaultCandidate_beanNameAmbiguous(final Object obj)
throws Exception
+ {
+ final Bean defaultCandidate = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ // add two beans to make ambiguous
+ applicationContext.putBean("anyBean", new Bean());
+ applicationContext.putBean("primaryBean", defaultCandidate);
+ final AbstractBeanDefinition abstractBeanDefinition =
(AbstractBeanDefinition)
applicationContext.getBeanFactory().getBeanDefinition("anyBean");
+ abstractBeanDefinition.setDefaultCandidate(false);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ final Object beanByClassProxy =
factory.getFieldValue(beanByClassField, obj);
+ final ILazyInitProxy lazyInitProxy =
assertInstanceOf(ILazyInitProxy.class, beanByClassProxy);
+ final IProxyTargetLocator beanByClassLocator =
lazyInitProxy.getObjectLocator();
+ assertSame(defaultCandidate,
beanByClassLocator.locateProxyTarget());
}
/**
- * Test the factory
- *
- * @throws Exception
+ * test the cache, make sure the same proxy is returned for the same
dependency it represents
*/
- @Test
- public void testFactory() throws Exception
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void testCacheForBeanName(final Object obj) throws Exception
{
- SpringBeanLocator locator;
- Object proxy;
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ applicationContext.putBean(new Bean());
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
- Field field = obj.getClass().getDeclaredField("nobean");
- proxy = factory.getFieldValue(field, obj);
- assertNull(proxy);
+ final Field field =
obj.getClass().getDeclaredField("beanByClass");
+ final Object proxy1 = factory.getFieldValue(field, obj);
+ final Object proxy2 = factory.getFieldValue(field, obj);
+ assertSame(proxy1, proxy2);
+ }
- field = obj.getClass().getDeclaredField("beanByClass");
- proxy = factory.getFieldValue(field, obj);
- locator =
(SpringBeanLocator)((ILazyInitProxy)proxy).getObjectLocator();
- assertEquals(Bean.class, locator.getBeanType());
- assertSame(locator.getSpringContextLocator(), mockCtxLocator);
- assertTrue(factory.getFieldValue(field, obj) instanceof
ILazyInitProxy);
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void testCacheForClass(final Object obj) throws Exception
+ {
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ applicationContext.putBean(new Bean());
+ applicationContext.putBean("somebean", new Bean());
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
- field = obj.getClass().getDeclaredField("beanByName");
- proxy = factory.getFieldValue(field, obj);
- locator =
(SpringBeanLocator)((ILazyInitProxy)proxy).getObjectLocator();
- assertEquals("somebean", locator.getBeanName());
- assertEquals(Bean2.class, locator.getBeanType());
- assertSame(locator.getSpringContextLocator(), mockCtxLocator);
- assertTrue(factory.getFieldValue(field, obj) instanceof
ILazyInitProxy);
+ final Field field =
obj.getClass().getDeclaredField("beanByName");
+ final Object proxy1 = factory.getFieldValue(field, obj);
+ final Object proxy2 = factory.getFieldValue(field, obj);
+ assertSame(proxy1, proxy2);
}
/**
- * test the cache, make sure the same proxy is returned for the same
dependency it represents
- *
+ * https://issues.apache.org/jira/browse/WICKET-5686
* @throws Exception
*/
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void required(final Object obj) throws Exception
+ {
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(ApplicationContextMock::new);
+ final Field field =
obj.getClass().getDeclaredField("beanByClass");
+ Assertions.assertThrows(IllegalStateException.class, () ->
factory.getFieldValue(field, obj));
+ }
+
@Test
- public void testCache() throws Exception
+ public void optional() throws Exception
{
- Field field = obj.getClass().getDeclaredField("beanByClass");
- Object proxy1 = factory.getFieldValue(field, obj);
- Object proxy2 = factory.getFieldValue(field, obj);
- assertSame(proxy1, proxy2);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(ApplicationContextMock::new);
+ final SpringBeanInjectable springBeanInjectable = new
SpringBeanInjectable();
+ final Field field =
springBeanInjectable.getClass().getDeclaredField("optional");
+ Assertions.assertNull(factory.getFieldValue(field,
springBeanInjectable));
+ }
- field = obj.getClass().getDeclaredField("beanByName");
- proxy1 = factory.getFieldValue(field, obj);
- proxy2 = factory.getFieldValue(field, obj);
- assertSame(proxy1, proxy2);
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void lookupNonProxy(final Object obj) throws Exception
+ {
+ final Bean bean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ applicationContext.putBean(bean);
+ final boolean wrapInProxies = false;
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext, wrapInProxies);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ final Object value = factory.getFieldValue(beanByClassField,
obj);
+ assertSame(bean, value);
+ }
+
+ private static Stream<Object> beans() {
+ return Stream.of(new SpringBeanInjectable(), new
JakartaInjectInjectable());
}
/**
* Test creation fails with null springcontextlocator
+ * Mock for an object with some Jakarta-Inject annotations
*/
- @Test
- public void testNullContextLocator()
+ @Test
+ public void testNullContextLocator()
+ {
+ Assertions.assertThrows(IllegalArgumentException.class, () -> new
AnnotProxyFieldValueFactory(null));
+ }
+
+ /**
+ * Class with Jakarta-Inject annotations for several scenarios:
+ * <UL>
+ * <LI>@{@link Inject} inject by class</LI>
+ * <LI>@{@link Inject} inject by name / @{@link Named}</LI>
+ * <LI>no possible injection as not annotated</LI>
+ * </UL>
+ * Same property names for the same scenarios as in {@link
SpringBeanInjectable}
+ * so both beans can be used with the same test methods, this makes
@ParameterizedTest - Tests possible
+ */
+ public static class JakartaInjectInjectable
{
- try
- {
- new AnnotProxyFieldValueFactory(null);
- fail();
- }
- catch (IllegalArgumentException e)
- {
- // noop
+ private Bean nobean;
+
+ @Inject
+ private Bean beanByClass;
+
+ @Inject
+ @Named("somebean")
+ private Bean beanByName;
+
+ @Override
+ public String toString() {
+ return "JakartaInjectInjectable";
}
}
/**
- * @throws Exception
+ * Class with SpringBean annotations for several scnenarios:
Review Comment:
```suggestion
* Class with SpringBean annotations for several scenarios:
```
##########
wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/AnnotProxyFieldValueFactoryTest.java:
##########
@@ -16,150 +16,310 @@
*/
package org.apache.wicket.spring.injection.annot;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+
+import java.lang.reflect.Field;
+import java.util.stream.Stream;
+
import org.apache.wicket.proxy.ILazyInitProxy;
-import org.apache.wicket.spring.ISpringContextLocator;
-import org.apache.wicket.spring.SpringBeanLocator;
-import org.apache.wicket.spring.injection.util.Bean;
-import org.apache.wicket.spring.injection.util.Bean2;
-import org.apache.wicket.spring.injection.util.InjectableInterface;
+import org.apache.wicket.proxy.IProxyTargetLocator;
import org.apache.wicket.spring.test.ApplicationContextMock;
+import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
-import org.springframework.context.ApplicationContext;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.springframework.beans.factory.support.AbstractBeanDefinition;
-import java.lang.reflect.Field;
-
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertNull;
-import static org.junit.jupiter.api.Assertions.assertSame;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
+import jakarta.inject.Inject;
+import jakarta.inject.Named;
/**
- * Tests for BeanAnnotLocatorFactory
+ * Tests for AnnotProxyFieldValueFactory
*
* @author igor
+ * @author hosea
*/
-public abstract class AnnotProxyFieldValueFactoryTest
+public class AnnotProxyFieldValueFactoryTest
{
- ISpringContextLocator mockCtxLocator = new ISpringContextLocator()
+
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldCreateProxyForBeanName(final Object obj) throws
Exception
{
- private static final long serialVersionUID = 1L;
+ final Bean somebean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ // add two beans to make sure wiring by name is different by
wiring by class
+ applicationContext.putBean(new Bean());
+ applicationContext.putBean("somebean", somebean);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByName");
+ final Object beanByClassProxy =
factory.getFieldValue(beanByClassField, obj);
+ final ILazyInitProxy lazyInitProxy =
assertInstanceOf(ILazyInitProxy.class, beanByClassProxy);
+ final IProxyTargetLocator beanByClassLocator =
lazyInitProxy.getObjectLocator();
+ assertSame(somebean, beanByClassLocator.locateProxyTarget());
+ }
- @Override
- public ApplicationContext getSpringContext()
- {
- ApplicationContextMock mock = new
ApplicationContextMock();
- mock.putBean(new Bean());
- mock.putBean("somebean", new Bean2());
- return mock;
- }
- };
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldThrowExceptionIfBeanNameNotFound(final Object obj)
throws Exception
+ {
+ final Bean somebean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ // add two beans to make sure wiring by name is different by
wiring by class
+ applicationContext.putBean(new Bean());
+ applicationContext.putBean("wrongNameBean", somebean);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
- protected final InjectableInterface obj;
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByName");
+ Assertions.assertThrows(IllegalStateException.class, () ->
factory.getFieldValue(beanByClassField, obj));
+ }
- protected final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(mockCtxLocator);
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldCreateProxyForClass(final Object obj) throws Exception
+ {
+ final Bean bean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ applicationContext.putBean(bean);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ final Object proxy = factory.getFieldValue(beanByClassField,
obj);
+ final ILazyInitProxy lazyInitProxy =
assertInstanceOf(ILazyInitProxy.class, proxy);
+ final IProxyTargetLocator beanByClassLocator =
lazyInitProxy.getObjectLocator();
+ assertSame(bean, beanByClassLocator.locateProxyTarget());
+ }
- protected AnnotProxyFieldValueFactoryTest(InjectableInterface
injectable)
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldThrowException_beanNameAmbiguous(final Object obj)
throws Exception
{
- obj = injectable;
+ final Bean primaryBean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ // add two beans to make ambiguous
+ applicationContext.putBean("anyBean", new Bean());
+ applicationContext.putBean("primaryBean", primaryBean);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ Assertions.assertThrows(IllegalStateException.class, () ->
factory.getFieldValue(beanByClassField, obj));
+ }
+
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldCreateProxyForUniquePrimary_beanNameAmbiguous(final
Object obj) throws Exception
+ {
+ final Bean primaryBean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ // add two beans to make ambiguous
+ applicationContext.putBean("anyBean", new Bean());
+ applicationContext.putBean("primaryBean", primaryBean);
+
applicationContext.getBeanFactory().getBeanDefinition("primaryBean").setPrimary(true);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ final Object beanByClassProxy =
factory.getFieldValue(beanByClassField, obj);
+ final ILazyInitProxy lazyInitProxy =
assertInstanceOf(ILazyInitProxy.class, beanByClassProxy);
+ final IProxyTargetLocator beanByClassLocator =
lazyInitProxy.getObjectLocator();
+ assertSame(primaryBean, beanByClassLocator.locateProxyTarget());
+ }
+
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldCreateProxyForFieldname_beanNameAmbiguous(final
Object obj) throws Exception
+ {
+ final Bean bean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ applicationContext.putBean(new Bean());
+ applicationContext.putBean("beanByClass", bean);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ final Object proxy = factory.getFieldValue(beanByClassField,
obj);
+ final ILazyInitProxy lazyInitProxy =
assertInstanceOf(ILazyInitProxy.class, proxy);
+ final IProxyTargetLocator beanByClassLocator =
lazyInitProxy.getObjectLocator();
+ assertSame(bean, beanByClassLocator.locateProxyTarget());
+ }
+
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void shouldIgnoreUnannotatedFields(final Object obj) throws
Exception
+ {
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(ApplicationContextMock::new);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("nobean");
+ final Object beanByClassProxy =
factory.getFieldValue(beanByClassField, obj);
+ assertNull(beanByClassProxy);
+ }
+
+ // https://issues.apache.org/jira/browse/WICKET-7170
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void
shouldCreateProxyForUniqueDefaultCandidate_beanNameAmbiguous(final Object obj)
throws Exception
+ {
+ final Bean defaultCandidate = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ // add two beans to make ambiguous
+ applicationContext.putBean("anyBean", new Bean());
+ applicationContext.putBean("primaryBean", defaultCandidate);
+ final AbstractBeanDefinition abstractBeanDefinition =
(AbstractBeanDefinition)
applicationContext.getBeanFactory().getBeanDefinition("anyBean");
+ abstractBeanDefinition.setDefaultCandidate(false);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ final Object beanByClassProxy =
factory.getFieldValue(beanByClassField, obj);
+ final ILazyInitProxy lazyInitProxy =
assertInstanceOf(ILazyInitProxy.class, beanByClassProxy);
+ final IProxyTargetLocator beanByClassLocator =
lazyInitProxy.getObjectLocator();
+ assertSame(defaultCandidate,
beanByClassLocator.locateProxyTarget());
}
/**
- * Test the factory
- *
- * @throws Exception
+ * test the cache, make sure the same proxy is returned for the same
dependency it represents
*/
- @Test
- public void testFactory() throws Exception
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void testCacheForBeanName(final Object obj) throws Exception
{
- SpringBeanLocator locator;
- Object proxy;
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ applicationContext.putBean(new Bean());
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
- Field field = obj.getClass().getDeclaredField("nobean");
- proxy = factory.getFieldValue(field, obj);
- assertNull(proxy);
+ final Field field =
obj.getClass().getDeclaredField("beanByClass");
+ final Object proxy1 = factory.getFieldValue(field, obj);
+ final Object proxy2 = factory.getFieldValue(field, obj);
+ assertSame(proxy1, proxy2);
+ }
- field = obj.getClass().getDeclaredField("beanByClass");
- proxy = factory.getFieldValue(field, obj);
- locator =
(SpringBeanLocator)((ILazyInitProxy)proxy).getObjectLocator();
- assertEquals(Bean.class, locator.getBeanType());
- assertSame(locator.getSpringContextLocator(), mockCtxLocator);
- assertTrue(factory.getFieldValue(field, obj) instanceof
ILazyInitProxy);
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void testCacheForClass(final Object obj) throws Exception
+ {
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ applicationContext.putBean(new Bean());
+ applicationContext.putBean("somebean", new Bean());
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext);
- field = obj.getClass().getDeclaredField("beanByName");
- proxy = factory.getFieldValue(field, obj);
- locator =
(SpringBeanLocator)((ILazyInitProxy)proxy).getObjectLocator();
- assertEquals("somebean", locator.getBeanName());
- assertEquals(Bean2.class, locator.getBeanType());
- assertSame(locator.getSpringContextLocator(), mockCtxLocator);
- assertTrue(factory.getFieldValue(field, obj) instanceof
ILazyInitProxy);
+ final Field field =
obj.getClass().getDeclaredField("beanByName");
+ final Object proxy1 = factory.getFieldValue(field, obj);
+ final Object proxy2 = factory.getFieldValue(field, obj);
+ assertSame(proxy1, proxy2);
}
/**
- * test the cache, make sure the same proxy is returned for the same
dependency it represents
- *
+ * https://issues.apache.org/jira/browse/WICKET-5686
* @throws Exception
*/
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void required(final Object obj) throws Exception
+ {
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(ApplicationContextMock::new);
+ final Field field =
obj.getClass().getDeclaredField("beanByClass");
+ Assertions.assertThrows(IllegalStateException.class, () ->
factory.getFieldValue(field, obj));
+ }
+
@Test
- public void testCache() throws Exception
+ public void optional() throws Exception
{
- Field field = obj.getClass().getDeclaredField("beanByClass");
- Object proxy1 = factory.getFieldValue(field, obj);
- Object proxy2 = factory.getFieldValue(field, obj);
- assertSame(proxy1, proxy2);
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(ApplicationContextMock::new);
+ final SpringBeanInjectable springBeanInjectable = new
SpringBeanInjectable();
+ final Field field =
springBeanInjectable.getClass().getDeclaredField("optional");
+ Assertions.assertNull(factory.getFieldValue(field,
springBeanInjectable));
+ }
- field = obj.getClass().getDeclaredField("beanByName");
- proxy1 = factory.getFieldValue(field, obj);
- proxy2 = factory.getFieldValue(field, obj);
- assertSame(proxy1, proxy2);
+ @ParameterizedTest
+ @MethodSource("beans")
+ public void lookupNonProxy(final Object obj) throws Exception
+ {
+ final Bean bean = new Bean();
+ final ApplicationContextMock applicationContext = new
ApplicationContextMock();
+ applicationContext.putBean(bean);
+ final boolean wrapInProxies = false;
+ final AnnotProxyFieldValueFactory factory = new
AnnotProxyFieldValueFactory(() -> applicationContext, wrapInProxies);
+
+ final Field beanByClassField =
obj.getClass().getDeclaredField("beanByClass");
+ final Object value = factory.getFieldValue(beanByClassField,
obj);
+ assertSame(bean, value);
+ }
+
+ private static Stream<Object> beans() {
+ return Stream.of(new SpringBeanInjectable(), new
JakartaInjectInjectable());
}
/**
* Test creation fails with null springcontextlocator
+ * Mock for an object with some Jakarta-Inject annotations
*/
- @Test
- public void testNullContextLocator()
+ @Test
+ public void testNullContextLocator()
Review Comment:
The indentation is off. It seems that spaces are used here instead of tabs.
Please make it consistent with the rest of the file.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]