@Before
  public void setUp() throws Exception {
    // setup property file
    data = File.createTempFile("retype", "data", new File("./target"));
    data.delete();
    data.mkdir();
    final String fileName = MockData.PRIMITIVEGEOFEATURE.getLocalPart() + ".properties";
    URL properties = MockData.class.getResource(fileName);
    IOUtils.copy(properties.openStream(), new File(data, fileName));

    // build a feature type with less attributes, extra attributes, type changes
    SimpleFeatureTypeBuilder ftb = new SimpleFeatureTypeBuilder();
    ftb.add("description", String.class);
    ftb.add("pointProperty", MultiPoint.class); // poly -> multi-poly
    ftb.add("intProperty", Long.class); // int -> long
    ftb.add("dateTimeProperty", Date.class); // timestamp -> date
    ftb.add("newProperty", String.class); // new property
    ftb.setName(RENAMED); // rename type
    primitive = ftb.buildFeatureType();

    PropertyDataStore pds = new PropertyDataStore(data);
    rts =
        new RetypingDataStore(pds) {

          @Override
          protected SimpleFeatureType transformFeatureType(SimpleFeatureType original)
              throws IOException {
            if (original.getTypeName().equals(MockData.PRIMITIVEGEOFEATURE.getLocalPart()))
              return primitive;
            else return super.transformFeatureType(original);
          }

          @Override
          protected String transformFeatureTypeName(String originalName) {
            if (originalName.equals(MockData.PRIMITIVEGEOFEATURE.getLocalPart()))
              return primitive.getTypeName();
            else return super.transformFeatureTypeName(originalName);
          }
        };

    // build a filter that will retrieve one feature only
    FilterFactory ff = CommonFactoryFinder.getFilterFactory(null);
    fid = RENAMED + ".f001";
    fidFilter = ff.id(Collections.singleton(ff.featureId(fid)));
  }
 @After
 public void tearDown() throws Exception {
   IOUtils.delete(data);
 }
Ejemplo n.º 3
0
  protected void doTestRasterPlacemark(boolean doPlacemarks) throws Exception {
    // the style selects a single feature
    final String requestUrl =
        "wms/reflect?layers="
            + getLayerId(MockData.BASIC_POLYGONS)
            + "&styles=&format_options=mode:refresh;kmscore:0;kmplacemark:"
            + doPlacemarks
            + "&format="
            + KMZMapOutputFormat.MIME_TYPE;
    MockHttpServletResponse response = getAsServletResponse(requestUrl);
    assertEquals(KMZMapOutputFormat.MIME_TYPE, response.getContentType());

    ZipFile zipFile = null;
    try {
      // create the kmz
      File tempDir =
          org.geoserver.util.IOUtils.createRandomDirectory("./target", "kmplacemark", "test");
      tempDir.deleteOnExit();

      File zip = new File(tempDir, "kmz.zip");
      zip.deleteOnExit();

      FileOutputStream output = new FileOutputStream(zip);
      FileUtils.writeByteArrayToFile(zip, getBinary(response));

      output.flush();
      output.close();

      assertTrue(zip.exists());

      // unzip and test it
      zipFile = new ZipFile(zip);

      ZipEntry entry = zipFile.getEntry("wms.kml");
      assertNotNull(entry);
      assertNotNull(zipFile.getEntry("images/layers_0.png"));

      // unzip the wms.kml to file
      byte[] buffer = new byte[1024];
      int len;

      InputStream inStream = zipFile.getInputStream(entry);
      File temp = File.createTempFile("test_out", "kmz", tempDir);
      temp.deleteOnExit();
      BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(temp));

      while ((len = inStream.read(buffer)) >= 0) outStream.write(buffer, 0, len);
      inStream.close();
      outStream.close();

      // read in the wms.kml and check its contents
      Document document = dom(new BufferedInputStream(new FileInputStream(temp)));
      // print(document);

      assertEquals("kml", document.getDocumentElement().getNodeName());
      if (doPlacemarks) {
        assertEquals(
            getFeatureSource(MockData.BASIC_POLYGONS).getFeatures().size(),
            document.getElementsByTagName("Placemark").getLength());
        XMLAssert.assertXpathEvaluatesTo("3", "count(//kml:Placemark//kml:Point)", document);
      } else {
        assertEquals(0, document.getElementsByTagName("Placemark").getLength());
      }

    } finally {
      if (zipFile != null) {
        zipFile.close();
      }
    }
  }