/** * Renders the given WComponent to a String outside of the context of a Servlet. This is good for getting hold of * the XML for debugging, unit testing etc. Also it is good for using the WComponent framework as a more generic * templating framework. * * @param component the root WComponent to render. * @return the rendered output as a String. */ public static String render(final WComponent component) { return render(new MockRequest(), component); }
UIContextHolder.pushContext(new UIContextImpl()); try { html = WebUtilities.render(errorPage); } catch (Exception e) { LOG.warn("Could not render error page.", e);
@Test public void testRenderWithPlainText() { String msg = "Test error message"; WText text = new WText(msg); String output = WebUtilities.render(text); Assert.assertEquals("Invalid output returned", msg, output); }
@Test public void testRenderWithXML() { WText text = new WText(TransformXMLTestHelper.TEST_XML); text.setEncodeText(false); String output = WebUtilities.render(text); Assert.assertEquals("Invalid output with XML", TransformXMLTestHelper.TEST_XML, output); }
@Test public void testDisplaySearch() { TestApp testApp = new TestApp(); setActiveContext(createUIContext()); MockRequest request = new MockRequest(); String result = WebUtilities.render(request, testApp); LogFactory.getLog(getClass()).debug(result); Assert.assertNotNull("Html output should not be null", result); Assert.assertTrue("Should be on search criteria page", result.contains("Search Criteria")); }
@Test public void testRender() { WLabel ui = new WLabel("InterceptorComponent_Test.testRender"); setActiveContext(createUIContext()); String expected = WebUtilities.render(new MockRequest(), ui); String actual = InterceptorComponent.render(ui); Assert.assertEquals("Incorrect render output", expected, actual); } }
@Test public void testDetailsTab() { TestApp testApp = TestApp.getInstance(); SearchResultRowBO row = testApp.createRow(5, "test", "Huge"); setActiveContext(createUIContext()); testApp.doDetails(row); String result = WebUtilities.render(new MockRequest(), testApp); Assert.assertTrue("Should be on search results page", result.contains("Details")); } }
result = WebUtilities.render(request, testApp); } finally { elapsed = System.currentTimeMillis() - elapsed;
@Test public void testRequest() { // Create a test wcomponent with a label and an entry field. WTextField name = new WTextField(); WLabel label = new WLabel("Hero", name); WPanel panel = new WPanel(); panel.add(label); panel.add(name); // Create a mock context and request. setActiveContext(createUIContext()); MockRequest request = new MockRequest(); // Check that the text field successfully stores text. name.setText("Batman"); Assert.assertEquals("text accessors incorrect", "Batman", name.getText()); // Service a request that simulates a user entering a value // into the entry field. request.setParameter(name.getId(), "Superman"); panel.serviceRequest(request); Assert.assertEquals("text incorrect after request", "Superman", name.getText()); // Render the output and log it. String output = WebUtilities.render(request, panel); LOG.debug(output); } }