org.jboss.seam.mock
Class ResourceRequestEnvironment

java.lang.Object
  extended by org.jboss.seam.mock.ResourceRequestEnvironment

public class ResourceRequestEnvironment
extends Object

Executes (through local calls, not TCP sockets) an HTTP request in a unit test, passing it through the Seam resource handlers and filters.

This class is supposed to be used within a SeamTest, in fact, you need to pass an instance of SeamTest into its constructor. This prepares the environment for the resource request processing. You can either share an instance of the environment between all your test methods (prepare it in @BeforeClass) or you can create a new instance for each ResourceRequest:

 import org.jboss.seam.mock.ResourceRequestEnvironment;
 import org.jboss.seam.mock.EnhancedMockHttpServletRequest;
 import org.jboss.seam.mock.EnhancedMockHttpServletResponse;
 import static org.jboss.seam.mock.ResourceRequestEnvironment.ResourceRequest;
 import static org.jboss.seam.mock.ResourceRequestEnvironment.Method;

 public class MyTest extends SeamTest {

    ResourceRequestEnvironment sharedEnvironment;

    @BeforeClass
    public void prepareSharedEnvironment() throws Exception {
        sharedEnvironment = new ResourceRequestEnvironment(this) {
             @Override
             public Map getDefaultHeaders() {
                return new HashMap() {{
                    put("Accept", "text/plain");
                }};
             }
          };
    }

    @Test
    public void test() throws Exception
    {
       //Not shared: new ResourceRequest(new ResourceRequestEnvironment(this), Method.GET, "/my/relative/uri)

       new ResourceRequest(sharedEnvironment, Method.GET, "/my/relative/uri)
       {

          @Override
          protected void prepareRequest(EnhancedMockHttpServletRequest request)
          {
             request.addQueryParameter("foo", "123");
             request.addHeader("Accept-Language", "en_US, de");
          }

          @Override
          protected void onResponse(EnhancedMockHttpServletResponse response)
          {
             assert response.getStatus() == 200;
             assert response.getContentAsString().equals("foobar");
          }

       }.run();
    }

 }
 

Note that in a SeamTest the (mock) HTTP session is always shared between all requests in a particular test method. Each test method however executes with a new (mock) HTTP session. Design your tests accordingly, this is not configurable.

IMPORTANT: A ResourceRequest has to be executed in a @Test method or in a @BeforeMethod callback. You can not execute it in any other callback, such * as @BeforeClass.

Author:
Christian Bauer

Nested Class Summary
static class ResourceRequestEnvironment.Method
           
static class ResourceRequestEnvironment.ResourceRequest
           
 
Field Summary
protected  SeamResourceServlet resourceServlet
           
protected  AbstractSeamTest seamTest
           
 
Constructor Summary
ResourceRequestEnvironment(AbstractSeamTest seamTest)
           
 
Method Summary
 Map<String,Object> getDefaultHeaders()
           
 String getServletPath()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

seamTest

protected final AbstractSeamTest seamTest

resourceServlet

protected final SeamResourceServlet resourceServlet
Constructor Detail

ResourceRequestEnvironment

public ResourceRequestEnvironment(AbstractSeamTest seamTest)
Method Detail

getServletPath

public String getServletPath()

getDefaultHeaders

public Map<String,Object> getDefaultHeaders()