You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by em...@apache.org on 2017/08/07 15:08:31 UTC
svn commit: r1804343 - in /myfaces/core/branches/2.3.x/impl: ./
src/main/java/org/apache/myfaces/cdi/bean/
src/test/java/org/apache/myfaces/cdi/
src/test/java/org/apache/myfaces/cdi/bean/
src/test/resources/org/apache/myfaces/cdi/ src/test/resources/or...
Author: embreijo
Date: Mon Aug 7 15:08:31 2017
New Revision: 1804343
URL: http://svn.apache.org/viewvc?rev=1804343&view=rev
Log:
MYFACES-4130 CDI @ManagedProperty does not work with all types
Added:
myfaces/core/branches/2.3.x/impl/src/test/java/org/apache/myfaces/cdi/
myfaces/core/branches/2.3.x/impl/src/test/java/org/apache/myfaces/cdi/bean/
myfaces/core/branches/2.3.x/impl/src/test/java/org/apache/myfaces/cdi/bean/DynamicManagedPropertyProducerTest.java
myfaces/core/branches/2.3.x/impl/src/test/java/org/apache/myfaces/cdi/bean/ManagedPropertyBean.java
myfaces/core/branches/2.3.x/impl/src/test/java/org/apache/myfaces/cdi/bean/TestBean.java
myfaces/core/branches/2.3.x/impl/src/test/resources/org/apache/myfaces/cdi/
myfaces/core/branches/2.3.x/impl/src/test/resources/org/apache/myfaces/cdi/bean/
myfaces/core/branches/2.3.x/impl/src/test/resources/org/apache/myfaces/cdi/bean/CDIManagedProperty.xhtml
Modified:
myfaces/core/branches/2.3.x/impl/pom.xml
myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/bean/DynamicManagedPropertyProducer.java
Modified: myfaces/core/branches/2.3.x/impl/pom.xml
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.3.x/impl/pom.xml?rev=1804343&r1=1804342&r2=1804343&view=diff
==============================================================================
--- myfaces/core/branches/2.3.x/impl/pom.xml (original)
+++ myfaces/core/branches/2.3.x/impl/pom.xml Mon Aug 7 15:08:31 2017
@@ -1291,14 +1291,19 @@
<!-- We need a real EL implementation for test ui:param tag,
because myfaces-test MockExpressionFactory is not designed to
- handle VariableMapper stuff (see IncludeParamTestCase) -->
+ handle VariableMapper stuff (see IncludeParamTestCase). Needed
+ to update to 7.0.0 for method invocation to work. Later
+ versions cause failures and would require org.apache.el.parser.SKIP_IDENTIFIER_CHECK=true
+ to be set.-->
<dependency>
<groupId>org.apache.tomcat</groupId>
- <artifactId>el-api</artifactId>
+ <artifactId>tomcat-el-api</artifactId>
+ <version>7.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
- <artifactId>jasper-el</artifactId>
+ <artifactId>tomcat-jasper-el</artifactId>
+ <version>7.0.0</version>
</dependency>
<!-- Openwebbeans for CDI Test -->
Modified: myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/bean/DynamicManagedPropertyProducer.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/bean/DynamicManagedPropertyProducer.java?rev=1804343&r1=1804342&r2=1804343&view=diff
==============================================================================
--- myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/bean/DynamicManagedPropertyProducer.java (original)
+++ myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/cdi/bean/DynamicManagedPropertyProducer.java Mon Aug 7 15:08:31 2017
@@ -21,6 +21,7 @@ package org.apache.myfaces.cdi.bean;
import java.io.Serializable;
import java.lang.annotation.Annotation;
+import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import static java.util.Arrays.asList;
import java.util.Collections;
@@ -55,7 +56,18 @@ public class DynamicManagedPropertyProdu
this.beanManager = beanManager;
this.typeInfo = typeInfo;
types = new HashSet<Type>(asList(typeInfo.getType(), Object.class));
- beanClass = ClassUtils.simpleClassForName(typeInfo.getType().getTypeName());
+ Type beanType = typeInfo.getType();
+ // Need to check for ParameterizedType to support types such as List<String>
+ if ( beanType instanceof ParameterizedType )
+ {
+ beanClass = ClassUtils.simpleClassForName(((ParameterizedType) beanType).getRawType().getTypeName());
+ }
+ else
+ {
+ // need to use simpleJavaTypeToClass to support Arrays and primitive types
+ beanClass = ClassUtils.simpleJavaTypeToClass(beanType.getTypeName());
+ }
+
}
@Override
Added: myfaces/core/branches/2.3.x/impl/src/test/java/org/apache/myfaces/cdi/bean/DynamicManagedPropertyProducerTest.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.3.x/impl/src/test/java/org/apache/myfaces/cdi/bean/DynamicManagedPropertyProducerTest.java?rev=1804343&view=auto
==============================================================================
--- myfaces/core/branches/2.3.x/impl/src/test/java/org/apache/myfaces/cdi/bean/DynamicManagedPropertyProducerTest.java (added)
+++ myfaces/core/branches/2.3.x/impl/src/test/java/org/apache/myfaces/cdi/bean/DynamicManagedPropertyProducerTest.java Mon Aug 7 15:08:31 2017
@@ -0,0 +1,60 @@
+/*
+ * 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.myfaces.cdi.bean;
+
+import javax.el.ExpressionFactory;
+import javax.faces.component.UIOutput;
+import org.apache.myfaces.mc.test.core.AbstractMyFacesCDIRequestTestCase;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * A unit test to test the CDI @ManagedProperty
+ */
+public class DynamicManagedPropertyProducerTest extends AbstractMyFacesCDIRequestTestCase
+{
+
+ @Test
+ public void testCDIManagedProperty() throws Exception {
+ String expectedValue = "numberManagedProperty=0textManagedProperty=zerolistManagedProperty" +
+ "=zerostringArrayManagedProperty=zerobean=org.apache.myfaces.cdi.bean.TestBean";
+ String result;
+
+ startViewRequest("/CDIManagedProperty.xhtml");
+ processLifecycleExecuteAndRender();
+
+ UIOutput out = (UIOutput) facesContext.getViewRoot().findComponent("form1:out1");
+ result = out.getValue().toString();
+
+ result = result.substring(0, result.indexOf("@"));
+ result = result.replaceAll("\\s","");
+
+ Assert.assertTrue("The value output should have matched: " + expectedValue + " but was : " + result, result.equals(expectedValue));
+ }
+
+ @Override
+ protected ExpressionFactory createExpressionFactory()
+ {
+ // For this test we need the a real one so EL method invocation works.
+ return new org.apache.el.ExpressionFactoryImpl();
+ }
+
+
+}
Added: myfaces/core/branches/2.3.x/impl/src/test/java/org/apache/myfaces/cdi/bean/ManagedPropertyBean.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.3.x/impl/src/test/java/org/apache/myfaces/cdi/bean/ManagedPropertyBean.java?rev=1804343&view=auto
==============================================================================
--- myfaces/core/branches/2.3.x/impl/src/test/java/org/apache/myfaces/cdi/bean/ManagedPropertyBean.java (added)
+++ myfaces/core/branches/2.3.x/impl/src/test/java/org/apache/myfaces/cdi/bean/ManagedPropertyBean.java Mon Aug 7 15:08:31 2017
@@ -0,0 +1,59 @@
+/*
+ * 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.myfaces.cdi.bean;
+
+import java.util.List;
+
+import javax.enterprise.context.RequestScoped;
+import javax.faces.annotation.ManagedProperty;
+import javax.inject.Inject;
+import javax.inject.Named;
+
+@Named
+@RequestScoped
+public class ManagedPropertyBean {
+
+ @Inject
+ @ManagedProperty("#{testBean.number}")
+ private int numberManagedProperty;
+
+ @Inject
+ @ManagedProperty("#{testBean.text}")
+ private String textManagedProperty;
+
+ @Inject
+ @ManagedProperty("#{testBean.list}")
+ private List<String> listManagedProperty;
+
+ @Inject
+ @ManagedProperty("#{testBean.stringArray}")
+ private String[] stringArrayManagedProperty;
+
+ @Inject
+ @ManagedProperty("#{testBean}")
+ private TestBean bean;
+
+ public String test() {
+ return "numberManagedProperty = " + numberManagedProperty +
+ " textManagedProperty = " + textManagedProperty +
+ " listManagedProperty = " + listManagedProperty.get(0) +
+ " stringArrayManagedProperty = " + stringArrayManagedProperty[0] +
+ " bean = " + bean.toString();
+ }
+}
Added: myfaces/core/branches/2.3.x/impl/src/test/java/org/apache/myfaces/cdi/bean/TestBean.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.3.x/impl/src/test/java/org/apache/myfaces/cdi/bean/TestBean.java?rev=1804343&view=auto
==============================================================================
--- myfaces/core/branches/2.3.x/impl/src/test/java/org/apache/myfaces/cdi/bean/TestBean.java (added)
+++ myfaces/core/branches/2.3.x/impl/src/test/java/org/apache/myfaces/cdi/bean/TestBean.java Mon Aug 7 15:08:31 2017
@@ -0,0 +1,67 @@
+/*
+ * 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.myfaces.cdi.bean;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import javax.enterprise.context.RequestScoped;
+import javax.inject.Named;
+
+@Named
+@RequestScoped
+public class TestBean {
+ private int number = 0;
+ private String text = "zero";
+ private List<String> list = new ArrayList<String>(Arrays.asList("zero"));
+ private String[] stringArray = new String[] { "zero" };
+
+ public void setNumber(int number) {
+ this.number = number;
+ }
+
+ public int getNumber() {
+ return this.number;
+ }
+
+ public void setText(String text) {
+ this.text = text;
+ }
+
+ public String getText() {
+ return this.text;
+ }
+
+ public void setList(List<String> list) {
+ this.list = list;
+ }
+
+ public List<String> getList() {
+ return this.list;
+ }
+
+ public void setStringArray(String[] stringArray) {
+ this.stringArray = stringArray;
+ }
+
+ public String[] getStringArray() {
+ return this.stringArray;
+ }
+}
Added: myfaces/core/branches/2.3.x/impl/src/test/resources/org/apache/myfaces/cdi/bean/CDIManagedProperty.xhtml
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.3.x/impl/src/test/resources/org/apache/myfaces/cdi/bean/CDIManagedProperty.xhtml?rev=1804343&view=auto
==============================================================================
--- myfaces/core/branches/2.3.x/impl/src/test/resources/org/apache/myfaces/cdi/bean/CDIManagedProperty.xhtml (added)
+++ myfaces/core/branches/2.3.x/impl/src/test/resources/org/apache/myfaces/cdi/bean/CDIManagedProperty.xhtml Mon Aug 7 15:08:31 2017
@@ -0,0 +1,35 @@
+<!--
+ 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.
+-->
+<!DOCTYPE html>
+<html xmlns="http://www.w3.org/1999/xhtml"
+ xmlns:h="http://xmlns.jcp.org/jsf/html">
+
+<h:head>
+ <title>CDI ManagedProperty Test</title>
+</h:head>
+
+<h:body>
+ <h:form id="form1">
+ CDI ManagedProperty Test
+ <p/>
+ <h:outputText id="out1" value="#{managedPropertyBean.test()}"/>
+ <p/>
+ </h:form>
+</h:body>
+</html>