You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by mg...@apache.org on 2013/06/17 09:55:38 UTC

[2/2] git commit: WICKET-5226 CDI integration fails in Glassfish 4.0 with WELD-000070

WICKET-5226 CDI integration fails in Glassfish 4.0 with WELD-000070


Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/4bc20743
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/4bc20743
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/4bc20743

Branch: refs/heads/master
Commit: 4bc20743fe628341a7cce407b36fa1984b912f92
Parents: 757f4b9
Author: Martin Tzvetanov Grigorov <mg...@apache.org>
Authored: Mon Jun 17 09:51:50 2013 +0200
Committer: Martin Tzvetanov Grigorov <mg...@apache.org>
Committed: Mon Jun 17 09:52:21 2013 +0200

----------------------------------------------------------------------
 wicket-cdi/pom.xml                              |   6 +
 .../apache/wicket/cdi/ComponentInjector.java    |  18 ++-
 .../wicket/cdi/ComponentInjectorTest.java       | 131 +++++++++++++++++++
 .../wicket/examples/cdi/ConversationPage2.java  |  23 ++--
 4 files changed, 168 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/4bc20743/wicket-cdi/pom.xml
----------------------------------------------------------------------
diff --git a/wicket-cdi/pom.xml b/wicket-cdi/pom.xml
index 4e8626a..54611f1 100644
--- a/wicket-cdi/pom.xml
+++ b/wicket-cdi/pom.xml
@@ -52,6 +52,12 @@
 			<artifactId>junit</artifactId>
 			<scope>test</scope>
 		</dependency>
+		<dependency>
+			<groupId>javax.el</groupId>
+			<artifactId>javax.el-api</artifactId>
+			<version>2.2.4</version>
+			<scope>test</scope>
+		</dependency>
 	</dependencies>
 	<build>
 		<pluginManagement>

http://git-wip-us.apache.org/repos/asf/wicket/blob/4bc20743/wicket-cdi/src/main/java/org/apache/wicket/cdi/ComponentInjector.java
----------------------------------------------------------------------
diff --git a/wicket-cdi/src/main/java/org/apache/wicket/cdi/ComponentInjector.java b/wicket-cdi/src/main/java/org/apache/wicket/cdi/ComponentInjector.java
index bc302ef..0316004 100644
--- a/wicket-cdi/src/main/java/org/apache/wicket/cdi/ComponentInjector.java
+++ b/wicket-cdi/src/main/java/org/apache/wicket/cdi/ComponentInjector.java
@@ -16,8 +16,12 @@
  */
 package org.apache.wicket.cdi;
 
+import java.lang.reflect.Modifier;
+
 import org.apache.wicket.Component;
 import org.apache.wicket.application.IComponentInstantiationListener;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Injects components with CDI dependencies
@@ -27,6 +31,8 @@ import org.apache.wicket.application.IComponentInstantiationListener;
  */
 class ComponentInjector extends AbstractInjector implements IComponentInstantiationListener
 {
+	private static final Logger LOG = LoggerFactory.getLogger(ComponentInjector.class);
+
 	/**
 	 * Constructor
 	 * 
@@ -40,7 +46,15 @@ class ComponentInjector extends AbstractInjector implements IComponentInstantiat
 	@Override
 	public void onInstantiation(Component component)
 	{
-		inject(component);
-	}
+		Class<? extends Component> componentClass = component.getClass();
 
+		if (componentClass.isMemberClass() && Modifier.isStatic(componentClass.getModifiers()) == false)
+		{
+			LOG.debug("Skipping non-static inner class '{}' ", componentClass);
+		}
+		else
+		{
+			inject(component);
+		}
+	}
 }

http://git-wip-us.apache.org/repos/asf/wicket/blob/4bc20743/wicket-cdi/src/test/java/org/apache/wicket/cdi/ComponentInjectorTest.java
----------------------------------------------------------------------
diff --git a/wicket-cdi/src/test/java/org/apache/wicket/cdi/ComponentInjectorTest.java b/wicket-cdi/src/test/java/org/apache/wicket/cdi/ComponentInjectorTest.java
new file mode 100644
index 0000000..55d00cd
--- /dev/null
+++ b/wicket-cdi/src/test/java/org/apache/wicket/cdi/ComponentInjectorTest.java
@@ -0,0 +1,131 @@
+/*
+ * 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.cdi;
+
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+
+import javax.enterprise.inject.spi.BeanManager;
+import javax.inject.Inject;
+
+import org.apache.wicket.markup.html.WebComponent;
+import org.apache.wicket.util.tester.WicketTester;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+/**
+ * Tests for ComponentInjector
+ */
+public class ComponentInjectorTest extends Assert
+{
+	private WicketTester tester;
+
+	@Before
+	public void before()
+	{
+		// starts an application so we can instantiate components
+		tester = new WicketTester();
+	}
+
+	@After
+	public void after()
+	{
+		tester.destroy();
+		tester = null;
+	}
+
+	/**
+	 * https://issues.apache.org/jira/browse/WICKET-5226
+	 */
+	@Test
+	public void innerNonStaticClass()
+	{
+		BeanManager beanManager = mock(BeanManager.class);
+		INonContextualManager nonContextualManager = mock(INonContextualManager.class);
+		CdiContainer cdiContainer = new CdiContainer(beanManager, nonContextualManager);
+		ComponentInjector injector = new ComponentInjector(cdiContainer);
+
+		TestNonStaticComponent component = new TestNonStaticComponent("someId");
+		assertNull(component.dependency);
+
+		injector.onInstantiation(component);
+
+		verify(nonContextualManager, never()).inject(any());
+	}
+
+	/**
+	 * https://issues.apache.org/jira/browse/WICKET-5226
+	 */
+	@Test
+	public void innerStaticClass()
+	{
+		BeanManager beanManager = mock(BeanManager.class);
+		INonContextualManager nonContextualManager = mock(INonContextualManager.class);
+		final String expectedValue = "injected";
+
+		doAnswer(new Answer<Void>()
+		{
+			@Override
+			public Void answer(InvocationOnMock invocation) throws Throwable
+			{
+				TestStaticComponent component = (TestStaticComponent) invocation.getArguments()[0];
+				component.dependency = expectedValue;
+
+				return null;
+			}
+		}).when(nonContextualManager).inject(any(TestStaticComponent.class));
+
+		CdiContainer cdiContainer = new CdiContainer(beanManager, nonContextualManager);
+		ComponentInjector injector = new ComponentInjector(cdiContainer);
+
+		TestStaticComponent component = new TestStaticComponent("someId");
+		assertNull(component.dependency);
+
+		injector.onInstantiation(component);
+
+		assertEquals(expectedValue, component.dependency);
+	}
+
+	private class TestNonStaticComponent extends WebComponent
+	{
+		@Inject
+		private String dependency;
+
+		public TestNonStaticComponent(String id)
+		{
+			super(id);
+		}
+	}
+
+	private static class TestStaticComponent extends WebComponent
+	{
+		@Inject
+		private String dependency;
+
+		public TestStaticComponent(String id)
+		{
+			super(id);
+		}
+	}
+}

http://git-wip-us.apache.org/repos/asf/wicket/blob/4bc20743/wicket-examples/src/main/java/org/apache/wicket/examples/cdi/ConversationPage2.java
----------------------------------------------------------------------
diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/cdi/ConversationPage2.java b/wicket-examples/src/main/java/org/apache/wicket/examples/cdi/ConversationPage2.java
index 89b9e6a..df175f8 100644
--- a/wicket-examples/src/main/java/org/apache/wicket/examples/cdi/ConversationPage2.java
+++ b/wicket-examples/src/main/java/org/apache/wicket/examples/cdi/ConversationPage2.java
@@ -32,15 +32,22 @@ public class ConversationPage2 extends CdiExamplePage
 	{
 		add(new Label("count", new PropertyModel(this, "counter.count")));
 
-		add(new Link<Void>("increment")
-		{
-			@Override
-			public void onClick()
-			{
-				counter.increment();
-			}
-		});
+		add(new IncrementLink("increment"));
 
 		add(new BookmarkablePageLink<Void>("next", ConversationPage3.class));
 	}
+
+	private class IncrementLink extends Link<Void> {
+
+		public IncrementLink(String id)
+		{
+			super(id);
+		}
+
+		@Override
+		public void onClick()
+		{
+			counter.increment();
+		}
+	}
 }