use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.
the class SubordinateControlInterceptor_Test method testServiceRequestApplyControls.
@Test
public void testServiceRequestApplyControls() {
// Create Target
WButton target = new WButton();
target.setAction(new Action() {
@Override
public void execute(final ActionEvent event) {
buttonClicked = true;
}
});
// Create Control - Enable/Disable Button
WCheckBox box = new WCheckBox();
Rule rule = new Rule();
rule.setCondition(new Equal(box, Boolean.TRUE));
rule.addActionOnTrue(new Enable(target));
rule.addActionOnFalse(new Disable(target));
WSubordinateControl control = new WSubordinateControl();
control.addRule(rule);
// Create component tree
WContainer root = new WContainer();
root.add(control);
root.add(box);
root.add(target);
// Setup Intercepter
SubordinateControlInterceptor interceptor = new SubordinateControlInterceptor();
interceptor.setBackingComponent(root);
UIContext uic = createUIContext();
uic.setUI(root);
setActiveContext(uic);
buttonClicked = false;
// Test Service Request - Empty Request and no control registered, so the control should not be applied
MockRequest request = new MockRequest();
interceptor.serviceRequest(request);
// Target should still be enabled (control not applied)
Assert.assertFalse("After service request target should be enabled", target.isDisabled());
// Button not clicked
Assert.assertFalse("Button should not have been clicked", buttonClicked);
// Test Service Request - Try to click button while it is disabled and should not be clicked
target.setDisabled(true);
request.setParameter(target.getId(), "x");
interceptor.serviceRequest(request);
// Target should still be disabled (control not applied, as still not registered)
Assert.assertTrue("After service request target should be disabled", target.isDisabled());
// Button not clicked
Assert.assertFalse("Button should not have been clicked while disabled", buttonClicked);
// Test Prepare Paint - Should register and apply the subordinate control
target.setDisabled(false);
request = new MockRequest();
interceptor.preparePaint(request);
// Target should be disabled (Disabled by control as box is not selected)
Assert.assertTrue("After service request target should be disabled", target.isDisabled());
// Test Service Request - Simulate button click as it was enabled on the client by the check box being selected.
// As the controls have been registered from the Prepare Paint, they will be applied in the Service Request and
// this will enable the button and allow it to be clicked.
buttonClicked = false;
request.setParameter(target.getId(), "x");
setupCheckBoxRequest(box, request, true);
interceptor.serviceRequest(request);
// Target should be enabled (enabled by control as box is selected)
Assert.assertFalse("After service request target should be enabled", target.isDisabled());
// Button should have been clicked
Assert.assertTrue("Button should have been clicked", buttonClicked);
// // Check Subordinate Controls have not been cleared from session
// Assert.assertNotNull("Registered Controls should not have been cleared on the session",
// request.getSessionAttribute(SubordinateControlHelper.SUBORDINATE_CONTROL_SESSION_KEY));
//
// interceptor.preparePaint(request);
//
// // Check Subordinate Controls have been cleared from session
// Assert.assertNull("Registered Controls should have been cleared on the session",
// request.getSessionAttribute(SubordinateControlHelper.SUBORDINATE_CONTROL_SESSION_KEY));
}
use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.
the class TargetableInterceptor_Test method setUp.
@Before
public void setUp() {
UIContext uic = new UIContextImpl();
WPanel root = new WPanel();
originalUI = new WLabel();
targetUI = new TargetableWLabel();
root.add(originalUI);
root.add(targetUI);
uic.setUI(root);
setActiveContext(uic);
interceptor = new TargetableInterceptor();
interceptor.setBackingComponent(originalUI);
}
use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.
the class TemplateRenderInterceptorTest method generateOutput.
/**
* Render the component and execute the interceptor.
*
* @param testUI the test component
* @param headers Request headers to set (key/value pairs).
* @return the response
*/
private TestResult generateOutput(final MyComponent testUI, final Map<String, String> headers) {
InterceptorComponent interceptor = new TemplateRenderInterceptor();
interceptor.attachUI(testUI);
MockHttpServletRequest backing = new MockHttpServletRequest();
if (headers != null) {
for (String headerName : headers.keySet()) {
backing.setHeader(headerName, headers.get(headerName));
}
}
ServletRequest request = new ServletRequest(backing);
MockResponse response = new MockResponse();
interceptor.attachResponse(response);
StringWriter writer = new StringWriter();
UIContext uic = createUIContext();
uic.setLocale(new Locale("en"));
setActiveContext(uic);
try {
interceptor.preparePaint(request);
interceptor.paint(new WebXmlRenderContext(new PrintWriter(writer)));
} finally {
resetContext();
}
return new TestResult(writer.toString(), response.getContentType());
}
use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.
the class TransformXMLInterceptor_Test method generateOutput.
/**
* Render the component and execute the interceptor.
*
* @param testUI the test component
* @param headers Request headers to set (key/value pairs).
* @return the response
*/
private TestResult generateOutput(final MyComponent testUI, final Map<String, String> headers) {
InterceptorComponent interceptor = new TransformXMLInterceptor();
interceptor.attachUI(testUI);
MockHttpServletRequest backing = new MockHttpServletRequest();
if (headers != null) {
for (String headerName : headers.keySet()) {
backing.setHeader(headerName, headers.get(headerName));
}
}
ServletRequest request = new ServletRequest(backing);
MockResponse response = new MockResponse();
interceptor.attachResponse(response);
StringWriter writer = new StringWriter();
UIContext uic = createUIContext();
uic.setLocale(new Locale("en"));
setActiveContext(uic);
try {
interceptor.preparePaint(request);
interceptor.paint(new WebXmlRenderContext(new PrintWriter(writer)));
} finally {
resetContext();
}
return new TestResult(writer.toString(), response.getContentType());
}
use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.
the class TransformXMLInterceptor_Test method testPaintWithCorruptCharacterAllowed.
/**
* Ensure that the interceptor does nothing as long as the controlling property is disabled.
*
* @throws java.lang.NoSuchFieldException an exception
* @throws java.lang.IllegalAccessException an exception
*/
@Test
public void testPaintWithCorruptCharacterAllowed() throws NoSuchFieldException, IllegalAccessException {
/**
* Have to use reflection to swap out the log implementation as there's no way to programmatically disable
* logging without coupling to a log implementation.
*/
// Override the logger temporarily.
Field field = TransformXMLInterceptor.class.getDeclaredField("LOG");
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
Object oldValue = field.get(null);
field.set(null, new NoLogLogger());
MyComponent testUI = new MyComponent(TEST_CORRUPT_CHAR_XML);
Config.getInstance().setProperty(ConfigurationProperties.THEME_CONTENT_PATH, "");
Config.getInstance().setProperty(ConfigurationProperties.XSLT_ALLOW_CORRUPT_CHARACTER, "true");
TransformXMLTestHelper.reloadTransformer();
testUI.setLocked(true);
UIContext uic = createUIContext();
uic.setUI(testUI);
setActiveContext(uic);
TestResult actual = generateOutput(testUI, null);
// Set the original value
Field resetValueField = TransformXMLInterceptor.class.getDeclaredField("LOG");
resetValueField.setAccessible(true);
field.set(null, oldValue);
}
Aggregations