ReducerCommand extends Reducer { public void run( Options options) throws Exception { File inputDefOption = options.getInputDef(); File inputDefFile; if( !(inputDefFile = inputDefOption).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=" + inputDefOption); }; Resource.Type defaultContentType = firstNonNull ( options.getContentType(), Resource.Type.of( inputDefFile), Resource.Type.XML); File inputDir = inputDefFile.getParentFile(); String project = TcasesCommand.getProjectName( inputDefFile); 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); } File baseDefFile = options.getTestDef(); if( baseDefFile != null && !baseDefFile.isAbsolute()) { baseDefFile = new File( inputDir, baseDefFile.getPath()); } SystemTestDef baseDef = null; if( baseDefFile != null) { 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(); if( genDefFile == null) { genDefFile = withDefaultType( new File( inputDir, project + "-Generators"), defaultContentType); } else if( !genDefFile.isAbsolute()) { genDefFile = new File( inputDir, genDefFile.getPath()); } GeneratorSet genDef = null; if( genDefFile.exists()) { logger_.info( "Reading generator definition={}", genDefFile); try( GeneratorSetResource reader = withDefaultType( GeneratorSetResource.of( genDefFile), defaultContentType)) { genDef = (GeneratorSet) reader.getGeneratorSet(); } catch( Exception e) { throw new RuntimeException( "Can't read generator definition file=" + genDefFile, e); } } else { GeneratorSet genSet = new GeneratorSet(); genSet.addGenerator( GeneratorSet.ALL, new TupleGenerator()); genDef = genSet; } Optional<GeneratorSet> genDefNew = reduce( inputDef, genDef, baseDef, options.getReducerOptions()); if( !genDefNew.isPresent()) { logger_.info( "[{}] Generator definition not changed", project); } else { try { Resource.Type genDefType = firstNonNull( Resource.Type.of( withDefaultType( genDefFile, defaultContentType)), defaultContentType); if( genDefType == Resource.Type.JSON) { TcasesJson.writeGenerators( genDefNew.get(), new FileOutputStream( genDefFile)); } else { TcasesIO.writeGenerators( genDefNew.get(), new FileOutputStream( genDefFile)); } } catch( Exception e) { throw new RuntimeException( "Can't write generator definition file=" + genDefFile, e); } } } ReducerCommand(); static void main( String[] args); void run( Options options);  }
@Test public void whenFunctionDefined() throws Exception { File inFile = getResourceFile( "Reducer-whenFunctionDefined-Input.xml"); File genFile = getResourceFile( "Reducer-whenFunctionDefined-Generators.xml"); String function1 = "find-1"; String function2 = "find-2"; String[] args = { "-f", function2, "-g", genFile.getName(), "-r", "0.5", "-s", "8", inFile.getPath() }; ReducerCommand reducer = new ReducerCommand(); reducer.run( new Options( args)); IGeneratorSet generators = generatorResources_.read( genFile); TupleGenerator gen = getDefaultTupleGenerator( generators); assertThat( "Default, generator defined", gen != null, is( false)); TupleGenerator gen1 = getTupleGenerator( generators, function1); assertThat( function1 + ", generator defined", gen1 != null, is( true)); assertThat( function1 + ", seed defined", gen1.getRandomSeed() != null, is( false)); TupleGenerator gen2 = getTupleGenerator( generators, function2); assertThat( function2 + ", generator defined", gen2 != null, is( true)); assertThat( function2 + ", seed defined", gen2.getRandomSeed() != null, is( true)); }
@Test public void whenMatchesProjectXml() throws Exception { File inFile = getResourceFile( "Reducer-whenMatchesProjectXml.xml"); File genFile = getResourceFile( "Reducer-whenMatchesProjectXml-Generators.xml"); String[] args = { new File( inFile.getParentFile(), FilenameUtils.getBaseName( inFile.getName())).getPath() }; ReducerCommand reducer = new ReducerCommand(); reducer.run( new Options( args)); IGeneratorSet generators = generatorResources_.read( genFile); TupleGenerator gen = getDefaultTupleGenerator( generators); assertThat( "Default, generator defined", gen != null, is( true)); assertThat( "Default, seed defined", gen.getRandomSeed() != null, is( false)); String function1 = "find-1"; String function2 = "find-2"; TupleGenerator gen1 = getTupleGenerator( generators, function1); assertThat( function1 + ", generator defined", gen1 != null, is( true)); assertThat( function1 + ", seed defined", gen1.getRandomSeed() != null, is( true)); TupleGenerator gen2 = getTupleGenerator( generators, function2); assertThat( function2 + ", generator defined", gen2 != null, is( true)); assertThat( function2 + ", seed defined", gen2.getRandomSeed() != null, is( true)); }
@Test public void whenGeneratorsNew() throws Exception { File inFile = getResourceFile( "Reducer-whenGeneratorsNew-Input.xml"); String genFileName = "Reducer-Generators-New.xml"; String[] args = { "-g", genFileName, "-r", "-0.5", new File( inFile.getParentFile(), "Reducer-whenGeneratorsNew").getPath() }; ReducerCommand reducer = new ReducerCommand(); reducer.run( new Options( args)); File genFile = new File( inFile.getParentFile(), genFileName); IGeneratorSet generators = generatorResources_.read( genFile); TupleGenerator gen = getDefaultTupleGenerator( generators); assertThat( "Default, generator defined", gen != null, is( true)); assertThat( "Default, seed defined", gen.getRandomSeed() != null, is( false)); String function1 = "find-1"; String function2 = "find-2"; TupleGenerator gen1 = getTupleGenerator( generators, function1); assertThat( function1 + ", generator defined", gen1 != null, is( true)); assertThat( function1 + ", seed defined", gen1.getRandomSeed() != null, is( true)); TupleGenerator gen2 = getTupleGenerator( generators, function2); assertThat( function2 + ", generator defined", gen2 != null, is( true)); assertThat( function2 + ", seed defined", gen2.getRandomSeed() != null, is( true)); }
@Test public void whenTestDefNotExists() throws Exception { File inFile = getResourceFile( "Reducer-whenFunctionDefined-Input.xml"); File genFile = getResourceFile( "Reducer-whenFunctionDefined-Generators.xml"); File testFile = new File( inFile.getParent(), "Undefined"); String function1 = "find-1"; String[] args = { "-f", function1, "-g", genFile.getName(), "-t", testFile.getPath(), inFile.getPath() }; expectFailure( RuntimeException.class) .when( () -> new ReducerCommand().run( new Options( args))) .then( failure -> assertThat( "Expected failure", failure.getMessage(), is( "Can't read test definition file=" + testFile))); }
@Test public void whenNoFunctionGenerator() throws Exception { File inFile = getResourceFile( "Reducer-whenNoFunctionGenerator-Input.xml"); String function1 = "find-1"; String function2 = "find-2"; String[] args = { "-f", function1, "-r", "0.5", "-s", "8", new File( inFile.getParentFile(), "Reducer-whenNoFunctionGenerator-Input").getPath() }; ReducerCommand reducer = new ReducerCommand(); reducer.run( new Options( args)); File genFile = new File( inFile.getParentFile(), "Reducer-whenNoFunctionGenerator-Generators.xml"); IGeneratorSet generators = generatorResources_.read( genFile); TupleGenerator gen = getDefaultTupleGenerator( generators); assertThat( "Default, generator defined", gen != null, is( true)); assertThat( "Default, seed defined", gen.getRandomSeed() != null, is( false)); TupleGenerator gen1 = getTupleGenerator( generators, function1); assertThat( function1 + ", generator defined", gen1 != null, is( true)); assertThat( function1 + ", seed defined", gen1.getRandomSeed() != null, is( true)); TupleGenerator gen2 = getTupleGenerator( generators, function2); assertThat( function2 + ", generator defined", gen2 != null, is( false)); }
@Test public void whenResampleFactorInvalid() throws Exception { File inFile = getResourceFile( "Reducer-whenGeneratorsNew-Input.xml"); String genFileName = "Reducer-Generators-New.xml"; String[] args = { "-g", genFileName, "-r", "?", new File( inFile.getParentFile(), "Reducer-whenGeneratorsNew").getPath() }; expectFailure( RuntimeException.class) .when( () -> new ReducerCommand().run( new Options( args))) .then( failure -> assertThat( "Expected failure", failure.getCause().getMessage(), is( "Invalid resample factor"))); }
@Test public void whenResampleFactorBelowMin() throws Exception { File inFile = getResourceFile( "Reducer-whenGeneratorsNew-Input.xml"); String genFileName = "Reducer-Generators-New.xml"; String[] args = { "-g", genFileName, "-r", "-1.1", new File( inFile.getParentFile(), "Reducer-whenGeneratorsNew").getPath() }; expectFailure( RuntimeException.class) .when( () -> new ReducerCommand().run( new Options( args))) .then( failure -> { assertThat( "Expected failure", failure.getCause().getMessage(), is( "Invalid resample factor")); Throwable cause = failure.getCause().getCause(); assertThat( "Expected cause", cause == null? null : cause.getClass(), equalTo( IllegalArgumentException.class)); assertThat( "Expected cause", cause.getMessage(), is( "Resample factor must be >= -1.0")); }); }
@Test public void whenSampleCountInvalid() throws Exception { File inFile = getResourceFile( "Reducer-whenFunctionDefined-Input.xml"); File genFile = getResourceFile( "Reducer-whenFunctionDefined-Generators.xml"); String function2 = "find-2"; String[] args = { "-f", function2, "-g", genFile.getName(), "-s", "WTF", inFile.getPath() }; expectFailure( RuntimeException.class) .when( () -> new ReducerCommand().run( new Options( args))) .then( failure -> assertThat( "Expected failure", failure.getCause().getMessage(), is( "Invalid sample count"))); }
@Test public void whenFunctionUndefined() throws Exception { File inFile = getResourceFile( "Reducer-whenFunctionDefined-Input.xml"); File genFile = getResourceFile( "Reducer-whenFunctionDefined-Generators.xml"); String function3 = "find-3"; String[] args = { "-f", function3, "-g", genFile.getPath(), "-r", "0.5", "-s", "2", inFile.getPath() }; expectFailure( RuntimeException.class) .when( () -> new ReducerCommand().run( new Options( args))) .then( failure -> assertThat( "Expected failure", failure.getMessage(), is( "Function=" + function3 + " is not defined"))); }
@Test public void whenInputDefNotExists() throws Exception { File inFile = new File( getResourceFile( "Reducer-whenFunctionDefined-Input.xml").getParentFile(), "Undefined"); String[] args = { inFile.getPath() }; expectFailure( RuntimeException.class) .when( () -> new ReducerCommand().run( new Options( args))) .then( failure -> assertThat( "Expected failure", failure.getMessage(), is( "Can't locate input file for path=" + inFile))); }
@Test public void whenReducedTestFileDefined() throws Exception { File inFile = getResourceFile( "Reducer-whenBaseTests-Input.xml"); String function2 = "find-2"; String[] args = { "-f", function2, "-r", "0.5", "-s", "8", "-t", "Reducer-whenBaseTests-Test.xml", inFile.getPath() }; ReducerCommand reducer = new ReducerCommand(); reducer.run( new Options( args)); File genFile = getResourceFile( "Reducer-whenBaseTests-Generators.xml"); assertThat( "Generator file created", genFile.exists(), is( false)); }
@Test public void runWithContentType_0() throws Exception { File genFile = getResourceFile( "Reducer-withContentType-0-GenDef.xml"); File inFile = getResourceFile( "Reducer-withContentType-0"); String[] args = { "-g", genFile.getName(), "-T", "JSON", inFile.getPath() }; ReducerCommand reducer = new ReducerCommand(); reducer.run( new Options( args)); assertThat( "Generator file content type", generatorResources_.read( genFile), is( notNullValue())); }
@Test public void runWithContentType_1() throws Exception { File genFile = getResourceFile( "Reducer-withContentType-1-Generators.json"); File testFile = getResourceFile( "Reducer-withContentType-1-Base.xml"); File inFile = getResourceFile( "Reducer-withContentType-1.json"); genFile.delete(); String[] args = { "-t", testFile.getName(), inFile.getPath() }; ReducerCommand reducer = new ReducerCommand(); reducer.run( new Options( args)); assertThat( "Generator file created", genFile.exists(), is( true)); assertThat( "Generator file content type", generatorResources_.readJson( genFile.getName()), is( notNullValue())); }
@Test public void runWithContentType_2() throws Exception { File genFile = getResourceFile( "Reducer-withContentType-2-G.json"); File testFile = getResourceFile( "Reducer-withContentType-2-Base.json"); File inFile = getResourceFile( "Reducer-withContentType-2"); String[] args = { "-g", genFile.getPath(), "-t", testFile.getName(), "-T", "XML", inFile.getPath() }; ReducerCommand reducer = new ReducerCommand(); reducer.run( new Options( args)); assertThat( "Generator file content type", generatorResources_.readJson( genFile.getName()), is( notNullValue())); }
@Test public void runWithContentType_3() throws Exception { File genFile = getResourceFile( "Reducer-withContentType-3-Generators.json"); File inFile = getResourceFile( "Reducer-withContentType-3"); genFile.delete(); String[] args = { inFile.getPath() }; ReducerCommand reducer = new ReducerCommand(); reducer.run( new Options( args)); assertThat( "Generator file created", genFile.exists(), is( true)); assertThat( "Generator file content type", generatorResources_.readJson( genFile.getName()), is( notNullValue())); }
@Test public void runWithContentType_4() throws Exception { File genFile = getResourceFile( "Reducer-withContentType-4-GenDef"); File testFile = getResourceFile( "Reducer-withContentType-4-Test"); File inFile = getResourceFile( "Reducer-withContentType-4"); genFile.delete(); String[] args = { "-g", genFile.getName(), "-t", testFile.getName(), "-T", "json", inFile.getPath() }; ReducerCommand reducer = new ReducerCommand(); reducer.run( new Options( args)); assertThat( "Generator file created", genFile.exists(), is( true)); assertThat( "Generator file content type", generatorResources_.readJson( genFile.getName()), is( notNullValue())); }