[seam-commits] Seam SVN: r12393 - modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/environment.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Mon Apr 5 16:04:51 EDT 2010


Author: dan.j.allen
Date: 2010-04-05 16:04:50 -0400 (Mon, 05 Apr 2010)
New Revision: 12393

Added:
   modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/environment/ExternalContextProducerTest.java
   modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/environment/MockExternalContext.java
   modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/environment/MockFacesContext.java
Modified:
   modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/environment/FacesContextProducerTest.java
Log:
test for ExternalContextProducer
refactor out mocks
cleanup FacesContextProducerTest


Added: modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/environment/ExternalContextProducerTest.java
===================================================================
--- modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/environment/ExternalContextProducerTest.java	                        (rev 0)
+++ modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/environment/ExternalContextProducerTest.java	2010-04-05 20:04:50 UTC (rev 12393)
@@ -0,0 +1,73 @@
+package org.jboss.seam.faces.environment;
+
+import javax.enterprise.context.ContextNotActiveException;
+import javax.enterprise.inject.Instance;
+import javax.faces.context.ExternalContext;
+
+import javax.faces.context.FacesContext;
+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.ExternalContextProducer;
+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 ExternalContextProducer produces the same ExternalContext
+ * as returned by FacesContext#getExternalContext().
+ *
+ * @author Dan Allen
+ */
+ at RunWith(Arquillian.class)
+public class ExternalContextProducerTest
+{
+   @Deployment
+   public static Archive<?> createTestArchive()
+   {
+      return Archives.create("test.jar", JavaArchive.class)
+            .addClass(FacesContextProducer.class).addClass(ExternalContextProducer.class)
+            .addManifestResource(new ByteArrayAsset(new byte[0]), ArchivePaths.create("beans.xml"));
+   }
+
+   @Inject Instance<ExternalContext> externalContextInstance;
+
+   @Test
+   public void testReturnsCurrentExternalContext()
+   {
+      new MockFacesContext().set();
+      FacesContext ctx = FacesContext.getCurrentInstance();
+      Assert.assertSame(new ExternalContextProducer().getExternalContext(ctx), ctx.getExternalContext());
+   }
+
+   @Test
+   public void testProducesContextualCurrentFacesContext()
+   {
+      new MockFacesContext().set();
+
+      ExternalContext actualExternalContext = FacesContext.getCurrentInstance().getExternalContext();
+      ExternalContext producedExternalContext = externalContextInstance.get();
+
+      // not equal since the produced ExternalContext is a proxy
+      Assert.assertFalse(actualExternalContext == producedExternalContext);
+      // verify we have same object through proxy by comparing hash codes
+      Assert.assertEquals(actualExternalContext.hashCode(), producedExternalContext.hashCode());
+      //Assert.assertEquals(actualExternalContext, producedExternalContext);
+      Assert.assertEquals("/app", producedExternalContext.getRequestContextPath());
+   }
+
+   @Test(expected = ContextNotActiveException.class)
+   public void testProducerThrowsExceptionWhenFacesContextNotActive()
+   {
+      new MockFacesContext().release();
+      // NOTE the return value must be invoked to carry out the lookup
+      externalContextInstance.get().toString();
+   }
+}

Modified: 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	2010-04-05 19:37:40 UTC (rev 12392)
+++ modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/environment/FacesContextProducerTest.java	2010-04-05 20:04:50 UTC (rev 12393)
@@ -1,16 +1,7 @@
 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;
@@ -40,7 +31,9 @@
    @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"));
+      return Archives.create("test.jar", JavaArchive.class)
+            .addClass(FacesContextProducer.class)
+            .addManifestResource(new ByteArrayAsset(new byte[0]), ArchivePaths.create("beans.xml"));
    }
 
    @Inject Instance<FacesContext> facesContextInstance;
@@ -48,7 +41,7 @@
    @Test
    public void testReturnsCurrentFacesContext()
    {
-      new MockFacesContext().set().setCurrentPhaseId(PhaseId.RENDER_RESPONSE);
+      new MockFacesContext().set();
       Assert.assertSame(new FacesContextProducer().getFacesContext(), FacesContext.getCurrentInstance());
    }
 
@@ -65,7 +58,7 @@
       // 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);
+      Assert.assertSame(PhaseId.RENDER_RESPONSE, producedFacesContext.getCurrentPhaseId());
    }
 
    @Test(expected = ContextNotActiveException.class)
@@ -75,141 +68,4 @@
       // 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.");
-      }
-   }
 }

Added: modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/environment/MockExternalContext.java
===================================================================
--- modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/environment/MockExternalContext.java	                        (rev 0)
+++ modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/environment/MockExternalContext.java	2010-04-05 20:04:50 UTC (rev 12393)
@@ -0,0 +1,22 @@
+package org.jboss.seam.faces.environment;
+
+import javax.faces.context.ExternalContext;
+import javax.faces.context.ExternalContextWrapper;
+
+/**
+ * @author Dan Allen
+ */
+public class MockExternalContext extends ExternalContextWrapper
+{
+   @Override
+   public String getRequestContextPath()
+   {
+      return "/app";
+   }
+
+   @Override
+   public ExternalContext getWrapped()
+   {
+      throw new UnsupportedOperationException("Not supported.");
+   }
+}

Added: modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/environment/MockFacesContext.java
===================================================================
--- modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/environment/MockFacesContext.java	                        (rev 0)
+++ modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/environment/MockFacesContext.java	2010-04-05 20:04:50 UTC (rev 12393)
@@ -0,0 +1,154 @@
+package org.jboss.seam.faces.environment;
+
+import java.util.Iterator;
+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.FacesContext;
+import javax.faces.context.ResponseStream;
+import javax.faces.context.ResponseWriter;
+import javax.faces.event.PhaseId;
+import javax.faces.render.RenderKit;
+
+/**
+ * @author Dan Allen
+ */
+public class MockFacesContext extends FacesContext
+{
+   private PhaseId currentPhaseId;
+   private ExternalContext externalContext = new MockExternalContext();
+
+   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()
+   {
+      return externalContext;
+   }
+
+   @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