Example #1
0
  private void save(File file, OsmDataLayer layer, boolean noBackup) {
    File tmpFile = null;
    try {
      // use a tmp file because if something errors out in the
      // process of writing the file, we might just end up with
      // a truncated file.  That can destroy lots of work.
      if (file.exists()) {
        tmpFile = new File(file.getPath() + "~");
        copy(file, tmpFile);
      }

      // create outputstream and wrap it with gzip or bzip, if necessary
      OutputStream out = getOutputStream(file);
      Writer writer = new OutputStreamWriter(out, "UTF-8");

      OsmWriter w =
          OsmWriterFactory.createOsmWriter(new PrintWriter(writer), false, layer.data.getVersion());
      layer.data.getReadLock().lock();
      try {
        w.writeLayer(layer);
        w.close();
      } finally {
        layer.data.getReadLock().unlock();
      }
      // FIXME - how to close?
      if (noBackup || !Main.pref.getBoolean("save.keepbackup", false)) {
        if (tmpFile != null) {
          tmpFile.delete();
        }
      }
      layer.onPostSaveToFile();
    } catch (IOException e) {
      e.printStackTrace();
      JOptionPane.showMessageDialog(
          Main.parent,
          tr("<html>An error occurred while saving.<br>Error is:<br>{0}</html>", e.getMessage()),
          tr("Error"),
          JOptionPane.ERROR_MESSAGE);

      try {
        // if the file save failed, then the tempfile will not
        // be deleted.  So, restore the backup if we made one.
        if (tmpFile != null && tmpFile.exists()) {
          copy(tmpFile, file);
        }
      } catch (IOException e2) {
        e2.printStackTrace();
        JOptionPane.showMessageDialog(
            Main.parent,
            tr(
                "<html>An error occurred while restoring backup file.<br>Error is:<br>{0}</html>",
                e2.getMessage()),
            tr("Error"),
            JOptionPane.ERROR_MESSAGE);
      }
    }
  }
  @BeforeClass
  public static void init() throws OsmTransferException {
    logger.info("initializing ...");
    JOSMFixture.createFunctionalTestFixture().init();

    // don't use atomic upload, the test API server can't cope with large diff uploads
    Main.pref.put("osm-server.atomic-upload", false);

    File dataSetCacheOutputFile =
        new File(
            System.getProperty("java.io.tmpdir"),
            MultiFetchServerObjectReaderTest.class.getName() + ".dataset");

    String p = System.getProperties().getProperty("useCachedDataset");
    if (p != null && Boolean.parseBoolean(p.trim().toLowerCase())) {
      logger.info(
          MessageFormat.format("property ''{0}'' set, using cached dataset", "useCachedDataset"));
      return;
    }

    logger.info(
        MessageFormat.format(
            "property ''{0}'' not set to true, creating test dataset on the server. property is ''{1}''",
            "useCachedDataset", p));

    // build and upload the test data set
    logger.info("creating test data set ....");
    testDataSet = buildTestDataSet();
    logger.info("uploading test data set ...");
    createDataSetOnServer(testDataSet);

    try (PrintWriter pw =
        new PrintWriter(
            new OutputStreamWriter(
                new FileOutputStream(dataSetCacheOutputFile), StandardCharsets.UTF_8))) {
      logger.info(
          MessageFormat.format(
              "caching test data set in ''{0}'' ...", dataSetCacheOutputFile.toString()));
      try (OsmWriter w = new OsmWriter(pw, false, testDataSet.getVersion())) {
        w.header();
        w.writeDataSources(testDataSet);
        w.writeContent(testDataSet);
        w.footer();
      }
    } catch (IOException e) {
      fail(
          MessageFormat.format(
              "failed to open file ''{0}'' for writing", dataSetCacheOutputFile.toString()));
    }
  }
  @BeforeClass
  public static void init() throws OsmTransferException {
    logger.info("initializing ...");
    testProperties = new Properties();

    // load properties
    //
    try {
      testProperties.load(
          MultiFetchServerObjectReaderTest.class.getResourceAsStream(
              "/test-functional-env.properties"));
    } catch (Exception e) {
      logger.log(
          Level.SEVERE,
          MessageFormat.format(
              "failed to load property file ''{0}''", "test-functional-env.properties"));
      fail(
          MessageFormat.format(
              "failed to load property file ''{0}''", "test-functional-env.properties"));
    }

    // check josm.home
    //
    String josmHome = testProperties.getProperty("josm.home");
    if (josmHome == null) {
      fail(MessageFormat.format("property ''{0}'' not set in test environment", "josm.home"));
    } else {
      File f = new File(josmHome);
      if (!f.exists() || !f.canRead()) {
        fail(
            MessageFormat.format(
                "property ''{0}'' points to ''{1}'' which is either not existing or not readable",
                "josm.home", josmHome));
      }
    }

    // check temp output dir
    //
    String tempOutputDir = testProperties.getProperty("test.functional.tempdir");
    if (tempOutputDir == null) {
      fail(
          MessageFormat.format(
              "property ''{0}'' not set in test environment", "test.functional.tempdir"));
    } else {
      File f = new File(tempOutputDir);
      if (!f.exists() || !f.isDirectory() || !f.canWrite()) {
        fail(
            MessageFormat.format(
                "property ''{0}'' points to ''{1}'' which is either not existing, not a directory, or not writeable",
                "test.functional.tempdir", tempOutputDir));
      }
    }

    // init preferences
    //
    System.setProperty("josm.home", josmHome);
    Main.pref.init(false);
    // don't use atomic upload, the test API server can't cope with large diff uploads
    //
    Main.pref.put("osm-server.atomic-upload", false);
    Main.proj = new Mercator();

    File dataSetCacheOutputFile =
        new File(tempOutputDir, MultiFetchServerObjectReaderTest.class.getName() + ".dataset");

    // make sure we don't upload to production
    //
    String url = OsmApi.getOsmApi().getBaseUrl().toLowerCase().trim();
    if (url.startsWith("http://www.openstreetmap.org")
        || url.startsWith("http://api.openstreetmap.org")) {
      fail(
          MessageFormat.format(
              "configured url ''{0}'' seems to be a productive url, aborting.", url));
    }

    String p = System.getProperties().getProperty("useCachedDataset");
    if (p != null && Boolean.parseBoolean(p.trim().toLowerCase())) {
      logger.info(
          MessageFormat.format("property ''{0}'' set, using cached dataset", "useCachedDataset"));
      return;
    }

    logger.info(
        MessageFormat.format(
            "property ''{0}'' not set to true, creating test dataset on the server. property is ''{1}''",
            "useCachedDataset", p));

    // build and upload the test data set
    //
    logger.info("creating test data set ....");
    testDataSet = buildTestDataSet();
    logger.info("uploading test data set ...");
    createDataSetOnServer(testDataSet);

    try {
      PrintWriter pw = new PrintWriter(new FileWriter(dataSetCacheOutputFile));
      logger.info(
          MessageFormat.format(
              "caching test data set in ''{0}'' ...", dataSetCacheOutputFile.toString()));
      OsmWriter w = new OsmWriter(pw, false, testDataSet.getVersion());
      w.header();
      w.writeDataSources(testDataSet);
      w.writeContent(testDataSet);
      w.footer();
      w.close();
      pw.close();
    } catch (IOException e) {
      fail(
          MessageFormat.format(
              "failed to open file ''{0}'' for writing", dataSetCacheOutputFile.toString()));
    }
  }