public static void run( Options options) throws Exception
    {
    if( options.showVersion())
      {
      System.out.println( getVersion());
      return;
      }
    logger_.info( "{}", getVersion());

    // Identify the system input definition file.
    File inputDefOption = options.getInputDef();
    if( inputDefOption != null && !inputDefOption.isAbsolute())
      {
      inputDefOption = new File( options.getWorkingDir(), inputDefOption.getPath());
      }

    File inputDefFile = inputDefOption;
    if( inputDefFile != null
        && !inputDefFile.exists()
        && !(inputDefFile = new File( inputDefOption.getPath() + "-Input.xml")).exists()
        && !(inputDefFile = new File( inputDefOption.getPath() + ".xml")).exists()
        && !(inputDefFile = new File( inputDefOption.getPath() + "-Input.json")).exists()
        && !(inputDefFile = new File( inputDefOption.getPath() + ".json")).exists())
        {
        throw new RuntimeException( "Can't locate input file for path=" + options.getInputDef());
        }

    File inputDir =
      inputDefFile==null
      ? options.getWorkingDir()
      : inputDefFile.getParentFile();

    Resource.Type defaultContentType =
      firstNonNull
      ( options.getContentType(),
        Resource.Type.of( inputDefFile),
        Resource.Type.XML);

    // Read the system input definition.
    logger_.info( "Reading system input definition={}", inputDefFile);
    SystemInputDef inputDef = null;
    try( SystemInputResource reader = withDefaultType( SystemInputResource.of( inputDefFile), defaultContentType))
      {
      inputDef = reader.getSystemInputDef();
      }
    catch( Exception e)
      {
      throw new RuntimeException( "Can't read input definition file=" + inputDefFile, e);
      }

    // Test definition defined?
    String projectName = getProjectName( inputDefFile);
    File testDefFile = options.getTestDef();
    if( testDefFile == null && inputDefFile != null)
      {
      // No, derive default from input file.
      testDefFile = withDefaultType( new File( projectName + "-Test"), defaultContentType);
      }

    // Identify base test definition file.
    File baseDefFile = testDefFile;
    if( baseDefFile != null && !baseDefFile.isAbsolute())
      {
      // For relative path, read base test definitions from input directory.
      baseDefFile = new File( inputDir, baseDefFile.getPath());
      }

    // Output file defined?
    File outputDir = options.getOutDir();
    File outputFile = options.getOutFile();
    if ( outputFile == null)
      {
      // No, defaults to...
      outputFile =
        // ... JUnit test class file, if generating JUnit
        options.getTransformType() == Options.TransformType.JUNIT && inputDefFile != null
        ? new File( projectName.replaceAll( "\\W+", "") + "Test.java") :

        // ... HTML file, if generating HTML
        options.getTransformType() == Options.TransformType.HTML && inputDefFile != null
        ? new File( projectName + "-Test.htm") :

        // ... else test definition file.
        testDefFile;
      }
    if( outputFile != null)
      {
      // Ensure output directory exists.
      if( outputDir == null)
        {
        outputDir =
          outputFile.isAbsolute()
          ? outputFile.getParentFile()
          : inputDir;
        }
      if( !outputDir.exists() && !outputDir.mkdirs())
        {
        throw new RuntimeException( "Can't create output directory=" + outputDir);
        }

      outputFile =
        new File
        ( outputDir,
          outputFile.isAbsolute()? outputFile.getName() : outputFile.getPath());
      }

    SystemTestDef baseDef = null;
    if( options.isExtended() && baseDefFile != null && baseDefFile.exists())
      {
      // Read the previous base test definitions.
      logger_.info( "Reading base test definition={}", baseDefFile);
      try( SystemTestResource reader = withDefaultType( SystemTestResource.of( baseDefFile), defaultContentType))
        {
        baseDef = reader.getSystemTestDef();
        }
      catch( Exception e)
        {
        throw new RuntimeException( "Can't read test definition file=" + baseDefFile, e);
        }
      }

    // Identify the generator definition file.
    File genDefFile = options.getGenDef();
    File genDefDefault = null;
    if( genDefFile != null)
      {
      if( !genDefFile.isAbsolute())
        {
        genDefFile = new File( inputDir, genDefFile.getPath());
        }
      }
    else if( inputDefFile != null)
      {
      genDefDefault = withDefaultType( new File( inputDir, projectName + "-Generators"), defaultContentType);
      if( genDefDefault.exists())
        {
        genDefFile = genDefDefault;
        }
      }

    // Generator definitions specified?
    IGeneratorSet genDef = null;
    if( genDefFile != null)
      {
      // Yes, read generator definitions.
      logger_.info( "Reading generator definition={}", genDefFile);
      try( GeneratorSetResource reader = withDefaultType( GeneratorSetResource.of( genDefFile), defaultContentType))
        {
        genDef = reader.getGeneratorSet();
        }
      catch( Exception e)
        {
        throw new RuntimeException( "Can't read generator definition file=" + genDefFile, e);
        }
      }
    else
      {
      // No, use default TupleGenerator.
      genDef = GeneratorSet.basicGenerator();
      }

    // Generate new test definitions.
    SystemTestDef testDef = Tcases.getTests( inputDef, genDef, baseDef, options.getGeneratorOptions());

    // Identify test definition transformations.
    AbstractFilter transformer = null;
    File transformDefFile = options.getTransformDef();
    if( transformDefFile != null)
      {
      if( !transformDefFile.isAbsolute())
        {
        transformDefFile = new File( inputDir, transformDefFile.getPath());
        }
      transformer = new TransformFilter( transformDefFile, options.getTransformParams());
      }
    else if( options.getTransformType() == Options.TransformType.JUNIT)
      {
      transformer = new TestDefToJUnitFilter( options.getTransformParams());
      }
    else if( options.getTransformType() == Options.TransformType.HTML)
      {
      transformer = new TestDefToHtmlFilter();
      }

    if( transformer != null)
      {
      if( outputFile != null && outputFile.equals( baseDefFile) && baseDefFile.exists())
        {
        throw new RuntimeException( "Transformed output will overwrite test definition file=" + baseDefFile);
        }
      transformer.setTarget( outputFile);
      }

    // Write new test definitions.
    try
      {
      logger_.info( "Updating test definition file={}", outputFile);

      OutputStream output =
        // Transformed output?
        transformer != null?
        transformer.getSource() :

        // Output file?
        outputFile != null?
        new FileOutputStream( outputFile) :

        // Standard output?
        null;

      Resource.Type outputFileType = firstNonNull( Resource.Type.of( withDefaultType( outputFile, defaultContentType)), defaultContentType);
      if( outputFileType == Resource.Type.JSON)
        {
        TcasesJson.writeTests( testDef, output);
        }
      else
        {
        TcasesIO.writeTests( testDef, output);
        }
      }
    catch( Exception e)
      {
      throw new RuntimeException( "Can't write test definition file=" + outputFile, e);
      }

    // Write any updates to generator definitions.
    File genUpdateFile =
      genDefFile != null?
      genDefFile :

      genDefDefault != null?
      genDefDefault :

      null;

    if( genUpdateFile != null && (options.getRandomSeed() != null || options.getDefaultTupleSize() != null))
      {
      logger_.info( "Updating generator definition={}", genUpdateFile);
      try
        {
        Resource.Type genUpdateType = firstNonNull( Resource.Type.of( withDefaultType( genUpdateFile, defaultContentType)), defaultContentType);
        if( genUpdateType == Resource.Type.JSON)
          {
          TcasesJson.writeGenerators( genDef, new FileOutputStream( genUpdateFile));
          }
        else
          {
          TcasesIO.writeGenerators( genDef, new FileOutputStream( genUpdateFile));
          }
        }
      catch( Exception e)
        {
        throw new RuntimeException( "Can't write generator definition file=" + genUpdateFile, e);
        }
      }
    }
public static void run ( Options options ) throws Exception { if ( options . showVersion ( ) ) { System . out . println ( getVersion ( ) ) ; return ; } logger_ . info ( "{}" , getVersion ( ) ) ; File inputDefOption = options . getInputDef ( ) ; if ( inputDefOption != null && ! inputDefOption . isAbsolute ( ) ) { inputDefOption = new File ( options . getWorkingDir ( ) , inputDefOption . getPath ( ) ) ; } File inputDefFile = inputDefOption ; if ( inputDefFile != null && ! inputDefFile . exists ( ) && ! ( inputDefFile = new File ( inputDefOption . getPath ( ) + "-Input.xml" ) ) . exists ( ) && ! ( inputDefFile = new File ( inputDefOption . getPath ( ) + ".xml" ) ) . exists ( ) && ! ( inputDefFile = new File ( inputDefOption . getPath ( ) + "-Input.json" ) ) . exists ( ) && ! ( inputDefFile = new File ( inputDefOption . getPath ( ) + ".json" ) ) . exists ( ) ) { throw new RuntimeException ( "Can't locate input file for path=" + options . getInputDef ( ) ) ; } File inputDir = inputDefFile == null ? options . getWorkingDir ( ) : inputDefFile . getParentFile ( ) ; Resource . Type defaultContentType = firstNonNull ( options . getContentType ( ) , Resource . Type . of ( inputDefFile ) , Resource . Type . XML ) ; logger_ . info ( "Reading system input definition={}" , inputDefFile ) ; SystemInputDef inputDef = null ; try ( SystemInputResource reader = withDefaultType ( SystemInputResource . of ( inputDefFile ) , defaultContentType ) ) { inputDef = reader . getSystemInputDef ( ) ; } catch ( Exception e ) { throw new RuntimeException ( "Can't read input definition file=" + inputDefFile , e ) ; } String projectName = getProjectName ( inputDefFile ) ; File testDefFile = options . getTestDef ( ) ; if ( testDefFile == null && inputDefFile != null ) { testDefFile = withDefaultType ( new File ( projectName + "-Test" ) , defaultContentType ) ; } File baseDefFile = testDefFile ; if ( baseDefFile != null && ! baseDefFile . isAbsolute ( ) ) { baseDefFile = new File ( inputDir , baseDefFile . getPath ( ) ) ; } File outputDir = options . getOutDir ( ) ; File outputFile = options . getOutFile ( ) ; if ( outputFile == null ) { outputFile = options . getTransformType ( ) == Options . TransformType . JUNIT && inputDefFile != null ? new File ( projectName . replaceAll ( "\\W+" , "" ) + "Test.java" ) : options . getTransformType ( ) == Options . TransformType . HTML && inputDefFile != null ? new File ( projectName + "-Test.htm" ) : testDefFile ; } if ( outputFile != null ) { if ( outputDir == null ) { outputDir = outputFile . isAbsolute ( ) ? outputFile . getParentFile ( ) : inputDir ; } if ( ! outputDir . exists ( ) && ! outputDir . mkdirs ( ) ) { throw new RuntimeException ( "Can't create output directory=" + outputDir ) ; } outputFile = new File ( outputDir , outputFile . isAbsolute ( ) ? outputFile . getName ( ) : outputFile . getPath ( ) ) ; } SystemTestDef baseDef = null ; if ( options . isExtended ( ) && baseDefFile != null && baseDefFile . exists ( ) ) { logger_ . info ( "Reading base test definition={}" , baseDefFile ) ; try ( SystemTestResource reader = withDefaultType ( SystemTestResource . of ( baseDefFile ) , defaultContentType ) ) { baseDef = reader . getSystemTestDef ( ) ; } catch ( Exception e ) { throw new RuntimeException ( "Can't read test definition file=" + baseDefFile , e ) ; } } File genDefFile = options . getGenDef ( ) ; File genDefDefault = null ; if ( genDefFile != null ) { if ( ! genDefFile . isAbsolute ( ) ) { genDefFile = new File ( inputDir , genDefFile . getPath ( ) ) ; } } else if ( inputDefFile != null ) { genDefDefault = withDefaultType ( new File ( inputDir , projectName + "-Generators" ) , defaultContentType ) ; if ( genDefDefault . exists ( ) ) { genDefFile = genDefDefault ; } } IGeneratorSet genDef = null ; if ( genDefFile != null ) { logger_ . info ( "Reading generator definition={}" , genDefFile ) ; try ( GeneratorSetResource reader = withDefaultType ( GeneratorSetResource . of ( genDefFile ) , defaultContentType ) ) { genDef = reader . getGeneratorSet ( ) ; } catch ( Exception e ) { throw new RuntimeException ( "Can't read generator definition file=" + genDefFile , e ) ; } } else { genDef = GeneratorSet . basicGenerator ( ) ; } SystemTestDef testDef = Tcases . getTests ( inputDef , genDef , baseDef , options . getGeneratorOptions ( ) ) ; AbstractFilter transformer = null ; File transformDefFile = options . getTransformDef ( ) ; if ( transformDefFile != null ) { if ( ! transformDefFile . isAbsolute ( ) ) { transformDefFile = new File ( inputDir , transformDefFile . getPath ( ) ) ; } transformer = new TransformFilter ( transformDefFile , options . getTransformParams ( ) ) ; } else if ( options . getTransformType ( ) == Options . TransformType . JUNIT ) { transformer = new TestDefToJUnitFilter ( options . getTransformParams ( ) ) ; } else if ( options . getTransformType ( ) == Options . TransformType . HTML ) { transformer = new TestDefToHtmlFilter ( ) ; } if ( transformer != null ) { if ( outputFile != null && outputFile . equals ( baseDefFile ) && baseDefFile . exists ( ) ) { throw new RuntimeException ( "Transformed output will overwrite test definition file=" + baseDefFile ) ; } transformer . setTarget ( outputFile ) ; } try { logger_ . info ( "Updating test definition file={}" , outputFile ) ; OutputStream output = transformer != null ? transformer . getSource ( ) : outputFile != null ? new FileOutputStream ( outputFile ) : null ; Resource . Type outputFileType = firstNonNull ( Resource . Type . of ( withDefaultType ( outputFile , defaultContentType ) ) , defaultContentType ) ; if ( outputFileType == Resource . Type . JSON ) { TcasesJson . writeTests ( testDef , output ) ; } else { TcasesIO . writeTests ( testDef , output ) ; } } catch ( Exception e ) { throw new RuntimeException ( "Can't write test definition file=" + outputFile , e ) ; } File genUpdateFile = genDefFile != null ? genDefFile : genDefDefault != null ? genDefDefault : null ; if ( genUpdateFile != null && ( options . getRandomSeed ( ) != null || options . getDefaultTupleSize ( ) != null ) ) { logger_ . info ( "Updating generator definition={}" , genUpdateFile ) ; try { Resource . Type genUpdateType = firstNonNull ( Resource . Type . of ( withDefaultType ( genUpdateFile , defaultContentType ) ) , defaultContentType ) ; if ( genUpdateType == Resource . Type . JSON ) { TcasesJson . writeGenerators ( genDef , new FileOutputStream ( genUpdateFile ) ) ; } else { TcasesIO . writeGenerators ( genDef , new FileOutputStream ( genUpdateFile ) ) ; } } catch ( Exception e ) { throw new RuntimeException ( "Can't write generator definition file=" + genUpdateFile , e ) ; } } }
Generates a set of {@link TestCase test cases} from a {@link SystemInputDef system input definition}
using the given {@link Options command line options}.