public void testCoverageContents() throws Exception {
    final Long processId = issueProcessAndWaitForTermination();

    final String request = RESTLET_PATH + "/" + processId.longValue();

    final MockHttpServletResponse response = getAsServletResponse(request);
    assertEquals(Status.SUCCESS_OK, Status.valueOf(response.getStatus()));
    assertEquals(MediaType.APPLICATION_ZIP, MediaType.valueOf(response.getContentType()));

    final ByteArrayInputStream responseStream = getBinaryInputStream(response);
    File dataDirectoryRoot = super.getTestData().getDataDirectoryRoot();
    File file = new File(dataDirectoryRoot, "testCoverageContents.zip");
    super.getTestData().copyTo(responseStream, file.getName());

    ZipFile zipFile = new ZipFile(file);
    try {
      // TODO: change this expectation once we normalize the raster file name
      String rasterName = RASTER_LAYER.getPrefix() + ":" + RASTER_LAYER.getLocalPart() + ".tiff";
      ZipEntry nextEntry = zipFile.getEntry(rasterName);
      assertNotNull(nextEntry);
      InputStream coverageInputStream = zipFile.getInputStream(nextEntry);
      // Use a file, geotiffreader might not work well reading out of a plain input stream
      File covFile = new File(file.getParentFile(), "coverage.tiff");
      IOUtils.copy(coverageInputStream, covFile);
      GeoTiffReader geoTiffReader = new GeoTiffReader(covFile);
      GridCoverage2D coverage = geoTiffReader.read(null);
      CoordinateReferenceSystem crs = coverage.getCoordinateReferenceSystem();
      assertEquals(CRS.decode("EPSG:4326", true), crs);
    } finally {
      zipFile.close();
    }
  }
Пример #2
0
  /**
   * Copy the contents of fromDir into toDir (if the latter is missing it will be created)
   *
   * @param fromDir
   * @param toDir
   * @throws IOException
   */
  public static void deepCopy(File fromDir, File toDir) throws IOException {
    if (!fromDir.isDirectory() || !fromDir.exists())
      throw new IllegalArgumentException(
          "Invalid source directory " + "(it's either not a directory, or does not exist");
    if (toDir.exists() && toDir.isFile())
      throw new IllegalArgumentException(
          "Invalid destination directory, " + "it happens to be a file instead");

    // create destination if not available
    if (!toDir.exists()) if (!toDir.mkdir()) throw new IOException("Could not create " + toDir);

    File[] files = fromDir.listFiles();
    for (File file : files) {
      File destination = new File(toDir, file.getName());
      if (file.isDirectory()) deepCopy(file, destination);
      else copy(file, destination);
    }
  }
  /**
   * Check for the existence of the file, if the file exists do nothing.
   *
   * <p>If the file does not exist, check for a template file contained in the jar with the same
   * name, if found, use it.
   *
   * <p>If no template was found, use the default template
   *
   * @param fileName target location
   * @param namedRoot parent dir if fileName is relative
   * @param defaultResource the standard template
   * @throws IOException
   * @return the file to use
   */
  protected Resource checkORCreateJDBCPropertyFile(
      String fileName, Resource namedRoot, String defaultResource) throws IOException {

    Resource resource;
    fileName = fileName != null ? fileName : defaultResource;
    File file = new File(fileName);
    if (file.isAbsolute()) {
      resource = Files.asResource(file);
    } else {
      resource = namedRoot.get(fileName);
    }

    if (Resources.exists(resource)) {
      return resource; // we are happy
    }

    // try to find a template with the same name
    InputStream is = this.getClass().getResourceAsStream(fileName);
    if (is != null) IOUtils.copy(is, resource.out());
    else // use the default template
    FileUtils.copyURLToFile(getClass().getResource(defaultResource), file);

    return resource;
  }
  @Override
  public void afterPropertiesSet() throws Exception {
    File properties =
        new File(
            config
                .getConfiguration(EmbeddedBrokerConfiguration.EMBEDDED_BROKER_PROPERTIES_KEY)
                .toString());
    if (!properties.isAbsolute() && !properties.isFile()) {
      // try to resolve as absolute
      properties = new File(JMSConfiguration.getConfigPathDir(), properties.getPath());
      if (!properties.isFile()) {
        // copy the defaults
        IOUtils.copy(defaults.getFile(), properties);
      }
    }
    final Resource res = new FileSystemResource(properties);
    super.setLocation(res);

    // make sure the activemq.base is set to a valuable default
    final Properties props = new Properties();
    props.setProperty("activemq.base", (String) config.getConfiguration("CLUSTER_CONFIG_DIR"));
    props.setProperty("instanceName", (String) config.getConfiguration("instanceName"));
    setProperties(props);
  }
Пример #5
0
  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;
  }
Пример #6
0
  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;
  }
Пример #7
0
 /**
  * Copies some content to a file under the base of the data directory.
  *
  * <p>The <code>location</code> is considred to be a path relative to the data directory root.
  *
  * <p>Note that the resulting file will be deleted when {@link #tearDown()} is called.
  *
  * @param input The content to copy.
  * @param location A relative path
  */
 public void copyTo(InputStream input, String location) throws IOException {
   IOUtils.copy(input, new File(getDataDirectoryRoot(), location));
 }
Пример #8
0
 /**
  * Copies the provided file onto the specified destination file
  *
  * @param from
  * @param to
  * @throws IOException
  */
 public static void copy(File from, File to) throws IOException {
   copy(new FileInputStream(from), to);
 }