Updated Branches: refs/heads/master 7f14a66a3 -> fc03723bb
DELTASPIKE-468 optional integration with myfaces-test (first draft) Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/fc03723b Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/fc03723b Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/fc03723b Branch: refs/heads/master Commit: fc03723bb17eaf846a8090f93f5059df27524c19 Parents: 7ec889f Author: gpetracek <[email protected]> Authored: Sun Dec 15 20:21:49 2013 +0100 Committer: gpetracek <[email protected]> Committed: Sun Dec 15 20:44:09 2013 +0100 ---------------------------------------------------------------------- deltaspike/modules/jsf/pom.xml | 11 - deltaspike/modules/test-control/impl/pom.xml | 36 +++ .../impl/jsf/MockedJsf2TestContainer.java | 230 +++++++++++++++++++ .../testcontrol/uc005/JsfContainerTest.java | 114 +++++++++ .../uc006/SkipExternalContainerTest.java | 45 ++++ ...deltaspike.testcontrol.spi.ExternalContainer | 18 ++ deltaspike/parent/pom.xml | 27 +++ 7 files changed, 470 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/deltaspike/blob/fc03723b/deltaspike/modules/jsf/pom.xml ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jsf/pom.xml b/deltaspike/modules/jsf/pom.xml index 06a79b3..8f8c53f 100644 --- a/deltaspike/modules/jsf/pom.xml +++ b/deltaspike/modules/jsf/pom.xml @@ -39,17 +39,6 @@ <module>impl</module> </modules> - <dependencyManagement> - <dependencies> - <dependency> - <groupId>org.apache.myfaces.core</groupId> - <artifactId>myfaces-api</artifactId> - <version>${myfaces2.version}</version> - <scope>provided</scope> - </dependency> - </dependencies> - </dependencyManagement> - <dependencies> <!-- JSF API, just for compiling --> <dependency> http://git-wip-us.apache.org/repos/asf/deltaspike/blob/fc03723b/deltaspike/modules/test-control/impl/pom.xml ---------------------------------------------------------------------- diff --git a/deltaspike/modules/test-control/impl/pom.xml b/deltaspike/modules/test-control/impl/pom.xml index 2b6c0fe..4197e63 100644 --- a/deltaspike/modules/test-control/impl/pom.xml +++ b/deltaspike/modules/test-control/impl/pom.xml @@ -65,6 +65,42 @@ <artifactId>junit</artifactId> <scope>provided</scope> </dependency> + + <dependency> + <groupId>org.apache.myfaces.test</groupId> + <artifactId>myfaces-test20</artifactId> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>org.apache.myfaces.core</groupId> + <artifactId>myfaces-api</artifactId> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>org.apache.myfaces.core</groupId> + <artifactId>myfaces-impl</artifactId> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>org.apache.geronimo.specs</groupId> + <artifactId>geronimo-el_2.2_spec</artifactId> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>org.apache.geronimo.specs</groupId> + <artifactId>geronimo-servlet_2.5_spec</artifactId> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + <scope>test</scope> + </dependency> </dependencies> <build> http://git-wip-us.apache.org/repos/asf/deltaspike/blob/fc03723b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/jsf/MockedJsf2TestContainer.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/jsf/MockedJsf2TestContainer.java b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/jsf/MockedJsf2TestContainer.java new file mode 100644 index 0000000..e6e2b2b --- /dev/null +++ b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/jsf/MockedJsf2TestContainer.java @@ -0,0 +1,230 @@ +/* + * 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.deltaspike.testcontrol.impl.jsf; + +import org.apache.deltaspike.testcontrol.spi.ExternalContainer; +import org.apache.myfaces.test.mock.MockApplicationFactory; +import org.apache.myfaces.test.mock.MockExceptionHandlerFactory; +import org.apache.myfaces.test.mock.MockExternalContext; +import org.apache.myfaces.test.mock.MockFacesContext; +import org.apache.myfaces.test.mock.MockFacesContextFactory; +import org.apache.myfaces.test.mock.MockHttpServletRequest; +import org.apache.myfaces.test.mock.MockHttpServletResponse; +import org.apache.myfaces.test.mock.MockHttpSession; +import org.apache.myfaces.test.mock.MockPartialViewContextFactory; +import org.apache.myfaces.test.mock.MockRenderKit; +import org.apache.myfaces.test.mock.MockRenderKitFactory; +import org.apache.myfaces.test.mock.MockServletConfig; +import org.apache.myfaces.test.mock.MockServletContext; +import org.apache.myfaces.test.mock.lifecycle.MockLifecycleFactory; +import org.apache.myfaces.test.mock.visit.MockVisitContextFactory; + +import javax.enterprise.context.RequestScoped; +import javax.enterprise.context.SessionScoped; +import javax.faces.FactoryFinder; +import javax.faces.application.Application; +import javax.faces.application.ApplicationFactory; +import javax.faces.component.UIViewRoot; +import javax.faces.context.ExceptionHandler; +import javax.faces.context.ExceptionHandlerFactory; +import javax.faces.context.FacesContext; +import javax.faces.context.FacesContextFactory; +import javax.faces.lifecycle.Lifecycle; +import javax.faces.lifecycle.LifecycleFactory; +import javax.faces.render.RenderKit; +import javax.faces.render.RenderKitFactory; +import java.lang.annotation.Annotation; +import java.util.Locale; + +public class MockedJsf2TestContainer implements ExternalContainer +{ + protected MockServletConfig config = null; + protected MockServletContext servletContext = null; + + protected Lifecycle lifecycle = null; + protected RenderKit renderKit = null; + protected Application application = null; + + protected FacesContext facesContext = null; + protected MockHttpServletRequest request = null; + protected MockHttpServletResponse response = null; + protected MockHttpSession session = null; + + public void boot() + { + initServletObjects(); + initJsf(); + } + + protected void initServletObjects() + { + this.servletContext = new MockServletContext(); + this.config = new MockServletConfig(this.servletContext); + } + + protected void initJsf() + { + initJsfFactories(); + + initLifecycle(); + initApplication(); + initRenderKit(); + } + + protected void initJsfFactories() + { + FactoryFinder.releaseFactories(); + + addFactory(FactoryFinder.APPLICATION_FACTORY, MockApplicationFactory.class.getName()); + addFactory(FactoryFinder.FACES_CONTEXT_FACTORY, MockFacesContextFactory.class.getName()); + addFactory(FactoryFinder.LIFECYCLE_FACTORY, MockLifecycleFactory.class.getName()); + addFactory(FactoryFinder.RENDER_KIT_FACTORY, MockRenderKitFactory.class.getName()); + addFactory(FactoryFinder.EXCEPTION_HANDLER_FACTORY, MockExceptionHandlerFactory.class.getName()); + addFactory(FactoryFinder.PARTIAL_VIEW_CONTEXT_FACTORY, MockPartialViewContextFactory.class.getName()); + addFactory(FactoryFinder.VISIT_CONTEXT_FACTORY, MockVisitContextFactory.class.getName()); + } + + protected void addFactory(String factoryName, String className) + { + FactoryFinder.setFactory(factoryName, className); + } + + protected void initLifecycle() + { + LifecycleFactory lifecycleFactory = + (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY); + this.lifecycle = lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE); + } + + protected void initApplication() + { + ApplicationFactory applicationFactory = + (ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY); + this.application = applicationFactory.getApplication(); + } + + protected void initRenderKit() + { + RenderKitFactory renderKitFactory = (RenderKitFactory) FactoryFinder + .getFactory(FactoryFinder.RENDER_KIT_FACTORY); + this.renderKit = new MockRenderKit(); + renderKitFactory.addRenderKit(RenderKitFactory.HTML_BASIC_RENDER_KIT, this.renderKit); + } + + @Override + public void startScope(Class<? extends Annotation> scopeClass) + { + if (RequestScoped.class.equals(scopeClass)) + { + initRequest(); + initResponse(); + + initFacesContext(); + + initDefaultView(); + } + else if (SessionScoped.class.equals(scopeClass)) + { + initSession(); + } + } + + protected void initRequest() + { + this.request = new MockHttpServletRequest(this.session); + this.request.setServletContext(this.servletContext); + } + + private void initResponse() + { + this.response = new MockHttpServletResponse(); + } + + protected void initFacesContext() + { + FacesContextFactory facesContextFactory = + (FacesContextFactory) FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY); + this.facesContext = facesContextFactory.getFacesContext( + this.servletContext, this.request, this.response, this.lifecycle); + + ((MockFacesContext) this.facesContext).setApplication(this.application); + ExceptionHandler exceptionHandler = ((ExceptionHandlerFactory) + FactoryFinder.getFactory(FactoryFinder.EXCEPTION_HANDLER_FACTORY)).getExceptionHandler(); + this.facesContext.setExceptionHandler(exceptionHandler); + + ((MockFacesContext) this.facesContext).setExternalContext( + new MockExternalContext(this.servletContext, this.request, this.response)); + } + + protected void initDefaultView() + { + UIViewRoot root = new UIViewRoot(); + root.setViewId("/viewId"); + root.setLocale(getLocale()); + root.setRenderKitId(RenderKitFactory.HTML_BASIC_RENDER_KIT); + this.facesContext.setViewRoot(root); + } + + protected void initSession() + { + this.session = new MockHttpSession(); + this.session.setServletContext(this.servletContext); + } + + @Override + public void stopScope(Class<? extends Annotation> scopeClass) + { + if (RequestScoped.class.equals(scopeClass)) + { + if (this.facesContext != null) + { + this.facesContext.release(); + } + this.facesContext = null; + this.request = null; + this.response = null; + } + else if (SessionScoped.class.equals(scopeClass)) + { + this.session = null; + } + } + + protected Locale getLocale() + { + return Locale.getDefault(); + } + + public void shutdown() + { + this.application = null; + this.config = null; + this.lifecycle = null; + this.renderKit = null; + this.servletContext = null; + + FactoryFinder.releaseFactories(); + } + + @Override + public int getOrdinal() + { + return 1000; //default in ds + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/fc03723b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc005/JsfContainerTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc005/JsfContainerTest.java b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc005/JsfContainerTest.java new file mode 100644 index 0000000..caebfa7 --- /dev/null +++ b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc005/JsfContainerTest.java @@ -0,0 +1,114 @@ +/* + * 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.deltaspike.test.testcontrol.uc005; + +import org.apache.deltaspike.test.category.SeCategory; +import org.apache.deltaspike.test.testcontrol.shared.RequestScopedBean; +import org.apache.deltaspike.test.testcontrol.shared.SessionScopedBean; +import org.apache.deltaspike.testcontrol.api.TestControl; +import org.apache.deltaspike.testcontrol.api.junit.CdiTestRunner; +import org.junit.Assert; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; + +import javax.faces.component.UIViewRoot; +import javax.faces.context.FacesContext; +import javax.faces.render.RenderKitFactory; +import javax.inject.Inject; + +//Usually NOT needed! Currently only needed due to our arquillian-setup +@Category(SeCategory.class) + + + +@RunWith(CdiTestRunner.class) +@TestControl(startExternalContainers = true) +public class JsfContainerTest +{ + @Inject + private SessionScopedBean sessionScopedBean; + + @Inject + private RequestScopedBean requestScopedBean; + + @Test + public void firstTest() + { + Assert.assertEquals(0, requestScopedBean.getCount()); + requestScopedBean.increaseCount(); + Assert.assertEquals(1, requestScopedBean.getCount()); + + Assert.assertEquals(0, sessionScopedBean.getCount()); + sessionScopedBean.increaseCount(); + Assert.assertEquals(1, sessionScopedBean.getCount()); + + Assert.assertNotNull(FacesContext.getCurrentInstance().getViewRoot()); + Assert.assertEquals("/viewId", FacesContext.getCurrentInstance().getViewRoot().getViewId()); + + UIViewRoot uiViewRoot = new UIViewRoot(); + uiViewRoot.setViewId("/test1.xhtml"); + uiViewRoot.setRenderKitId(RenderKitFactory.HTML_BASIC_RENDER_KIT); + FacesContext.getCurrentInstance().setViewRoot(uiViewRoot); + Assert.assertEquals("/test1.xhtml", FacesContext.getCurrentInstance().getViewRoot().getViewId()); + + Assert.assertNotNull(FacesContext.getCurrentInstance().getExternalContext()); + Assert.assertNotNull(FacesContext.getCurrentInstance().getApplication()); + Assert.assertNotNull(FacesContext.getCurrentInstance().getELContext()); + Assert.assertNotNull(FacesContext.getCurrentInstance().getPartialViewContext()); + Assert.assertNotNull(FacesContext.getCurrentInstance().getRenderKit()); + Assert.assertNotNull(FacesContext.getCurrentInstance().getExceptionHandler()); + + Assert.assertNull(FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("test")); + FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("test", "1"); + Assert.assertEquals("1", FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("test")); + } + + @Test + public void secondTest() + { + Assert.assertEquals(0, requestScopedBean.getCount()); + requestScopedBean.increaseCount(); + Assert.assertEquals(1, requestScopedBean.getCount()); + + Assert.assertEquals(0, sessionScopedBean.getCount()); + sessionScopedBean.increaseCount(); + Assert.assertEquals(1, sessionScopedBean.getCount()); + + Assert.assertNotNull(FacesContext.getCurrentInstance().getViewRoot()); + Assert.assertEquals("/viewId", FacesContext.getCurrentInstance().getViewRoot().getViewId()); + + UIViewRoot uiViewRoot = new UIViewRoot(); + uiViewRoot.setViewId("/test2.xhtml"); + uiViewRoot.setRenderKitId(RenderKitFactory.HTML_BASIC_RENDER_KIT); + FacesContext.getCurrentInstance().setViewRoot(uiViewRoot); + Assert.assertEquals("/test2.xhtml", FacesContext.getCurrentInstance().getViewRoot().getViewId()); + + Assert.assertNotNull(FacesContext.getCurrentInstance().getExternalContext()); + Assert.assertNotNull(FacesContext.getCurrentInstance().getApplication()); + Assert.assertNotNull(FacesContext.getCurrentInstance().getELContext()); + Assert.assertNotNull(FacesContext.getCurrentInstance().getPartialViewContext()); + Assert.assertNotNull(FacesContext.getCurrentInstance().getRenderKit()); + Assert.assertNotNull(FacesContext.getCurrentInstance().getExceptionHandler()); + + Assert.assertNull(FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("test")); + FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("test", "2"); + Assert.assertEquals("2", FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("test")); + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/fc03723b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc006/SkipExternalContainerTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc006/SkipExternalContainerTest.java b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc006/SkipExternalContainerTest.java new file mode 100644 index 0000000..c908a8f --- /dev/null +++ b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc006/SkipExternalContainerTest.java @@ -0,0 +1,45 @@ +/* + * 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.deltaspike.test.testcontrol.uc006; + +import org.apache.deltaspike.test.category.SeCategory; +import org.apache.deltaspike.testcontrol.api.TestControl; +import org.apache.deltaspike.testcontrol.api.junit.CdiTestRunner; +import org.junit.Assert; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; + +import javax.faces.context.FacesContext; + +//Usually NOT needed! Currently only needed due to our arquillian-setup +@Category(SeCategory.class) + + + +@RunWith(CdiTestRunner.class) +@TestControl(startExternalContainers = false) +public class SkipExternalContainerTest +{ + @Test + public void skippedSetupTest() + { + Assert.assertNull(FacesContext.getCurrentInstance()); + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/fc03723b/deltaspike/modules/test-control/impl/src/test/resources/META-INF/services/org.apache.deltaspike.testcontrol.spi.ExternalContainer ---------------------------------------------------------------------- diff --git a/deltaspike/modules/test-control/impl/src/test/resources/META-INF/services/org.apache.deltaspike.testcontrol.spi.ExternalContainer b/deltaspike/modules/test-control/impl/src/test/resources/META-INF/services/org.apache.deltaspike.testcontrol.spi.ExternalContainer new file mode 100644 index 0000000..5aff81b --- /dev/null +++ b/deltaspike/modules/test-control/impl/src/test/resources/META-INF/services/org.apache.deltaspike.testcontrol.spi.ExternalContainer @@ -0,0 +1,18 @@ +# 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. + +org.apache.deltaspike.testcontrol.impl.jsf.MockedJsf2TestContainer \ No newline at end of file http://git-wip-us.apache.org/repos/asf/deltaspike/blob/fc03723b/deltaspike/parent/pom.xml ---------------------------------------------------------------------- diff --git a/deltaspike/parent/pom.xml b/deltaspike/parent/pom.xml index d91db0c..d105ab9 100644 --- a/deltaspike/parent/pom.xml +++ b/deltaspike/parent/pom.xml @@ -613,6 +613,33 @@ <scope>test</scope> </dependency> + <dependency> + <groupId>org.apache.myfaces.core</groupId> + <artifactId>myfaces-api</artifactId> + <version>${myfaces2.version}</version> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>org.apache.myfaces.core</groupId> + <artifactId>myfaces-impl</artifactId> + <version>${myfaces2.version}</version> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>org.apache.myfaces.test</groupId> + <artifactId>myfaces-test20</artifactId> + <version>1.0.5</version> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + <version>1.1.1</version> + <scope>provided</scope> + </dependency> </dependencies> </dependencyManagement>
