use of com.github.bordertech.wcomponents.servlet.WebXmlRenderContext in project wcomponents by BorderTech.
the class WhitespaceFilterInterceptor_Test method testPaint.
@Test
public void testPaint() {
final String testString = " foo bar ";
final String filteredString = " foo bar ";
WLabel label = new WLabel(testString);
WhitespaceFilterInterceptor interceptor = new WhitespaceFilterInterceptor();
interceptor.attachUI(label);
label.setLocked(true);
setActiveContext(createUIContext());
// Test when disabled
Config.getInstance().setProperty(ConfigurationProperties.WHITESPACE_FILTER, "false");
StringWriter writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
interceptor.paint(new WebXmlRenderContext(printWriter));
printWriter.close();
Assert.assertTrue("Should not have filtered text when disabled", writer.toString().contains(testString));
// Test when enabled
Config.getInstance().setProperty(ConfigurationProperties.WHITESPACE_FILTER, "true");
writer = new StringWriter();
printWriter = new PrintWriter(writer);
interceptor.paint(new WebXmlRenderContext(printWriter));
printWriter.close();
Assert.assertTrue("Should have filtered text when enabled", writer.toString().contains(filteredString));
}
use of com.github.bordertech.wcomponents.servlet.WebXmlRenderContext 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.servlet.WebXmlRenderContext 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.servlet.WebXmlRenderContext in project wcomponents by BorderTech.
the class AbstractWebXmlRendererTestCase method render.
/**
* Renders the given component to xml.
*
* @param component the component to render.
* @return the rendered format of the component.
*/
public String render(final WebComponent component) {
boolean needsContext = UIContextHolder.getCurrent() == null;
if (needsContext) {
setActiveContext(createUIContext());
}
try {
StringWriter buffer = new StringWriter();
component.preparePaint(new MockRequest());
PrintWriter writer = new PrintWriter(buffer);
component.paint(new WebXmlRenderContext(writer));
writer.close();
return buffer.toString();
} finally {
if (needsContext) {
UIContextHolder.popContext();
}
}
}
use of com.github.bordertech.wcomponents.servlet.WebXmlRenderContext in project wcomponents by BorderTech.
the class AjaxInterceptor method paintResponse.
/**
* Paint the ajax response.
*
* @param renderContext the render context
* @param operation the ajax operation
*/
private void paintResponse(final RenderContext renderContext, final AjaxOperation operation) {
WebXmlRenderContext webRenderContext = (WebXmlRenderContext) renderContext;
XmlStringBuilder xml = webRenderContext.getWriter();
// Get trigger's context
ComponentWithContext trigger = AjaxHelper.getCurrentTriggerAndContext();
if (trigger == null) {
throw new SystemException("No context available for trigger " + operation.getTriggerId());
}
for (String targetId : operation.getTargets()) {
ComponentWithContext target;
if (targetId.equals(operation.getTriggerId())) {
target = trigger;
} else {
target = WebUtilities.getComponentById(targetId, true);
if (target == null) {
LOG.warn("Could not find ajax target to render [" + targetId + "]");
continue;
}
}
UIContextHolder.pushContext(target.getContext());
try {
xml.appendTagOpen("ui:ajaxtarget");
xml.appendAttribute("id", targetId);
xml.appendAttribute("action", operation.getAction().getDesc());
xml.appendClose();
target.getComponent().paint(renderContext);
xml.appendEndTag("ui:ajaxtarget");
} finally {
UIContextHolder.popContext();
}
}
}
Aggregations