示例#1
0
  /**
   * Command line application entry point which parses CLI arguments and passes them into the
   * importer. Return codes are:
   *
   * <ul>
   *   <li>0 on success
   *   <li>1 on argument parsing failure
   *   <li>2 on exception during import
   * </ul>
   *
   * @param args Command line arguments.
   */
  public static void main(String[] args) throws Throwable {
    LongOpt[] longOptions =
        new LongOpt[] {
          new LongOpt("error-on", LongOpt.REQUIRED_ARGUMENT, null, 'e'),
          new LongOpt("feedback", LongOpt.REQUIRED_ARGUMENT, null, 'f'),
          new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h'),
          new LongOpt("no-recurse", LongOpt.OPTIONAL_ARGUMENT, null, 'r')
        };

    // First find our configuration file, if it has been provided.
    Getopt g = new Getopt(APP_NAME, args, "s:u:w:p:k:c:x", longOptions);
    int a;
    InputStream configFile = new ByteArrayInputStream(new byte[0]);
    while ((a = g.getopt()) != -1) {
      switch (a) {
        case 'c':
          {
            configFile = new FileInputStream(g.getOptarg());
            break;
          }
      }
    }
    // Second check for the configuration file on the CLASSPATH if we
    // haven't been given a configuration file on the command line.
    if (configFile instanceof ByteArrayInputStream) {
      InputStream fromClasspath =
          TestEngine.class.getClassLoader().getResourceAsStream("test_engine.ini");
      configFile = fromClasspath == null ? configFile : fromClasspath;
    }

    // Now parse our options.
    g = new Getopt(APP_NAME, args, "s:u:w:p:c:k:x", longOptions);
    TestEngineConfig config = new TestEngineConfig(configFile);
    while ((a = g.getopt()) != -1) {
      switch (a) {
        case 's':
          {
            config.setHostname(g.getOptarg());
            break;
          }
        case 'u':
          {
            config.setUsername(g.getOptarg());
            break;
          }
        case 'w':
          {
            config.setPassword(g.getOptarg());
            break;
          }
        case 'k':
          {
            config.setSessionKey(g.getOptarg());
            break;
          }
        case 'p':
          {
            config.setPort(Integer.parseInt(g.getOptarg()));
            break;
          }
        case 'x':
          {
            config.setPopulate(!config.getPopulate());
            break;
          }
        case 'r':
          {
            config.setRecurse(!config.getRecurse());
            break;
          }
        case 'e':
          {
            config.setErrorOn(g.getOptarg());
          }
        case 'f':
          {
            config.setFeedbackUrl(g.getOptarg());
            break;
          }
        case 'c':
          {
            // Ignore, we've dealt with this already.
            break;
          }
        default:
          {
            usage();
          }
      }
    }

    // Ensure that we have all of our required login arguments
    if (!config.validateLogin()) {
      usage();
    }

    // Ensure that we have a valid target path.
    String path = config.getTarget();
    if (args.length - g.getOptind() == 1) {
      path = args[g.getOptind()];
    } else if (path == null) {
      usage();
    }

    TestEngine engine = new TestEngine(config);
    engine.run(path);
    engine.exit();
  }