[seam-commits] Seam SVN: r14335 - in branches/community/Seam_2_3/jboss-seam-jsf2: src/main/java/org/jboss/seam/mock and 1 other directory.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Tue Feb 28 08:59:50 EST 2012


Author: maschmid
Date: 2012-02-28 08:59:49 -0500 (Tue, 28 Feb 2012)
New Revision: 14335

Added:
   branches/community/Seam_2_3/jboss-seam-jsf2/src/main/java/org/jboss/seam/mock/JUnitSeamTest.java
   branches/community/Seam_2_3/jboss-seam-jsf2/src/main/java/org/jboss/seam/mock/ServletContextWrapper.java
Modified:
   branches/community/Seam_2_3/jboss-seam-jsf2/pom.xml
   branches/community/Seam_2_3/jboss-seam-jsf2/src/main/java/org/jboss/seam/mock/AbstractSeamTest.java
Log:
Add JUnitSeamTest and ServletContextWrapper, update AbstractSeamTest to use the ServletContextWrapper


Modified: branches/community/Seam_2_3/jboss-seam-jsf2/pom.xml
===================================================================
--- branches/community/Seam_2_3/jboss-seam-jsf2/pom.xml	2012-02-28 13:57:52 UTC (rev 14334)
+++ branches/community/Seam_2_3/jboss-seam-jsf2/pom.xml	2012-02-28 13:59:49 UTC (rev 14335)
@@ -357,6 +357,11 @@
 		</dependency>
 
 		<dependency>
+			<groupId>junit</groupId>
+			<artifactId>junit</artifactId>
+		</dependency>
+
+		<dependency>
 			<groupId>org.dbunit</groupId>
 			<artifactId>dbunit</artifactId>
 			<optional>true</optional>
@@ -436,18 +441,18 @@
 			<version>1.6.1</version>
 		</dependency>
 		
-        <dependency>
-            <groupId>com.sun.faces</groupId>
-            <artifactId>jsf-api</artifactId>
-            <optional>true</optional>
-            <scope>provided</scope>
-        </dependency>
+		<dependency>
+			<groupId>com.sun.faces</groupId>
+			<artifactId>jsf-api</artifactId>
+			<optional>true</optional>
+			<scope>provided</scope>
+		</dependency>
 
-        <dependency>
-            <groupId>com.sun.faces</groupId>
-            <artifactId>jsf-impl</artifactId>
-            <optional>true</optional>
-        </dependency>
+		<dependency>
+			<groupId>com.sun.faces</groupId>
+			<artifactId>jsf-impl</artifactId>
+			<optional>true</optional>
+		</dependency>
 	</dependencies>
 
 	<profiles>

Modified: branches/community/Seam_2_3/jboss-seam-jsf2/src/main/java/org/jboss/seam/mock/AbstractSeamTest.java
===================================================================
--- branches/community/Seam_2_3/jboss-seam-jsf2/src/main/java/org/jboss/seam/mock/AbstractSeamTest.java	2012-02-28 13:57:52 UTC (rev 14334)
+++ branches/community/Seam_2_3/jboss-seam-jsf2/src/main/java/org/jboss/seam/mock/AbstractSeamTest.java	2012-02-28 13:59:49 UTC (rev 14335)
@@ -79,6 +79,8 @@
    private Map<String, Map> conversationViewRootAttributes;
    protected Filter seamFilter;
    
+   private static ServletContext realServletContext = null;
+   
    static 
    {
       phases = new SeamPhaseListener();
@@ -933,19 +935,34 @@
     */
    protected void startSeam() throws Exception
    {
-      startJbossEmbeddedIfNecessary();
-      this.servletContext = createServletContext();
+      // If the Seam Filter is already initialized, we can grab the real servlet context
+     if (realServletContext == null && ServletLifecycle.getServletContext() != null) {
+        realServletContext = ServletLifecycle.getServletContext();
+     }
+     
+     ServletContext realContext = realServletContext;
+      this.servletContext = createServletContext(realContext);
       ServletLifecycle.beginApplication(servletContext);
       FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY, MockApplicationFactory.class.getName());
       new Initialization(servletContext).create().init();
       ((Init) servletContext.getAttribute(Seam.getComponentName(Init.class))).setDebug(false);
    }
    
-   protected ServletContext createServletContext()
+   protected ServletContext createServletContext(ServletContext realContext)
    {
-      MockServletContext mockServletContext = new MockServletContext();
-      initServletContext(mockServletContext.getInitParameters());
-      return mockServletContext;
+     if (realContext != null)
+     {
+        ServletContextWrapper wrappedServletContext = new ServletContextWrapper(realContext);
+        // TODO: 
+        //initServletContext(wrappedServletContext.getInitParameters());
+        return wrappedServletContext;
+     }
+     else
+     {
+        MockServletContext mockServletContext = new MockServletContext();
+        initServletContext(mockServletContext.getInitParameters());
+        return mockServletContext;
+     }
    }
    
    /**
@@ -987,7 +1004,7 @@
    {
       seamFilter.destroy();
       conversationViewRootAttributes = null;
-      applicationFactory.setApplication(null);
+      // applicationFactory.setApplication(null);
    }
 
    protected Filter createSeamFilter() throws ServletException
@@ -1038,29 +1055,6 @@
 
    private static boolean started;
 
-   protected void startJbossEmbeddedIfNecessary() throws Exception
-   {
-      if (!started && embeddedJBossAvailable())
-      {
-         new EmbeddedBootstrap().startAndDeployResources();
-      }
-
-      started = true;
-   }
-
-   private boolean embeddedJBossAvailable()
-   {
-      try
-      {
-         Class.forName("org.jboss.embedded.Bootstrap");
-         return true;
-      }
-      catch (ClassNotFoundException e)
-      {
-         return false;
-      }
-   }
-
    protected ELResolver[] getELResolvers()
    {
       return new ELResolver[0];

Added: branches/community/Seam_2_3/jboss-seam-jsf2/src/main/java/org/jboss/seam/mock/JUnitSeamTest.java
===================================================================
--- branches/community/Seam_2_3/jboss-seam-jsf2/src/main/java/org/jboss/seam/mock/JUnitSeamTest.java	                        (rev 0)
+++ branches/community/Seam_2_3/jboss-seam-jsf2/src/main/java/org/jboss/seam/mock/JUnitSeamTest.java	2012-02-28 13:59:49 UTC (rev 14335)
@@ -0,0 +1,61 @@
+/*
+ * JBoss, Home of Professional Open Source
+ *
+ * Distributable under LGPL license.
+ * See terms of license at gnu.org.
+ */
+package org.jboss.seam.mock;
+
+import org.junit.After;
+import org.junit.Before;
+
+/**
+ * Provides BaseSeamTest functionality for TestNG integration tests.
+ * 
+ * @author Gavin King
+ * @author <a href="mailto:theute at jboss.org">Thomas Heute</a>
+ * @author Mike Youngstrom
+ * @author <a href="http://community.jboss.org/people/jharting">Jozef Hartinger</a>
+ * @author <a href="http://community.jboss.org/people/maschmid">Marek Schmidt</a>
+ */
+public class JUnitSeamTest extends AbstractSeamTest
+{
+   
+   @Before
+   @Override
+   public void begin()
+   {
+      try {
+         startSeam();
+         setupClass();
+      }
+      catch (Exception x) {
+         throw new RuntimeException(x);
+      }
+      super.begin();
+   }
+
+   @After
+   @Override
+   public void end()
+   {
+      super.end();
+      try {
+         cleanupClass();
+         stopSeam();
+      }
+      catch (Exception x) {
+         throw new RuntimeException(x);
+      }
+   }
+   
+   /**
+    * Call this method within a test method to end the previous
+    * mock session and start another one. 
+    */
+   public void reset()
+   {
+      end();
+      begin();
+   }
+}

Added: branches/community/Seam_2_3/jboss-seam-jsf2/src/main/java/org/jboss/seam/mock/ServletContextWrapper.java
===================================================================
--- branches/community/Seam_2_3/jboss-seam-jsf2/src/main/java/org/jboss/seam/mock/ServletContextWrapper.java	                        (rev 0)
+++ branches/community/Seam_2_3/jboss-seam-jsf2/src/main/java/org/jboss/seam/mock/ServletContextWrapper.java	2012-02-28 13:59:49 UTC (rev 14335)
@@ -0,0 +1,147 @@
+/*
+ * JBoss, Home of Professional Open Source
+ *
+ * Distributable under LGPL license.
+ * See terms of license at gnu.org.
+ */
+
+package org.jboss.seam.mock;
+
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.Servlet;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+
+import org.jboss.seam.util.IteratorEnumeration;
+
+/**
+ * Wraps a ServletContext with own attributes.
+ *
+ * @author Marek Schmidt
+ */
+public class ServletContextWrapper implements ServletContext {
+   
+   private ServletContext delegate; 
+   
+   private Map<String, Object> attributes = new HashMap<String, Object>();
+   
+   public ServletContextWrapper(ServletContext delegate) {
+      this.delegate = delegate;
+   }
+
+   public Object getAttribute(String arg0) {
+      return attributes.get(arg0);
+   }
+
+   public Enumeration getAttributeNames() {
+      return new IteratorEnumeration(attributes.keySet().iterator());
+   }
+
+   public ServletContext getContext(String arg0) {
+      return delegate.getContext(arg0);
+   }
+
+   public String getContextPath() {
+      return delegate.getContextPath();
+   }
+
+   public String getInitParameter(String arg0) {
+      return delegate.getInitParameter(arg0);
+   }
+
+   public Enumeration getInitParameterNames() {
+      return delegate.getInitParameterNames();
+   }
+
+   public int getMajorVersion() {
+      return delegate.getMajorVersion();
+   }
+
+   public String getMimeType(String arg0) {
+      return delegate.getMimeType(arg0);
+   }
+
+   public int getMinorVersion() {
+      return delegate.getMinorVersion();
+   }
+
+   public RequestDispatcher getNamedDispatcher(String arg0) {
+      return delegate.getNamedDispatcher(arg0);
+   }
+
+   public String getRealPath(String arg0) {
+      return delegate.getRealPath(arg0);
+   }
+
+   public RequestDispatcher getRequestDispatcher(String arg0) {
+      return delegate.getRequestDispatcher(arg0);
+   }
+
+   public URL getResource(String arg0) throws MalformedURLException {
+      return delegate.getResource(arg0);
+   }
+
+   public InputStream getResourceAsStream(String arg0) {
+      InputStream ret = delegate.getResourceAsStream(arg0);
+      return ret;
+   }
+
+   public Set getResourcePaths(String arg0) {
+      return delegate.getResourcePaths(arg0);
+   }
+
+   public String getServerInfo() {
+      return delegate.getServerInfo();
+   }
+
+   public Servlet getServlet(String arg0) throws ServletException {
+      return delegate.getServlet(arg0);
+   }
+
+   public String getServletContextName() {
+      return "Wrap";
+   }
+
+   public Enumeration getServletNames() {
+      return delegate.getServletNames();
+   }
+
+   public Enumeration getServlets() {
+      return delegate.getServlets();
+   }
+
+   public void log(String arg0) {
+      delegate.log(arg0);
+   }
+
+   public void log(Exception arg0, String arg1) {
+      delegate.log(arg0, arg1);
+   }
+
+   public void log(String arg0, Throwable arg1) {
+      delegate.log(arg0, arg1);
+   }
+
+   public void removeAttribute(String arg0) {
+      attributes.remove(arg0);
+   }
+
+   public void setAttribute(String key, Object value) {
+      if (value == null)
+      {
+         attributes.remove(key);
+      }
+      else
+      {
+         attributes.put(key, value);
+      }
+   }
+}



More information about the seam-commits mailing list