@Test
  public void testSetCacheKey() throws IOException {
    final String defaultKey = "DEFAULT KEY";
    final String testKey = "TEST KEY";

    WImage image = new WImage();
    Assert.assertNull("CacheKey should be null by default", image.getCacheKey());

    image.setCacheKey(defaultKey);
    Assert.assertEquals(
        "Incorrect value returned for default cache key", defaultKey, image.getCacheKey());

    image.setLocked(true);
    setActiveContext(createUIContext());
    Assert.assertEquals(
        "Incorrect value returned for default cache key with user context",
        defaultKey,
        image.getCacheKey());

    image.setCacheKey(testKey);
    Assert.assertEquals(
        "Incorrect value returned for cache key with user context", testKey, image.getCacheKey());

    resetContext();
    Assert.assertEquals(
        "Incorrect value returned for default cache key", defaultKey, image.getCacheKey());
  }
  @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"));
    }
  }