@Test public void testSetImage() { MockImage sessionImage = new MockImage(); MockImage defaultImage = new MockImage(); WImage image = new WImage(); image.setImage(defaultImage); image.setLocked(true); setActiveContext(createUIContext()); Assert.assertSame( "Default image should be returned when no user specific image set", defaultImage, image.getImage()); Assert.assertTrue( "Should be in default state when no user specific image set", image.isDefaultState()); setActiveContext(createUIContext()); image.setImage(sessionImage); Assert.assertSame("Session image should be returned when set", sessionImage, image.getImage()); Assert.assertFalse( "Should not be in default state when session image set", image.isDefaultState()); resetContext(); Assert.assertSame("Default image should not be changed", defaultImage, image.getImage()); }
@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")); } }