private void seedTiles(StorageBroker storageBroker, TileRange tr, final WMSLayer tl)
      throws Exception {
    final String layerName = tl.getName();
    // define the meta tile size to 1,1 so we hit all the tiles
    final TileRangeIterator trIter = new TileRangeIterator(tr, tl.getMetaTilingFactors());

    long[] gridLoc = trIter.nextMetaGridLocation(new long[3]);

    while (gridLoc != null) {
      Map<String, String> fullParameters = tr.getParameters();

      final ConveyorTile tile =
          new ConveyorTile(
              storageBroker,
              layerName,
              tr.getGridSetId(),
              gridLoc,
              tr.getMimeType(),
              fullParameters,
              null,
              null);
      tile.setTileLayer(tl);

      tl.seedTile(tile, false);

      gridLoc = trIter.nextMetaGridLocation(gridLoc);
    }
  }
 @Override
 protected TileLayer getLayerWithFilters(Collection<ParameterFilter> filters) throws Exception {
   WMSLayer tl = createWMSLayer("image/png", 5, 6);
   @SuppressWarnings("unused")
   MockTileSupport mock = new MockTileSupport(tl);
   tl.getParameterFilters().addAll(filters);
   return tl;
 }
    private void installSourceHelper(WMSLayer tl) throws Exception {
      // WMSSourceHelper that on makeRequest() returns always the same fake image
      WMSSourceHelper mockSourceHelper = EasyMock.createMock(WMSSourceHelper.class);

      Capture<WMSMetaTile> wmsRequestsCapturer =
          new Capture<WMSMetaTile>() {

            @Override
            public void setValue(WMSMetaTile o) {
              wmsMetaRequestCounter.incrementAndGet();
            }
          };
      Capture<Resource> resourceCapturer =
          new Capture<Resource>() {

            @Override
            public void setValue(Resource target) {
              try {
                target.transferFrom(Channels.newChannel(new ByteArrayInputStream(fakeWMSResponse)));
                tileTransferCounter.incrementAndGet();
              } catch (IOException e) {
                throw new RuntimeException(e);
              }
            }
          };
      mockSourceHelper.makeRequest(capture(wmsRequestsCapturer), capture(resourceCapturer));
      expectLastCall().anyTimes().asStub();
      mockSourceHelper.setConcurrency(32);
      mockSourceHelper.setBackendTimeout(120);
      replay(mockSourceHelper);

      tl.setSourceHelper(mockSourceHelper);
    }
  private List<ConveyorTile> getTiles(StorageBroker storageBroker, TileRange tr, final WMSLayer tl)
      throws Exception {
    final String layerName = tl.getName();
    // define the meta tile size to 1,1 so we hit all the tiles
    final TileRangeIterator trIter = new TileRangeIterator(tr, new int[] {1, 1});

    long[] gridLoc = trIter.nextMetaGridLocation(new long[3]);

    // six concurrent requests max
    ExecutorService requests = Executors.newFixedThreadPool(6);
    ExecutorCompletionService completer = new ExecutorCompletionService(requests);

    List<Future<ConveyorTile>> futures = new ArrayList<Future<ConveyorTile>>();
    while (gridLoc != null) {
      Map<String, String> fullParameters = tr.getParameters();

      final ConveyorTile tile =
          new ConveyorTile(
              storageBroker,
              layerName,
              tr.getGridSetId(),
              gridLoc,
              tr.getMimeType(),
              fullParameters,
              null,
              null);
      futures.add(
          completer.submit(
              new Callable<ConveyorTile>() {

                public ConveyorTile call() throws Exception {
                  try {
                    return tl.getTile(tile);
                  } catch (OutsideCoverageException oce) {
                    return null;
                  }
                }
              }));

      gridLoc = trIter.nextMetaGridLocation(gridLoc);
    }

    // these assertions could be externalized
    List<ConveyorTile> results = new ArrayList<ConveyorTile>();
    for (int i = 0; i < futures.size(); i++) {
      ConveyorTile get = futures.get(i).get();
      if (get != null) {
        results.add(get);
      }
    }

    requests.shutdown();

    return results;
  }
  @Test
  public void testGetFeatureInfoQueryLayers() throws MimeException {

    // a layer with no query layers
    WMSLayer l = createFeatureInfoLayer("a,b", null);
    assertNotNull(l.getWmsLayers());
    assertNull(l.getWmsQueryLayers());
    Map<String, String> rt =
        l.getWMSRequestTemplate(MimeType.createFromFormat("text/plain"), RequestType.FEATUREINFO);
    assertEquals(l.getWmsLayers(), rt.get("QUERY_LAYERS"));

    // a layer with query layers
    l = createFeatureInfoLayer("a,b", "b");
    assertNotNull(l.getWmsLayers());
    assertNotNull(l.getWmsQueryLayers());
    rt = l.getWMSRequestTemplate(MimeType.createFromFormat("text/plain"), RequestType.FEATUREINFO);
    assertEquals(l.getWmsQueryLayers(), rt.get("QUERY_LAYERS"));
  }
  @Test
  public void testCascadeGetLegendGraphics() throws Exception {
    // setup the layer
    WMSLayer layer = createWMSLayer("image/png");
    final byte[] responseBody = new String("Fake body").getBytes();
    layer.setSourceHelper(
        new WMSHttpHelper() {
          @Override
          public GetMethod executeRequest(
              URL url, Map<String, String> queryParams, Integer backendTimeout)
              throws HttpException, IOException {
            GetMethod response = EasyMock.createMock(GetMethod.class);
            expect(response.getStatusCode()).andReturn(200);
            expect(response.getResponseBodyAsStream())
                .andReturn(new ByteArrayInputStream(responseBody));
            expect(response.getResponseCharSet()).andReturn("UTF-8");
            expect(response.getResponseHeader("Content-Type"))
                .andReturn(new Header("Content-Type", "image/png"));
            response.releaseConnection();
            expectLastCall();
            replay(response);
            return response;
          }
        });
    MockLockProvider lockProvider = new MockLockProvider();
    layer.setLockProvider(lockProvider);

    // setup the conveyor tile
    final StorageBroker mockStorageBroker = EasyMock.createMock(StorageBroker.class);

    String layerId = layer.getName();
    MockHttpServletRequest servletReq = new MockHttpServletRequest();
    servletReq.setQueryString(
        "REQUEST=GetLegendGraphic&VERSION=1.0.0&FORMAT=image/png&WIDTH=20&HEIGHT=20&LAYER=topp:states");
    MockHttpServletResponse servletResp = new MockHttpServletResponse();

    long[] gridLoc = {0, 0, 0}; // x, y, level
    MimeType mimeType = layer.getMimeTypes().get(0);
    GridSet gridSet = gridSetBroker.WORLD_EPSG4326;
    String gridSetId = gridSet.getName();
    ConveyorTile tile =
        new ConveyorTile(
            mockStorageBroker,
            layerId,
            gridSetId,
            gridLoc,
            mimeType,
            null,
            servletReq,
            servletResp);

    // proxy the request, and check the response
    layer.proxyRequest(tile);

    assertEquals(200, servletResp.getStatus());
    assertEquals("Fake body", servletResp.getContentAsString());
    assertEquals("image/png", servletResp.getContentType());
  }
  @Test
  public void testSeedMetaTiled() throws Exception {
    WMSLayer layer = createWMSLayer("image/png");

    WMSSourceHelper mockSourceHelper = new MockWMSSourceHelper();
    MockLockProvider lockProvider = new MockLockProvider();
    layer.setSourceHelper(mockSourceHelper);
    layer.setLockProvider(lockProvider);

    final StorageBroker mockStorageBroker = EasyMock.createMock(StorageBroker.class);
    Capture<TileObject> captured = new Capture<TileObject>();
    expect(mockStorageBroker.put(EasyMock.capture(captured))).andReturn(true).anyTimes();
    replay(mockStorageBroker);

    String layerId = layer.getName();
    HttpServletRequest servletReq = new MockHttpServletRequest();
    HttpServletResponse servletResp = new MockHttpServletResponse();

    long[] gridLoc = {0, 0, 0}; // x, y, level
    MimeType mimeType = layer.getMimeTypes().get(0);
    GridSet gridSet = gridSetBroker.WORLD_EPSG4326;
    String gridSetId = gridSet.getName();
    ConveyorTile tile =
        new ConveyorTile(
            mockStorageBroker,
            layerId,
            gridSetId,
            gridLoc,
            mimeType,
            null,
            servletReq,
            servletResp);

    boolean tryCache = false;
    layer.seedTile(tile, tryCache);

    assertEquals(1, captured.getValues().size());
    TileObject value = captured.getValue();
    assertNotNull(value);
    assertEquals("image/png", value.getBlobFormat());
    assertNotNull(value.getBlob());
    assertTrue(value.getBlob().getSize() > 0);

    verify(mockStorageBroker);

    // check the lock provider was called in a symmetric way
    lockProvider.verify();
    lockProvider.clear();
  }
  @Test
  public void testSeedJpegPngMetaTiled() throws Exception {
    WMSLayer layer = createWMSLayer("image/vnd.jpeg-png");

    WMSSourceHelper mockSourceHelper =
        new WMSSourceHelper() {

          @Override
          protected void makeRequest(
              TileResponseReceiver tileRespRecv,
              WMSLayer layer,
              Map<String, String> wmsParams,
              MimeType expectedMimeType,
              Resource target)
              throws GeoWebCacheException {
            int width = Integer.parseInt(wmsParams.get("WIDTH"));
            int height = Integer.parseInt(wmsParams.get("HEIGHT"));
            assertEquals(768, width);
            assertEquals(768, height);
            BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
            Graphics2D graphics = img.createGraphics();
            graphics.setColor(Color.BLACK);
            // fill an L shaped set of tiles, making a few partially filled
            graphics.fillRect(0, 0, width, 300);
            graphics.fillRect(0, 0, 300, height);
            graphics.dispose();
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            try {
              ImageIO.write(img, "PNG", output);
              ImageIO.write(img, "PNG", new java.io.File("/tmp/meta.png"));
            } catch (IOException e) {
              throw new RuntimeException(e);
            }

            try {
              target.transferFrom(
                  Channels.newChannel(new ByteArrayInputStream(output.toByteArray())));
            } catch (IOException e) {
              throw new RuntimeException(e);
            }
          }
        };
    MockLockProvider lockProvider = new MockLockProvider();
    layer.setSourceHelper(mockSourceHelper);
    layer.setLockProvider(lockProvider);

    final StorageBroker mockStorageBroker = EasyMock.createMock(StorageBroker.class);
    Capture<TileObject> captured = new Capture<TileObject>(CaptureType.ALL);
    expect(mockStorageBroker.put(EasyMock.capture(captured)))
        .andAnswer(
            new IAnswer<Boolean>() {

              @Override
              public Boolean answer() throws Throwable {
                TileObject to = (TileObject) EasyMock.getCurrentArguments()[0];
                assertEquals("image/vnd.jpeg-png", to.getBlobFormat());
                assertNotNull(to.getBlob());
                assertTrue(to.getBlob().getSize() > 0);
                String format = ImageMime.jpegPng.getMimeType(to.getBlob());
                long[] xyz = to.getXYZ();
                assertEquals(10, xyz[2]);
                // check the ones in the full black area are jpeg, the others png
                if (xyz[0] == 900 || xyz[1] == 602) {
                  assertEquals("image/jpeg", format);
                } else {
                  assertEquals("image/png", format);
                }

                return true;
              }
            })
        .anyTimes();
    replay(mockStorageBroker);

    String layerId = layer.getName();
    HttpServletRequest servletReq = new MockHttpServletRequest();
    HttpServletResponse servletResp = new MockHttpServletResponse();

    long[] gridLoc = {900, 600, 10}; // x, y, level
    MimeType mimeType = layer.getMimeTypes().get(0);
    GridSet gridSet = gridSetBroker.WORLD_EPSG4326;
    String gridSetId = gridSet.getName();
    ConveyorTile tile =
        new ConveyorTile(
            mockStorageBroker,
            layerId,
            gridSetId,
            gridLoc,
            mimeType,
            null,
            servletReq,
            servletResp);

    boolean tryCache = false;
    layer.seedTile(tile, tryCache);

    assertEquals(9, captured.getValues().size());
    verify(mockStorageBroker);

    // check the lock provider was called in a symmetric way
    lockProvider.verify();
    lockProvider.clear();
  }