LayerInfo createLayer(ResourceInfo r, String name, NamespaceInfo ns) {
    String lId = newId();
    StyleInfo s = styles.peekLast();

    final LayerInfo l = createNiceMock(LayerInfo.class);
    layers.add(l);

    expect(l.getId()).andReturn(lId).anyTimes();
    expect(l.getName()).andReturn(name).anyTimes();
    expect(l.getType()).andReturn(LayerInfo.Type.VECTOR).anyTimes();
    expect(l.getResource()).andReturn(r).anyTimes();
    expect(l.getDefaultStyle()).andReturn(s).anyTimes();
    expect(l.isEnabled()).andReturn(true).anyTimes();
    expect(l.isAdvertised()).andReturn(true).anyTimes();

    expect(catalog.getLayer(lId)).andReturn(l).anyTimes();
    expect(catalog.getLayerByName(name)).andReturn(l).anyTimes();
    expect(catalog.getLayerByName(ns.getPrefix() + ":" + name)).andReturn(l).anyTimes();
    expect(catalog.getLayerByName(new NameImpl(ns.getPrefix(), name))).andReturn(l).anyTimes();
    expect(catalog.getLayerByName(new NameImpl(ns.getURI(), name))).andReturn(l).anyTimes();
    expect(catalog.getLayers(r)).andReturn(Arrays.asList(l)).anyTimes();
    l.accept((CatalogVisitor) anyObject());
    expectLastCall()
        .andAnswer(
            new VisitAnswer() {
              @Override
              protected void doVisit(CatalogVisitor visitor) {
                visitor.visit(l);
              }
            })
        .anyTimes();

    callback.onLayer(name, l, this);
    return l;
  }
  public MockCatalogBuilder workspace(String name, String uri) {
    String wsId = newId();
    String nsId = newId();

    final WorkspaceInfo ws = createNiceMock(WorkspaceInfo.class);
    workspaces.add(ws);
    expect(ws.getId()).andReturn(wsId).anyTimes();
    expect(ws.getName()).andReturn(name).anyTimes();
    expect(ws.getMetadata()).andReturn(new MetadataMap()).anyTimes();

    expect(catalog.getWorkspace(wsId)).andReturn(ws).anyTimes();
    expect(catalog.getWorkspaceByName(name)).andReturn(ws).anyTimes();

    final NamespaceInfo ns = createNiceMock(NamespaceInfo.class);
    namespaces.add(ns);

    expect(ns.getId()).andReturn(nsId).anyTimes();
    expect(ns.getName()).andReturn(name).anyTimes();
    expect(ns.getPrefix()).andReturn(name).anyTimes();
    expect(ns.getMetadata()).andReturn(new MetadataMap()).anyTimes();

    expect(catalog.getNamespace(nsId)).andReturn(ns).anyTimes();
    expect(catalog.getNamespaceByPrefix(name)).andReturn(ns).anyTimes();
    expect(catalog.getNamespaceByURI(uri)).andReturn(ns).anyTimes();

    ws.accept((CatalogVisitor) anyObject());
    expectLastCall()
        .andAnswer(
            new VisitAnswer() {
              @Override
              protected void doVisit(CatalogVisitor visitor) {
                visitor.visit(ws);
              }
            })
        .anyTimes();

    ns.accept((CatalogVisitor) anyObject());
    expectLastCall()
        .andAnswer(
            new VisitAnswer() {
              @Override
              protected void doVisit(CatalogVisitor visitor) {
                visitor.visit(ns);
              }
            })
        .anyTimes();

    callback.onWorkspace(name, ws, this);

    replay(ws, ns);
    return this;
  }
  public MockCatalogBuilder coverage(QName qName, String fileName, String srs, Class scope) {
    scope = scope != null ? scope : getClass();

    String cId = newId();
    final CoverageStoreInfo cs = coverageStores.peekLast();
    NamespaceInfo ns = namespaces.peekLast();

    final String name = qName.getLocalPart();
    File dir = new File(dataDirRoot, name);
    dir.mkdir();

    try {
      IOUtils.copy(scope.getResourceAsStream(fileName), new File(dir, fileName));
    } catch (IOException e) {
      throw new RuntimeException(e);
    }

    // initialize the mock by actually building a real one first
    CatalogBuilder cb = new CatalogBuilder(new CatalogImpl());
    cb.setStore(cs);

    GridCoverage2DReader reader = cs.getFormat().getReader(cs.getURL());
    if (reader == null) {
      throw new RuntimeException("No reader for " + cs.getURL());
    }

    CoverageInfo real = null;
    try {
      real = cb.buildCoverage(reader, null);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }

    final CoverageInfo c = createNiceMock(CoverageInfo.class);
    coverages.add(c);
    final List<CoverageInfo> coverageList = coverages;

    if (srs == null) {
      srs = real.getSRS();
    }
    initResource(
        c,
        CoverageInfo.class,
        cId,
        name,
        cs,
        ns,
        srs,
        real.getProjectionPolicy(),
        real.getNativeBoundingBox(),
        real.getLatLonBoundingBox());

    expect(c.getDefaultInterpolationMethod())
        .andReturn(real.getDefaultInterpolationMethod())
        .anyTimes();
    expect(c.getDimensions()).andReturn(real.getDimensions()).anyTimes();
    expect(c.getGrid()).andReturn(real.getGrid()).anyTimes();

    expect(c.getInterpolationMethods()).andReturn(real.getInterpolationMethods()).anyTimes();
    expect(c.getRequestSRS()).andReturn(real.getRequestSRS()).anyTimes();
    expect(c.getResponseSRS()).andReturn(real.getResponseSRS()).anyTimes();

    try {
      expect(c.getGridCoverageReader(null, null)).andReturn(reader).anyTimes();
    } catch (IOException e) {
    }

    expect(catalog.getCoverageByName(or(eq(name), eq(ns.getPrefix() + ":" + name))))
        .andReturn(c)
        .anyTimes();
    expect(
            catalog.getCoverageByName(
                or(eq(new NameImpl(ns.getPrefix(), name)), eq(new NameImpl(ns.getURI(), name)))))
        .andReturn(c)
        .anyTimes();
    expect(catalog.getCoverageByName(ns, name)).andReturn(c).anyTimes();

    expect(catalog.getCoverageByName(ns.getPrefix(), name)).andReturn(c).anyTimes();
    // expect(catalog.getFeatureTypeByName(or(eq(ns.getPrefix()), eq(ns.getURI())), name))
    //    .andReturn(ft).anyTimes();

    // expect(catalog.getCoverageByStore(cs, name)).andReturn(c).anyTimes();
    expect(catalog.getCoveragesByStore(cs)).andReturn(coverageList).anyTimes();
    expect(catalog.getCoverageByCoverageStore(cs, name)).andReturn(c).anyTimes();

    c.accept((CatalogVisitor) anyObject());
    expectLastCall()
        .andAnswer(
            new VisitAnswer() {
              @Override
              protected void doVisit(CatalogVisitor visitor) {
                visitor.visit(c);
              }
            })
        .anyTimes();

    callback.onResource(name, c, cs, this);
    replay(c, createLayer(c, name, ns));
    return this;
  }
  <T extends ResourceInfo> void initResource(
      T r,
      Class<T> clazz,
      String rId,
      String name,
      StoreInfo s,
      NamespaceInfo ns,
      String srs,
      ProjectionPolicy projPolicy,
      ReferencedEnvelope envelope,
      ReferencedEnvelope latLonEnvelope) {

    expect(r.getId()).andReturn(rId).anyTimes();
    expect(r.getName()).andReturn(name).anyTimes();
    expect(r.getQualifiedName()).andReturn(new NameImpl(ns.getURI(), name)).anyTimes();
    expect(r.getNativeName()).andReturn(name).anyTimes();
    expect(r.getQualifiedNativeName()).andReturn(new NameImpl(ns.getURI(), name)).anyTimes();
    expect(r.getTitle()).andReturn(name).anyTimes();
    expect(r.getAbstract()).andReturn("abstract about " + name).anyTimes();
    expect(r.getStore()).andReturn(s).anyTimes();
    expect(r.getNamespace()).andReturn(ns).anyTimes();

    srs = srs != null ? srs : "EPSG:4326";
    expect(r.getSRS()).andReturn(srs).anyTimes();
    try {
      expect(r.getNativeCRS()).andReturn(CRS.decode(srs));
    } catch (Exception e) {
      throw new RuntimeException(e);
    }

    expect(r.getKeywords()).andReturn((List) Arrays.asList(new Keyword(name))).anyTimes();
    expect(r.isEnabled()).andReturn(true).anyTimes();
    expect(r.isAdvertised()).andReturn(true).anyTimes();
    expect(r.getProjectionPolicy()).andReturn(projPolicy).anyTimes();
    expect(r.getLatLonBoundingBox()).andReturn(latLonEnvelope).anyTimes();
    ;
    expect(r.getNativeBoundingBox()).andReturn(envelope).anyTimes();

    expect(catalog.getResource(rId, clazz)).andReturn(r).anyTimes();

    expect(catalog.getResourceByName(ns.getPrefix() + ":" + name, clazz)).andReturn(r).anyTimes();
    expect(catalog.getResourceByName(ns.getPrefix() + ":" + name, ResourceInfo.class))
        .andReturn(r)
        .anyTimes();

    expect(catalog.getResourceByName(name, clazz)).andReturn(r).anyTimes();
    expect(catalog.getResourceByName(name, ResourceInfo.class)).andReturn(r).anyTimes();

    expect(catalog.getResourceByName(new NameImpl(ns.getPrefix(), name), clazz))
        .andReturn(r)
        .anyTimes();
    expect(catalog.getResourceByName(new NameImpl(ns.getPrefix(), name), ResourceInfo.class))
        .andReturn(r)
        .anyTimes();

    expect(catalog.getResourceByName(new NameImpl(ns.getURI(), name), clazz))
        .andReturn(r)
        .anyTimes();
    expect(catalog.getResourceByName(new NameImpl(ns.getURI(), name), ResourceInfo.class))
        .andReturn(r)
        .anyTimes();

    expect(catalog.getResourceByName(ns, name, clazz)).andReturn(r).andReturn(r).anyTimes();
    expect(catalog.getResourceByName(ns, name, ResourceInfo.class))
        .andReturn(r)
        .andReturn(r)
        .anyTimes();

    expect(catalog.getResourceByName(ns.getPrefix(), name, clazz)).andReturn(r).anyTimes();
    expect(catalog.getResourceByName(ns.getPrefix(), name, ResourceInfo.class))
        .andReturn(r)
        .anyTimes();
    // expect(catalog.getResourceByName(or(eq(ns.getPrefix()), eq(ns.getURI())), name,
    //    clazz)).andReturn(r).anyTimes();
    expect(catalog.getResourceByStore(s, name, clazz)).andReturn(r).anyTimes();
    expect(catalog.getResourceByStore(s, name, ResourceInfo.class)).andReturn(r).anyTimes();
  }
  public MockCatalogBuilder featureType(
      final String name,
      String srs,
      ProjectionPolicy projPolicy,
      ReferencedEnvelope envelope,
      ReferencedEnvelope latLonEnvelope) {

    String ftId = newId();
    final DataStoreInfo ds = dataStores.peekLast();
    NamespaceInfo ns = namespaces.peekLast();

    final FeatureTypeInfo ft = createNiceMock(FeatureTypeInfo.class);
    featureTypes.add(ft);

    initResource(
        ft, FeatureTypeInfo.class, ftId, name, ds, ns, srs, projPolicy, envelope, latLonEnvelope);

    expect(ft.getNumDecimals()).andReturn(8);

    // setup the property file data
    File propDir = new File(dataDirRoot, ds.getName());
    propDir.mkdirs();

    String fileName = name + ".properties";
    try {
      IOUtils.copy(getClass().getResourceAsStream(fileName), new File(propDir, fileName));
    } catch (IOException e) {
      throw new RuntimeException(e);
    }

    try {
      expect(ft.getFeatureType())
          .andAnswer(
              new IAnswer<FeatureType>() {
                @Override
                public FeatureType answer() throws Throwable {
                  return ((DataStore) ds.getDataStore(null)).getSchema(name);
                }
              })
          .anyTimes();
      expect(ft.getFeatureSource(null, null))
          .andAnswer(
              (IAnswer)
                  new IAnswer<FeatureSource>() {
                    @Override
                    public FeatureSource answer() throws Throwable {
                      return ((DataStore) ds.getDataStore(null)).getFeatureSource(name);
                    }
                  })
          .anyTimes();
    } catch (IOException e) {
    }

    expect(catalog.getFeatureTypeByName(or(eq(name), eq(ns.getPrefix() + ":" + name))))
        .andReturn(ft)
        .anyTimes();

    expect(
            catalog.getFeatureTypeByName(
                or(eq(new NameImpl(ns.getPrefix(), name)), eq(new NameImpl(ns.getURI(), name)))))
        .andReturn(ft)
        .anyTimes();
    expect(catalog.getFeatureTypeByName(ns, name)).andReturn(ft).anyTimes();

    expect(catalog.getFeatureTypeByName(ns.getPrefix(), name)).andReturn(ft).anyTimes();
    // expect(catalog.getFeatureTypeByName(or(eq(ns.getPrefix()), eq(ns.getURI())), name))
    //    .andReturn(ft).anyTimes();

    expect(catalog.getFeatureTypeByStore(ds, name)).andReturn(ft).anyTimes();
    expect(catalog.getFeatureTypeByDataStore(ds, name)).andReturn(ft).anyTimes();

    ft.accept((CatalogVisitor) anyObject());
    expectLastCall()
        .andAnswer(
            new VisitAnswer() {
              @Override
              protected void doVisit(CatalogVisitor visitor) {
                visitor.visit(ft);
              }
            })
        .anyTimes();

    callback.onResource(name, ft, ds, this);
    replay(ft, createLayer(ft, name, ns));
    return this;
  }