Search in sources :

Example 61 with WebXmlRenderContext

use of com.github.bordertech.wcomponents.servlet.WebXmlRenderContext in project wcomponents by BorderTech.

the class ResponseCacheInterceptor_Test method testOverrideDefaultCache.

@Test
public void testOverrideDefaultCache() {
    /**
     * Original config.
     */
    Configuration originalConfig;
    originalConfig = Config.getInstance();
    String override = "OVERRIDE CACHE";
    try {
        // Test override cache settings
        Configuration config = Config.copyConfiguration(originalConfig);
        config.setProperty(ConfigurationProperties.RESPONSE_CACHE_SETTINGS, override);
        Config.setConfiguration(config);
        // Create interceptor
        ResponseCacheInterceptor interceptor = new ResponseCacheInterceptor(CacheType.CONTENT_CACHE);
        interceptor.setBackingComponent(new WText());
        // Mock Response
        MockResponse response = new MockResponse();
        interceptor.attachResponse(response);
        // Render phase
        interceptor.paint(new WebXmlRenderContext(response.getWriter()));
        // Check Override
        Assert.assertEquals("Cache-Control header not overriden correctly for CACHE", override, response.getHeaders().get("Cache-Control"));
    } finally {
        // Remove overrides
        Config.setConfiguration(originalConfig);
    }
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) Configuration(org.apache.commons.configuration.Configuration) WText(com.github.bordertech.wcomponents.WText) Test(org.junit.Test)

Example 62 with WebXmlRenderContext

use of com.github.bordertech.wcomponents.servlet.WebXmlRenderContext in project wcomponents by BorderTech.

the class VelocityInterceptor_Test method testPaint.

@Test
public void testPaint() {
    setActiveContext(createUIContext());
    StringWriter stringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stringWriter);
    RenderContext renderContext = new WebXmlRenderContext(printWriter);
    MyVelocityComponent component = new MyVelocityComponent();
    component.setTemplate("com/github/bordertech/wcomponents/container/VelocityComponent_Test1.vm");
    component.paint(renderContext);
    String renderedFormat = stringWriter.toString().trim();
    Assert.assertEquals("Incorrect rendered format", TEST_VALUE, renderedFormat);
    // Errors in a template should not throw an exception, or render anything
    // Method called using reflection that results in a MethodInvocationException
    component.setTemplate("com/github/bordertech/wcomponents/container/VelocityComponent_Test2.vm");
    stringWriter.getBuffer().setLength(0);
    component.paint(renderContext);
    renderedFormat = stringWriter.toString().trim();
    Assert.assertEquals("Incorrect rendered format", "", renderedFormat);
    // Template that #includes a non-existant template
    component.setTemplate("com/github/bordertech/wcomponents/container/VelocityComponent_Test3.vm");
    stringWriter.getBuffer().setLength(0);
    component.paint(renderContext);
    renderedFormat = stringWriter.toString().trim();
    Assert.assertEquals("Incorrect rendered format", "", renderedFormat);
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) RenderContext(com.github.bordertech.wcomponents.RenderContext) WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 63 with WebXmlRenderContext

use of com.github.bordertech.wcomponents.servlet.WebXmlRenderContext in project wcomponents by BorderTech.

the class WrongStepAjaxInterceptor_Test method doAjaxRequest.

/**
 * Does an AJAX request for the app.
 *
 * @param app the MyApp instance to do an AJAX request for.
 * @param clientStep the client-side step counter
 * @param serverStep the server-side step counter
 * @return the response object.
 */
private MockResponse doAjaxRequest(final MyApp app, final int clientStep, final int serverStep) {
    UIContext uic = createUIContext();
    WServlet.WServletEnvironment env = new WServlet.WServletEnvironment(APP_POSTPATH, "http://localhost", "");
    env.setStep(serverStep);
    env.setSessionToken("T");
    uic.setEnvironment(env);
    uic.setUI(app);
    setActiveContext(uic);
    MockRequest request = new MockRequest();
    MockResponse response = new MockResponse();
    // Create interceptors
    AjaxSetupInterceptor ajaxSetupInterceptor = new AjaxSetupInterceptor();
    WrongStepAjaxInterceptor wrongStepInterceptor = new WrongStepAjaxInterceptor();
    AjaxPageShellInterceptor ajaxPageInterceptor = new AjaxPageShellInterceptor();
    AjaxInterceptor ajaxInterceptor = new AjaxInterceptor();
    ajaxPageInterceptor.setBackingComponent(ajaxInterceptor);
    wrongStepInterceptor.setBackingComponent(ajaxPageInterceptor);
    ajaxSetupInterceptor.setBackingComponent(wrongStepInterceptor);
    ajaxSetupInterceptor.attachUI(app);
    ajaxSetupInterceptor.attachResponse(response);
    // Action phase
    try {
        AjaxHelper.registerComponent(app.target.getId(), app.trigger.getId());
        request.setParameter(WServlet.AJAX_TRIGGER_PARAM_NAME, app.trigger.getId());
        request.setParameter(Environment.STEP_VARIABLE, String.valueOf(clientStep));
        ajaxSetupInterceptor.serviceRequest(request);
        ajaxSetupInterceptor.preparePaint(request);
        // Render phase
        ajaxSetupInterceptor.paint(new WebXmlRenderContext(response.getWriter()));
    } catch (ActionEscape ignored) {
    // is thrown to skip render phase
    }
    return response;
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) ActionEscape(com.github.bordertech.wcomponents.ActionEscape) UIContext(com.github.bordertech.wcomponents.UIContext) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) WServlet(com.github.bordertech.wcomponents.servlet.WServlet)

Aggregations

WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)63 PrintWriter (java.io.PrintWriter)48 StringWriter (java.io.StringWriter)27 Test (org.junit.Test)27 MockRequest (com.github.bordertech.wcomponents.util.mock.MockRequest)17 MockResponse (com.github.bordertech.wcomponents.util.mock.MockResponse)17 UIContext (com.github.bordertech.wcomponents.UIContext)15 NullWriter (com.github.bordertech.wcomponents.util.NullWriter)7 SystemException (com.github.bordertech.wcomponents.util.SystemException)7 WText (com.github.bordertech.wcomponents.WText)6 RenderContext (com.github.bordertech.wcomponents.RenderContext)5 WComponent (com.github.bordertech.wcomponents.WComponent)5 XmlStringBuilder (com.github.bordertech.wcomponents.XmlStringBuilder)5 WServlet (com.github.bordertech.wcomponents.servlet.WServlet)5 MockHttpServletRequest (com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest)4 Configuration (org.apache.commons.configuration.Configuration)4 ActionEscape (com.github.bordertech.wcomponents.ActionEscape)3 AllComponents (com.github.bordertech.wcomponents.AllComponents)3 ComponentWithContext (com.github.bordertech.wcomponents.ComponentWithContext)3 Response (com.github.bordertech.wcomponents.Response)3