Search in sources :

Example 6 with MockHttpServletRequest

use of com.mockobjects.servlet.MockHttpServletRequest in project commons-configuration by apache.

the class TestServletRequestConfiguration method createConfiguration.

/**
 * Returns a new servlet request configuration that is backed by the passed in configuration.
 *
 * @param base the configuration with the underlying values
 * @return the servlet request configuration
 */
private ServletRequestConfiguration createConfiguration(final Configuration base) {
    final ServletRequest request = new MockHttpServletRequest() {

        @Override
        public Map<?, ?> getParameterMap() {
            return new ConfigurationMap(base);
        }

        @Override
        public String[] getParameterValues(final String key) {
            return base.getStringArray(key);
        }
    };
    final ServletRequestConfiguration config = new ServletRequestConfiguration(request);
    config.setListDelimiterHandler(new DefaultListDelimiterHandler(','));
    return config;
}
Also used : ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(com.mockobjects.servlet.MockHttpServletRequest) DefaultListDelimiterHandler(org.apache.commons.configuration2.convert.DefaultListDelimiterHandler) MockHttpServletRequest(com.mockobjects.servlet.MockHttpServletRequest) ConfigurationMap(org.apache.commons.configuration2.ConfigurationMap)

Example 7 with MockHttpServletRequest

use of com.mockobjects.servlet.MockHttpServletRequest in project struts by apache.

the class BackgroundProcessTest method testSerializeDeserialize.

public void testSerializeDeserialize() throws Exception {
    final NotSerializableException expectedException = new NotSerializableException(new MockHttpServletRequest());
    final Semaphore lock = new Semaphore(1);
    lock.acquire();
    MockActionInvocationWithActionInvoker invocation = new MockActionInvocationWithActionInvoker(new Callable<String>() {

        @Override
        public String call() throws Exception {
            lock.release();
            throw expectedException;
        }
    });
    invocation.setInvocationContext(ActionContext.getContext());
    BackgroundProcess bp = new BackgroundProcess("BackgroundProcessTest.testSerializeDeserialize", invocation, Thread.MIN_PRIORITY);
    if (!lock.tryAcquire(1500L, TimeUnit.MILLISECONDS)) {
        lock.release();
        fail("background thread did not release lock on timeout");
    }
    lock.release();
    bp.result = "BackgroundProcessTest.testSerializeDeserialize";
    bp.done = true;
    // give a chance to background thread to set exception
    Thread.sleep(1000);
    assertEquals(expectedException, bp.exception);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(bp);
    oos.close();
    byte[] b = baos.toByteArray();
    baos.close();
    ByteArrayInputStream bais = new ByteArrayInputStream(b);
    ObjectInputStream ois = new ObjectInputStream(bais);
    BackgroundProcess deserializedBp = (BackgroundProcess) ois.readObject();
    ois.close();
    bais.close();
    assertNull("invocation should not be serialized", deserializedBp.invocation);
    assertNull("exception should not be serialized", deserializedBp.exception);
    assertEquals(bp.result, deserializedBp.result);
    assertEquals(bp.done, deserializedBp.done);
}
Also used : MockHttpServletRequest(com.mockobjects.servlet.MockHttpServletRequest) Semaphore(java.util.concurrent.Semaphore) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 8 with MockHttpServletRequest

use of com.mockobjects.servlet.MockHttpServletRequest in project struts by apache.

the class RolesInterceptorTest method testIsAllowed_emptyAllowedAndDisallowed.

public void testIsAllowed_emptyAllowedAndDisallowed() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest() {

        public boolean isUserInRole(String role) {
            return "admin".equals(role);
        }
    };
    // allow all
    interceptor.setAllowedRoles("");
    interceptor.setDisallowedRoles("admin");
    assertFalse(interceptor.isAllowed(request, null));
}
Also used : MockHttpServletRequest(com.mockobjects.servlet.MockHttpServletRequest)

Example 9 with MockHttpServletRequest

use of com.mockobjects.servlet.MockHttpServletRequest in project struts by apache.

the class RolesInterceptorTest method testIsAllowed_adminAllowedExceptManager.

public void testIsAllowed_adminAllowedExceptManager() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest() {

        public boolean isUserInRole(String role) {
            return "admin".equals(role);
        }
    };
    // allow all
    interceptor.setAllowedRoles("admin");
    interceptor.setDisallowedRoles("manager");
    assertTrue(interceptor.isAllowed(request, null));
}
Also used : MockHttpServletRequest(com.mockobjects.servlet.MockHttpServletRequest)

Example 10 with MockHttpServletRequest

use of com.mockobjects.servlet.MockHttpServletRequest in project struts by apache.

the class DefaultActionMapperTest method setUp.

protected void setUp() throws Exception {
    super.setUp();
    req = new MockHttpServletRequest();
    req.setupGetParameterMap(new HashMap());
    req.setupGetContextPath("/my/namespace");
    config = new DefaultConfiguration();
    PackageConfig pkg = new PackageConfig.Builder("myns").namespace("/my/namespace").build();
    PackageConfig pkg2 = new PackageConfig.Builder("my").namespace("/my").build();
    config.addPackageConfig("mvns", pkg);
    config.addPackageConfig("my", pkg2);
    configManager = new ConfigurationManager(Container.DEFAULT_NAME) {

        public Configuration getConfiguration() {
            return config;
        }
    };
}
Also used : Configuration(com.opensymphony.xwork2.config.Configuration) DefaultConfiguration(com.opensymphony.xwork2.config.impl.DefaultConfiguration) HashMap(java.util.HashMap) StrutsMockHttpServletRequest(org.apache.struts2.views.jsp.StrutsMockHttpServletRequest) MockHttpServletRequest(com.mockobjects.servlet.MockHttpServletRequest) DefaultConfiguration(com.opensymphony.xwork2.config.impl.DefaultConfiguration) ConfigurationManager(com.opensymphony.xwork2.config.ConfigurationManager) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig)

Aggregations

MockHttpServletRequest (com.mockobjects.servlet.MockHttpServletRequest)10 HashMap (java.util.HashMap)3 Configuration (com.opensymphony.xwork2.config.Configuration)2 ConfigurationManager (com.opensymphony.xwork2.config.ConfigurationManager)2 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)2 DefaultConfiguration (com.opensymphony.xwork2.config.impl.DefaultConfiguration)2 MockHttpServletResponse (com.mockobjects.servlet.MockHttpServletResponse)1 MockServletContext (com.mockobjects.servlet.MockServletContext)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 Semaphore (java.util.concurrent.Semaphore)1 ServletRequest (javax.servlet.ServletRequest)1 ConfigurationMap (org.apache.commons.configuration2.ConfigurationMap)1 DefaultListDelimiterHandler (org.apache.commons.configuration2.convert.DefaultListDelimiterHandler)1 StrutsMockHttpServletRequest (org.apache.struts2.views.jsp.StrutsMockHttpServletRequest)1