Merge pull request #78 from wtlucy/MYFACES-4311_unittest

Myfaces 4311 unittest
diff --git a/impl/src/test/java/org/apache/myfaces/cdi/bean/CDIGenericConverterValidatorTest.java b/impl/src/test/java/org/apache/myfaces/cdi/bean/CDIGenericConverterValidatorTest.java
new file mode 100644
index 0000000..4c915ea
--- /dev/null
+++ b/impl/src/test/java/org/apache/myfaces/cdi/bean/CDIGenericConverterValidatorTest.java
@@ -0,0 +1,84 @@
+/*
+ * 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.UIInput;
+import javax.faces.component.UIOutput;
+import javax.faces.convert.Converter;
+import javax.faces.validator.ValidatorException;
+
+import org.apache.myfaces.test.core.AbstractMyFacesCDIRequestTestCase;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Unit tests to ensure that Generics work with CDI Converters and Validators
+ */
+public class CDIGenericConverterValidatorTest extends AbstractMyFacesCDIRequestTestCase {
+
+    @Test
+    public void testConverter() throws Exception {
+
+        String expectedValue = "zero";
+        String result = "";
+
+        startViewRequest("/CDIGenericConverterTest.xhtml");
+        application.addConverter("customConverter", "org.apache.myfaces.cdi.bean.CustomConverter");
+        processLifecycleExecuteAndRender();
+
+        UIOutput out = (UIOutput) facesContext.getViewRoot().findComponent("form1:out");
+        TestBean bean = (TestBean) out.getValue();
+        Converter converter = out.getConverter();
+
+        result = converter.getAsString(facesContext, out, bean);
+
+        Assert.assertTrue("The value output should have matched: " + expectedValue + " but was : " + result,
+                result.equals(expectedValue));
+
+    }
+
+    @Test
+    public void testValidator() throws Exception {
+
+        startViewRequest("/CDIGenericValidatorTest.xhtml");
+        application.addValidator("customValidator", "org.apache.myfaces.cdi.bean.CustomValidator");
+        processLifecycleExecuteAndRender();
+
+        UIInput out = (UIInput) facesContext.getViewRoot().findComponent("form1:out");
+        String r = (String) out.getValue();
+
+        //Expects a ValidatorException 
+        try {
+            out.getValidators()[0].validate(facesContext, out, r);
+            Assert.fail("ValidatorException was not thrown. Custom Generic validator failed.");
+        } catch (ValidatorException e) {
+            //Ignored
+        }
+
+    }
+
+    @Override
+    protected ExpressionFactory createExpressionFactory() {
+        // For this test we need the a real one so EL method invocation works.
+        return new org.apache.el.ExpressionFactoryImpl();
+    }
+
+}
diff --git a/impl/src/test/java/org/apache/myfaces/cdi/bean/CustomConverter.java b/impl/src/test/java/org/apache/myfaces/cdi/bean/CustomConverter.java
new file mode 100644
index 0000000..202ebfb
--- /dev/null
+++ b/impl/src/test/java/org/apache/myfaces/cdi/bean/CustomConverter.java
@@ -0,0 +1,38 @@
+/*
+ * 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.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.convert.Converter;
+import javax.faces.convert.FacesConverter;
+
+@FacesConverter(value="customConverter", managed = true)
+public class CustomConverter implements Converter<TestBean> {
+
+   @Override
+   public TestBean getAsObject(FacesContext facesContext, UIComponent component, String value) {
+      return new TestBean();
+   }
+
+   @Override
+   public String getAsString(FacesContext facesContext, UIComponent component, TestBean value) {
+      return value.getText();
+   }
+}
\ No newline at end of file
diff --git a/impl/src/test/java/org/apache/myfaces/cdi/bean/CustomValidator.java b/impl/src/test/java/org/apache/myfaces/cdi/bean/CustomValidator.java
new file mode 100644
index 0000000..6c16c45
--- /dev/null
+++ b/impl/src/test/java/org/apache/myfaces/cdi/bean/CustomValidator.java
@@ -0,0 +1,36 @@
+/*
+ * 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.faces.application.FacesMessage;
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.validator.Validator;
+import javax.faces.validator.FacesValidator;
+import javax.faces.validator.ValidatorException;
+
+@FacesValidator(value="customValidator", managed = true)
+public class CustomValidator implements Validator<String> {
+
+   @Override
+   public void validate(FacesContext facesContext, UIComponent component, String value)  throws ValidatorException {
+         FacesMessage msg = new FacesMessage("Validator Test Passes!");
+         throw new ValidatorException(msg);
+   }
+}
\ No newline at end of file
diff --git a/impl/src/test/resources/org/apache/myfaces/cdi/bean/CDIGenericConverterTest.xhtml b/impl/src/test/resources/org/apache/myfaces/cdi/bean/CDIGenericConverterTest.xhtml
new file mode 100644
index 0000000..8e8735a
--- /dev/null
+++ b/impl/src/test/resources/org/apache/myfaces/cdi/bean/CDIGenericConverterTest.xhtml
@@ -0,0 +1,32 @@
+<!--
+    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.
+-->
+<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html"
+     xmlns:f="http://xmlns.jcp.org/jsf/core">
+<h:head>
+    <title>CDI Generic Converter Test</title>
+</h:head>
+
+<h:body>
+    <h:form id="form1">
+        <h:outputText id="out" value="#{testBean}" converter="customConverter">
+        </h:outputText>
+    </h:form>
+</h:body>
+
+</html>
\ No newline at end of file
diff --git a/impl/src/test/resources/org/apache/myfaces/cdi/bean/CDIGenericValidatorTest.xhtml b/impl/src/test/resources/org/apache/myfaces/cdi/bean/CDIGenericValidatorTest.xhtml
new file mode 100644
index 0000000..36390c9
--- /dev/null
+++ b/impl/src/test/resources/org/apache/myfaces/cdi/bean/CDIGenericValidatorTest.xhtml
@@ -0,0 +1,32 @@
+<!--
+    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.
+-->
+<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html"
+     xmlns:f="http://xmlns.jcp.org/jsf/core">
+<h:head>
+    <title>CDI Generic Validator Test</title>
+</h:head>
+
+<h:body>
+    <h:form id="form1">
+        <h:inputText id="out" value="#{testBean.text}" validator="customValidator">
+        </h:inputText>
+    </h:form>
+</h:body>
+
+</html>
\ No newline at end of file