[seam-commits] Seam SVN: r12392 - in modules/faces/trunk/impl/src: test/java/org/jboss/seam/faces and 1 other directories.
seam-commits at lists.jboss.org
seam-commits at lists.jboss.org
Mon Apr 5 15:37:41 EDT 2010
Author: dan.j.allen
Date: 2010-04-05 15:37:40 -0400 (Mon, 05 Apr 2010)
New Revision: 12392
Added:
modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/environment/
modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/environment/FacesContextProducerTest.java
Modified:
modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/environment/ExternalContextProducer.java
modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/environment/FacesContextProducer.java
Log:
throw exception if retrieving FacesContext outside an active JSF lifecycle
test for FacesContextProducer
Modified: modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/environment/ExternalContextProducer.java
===================================================================
--- modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/environment/ExternalContextProducer.java 2010-04-05 19:22:20 UTC (rev 12391)
+++ modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/environment/ExternalContextProducer.java 2010-04-05 19:37:40 UTC (rev 12392)
@@ -1,34 +1,33 @@
//$Id: FacesContext.java 5350 2007-06-20 17:53:19Z gavin $
-package org.jboss.seam.faces.environment;
+package org.jboss.seam.faces.producer;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Produces;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
-import javax.inject.Inject;
/**
* <p>
- * A producer which retrieves the current JSF ExternalContext by calling
- * {@link FacesContext#getCurrentInstance#getExternalContext()}, thus allowing
- * it to be injected.
+ * A producer which retrieves the {@link ExternalContext} for the current request
+ * of the JavaServer Faces application by calling {@link FacesContext#getExternalContext()}
+ * and stores the result as a request-scoped bean instance.
* </p>
+ *
+ * <p>
+ * This producer allows the {@link ExternalContext} to be injected:
+ * </p>
+ *
+ * <pre>
+ * @Inject ExternalContext ctx;
+ * </pre>
*
* @author <a href="mailto:lincolnbaxter at gmail.com">Lincoln Baxter, III</a>
+ * @author Dan Allen
*/
public class ExternalContextProducer
{
- @Inject
- FacesContext context;
-
- public @Produces
- @RequestScoped
- ExternalContext getExternalContext()
+ public @Produces @RequestScoped ExternalContext getExternalContext(FacesContext context)
{
- if (context != null)
- {
- return context.getExternalContext();
- }
- return null;
+ return context.getExternalContext();
}
}
Modified: modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/environment/FacesContextProducer.java
===================================================================
--- modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/environment/FacesContextProducer.java 2010-04-05 19:22:20 UTC (rev 12391)
+++ modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/environment/FacesContextProducer.java 2010-04-05 19:37:40 UTC (rev 12392)
@@ -1,21 +1,40 @@
-//$Id: FacesContext.java 5350 2007-06-20 17:53:19Z gavin $
-package org.jboss.seam.faces.environment;
+/*
+ * JBoss, Community-driven Open Source Middleware
+ * Copyright 2010, JBoss by Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed 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.jboss.seam.faces.producer;
+import javax.enterprise.context.ContextNotActiveException;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Produces;
import javax.faces.context.FacesContext;
/**
* <p>
- * A producer which retrieves the current JSF FacesContext by calling
- * {@link FacesContext#getCurrentInstance}, thus allowing it to be injected.
+ * A producer which retrieves the {@link FacesContext} for the current request
+ * of the JavaServer Faces application by calling {@link FacesContext#getCurrentInstance()}
+ * and stores the result as a request-scoped bean instance.
* </p>
+ *
+ * <p>This producer allows the {@link FacesContext} to be injected:</p>
+ *
+ * <pre>
+ * @Inject FacesContext ctx;
+ * </pre>
*
* <p>
- * QUESTION should we return null if there is no current phase id? (seems to be
- * a common check)
- * </p>
- * <p>
* QUESTION is it correct to use a @RequestScoped producer? If it is @Dependent,
* then a developer could unknowingly bind it to a wider-scoped bean
* </p>
@@ -25,10 +44,13 @@
*/
public class FacesContextProducer
{
- public @Produces
- @RequestScoped
- FacesContext getFacesContext()
+ public @Produces @RequestScoped FacesContext getFacesContext()
{
- return FacesContext.getCurrentInstance();
+ FacesContext ctx = FacesContext.getCurrentInstance();
+ if (ctx == null)
+ {
+ throw new ContextNotActiveException("FacesContext is not active");
+ }
+ return ctx;
}
}
Added: modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/environment/FacesContextProducerTest.java
===================================================================
--- modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/environment/FacesContextProducerTest.java (rev 0)
+++ modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/environment/FacesContextProducerTest.java 2010-04-05 19:37:40 UTC (rev 12392)
@@ -0,0 +1,215 @@
+package org.jboss.seam.faces.environment;
+
+import java.util.Iterator;
+import javax.enterprise.context.ContextNotActiveException;
+import javax.enterprise.inject.Instance;
+import javax.faces.application.Application;
+import javax.faces.application.FacesMessage;
+import javax.faces.application.FacesMessage.Severity;
+import javax.faces.component.UIViewRoot;
+import javax.faces.context.ExternalContext;
+import javax.faces.context.ResponseStream;
+import javax.faces.context.ResponseWriter;
+import javax.faces.render.RenderKit;
+
+import javax.faces.context.FacesContext;
+import javax.faces.event.PhaseId;
+import javax.inject.Inject;
+import junit.framework.Assert;
+import org.jboss.arquillian.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.seam.faces.producer.FacesContextProducer;
+
+import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.api.ArchivePaths;
+import org.jboss.shrinkwrap.api.Archives;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.jboss.shrinkwrap.impl.base.asset.ByteArrayAsset;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/**
+ * Verify that the FacesContextProducer produces the same FacesContext
+ * as returned by FacesContext#getCurrentInstance().
+ *
+ * @author Dan Allen
+ */
+ at RunWith(Arquillian.class)
+public class FacesContextProducerTest
+{
+ @Deployment
+ public static Archive<?> createTestArchive()
+ {
+ return Archives.create("test.jar", JavaArchive.class).addClass(FacesContextProducer.class).addManifestResource(new ByteArrayAsset(new byte[0]), ArchivePaths.create("beans.xml"));
+ }
+
+ @Inject Instance<FacesContext> facesContextInstance;
+
+ @Test
+ public void testReturnsCurrentFacesContext()
+ {
+ new MockFacesContext().set().setCurrentPhaseId(PhaseId.RENDER_RESPONSE);
+ Assert.assertSame(new FacesContextProducer().getFacesContext(), FacesContext.getCurrentInstance());
+ }
+
+ @Test
+ public void testProducesContextualCurrentFacesContext()
+ {
+ new MockFacesContext().set().setCurrentPhaseId(PhaseId.RENDER_RESPONSE);
+
+ FacesContext actualFacesContext = FacesContext.getCurrentInstance();
+ FacesContext producedFacesContext = facesContextInstance.get();
+
+ // not equal since the produced FacesContext is a proxy
+ Assert.assertFalse(actualFacesContext == producedFacesContext);
+ // verify we have same object through proxy by comparing hash codes
+ Assert.assertEquals(actualFacesContext.hashCode(), producedFacesContext.hashCode());
+ //Assert.assertEquals(actualFacesContext, producedFacesContext);
+ Assert.assertSame(producedFacesContext.getCurrentPhaseId(), PhaseId.RENDER_RESPONSE);
+ }
+
+ @Test(expected = ContextNotActiveException.class)
+ public void testProducerThrowsExceptionWhenFacesContextNotActive()
+ {
+ new MockFacesContext().release();
+ // NOTE the return value must be invoked to carry out the lookup
+ facesContextInstance.get().toString();
+ }
+
+ private class MockFacesContext extends FacesContext
+ {
+ private PhaseId currentPhaseId;
+
+ public FacesContext set()
+ {
+ setCurrentInstance(this);
+ return this;
+ }
+
+ @Override
+ public void release()
+ {
+ setCurrentInstance(null);
+ }
+
+ @Override
+ public PhaseId getCurrentPhaseId()
+ {
+ return currentPhaseId;
+ }
+
+ @Override
+ public void setCurrentPhaseId(PhaseId currentPhaseId)
+ {
+ this.currentPhaseId = currentPhaseId;
+ }
+
+ @Override
+ public Application getApplication()
+ {
+ throw new UnsupportedOperationException("Not supported");
+ }
+
+ @Override
+ public Iterator<String> getClientIdsWithMessages()
+ {
+ throw new UnsupportedOperationException("Not supported");
+ }
+
+ @Override
+ public ExternalContext getExternalContext()
+ {
+ throw new UnsupportedOperationException("Not supported");
+ }
+
+ @Override
+ public Severity getMaximumSeverity()
+ {
+ throw new UnsupportedOperationException("Not supported");
+ }
+
+ @Override
+ public Iterator<FacesMessage> getMessages()
+ {
+ throw new UnsupportedOperationException("Not supported");
+ }
+
+ @Override
+ public Iterator<FacesMessage> getMessages(String clientId)
+ {
+ throw new UnsupportedOperationException("Not supported");
+ }
+
+ @Override
+ public RenderKit getRenderKit()
+ {
+ throw new UnsupportedOperationException("Not supported");
+ }
+
+ @Override
+ public boolean getRenderResponse()
+ {
+ throw new UnsupportedOperationException("Not supported");
+ }
+
+ @Override
+ public boolean getResponseComplete()
+ {
+ throw new UnsupportedOperationException("Not supported");
+ }
+
+ @Override
+ public ResponseStream getResponseStream()
+ {
+ throw new UnsupportedOperationException("Not supported");
+ }
+
+ @Override
+ public void setResponseStream(ResponseStream stream)
+ {
+ throw new UnsupportedOperationException("Not supported");
+ }
+
+ @Override
+ public ResponseWriter getResponseWriter()
+ {
+ throw new UnsupportedOperationException("Not supported");
+ }
+
+ @Override
+ public void setResponseWriter(ResponseWriter writer)
+ {
+ throw new UnsupportedOperationException("Not supported");
+ }
+
+ @Override
+ public UIViewRoot getViewRoot()
+ {
+ throw new UnsupportedOperationException("Not supported");
+ }
+
+ @Override
+ public void setViewRoot(UIViewRoot uivr)
+ {
+ throw new UnsupportedOperationException("Not supported");
+ }
+
+ @Override
+ public void addMessage(String clientId, FacesMessage message)
+ {
+ throw new UnsupportedOperationException("Not supported");
+ }
+
+ @Override
+ public void renderResponse()
+ {
+ throw new UnsupportedOperationException("Not supported.");
+ }
+
+ @Override
+ public void responseComplete()
+ {
+ throw new UnsupportedOperationException("Not supported.");
+ }
+ }
+}
More information about the seam-commits
mailing list