@Test public void testGetRequestValue() { WEmailField field = new WEmailField(); field.setLocked(true); setActiveContext(createUIContext()); // Set current value field.setText("A"); // Empty Request (not present, should return current value) MockRequest request = new MockRequest(); Assert.assertEquals( "Current value of the field should have been returned for empty request", "A", field.getRequestValue(request)); // Request with "empty" value (should return null as an empty value on the request is treated as // null) request = new MockRequest(); request.setParameter(field.getId(), ""); Assert.assertNull( "Null should have been returned for request with empty value", field.getRequestValue(request)); // Request with value (should return the value on the request) request = new MockRequest(); request.setParameter(field.getId(), "X"); Assert.assertEquals( "Value from the request should have been returned", "X", field.getRequestValue(request)); }
@Test public void testHandleRequest() throws IOException { byte[] data = "WImage_Test.testHandleRequest".getBytes(CHAR_ENCODING); MockRequest request = new MockRequest(); MockImage content = new MockImage(); content.setBytes(data); WImage image = new WImage(); image.setLocked(true); setActiveContext(createUIContext()); image.setImage(content); // Should not do anything when target is not present image.handleRequest(request); try { request.setParameter(Environment.TARGET_ID, image.getTargetId()); image.handleRequest(request); Assert.fail("Should have thrown a content escape"); } catch (ContentEscape escape) { MockResponse response = new MockResponse(); escape.setResponse(response); escape.escape(); String output = new String(response.getOutput(), CHAR_ENCODING); Assert.assertEquals("Incorrect content returned", new String(data, CHAR_ENCODING), output); Assert.assertFalse("Cache flag should not be set", escape.isCacheable()); Assert.assertEquals( "Response should have header set for no caching", ResponseCacheInterceptor.DEFAULT_NO_CACHE_SETTINGS, response.getHeaders().get("Cache-Control")); } // Test Cached Response image.setCacheKey("key"); // Should produce the content with cache flag set try { image.handleRequest(request); Assert.fail("Should have thrown a content escape"); } catch (ContentEscape escape) { MockResponse response = new MockResponse(); escape.setResponse(response); escape.escape(); String output = new String(response.getOutput(), CHAR_ENCODING); Assert.assertEquals("Incorrect content returned", new String(data, CHAR_ENCODING), output); Assert.assertTrue("Cache flag should be set", escape.isCacheable()); Assert.assertEquals( "Response should have header set for caching", ResponseCacheInterceptor.DEFAULT_CACHE_SETTINGS, response.getHeaders().get("Cache-Control")); } }
@Test public void testDoHandleRequest() { WEmailField field = new WEmailField(); field.setLocked(true); setActiveContext(createUIContext()); // Request with Empty Value and Field is null (No Change) field.setData(null); MockRequest request = new MockRequest(); request.setParameter(field.getId(), ""); boolean changed = field.doHandleRequest(request); Assert.assertFalse( "doHandleRequest should have returned false for request with empty value and field is null", changed); Assert.assertNull("Value should still be null after empty request", field.getData()); // Request with Empty Value and Field is empty (No Change) field.setData(""); request = new MockRequest(); request.setParameter(field.getId(), ""); changed = field.doHandleRequest(request); Assert.assertFalse( "doHandleRequest should have returned false for request with empty value and field is empty", changed); Assert.assertEquals("Value should still be empty after empty request", "", field.getData()); // Request with Different Value (Change) request = new MockRequest(); request.setParameter(field.getId(), "X"); changed = field.doHandleRequest(request); Assert.assertTrue( "doHandleRequest should have returned true for request with different value", changed); Assert.assertEquals("Value not set after request", "X", field.getData()); // Request with Same Value (No Change) request = new MockRequest(); request.setParameter(field.getId(), "X"); changed = field.doHandleRequest(request); Assert.assertFalse( "doHandleRequest should have returned false for request with same value", changed); Assert.assertEquals( "Value should not have changed after request with same value", "X", field.getData()); // Request with Empty Value (Change) request = new MockRequest(); request.setParameter(field.getId(), ""); changed = field.doHandleRequest(request); Assert.assertTrue( "doHandleRequest should have returned true for request going back to an empty value", changed); Assert.assertNull("Value should go back to null for request with empty value", field.getData()); }