TcasesCommand { 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()); } Resource.Type outputFileType = firstNonNull( Resource.Type.of( withDefaultType( outputFile, defaultContentType)), defaultContentType); 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); outputFileType = Resource.Type.XML; } try { logger_.info( "Updating test definition file={}", outputFile); OutputStream output = transformer != null? transformer.getSource() : outputFile != null? new FileOutputStream( outputFile) : null; 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); } } } private  TcasesCommand(); static void main( String[] args); static void run( Options options); static String getProjectName( File inputDefFile); static String getVersion();  }
@Test public void run_0() throws Exception { File outDir = getResourceFile( "run-0-outDir"); File testDefFile = getResourceFile( "run-0-test-other.xml"); File inFile = getResourceFile( "run-0"); File genFile = new File( inFile.getParent(), inFile.getName() + "-Generators.xml"); File outFile = new File( outDir, testDefFile.getName()); FileUtils.deleteDirectory( outDir); testDefFile.delete(); genFile.delete(); String[] args = { "-n", "-o", outDir.getPath(), "-t", testDefFile.getPath(), inFile.getPath() }; TcasesCommand.run( new Options( args)); assertThat( "Test def created", outFile.exists(), is( true)); }
@Test public void run_3() throws Exception { File testDefFile = getResourceFile( "run-3-test-other.xml"); File genFile = getResourceFile( "run-3-gen-other.xml"); File inFile = getResourceFile( "run-3-Input.xml"); File outFile = testDefFile; String[] args = { "-c", "12345", "-g", genFile.getName(), "-t", testDefFile.getPath(), "run-3" }; Options options = new Options( args); options.setWorkingDir( inFile.getParentFile()); TcasesCommand.run( options); assertThat( "Test def created", outFile.exists(), is( true)); }
@Test public void run_4() throws Exception { File outDir = getResourceFile( "run-4-outDir"); File inFile = getResourceFile( "run-4.xml"); File outFile = new File( outDir, "run-4-Test.xml"); FileUtils.deleteDirectory( outDir); String[] args = { "-n", "-o", outDir.getPath(), "-r", "12345", inFile.getPath() }; TcasesCommand.run( new Options( args)); assertThat( "Test def created", outFile.exists(), is( true)); }
@Test public void run_6() throws Exception { File outDir = getResourceFile( "run-6-outDir"); File testDefFile = getResourceFile( "run-6-test-other.xml"); File inFile = getResourceFile( "run-6-Input.xml"); outDir.delete(); outDir.createNewFile(); String[] args = { "-o", outDir.getPath(), "-n", "-t", testDefFile.getName(), inFile.getPath() }; expectFailure( RuntimeException.class) .when( () -> TcasesCommand.run( new Options( args))); }
@Test public void run_7() throws Exception { File testDefFile = getResourceFile( "run-7-test-other.xml"); File genFile = getResourceFile( "run-7-gen-other.xml"); File inFile = getResourceFile( "run-7-Input.xml"); String[] args = { "-g", genFile.getName(), "-t", testDefFile.getPath(), "run-7" }; Options options = new Options( args); options.setWorkingDir( inFile.getParentFile()); expectFailure( RuntimeException.class) .when( () -> TcasesCommand.run( new Options( args))); }
@Test public void run_8() throws Exception { File outDir = getResourceFile( "run-8-outDir"); File inFile = getResourceFile( "run-8.xml"); FileUtils.deleteDirectory( outDir); String[] args = { "-n", "-o", outDir.getPath(), inFile.getPath() }; expectFailure( RuntimeException.class) .when( () -> TcasesCommand.run( new Options( args))); }
@Test public void run_9() throws Exception { File testDefFile = getResourceFile( "run-9-test-other.xml"); File genFile = getResourceFile( "run-9-gen-other.xml"); File inFile = getResourceFile( "run-9-Input.xml"); String[] args = { "-c", "-g", genFile.getName(), "-t", testDefFile.getPath(), "run-3" }; expectFailure( RuntimeException.class) .when( () -> { Options options = new Options( args); options.setWorkingDir( inFile.getParentFile()); TcasesCommand.run( options); }); }
@Test public void run_10() throws Exception { File outDir = getResourceFile( "run-10-outDir"); File inFile = getResourceFile( "run-10.xml"); FileUtils.deleteDirectory( outDir); String[] args = { "-n", "-o", outDir.getPath(), "-r", "NaN", inFile.getPath() }; expectFailure( RuntimeException.class) .when( () -> TcasesCommand.run( new Options( args))); }
@Test public void run_Transform_0() throws Exception { File inFile = getResourceFile( "run-transform-0"); File outDir = getResourceFile( "run-transform-0-outDir"); File outFile = new File( "run-transform-0.java"); File outFilePath = new File( outDir, outFile.getName()); File testDefFile = getResourceFile( "run-transform-0-test.xml"); String[] args = { "-J", "-o", outDir.getPath(), "-f", outFile.getName(), "-t", testDefFile.getPath(), inFile.getPath() }; TcasesCommand.run( new Options( args)); assertThat( "Test def created", outFilePath.exists(), is( true)); }
@Test public void run_Transform_2() throws Exception { File inFile = getResourceFile( "run-transform-2"); File outDir = getResourceFile( "run-transform-2-outDir"); File outFile = getResourceFile( "run-transform-2.java"); File outFilePath = new File( outDir, outFile.getName()); File testDefFile = getResourceFile( "run-transform-2-test.xml"); String[] args = { "-J", "-p", "throws=yes", "-o", outDir.getPath(), "-f", outFile.getPath(), "-t", testDefFile.getPath(), "-p", "class=Run_Transform_2", inFile.getPath() }; TcasesCommand.run( new Options( args)); assertThat( "Test def created", outFilePath.exists(), is( true)); }
@Test public void run_Transform_3() throws Exception { File inFile = getResourceFile( "run-transform-3.xml"); File transformFile = getResourceFile( "run-transform-3.xsl"); File outFilePath = getResourceFile( "run-transform-3.java"); String[] args = { "-f", outFilePath.getName(), "-x", transformFile.getName(), "-p", "system=", inFile.getPath() }; TcasesCommand.run( new Options( args)); assertThat( "Test def created", outFilePath.exists(), is( true)); }
@Test public void run_Transform_5() throws Exception { File inFile = getResourceFile( "run-transform-5"); File transformFile = getResourceFile( "run-transform-5.xsl"); String[] args = { "-x", transformFile.getPath(), inFile.getPath() }; expectFailure( RuntimeException.class) .when( () -> TcasesCommand.run( new Options( args))); }
@Test public void run_Transform_7() throws Exception { File inFile = getResourceFile( "run-transform-7"); File testDefFile = getResourceFile( "run-transform-7-Test.xml"); File outFilePath = testDefFile; assertThat( "Default output file exists", outFilePath.exists(), is( true)); String[] args = { "-x", "-p", "throws=yes", "-p", "class=Run_Transform_7", inFile.getPath() }; expectFailure( RuntimeException.class) .when( () -> TcasesCommand.run( new Options( args))); }
@Test public void run_Transform_9() throws Exception { File inFile = getResourceFile( "run-transform-7"); File outFilePath = getResourceFile( "runtransform7Test.java"); String[] args = { "-J", "-p", "throws=yes", "-p", "class=Run_Transform_7", inFile.getPath() }; TcasesCommand.run( new Options( args)); assertThat( "Test def created", outFilePath.exists(), is( true)); }
@Test public void runWithContentType_0() throws Exception { File outFile = getResourceFile( "runWithContentType-0-Out.json"); File genFile = getResourceFile( "runWithContentType-0-Generators"); File inFile = getResourceFile( "runWithContentType-0"); outFile.delete(); String[] args = { "-f", outFile.getPath(), "-g", genFile.getPath(), inFile.getPath() }; TcasesCommand.run( new Options( args)); assertThat( "Test def created", outFile.exists(), is( true)); assertThat( "Test def content type", testResources_.readJson( outFile.getName()), is( notNullValue())); }
@Test public void runWithContentType_3() throws Exception { File outFile = getResourceFile( "runWithContentType-3-Out"); File inFile = getResourceFile( "runWithContentType-3"); File genFile = getResourceFile( "runWithContentType-3-Generators.xml"); genFile.delete(); String[] args = { "-r", "123456789", "-t", outFile.getName(), "-T", "xml", inFile.getPath() }; TcasesCommand.run( new Options( args)); assertThat( "Test def created", outFile.exists(), is( true)); assertThat( "Test def content type", testResources_.read( outFile.getName()), is( notNullValue())); assertThat( "Generator def created", genFile.exists(), is( true)); assertThat( "Generator def content type", generatorResources_.read( genFile.getName()), is( notNullValue())); }
@Test public void runWithContentType_4() throws Exception { File outFile = getResourceFile( "runWithContentType-4-Out"); File inFile = getResourceFile( "runWithContentType-4"); File genFile = getResourceFile( "runWithContentType-4-GenDefs.json"); outFile.delete(); String[] args = { "-f", outFile.getName(), "-g", genFile.getName(), inFile.getPath() }; TcasesCommand.run( new Options( args)); assertThat( "Test def created", outFile.exists(), is( true)); assertThat( "Test def content type", testResources_.read( outFile.getName()), is( notNullValue())); }
@Test public void runWithContentType_5() throws Exception { File outFile = getResourceFile( "runWithContentType-5-Out.json"); File inFile = getResourceFile( "runWithContentType-5"); File genFile = getResourceFile( "runWithContentType-5-Generators.json"); outFile.delete(); genFile.delete(); String[] args = { "-n", "-c", "2", "-t", outFile.getPath(), "-T", "json", inFile.getPath() }; TcasesCommand.run( new Options( args)); assertThat( "Test def created", outFile.exists(), is( true)); assertThat( "Test def content type", testResources_.readJson( outFile.getName()), is( notNullValue())); assertThat( "Generator def created", genFile.exists(), is( true)); assertThat( "Generator def content type", generatorResources_.readJson( genFile.getName()), is( notNullValue())); }
@Test public void runWithContentType_Transformed() throws Exception { File inFile = getResourceFile( "runWithContentType-0"); File outFile = new File( inFile.getParent(), "runWithContentType0Test.java"); outFile.delete(); String[] args = { "-J", "-T", "json", inFile.getPath() }; TcasesCommand.run( new Options( args)); assertThat( "Test def created", outFile.exists(), is( true)); }