WICKET-5686 @Inject should require the bean dependency instead of setting null
Make fields annotated with @Inject required (Spring) (cherry picked from commit ee4ebba656ceaa3f55f74158e681a4b46b339c74) Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/8e794fc4 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/8e794fc4 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/8e794fc4 Branch: refs/heads/master Commit: 8e794fc468c1142b218d33ea3cbb67584d6c1441 Parents: 450d89d Author: Martin Tzvetanov Grigorov <[email protected]> Authored: Thu Aug 28 11:04:08 2014 +0300 Committer: Martin Tzvetanov Grigorov <[email protected]> Committed: Thu Aug 28 12:21:42 2014 +0300 ---------------------------------------------------------------------- .../annot/AnnotProxyFieldValueFactory.java | 3 +- .../annot/AnnotProxyFieldValueFactoryTest.java | 22 ++----- ...axInjectAnnotProxyFieldValueFactoryTest.java | 29 +++++++- ...ringBeanAnnotProxyFieldValueFactoryTest.java | 34 ++++++++++ .../spring/injection/util/Injectable.java | 64 ------------------ .../injection/util/JavaxInjectInjectable.java | 69 ++++++++++++++++++++ .../spring/injection/util/JavaxInjectable.java | 66 ------------------- .../injection/util/SpringBeanInjectable.java | 63 ++++++++++++++++++ 8 files changed, 201 insertions(+), 149 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/8e794fc4/wicket-spring/src/main/java/org/apache/wicket/spring/injection/annot/AnnotProxyFieldValueFactory.java ---------------------------------------------------------------------- diff --git a/wicket-spring/src/main/java/org/apache/wicket/spring/injection/annot/AnnotProxyFieldValueFactory.java b/wicket-spring/src/main/java/org/apache/wicket/spring/injection/annot/AnnotProxyFieldValueFactory.java index 6b7d071..54d11de 100644 --- a/wicket-spring/src/main/java/org/apache/wicket/spring/injection/annot/AnnotProxyFieldValueFactory.java +++ b/wicket-spring/src/main/java/org/apache/wicket/spring/injection/annot/AnnotProxyFieldValueFactory.java @@ -68,6 +68,7 @@ import org.springframework.context.support.AbstractApplicationContext; * @see LazyInitProxyFactory * @see SpringBean * @see SpringBeanLocator + * @see javax.inject.Inject * * @author Igor Vaynberg (ivaynberg) * @author Istvan Devai @@ -123,7 +124,7 @@ public class AnnotProxyFieldValueFactory implements IFieldValueFactory { Named named = field.getAnnotation(Named.class); name = named != null ? named.value() : ""; - required = false; + required = true; } String beanName = getBeanName(field, name, required); http://git-wip-us.apache.org/repos/asf/wicket/blob/8e794fc4/wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/AnnotProxyFieldValueFactoryTest.java ---------------------------------------------------------------------- diff --git a/wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/AnnotProxyFieldValueFactoryTest.java b/wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/AnnotProxyFieldValueFactoryTest.java index 892c8a8..f8b7a81 100644 --- a/wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/AnnotProxyFieldValueFactoryTest.java +++ b/wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/AnnotProxyFieldValueFactoryTest.java @@ -25,7 +25,6 @@ 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.Injectable; import org.apache.wicket.spring.injection.util.InjectableInterface; import org.apache.wicket.spring.test.ApplicationContextMock; import org.junit.Assert; @@ -36,9 +35,8 @@ import org.springframework.context.ApplicationContext; * Tests for BeanAnnotLocatorFactory * * @author igor - * */ -public class AnnotProxyFieldValueFactoryTest extends Assert +public abstract class AnnotProxyFieldValueFactoryTest extends Assert { ISpringContextLocator mockCtxLocator = new ISpringContextLocator() { @@ -54,23 +52,15 @@ public class AnnotProxyFieldValueFactoryTest extends Assert } }; - final InjectableInterface obj; + protected final InjectableInterface obj; - /** - * Construct. - */ - public AnnotProxyFieldValueFactoryTest() - { - this(new Injectable()); - } + protected final AnnotProxyFieldValueFactory factory = new AnnotProxyFieldValueFactory(mockCtxLocator); protected AnnotProxyFieldValueFactoryTest(InjectableInterface injectable) { obj = injectable; } - AnnotProxyFieldValueFactory factory = new AnnotProxyFieldValueFactory(mockCtxLocator); - /** * Test the factory * @@ -79,8 +69,8 @@ public class AnnotProxyFieldValueFactoryTest extends Assert @Test public void testFactory() throws Exception { - SpringBeanLocator locator = null; - Object proxy = null; + SpringBeanLocator locator; + Object proxy; Field field = obj.getClass().getDeclaredField("nobean"); proxy = factory.getFieldValue(field, obj); @@ -159,6 +149,8 @@ public class AnnotProxyFieldValueFactoryTest extends Assert } catch (RuntimeException e) { + // expected + assertTrue(true); } } http://git-wip-us.apache.org/repos/asf/wicket/blob/8e794fc4/wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/JavaxInjectAnnotProxyFieldValueFactoryTest.java ---------------------------------------------------------------------- diff --git a/wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/JavaxInjectAnnotProxyFieldValueFactoryTest.java b/wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/JavaxInjectAnnotProxyFieldValueFactoryTest.java index 47e3772..6d3d9cc 100644 --- a/wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/JavaxInjectAnnotProxyFieldValueFactoryTest.java +++ b/wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/JavaxInjectAnnotProxyFieldValueFactoryTest.java @@ -16,11 +16,14 @@ */ package org.apache.wicket.spring.injection.annot; -import org.apache.wicket.spring.injection.util.JavaxInjectable; +import java.lang.reflect.Field; + +import org.apache.wicket.spring.injection.util.JavaxInjectInjectable; +import org.junit.Test; /** - * Tests for javax.inject.Inject when used with spring + * Tests for Spring injection with {@literal @javax.inject.Inject} annotation */ public class JavaxInjectAnnotProxyFieldValueFactoryTest extends AnnotProxyFieldValueFactoryTest { @@ -29,6 +32,26 @@ public class JavaxInjectAnnotProxyFieldValueFactoryTest extends AnnotProxyFieldV */ public JavaxInjectAnnotProxyFieldValueFactoryTest() { - super(new JavaxInjectable()); + super(new JavaxInjectInjectable()); + } + + /** + * https://issues.apache.org/jira/browse/WICKET-5686 + * @throws Exception + */ + @Test + public void required() throws Exception + { + Field field = obj.getClass().getDeclaredField("nonExisting"); + try + { + factory.getFieldValue(field, obj); + fail("Fields annotated with @Inject are required!"); + } + catch (IllegalStateException isx) + { + // expected + assertTrue(true); + } } } http://git-wip-us.apache.org/repos/asf/wicket/blob/8e794fc4/wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/SpringBeanAnnotProxyFieldValueFactoryTest.java ---------------------------------------------------------------------- diff --git a/wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/SpringBeanAnnotProxyFieldValueFactoryTest.java b/wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/SpringBeanAnnotProxyFieldValueFactoryTest.java new file mode 100644 index 0000000..424073b --- /dev/null +++ b/wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/SpringBeanAnnotProxyFieldValueFactoryTest.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.wicket.spring.injection.annot; + +import org.apache.wicket.spring.injection.util.SpringBeanInjectable; + + +/** + * Tests for Spring injections with {@literal @SpringBean} annotation + */ +public class SpringBeanAnnotProxyFieldValueFactoryTest extends AnnotProxyFieldValueFactoryTest +{ + /** + * Construct. + */ + public SpringBeanAnnotProxyFieldValueFactoryTest() + { + super(new SpringBeanInjectable()); + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/8e794fc4/wicket-spring/src/test/java/org/apache/wicket/spring/injection/util/Injectable.java ---------------------------------------------------------------------- diff --git a/wicket-spring/src/test/java/org/apache/wicket/spring/injection/util/Injectable.java b/wicket-spring/src/test/java/org/apache/wicket/spring/injection/util/Injectable.java deleted file mode 100644 index 7f5c61d..0000000 --- a/wicket-spring/src/test/java/org/apache/wicket/spring/injection/util/Injectable.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.wicket.spring.injection.util; - -import org.apache.wicket.spring.injection.annot.SpringBean; - -/** - * Mock for an object with some SpringBean annotations - * - * @author Igor Vaynberg (ivaynberg) - * - */ -public class Injectable implements InjectableInterface -{ - private Bean nobean; - - @SpringBean - private Bean beanByClass; - - @SpringBean(name = "somebean") - private Bean2 beanByName; - - /** - * @return test bean - */ - @Override - public Bean getBeanByClass() - { - return beanByClass; - } - - /** - * @return test bean - */ - @Override - public Bean2 getBeanByName() - { - return beanByName; - } - - /** - * @return test bean - */ - @Override - public Bean getNobean() - { - return nobean; - } - -} http://git-wip-us.apache.org/repos/asf/wicket/blob/8e794fc4/wicket-spring/src/test/java/org/apache/wicket/spring/injection/util/JavaxInjectInjectable.java ---------------------------------------------------------------------- diff --git a/wicket-spring/src/test/java/org/apache/wicket/spring/injection/util/JavaxInjectInjectable.java b/wicket-spring/src/test/java/org/apache/wicket/spring/injection/util/JavaxInjectInjectable.java new file mode 100644 index 0000000..0f378fc --- /dev/null +++ b/wicket-spring/src/test/java/org/apache/wicket/spring/injection/util/JavaxInjectInjectable.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.wicket.spring.injection.util; + +import javax.inject.Inject; +import javax.inject.Named; + +/** + * Mock for an object with some SpringBean annotations + * + * @author Igor Vaynberg (ivaynberg) + * + */ +public class JavaxInjectInjectable implements InjectableInterface +{ + private Bean nobean; + + @Inject + private Bean beanByClass; + + @Inject + @Named("somebean") + private Bean2 beanByName; + + @Inject + private String nonExisting; + + /** + * @return test bean + */ + @Override + public Bean getBeanByClass() + { + return beanByClass; + } + + /** + * @return test bean + */ + @Override + public Bean2 getBeanByName() + { + return beanByName; + } + + /** + * @return test bean + */ + @Override + public Bean getNobean() + { + return nobean; + } + +} http://git-wip-us.apache.org/repos/asf/wicket/blob/8e794fc4/wicket-spring/src/test/java/org/apache/wicket/spring/injection/util/JavaxInjectable.java ---------------------------------------------------------------------- diff --git a/wicket-spring/src/test/java/org/apache/wicket/spring/injection/util/JavaxInjectable.java b/wicket-spring/src/test/java/org/apache/wicket/spring/injection/util/JavaxInjectable.java deleted file mode 100644 index 2ac5767..0000000 --- a/wicket-spring/src/test/java/org/apache/wicket/spring/injection/util/JavaxInjectable.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.wicket.spring.injection.util; - -import javax.inject.Inject; -import javax.inject.Named; - -/** - * Mock for an object with some SpringBean annotations - * - * @author Igor Vaynberg (ivaynberg) - * - */ -public class JavaxInjectable implements InjectableInterface -{ - private Bean nobean; - - @Inject - private Bean beanByClass; - - @Inject - @Named("somebean") - private Bean2 beanByName; - - /** - * @return test bean - */ - @Override - public Bean getBeanByClass() - { - return beanByClass; - } - - /** - * @return test bean - */ - @Override - public Bean2 getBeanByName() - { - return beanByName; - } - - /** - * @return test bean - */ - @Override - public Bean getNobean() - { - return nobean; - } - -} http://git-wip-us.apache.org/repos/asf/wicket/blob/8e794fc4/wicket-spring/src/test/java/org/apache/wicket/spring/injection/util/SpringBeanInjectable.java ---------------------------------------------------------------------- diff --git a/wicket-spring/src/test/java/org/apache/wicket/spring/injection/util/SpringBeanInjectable.java b/wicket-spring/src/test/java/org/apache/wicket/spring/injection/util/SpringBeanInjectable.java new file mode 100644 index 0000000..f13329d --- /dev/null +++ b/wicket-spring/src/test/java/org/apache/wicket/spring/injection/util/SpringBeanInjectable.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.wicket.spring.injection.util; + +import org.apache.wicket.spring.injection.annot.SpringBean; + +/** + * Mock for an object with some SpringBean annotations + * + * @author Igor Vaynberg (ivaynberg) + */ +public class SpringBeanInjectable implements InjectableInterface +{ + private Bean nobean; + + @SpringBean + private Bean beanByClass; + + @SpringBean(name = "somebean") + private Bean2 beanByName; + + /** + * @return test bean + */ + @Override + public Bean getBeanByClass() + { + return beanByClass; + } + + /** + * @return test bean + */ + @Override + public Bean2 getBeanByName() + { + return beanByName; + } + + /** + * @return test bean + */ + @Override + public Bean getNobean() + { + return nobean; + } + +}
