use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.
the class WWindowInterceptor_Test method testServiceRequest.
@Test
public void testServiceRequest() {
final int initialServletStep = 123;
final int initialWindowStep = 111;
// Create app
WApplication app = new WApplication();
WWindow window = new WWindow();
window.setContent(new MockLabel("dummy"));
app.add(window);
WContent content = new WContent();
content.setContentAccess(new InternalResource("/wcomponents-test.properties", "test"));
app.add(content);
app.setLocked(true);
// Set up servlet env
WServlet.WServletEnvironment servletEnvironment = new WServlet.WServletEnvironment("/app", "", "");
servletEnvironment.setStep(initialServletStep);
// Set user session
UIContext uic = createUIContext();
uic.setUI(app);
uic.setEnvironment(servletEnvironment);
setActiveContext(uic);
window.setStep(initialWindowStep);
window.display();
// Target the content first - should pass through and update the environment's step
WWindowInterceptor interceptor = new WWindowInterceptor(true);
TestInterceptor testInterceptor = new TestInterceptor();
interceptor.setBackingComponent(testInterceptor);
interceptor.attachUI(app);
MockRequest request = new MockRequest();
request.setParameter(Environment.TARGET_ID, content.getId());
interceptor.serviceRequest(request);
Assert.assertEquals("Servlet step should have changed after serviceRequest", initialServletStep + 1, servletEnvironment.getStep());
Assert.assertEquals("Window step should not have changed", initialWindowStep, window.getStep());
interceptor.preparePaint(request);
Assert.assertEquals("Servlet step should have changed after preparePaint", initialServletStep + 2, servletEnvironment.getStep());
Assert.assertEquals("Window step should not have changed", initialWindowStep, window.getStep());
interceptor.paint(new WebXmlRenderContext(new PrintWriter(new NullWriter())));
Assert.assertEquals("Servlet step should have changed after paint", initialServletStep + 3, servletEnvironment.getStep());
Assert.assertEquals("Window step should not have changed", initialWindowStep, window.getStep());
servletEnvironment.setStep(initialServletStep);
request = new MockRequest();
request.setParameter(WWindow.WWINDOW_REQUEST_PARAM_KEY, window.getId());
interceptor.serviceRequest(request);
Assert.assertEquals("Window step should have changed after serviceRequest", initialWindowStep + 1, window.getStep());
Assert.assertEquals("Servlet step should not have changed", initialServletStep, servletEnvironment.getStep());
interceptor.preparePaint(request);
Assert.assertEquals("Window step should have changed after preparePaintnot have changed", initialWindowStep + 2, window.getStep());
Assert.assertEquals("Servlet step should not have changed", initialServletStep, servletEnvironment.getStep());
interceptor.paint(new WebXmlRenderContext(new PrintWriter(new NullWriter())));
Assert.assertEquals("Window step should have changed after paint", initialWindowStep + 3, window.getStep());
Assert.assertEquals("Servlet step should not have changed", initialServletStep, servletEnvironment.getStep());
String actualTargetId = testInterceptor.hiddenParams.get(WWindow.WWINDOW_REQUEST_PARAM_KEY);
Assert.assertEquals("Hidden params target id should be window id", window.getId(), actualTargetId);
}
use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.
the class WrongStepContentInterceptor_Test method sendContentRequest.
/**
* Utility method to send a mock request to the application.
*
* @param target the target content.
* @param clientStep the client-side step count
* @param serverStep the server-side step count
* @return the response.
*/
private MockResponse sendContentRequest(final WComponent target, final int clientStep, final int serverStep) {
UIContext uic = createUIContext();
uic.setUI(WebUtilities.getTop(target));
WServlet.WServletEnvironment env = new WServlet.WServletEnvironment("/app", "http://localhost", "");
env.setStep(serverStep);
uic.setEnvironment(env);
setActiveContext(uic);
MockRequest request = new MockRequest();
request.setParameter(Environment.TARGET_ID, target.getId());
request.setParameter(Environment.STEP_VARIABLE, String.valueOf(clientStep));
MockResponse response = new MockResponse();
InterceptorComponent interceptor = new WrongStepContentInterceptor();
interceptor.attachUI(uic.getUI());
interceptor.attachResponse(response);
// Handle the request. This will either return the content or a step error
try {
interceptor.serviceRequest(request);
interceptor.preparePaint(request);
interceptor.paint(new WebXmlRenderContext(response.getWriter()));
} catch (ContentEscape escape) {
try {
// Content has been returned
escape.setRequest(request);
escape.setResponse(response);
escape.escape();
} catch (IOException e) {
Assert.fail("Failed to write content");
}
} catch (ErrorCodeEscape escape) {
try {
escape.setRequest(request);
escape.setResponse(response);
escape.escape();
} catch (IOException e) {
Assert.fail("Failed to write error content");
}
} catch (ActionEscape ignored) {
// don't care
}
// Step error
return response;
}
use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.
the class ProfileContainer_Test method testAfterPaint.
/**
* Test afterPaint.
*/
@Test
public void testAfterPaint() {
ProfileContainer app = new ProfileContainer();
app.setLocked(true);
UIContext uic = new UIContextImpl();
uic.setUI(app);
setActiveContext(uic);
WButton button = new WButton("PUSH");
app.add(button);
WLabel label = new WLabel("HERE");
app.add(label);
StringWriter outStr = new StringWriter();
PrintWriter writer = new PrintWriter(outStr);
RenderContext renderContext = new WebXmlRenderContext(writer);
app.afterPaint(renderContext);
// expecting 1 root class, 3 components, class names as shown, profiler
// class
String profileLine0 = PROFILER_UIC_HEADER;
String profileLine1 = PROFILER_LINE1.replaceAll("<<NUM_ROOTS>>", "1");
String profileLine2 = PROFILER_LINE2.replaceAll("<<NUM_COMPONENTS>>", "4");
String profileLine31 = PROFILER_LINE3.replaceAll("<<CLASS_NAME>>", "com.github.bordertech.wcomponents.WButton");
String profileLine32 = PROFILER_LINE3.replaceAll("<<CLASS_NAME>>", "com.github.bordertech.wcomponents.monitor.ProfileContainer");
String profileLine33 = PROFILER_LINE3.replaceAll("<<CLASS_NAME>>", "com.github.bordertech.wcomponents.WLabel");
String profileLine4 = PROFILER_PROFILE_HEADER.replaceAll("<<CLASS_NAME>>", "com.github.bordertech.wcomponents.monitor.ProfileContainer");
String[] expectedResults = { profileLine0, profileLine1, profileLine2, profileLine31, profileLine32, profileLine33, profileLine4 };
String result = outStr.toString();
for (int i = 0; i < expectedResults.length; i++) {
Assert.assertTrue("result should contain substring " + i + " ", result.indexOf(expectedResults[i]) != -1);
}
}
use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.
the class UicStatsAsHtml_Test method testWriter.
/**
* Test writer.
*/
@Test
public void testWriter() {
// Set up a mock UI to test
WApplication app = new WApplication();
app.setLocked(true);
UIContext uic = new UIContextImpl();
uic.setUI(app);
setActiveContext(uic);
WButton button = new WButton("PUSH");
app.add(button);
WLabel label = new WLabel("HERE");
app.add(label);
// Run the UIStats extract
UicStats stats = new UicStats(uic);
stats.analyseWC(app);
StringWriter outStr = new StringWriter();
PrintWriter writer = new PrintWriter(outStr);
UicStatsAsHtml.write(writer, stats);
// expecting 1 root class, 3 components, class names as shown
String uicStatsHtml1 = UICSTATS_HTML1.replaceAll("<<NUM_ROOTS>>", "1");
String uicStatsHtml2 = UICSTATS_HTML2.replaceAll("<<NUM_COMPONENTS>>", "4");
String uicStatsHtml31 = UICSTATS_HTML3.replaceAll("<<CLASS_NAME>>", "com.github.bordertech.wcomponents.WApplication");
String uicStatsHtml32 = UICSTATS_HTML3.replaceAll("<<CLASS_NAME>>", "com.github.bordertech.wcomponents.WButton");
String uicStatsHtml33 = UICSTATS_HTML3.replaceAll("<<CLASS_NAME>>", "com.github.bordertech.wcomponents.WLabel");
String[] expectedResults = { uicStatsHtml1, uicStatsHtml2, uicStatsHtml31, uicStatsHtml32, uicStatsHtml33 };
String result = outStr.toString();
for (int i = 0; i < expectedResults.length; i++) {
Assert.assertTrue("result should contain substring " + i + " ", result.indexOf(expectedResults[i]) != -1);
}
}
use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.
the class AjaxPageShellInterceptor method preparePaint.
/**
* Override to set the content type of the response and reset the headers.
*
* @param request The request being serviced.
*/
@Override
public void preparePaint(final Request request) {
UIContext uic = UIContextHolder.getCurrent();
Headers headers = uic.getUI().getHeaders();
headers.reset();
headers.setContentType(WebUtilities.CONTENT_TYPE_XML);
super.preparePaint(request);
}
Aggregations