Package net.sourceforge.stripes.mock

Examples of net.sourceforge.stripes.mock.MockServletContext


     * Create and return a new MockServletContext.
     *
     * @return an instance of MockServletContext for testing wiith
     */
    public static synchronized MockServletContext createServletContext() {
        return new MockServletContext("test")
                .addFilter(StripesFilter.class, "StripesFilter", getDefaultFilterParams())
                .setServlet(DispatcherServlet.class, "StripesDispatcher", null);
    }
View Full Code Here


    public static synchronized Configuration getDefaultConfiguration() {
        if (configuration == null) {
            Configuration configuration = new DefaultConfiguration();
            MockFilterConfig filterConfig = new MockFilterConfig();
            filterConfig.addAllInitParameters(getDefaultFilterParams());
            MockServletContext mockServletContext = createServletContext();
            try {
                filterConfig.setServletContext(mockServletContext);
                configuration.setBootstrapPropertyResolver(new BootstrapPropertyResolver(filterConfig));
                configuration.init();
                StripesTestFixture.configuration = configuration;
            } finally {
                mockServletContext.close();
            }
        }

        return configuration;
    }
View Full Code Here

     * Check that the validations from the superclass are active, except where
     * overridden by this class.
     */
    @Test(groups="fast")
    public void testInheritedValidations() throws Exception {
        MockServletContext ctx = StripesTestFixture.createServletContext();
        try {
            MockRoundtrip trip = new MockRoundtrip(ctx, InheritanceTests.class);
            trip.addParameter("two", "not25chars");
            trip.addParameter("three", "3");
            trip.addParameter("four", "onetwothree");
            trip.execute("/Validate.action");

            ValidationErrors errors = trip.getValidationErrors();
            Assert.assertNull(errors.get("one"), "Field one should not have errors.");
            Assert.assertEquals(errors.get("two").size(), 1, "Field two should not have 1 error.");
            Assert.assertEquals(errors.get("three").size(), 1, "Field three should not have errors.");
            Assert.assertEquals(errors.get("four").size(), 1, "Field one should not have errors.");
        } finally {
            ctx.close();
        }
    }
View Full Code Here

        return null;
    }

    @Test(groups="fast")
    public void positiveCase() throws Exception {
        MockServletContext ctx = StripesTestFixture.createServletContext();
        try {
            MockRoundtrip trip = new MockRoundtrip(ctx, FlashScopeTests.class);
            trip.addParameter("foo", "foo123");
            trip.execute();

            String url = trip.getDestination();
            Matcher matcher = FLASH_ID_REGEX.matcher(url);
            Assert.assertTrue(matcher.matches(),
                              "Redirect URL should contain request parameter for flash scope id.");

            Assert.assertEquals("foo123", trip.getRequest().getAttribute("foo"),
                                "FlashScope should have inserted 'foo' into a request attribute.");

            MockRoundtrip trip2 = new MockRoundtrip
                    (ctx, FlashScopeTests.class, (MockHttpSession) trip.getRequest().getSession());

            // Get the flash scope ID from the redirect URL and add it back as a parameter
            String id = matcher.group(1);
            trip2.addParameter(StripesConstants.URL_KEY_FLASH_SCOPE_ID, id);

            Assert.assertNull(trip2.getRequest().getAttribute("foo"),
                              "Request attribute 'foo' should not exist prior to request.");

            trip2.execute("DoNothing");
            Assert.assertEquals("foo123", trip2.getRequest().getAttribute("foo"),
                                "Request attribute 'foo' should have been set by FlashScope.");

            Assert.assertEquals(FlashScope.getAllFlashScopes(trip2.getRequest()).size(), 0,
                                "FlashScope should have been removed from session after use.");

            // Test flashing an ActionBean
            MockRoundtrip trip3 = new MockRoundtrip(ctx, FlashScopeTests.class, (MockHttpSession) trip
                    .getRequest().getSession());

            // Get the flash scope ID from the redirect URL and add it back as a parameter
            trip3.addParameter(StripesConstants.URL_KEY_FLASH_SCOPE_ID, id);
            trip3.execute("FlashBean");

            try {
                ActionBeanContext tmp = trip3.getActionBean(getClass()).getContext();
                HttpServletResponse response = tmp.getResponse();
                HttpServletRequest request = tmp.getRequest();
                Assert.assertNotNull(request);
                Assert.assertNotNull(response);
                Assert.assertTrue(Proxy.class.isAssignableFrom(response.getClass()));
                Assert.assertEquals(StripesRequestWrapper.class, request.getClass());
                response.isCommitted();
                Assert.fail(
                        "Response should have thrown IllegalStateException after request cycle complete");
            }
            catch (IllegalStateException e) {
            }
        } finally {
            ctx.close();
        }
    }
View Full Code Here

TOP

Related Classes of net.sourceforge.stripes.mock.MockServletContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.