/**
   * Test that {@link CommandOptions#getLoopTime()} returns a random time between min loop time and
   * max-random-loop-time.
   */
  public void testGetLoopTime_maxrandomset() throws ConfigurationException {
    CommandOptions co = new CommandOptions();
    ArgsOptionParser p = new ArgsOptionParser(co);
    p.parse("--max-random-loop-time", "10", "--min-loop-time", "5");

    long loop = co.getLoopTime();
    assertTrue("Loop time less than min loop time " + loop, loop >= 5);
    assertTrue("Loop time too high: " + loop, loop <= 10);
  }
  @Override
  protected CommandOptions getExecutablePath() {
    // TODO Auto-generated method stub
    CommandOptions options = new CommandOptions();

    String logShipPath = properties.getProperty("log_shipping.path");

    try {
      File configFile = File.createTempFile("logshipping", ".conf");
      configFile.deleteOnExit();
      FileWriter writer = new FileWriter(configFile);

      writer.write("logfile='/tmp/logshipping.out';\n");
      writer.write("cluster name='" + properties.getProperty("clustername") + "';\n");
      writer.write("destination database='dbname=");
      String dbName = properties.getProperty("database." + logicalDb + ".dbname");
      writer.write(dbName);
      String host = properties.getProperty("database." + logicalDb + ".host");
      if (host != null) {
        writer.write(" host=");
        writer.write(host);
      }
      String user = properties.getProperty("database." + logicalDb + ".user.slony");
      if (user != null) {
        writer.write(" user="******"database." + logicalDb + ".user.slony");
      if (password != null) {
        writer.write(" password="******"database." + logicalDb + ".port");
      if (port != null) {
        writer.write(" port=");
        writer.write(port);
      }
      writer.write("';\n");
      writer.write("archive dir='" + spoolDirectory.getAbsolutePath() + "';\n");
      writer.close();

      options.commandOptions =
          new String[] {logShipPath, "-f", "-s", "3", configFile.getAbsolutePath()};

      return options;
    } catch (IOException e) {
      log.error("error writing archive file");
      return null;
    }
  }
  public static void setOptionVariableValues(
      ServiceCommand serviceCommand, Map<String, String> variableToValueMap) {

    CommandOptions commandOptions = serviceCommand.getOptions();

    if (commandOptions == null || commandOptions.getOptions() == null) {
      return;
    }

    String options = commandOptions.getOptions();

    // Parse all option values that start with '$'
    StringBuffer variableBuffer = null;

    StringBuffer resolvedOptions = new StringBuffer(options);

    int dollarSignIndex = -1;

    // Note that the resolvedOptions buffer MAY grow therefore length will
    // vary during each iteration
    for (int i = 0; i < resolvedOptions.length(); ) {
      boolean inserted = false;
      if ((resolvedOptions.charAt(i) == '$')
          && i + 1 < resolvedOptions.length()
          && (resolvedOptions.charAt(i + 1) == '{')) {
        // Start parsing the variable
        dollarSignIndex = i;

        // Advance by one more to skip '{'
        i++;
        variableBuffer = new StringBuffer();
      }
      // Flush the variable if an ending bracket or end of string is
      // encountered
      else if ((resolvedOptions.charAt(i) == '}') && variableBuffer != null) {

        // Look up the value
        if (variableBuffer.length() > 0) {
          String variable = variableBuffer.toString();
          String value = variableToValueMap.get(variable);
          // ending index should be the next index after the last
          // position where the value needs to be inserted, so in this
          // case the ending '}'
          // So if the end of the line is reached, the index should be
          // equal to the length of the options buffer.
          int endingIndex = i + 1;
          if (value != null && dollarSignIndex >= 0 && (endingIndex <= resolvedOptions.length())) {

            // delete the variable
            resolvedOptions.replace(dollarSignIndex, endingIndex, ""); // $NON-NLS-1$

            // insert the value
            resolvedOptions.insert(dollarSignIndex, value);

            // move the index past the inserted value
            i = dollarSignIndex + value.length();
            inserted = true;
          }
        }
        // Prepare for the next variable
        variableBuffer = null;
        dollarSignIndex = -1;
      } else if (variableBuffer != null) {
        variableBuffer.append(resolvedOptions.charAt(i));
      }

      // Advance to the next character unless a variable was replaced
      if (!inserted) {
        i++;
      }
    }
    CommandOptions resolvedOp = new CommandOptions();
    resolvedOp.setOptions(resolvedOptions.toString());
    serviceCommand.setOptions(resolvedOp);
  }
 /** Test that setting multiple --min-loop-time values results in the lowest value provided. */
 public void testGetLoopTime_least() throws ConfigurationException {
   CommandOptions co = new CommandOptions();
   ArgsOptionParser p = new ArgsOptionParser(co);
   p.parse("--min-loop-time", "0", "--min-loop-time", "5");
   assertEquals(0, co.getLoopTime());
 }