List of usage examples for org.apache.mahout.common AbstractJob AbstractJob
protected AbstractJob()
From source file:org.gpfvic.mahout.common.AbstractJobTest.java
License:Apache License
@Test public void testFlag() throws Exception { final Map<String, List<String>> testMap = Maps.newHashMap(); AbstractJobFactory fact = new AbstractJobFactory() { @Override//from w w w . jav a2 s . c om public AbstractJob getJob() { return new AbstractJob() { public int run(String[] args) throws IOException { addFlag("testFlag", "t", "a simple test flag"); Map<String, List<String>> argMap = parseArguments(args); testMap.clear(); testMap.putAll(argMap); return 1; } }; } }; // testFlag will only be present if speciied on the command-line ToolRunner.run(fact.getJob(), new String[0]); assertFalse("test map for absent flag", testMap.containsKey("--testFlag")); String[] withFlag = { "--testFlag" }; ToolRunner.run(fact.getJob(), withFlag); assertTrue("test map for present flag", testMap.containsKey("--testFlag")); }
From source file:org.gpfvic.mahout.common.AbstractJobTest.java
License:Apache License
@Test public void testOptions() throws Exception { final Map<String, List<String>> testMap = Maps.newHashMap(); AbstractJobFactory fact = new AbstractJobFactory() { @Override/*from w w w . jav a 2s . co m*/ public AbstractJob getJob() { return new AbstractJob() { @Override public int run(String[] args) throws IOException { this.addOption(DefaultOptionCreator.overwriteOption().create()); this.addOption("option", "o", "option"); this.addOption("required", "r", "required", true /* required */); this.addOption("notRequired", "nr", "not required", false /* not required */); this.addOption("hasDefault", "hd", "option w/ default", "defaultValue"); Map<String, List<String>> argMap = parseArguments(args); if (argMap == null) { return -1; } testMap.clear(); testMap.putAll(argMap); return 0; } }; } }; int ret = ToolRunner.run(fact.getJob(), new String[0]); assertEquals("-1 for missing required options", -1, ret); ret = ToolRunner.run(fact.getJob(), new String[] { "--required", "requiredArg" }); assertEquals("0 for no missing required options", 0, ret); assertEquals(Collections.singletonList("requiredArg"), testMap.get("--required")); assertEquals(Collections.singletonList("defaultValue"), testMap.get("--hasDefault")); assertNull(testMap.get("--option")); assertNull(testMap.get("--notRequired")); assertFalse(testMap.containsKey("--overwrite")); ret = ToolRunner.run(fact.getJob(), new String[] { "--required", "requiredArg", "--unknownArg" }); assertEquals("-1 for including unknown options", -1, ret); ret = ToolRunner.run(fact.getJob(), new String[] { "--required", "requiredArg", "--required", "requiredArg2", }); assertEquals("-1 for including duplicate options", -1, ret); ret = ToolRunner.run(fact.getJob(), new String[] { "--required", "requiredArg", "--overwrite", "--hasDefault", "nonDefault", "--option", "optionValue", "--notRequired", "notRequired" }); assertEquals("0 for no missing required options", 0, ret); assertEquals(Collections.singletonList("requiredArg"), testMap.get("--required")); assertEquals(Collections.singletonList("nonDefault"), testMap.get("--hasDefault")); assertEquals(Collections.singletonList("optionValue"), testMap.get("--option")); assertEquals(Collections.singletonList("notRequired"), testMap.get("--notRequired")); assertTrue(testMap.containsKey("--overwrite")); ret = ToolRunner.run(fact.getJob(), new String[] { "-r", "requiredArg", "-ow", "-hd", "nonDefault", "-o", "optionValue", "-nr", "notRequired" }); assertEquals("0 for no missing required options", 0, ret); assertEquals(Collections.singletonList("requiredArg"), testMap.get("--required")); assertEquals(Collections.singletonList("nonDefault"), testMap.get("--hasDefault")); assertEquals(Collections.singletonList("optionValue"), testMap.get("--option")); assertEquals(Collections.singletonList("notRequired"), testMap.get("--notRequired")); assertTrue(testMap.containsKey("--overwrite")); }
From source file:org.gpfvic.mahout.common.AbstractJobTest.java
License:Apache License
@Test public void testInputOutputPaths() throws Exception { AbstractJobFactory fact = new AbstractJobFactory() { @Override//from w w w .ja v a2 s .c o m public AbstractJob getJob() { return new AbstractJob() { @Override public int run(String[] args) throws IOException { addInputOption(); addOutputOption(); // arg map should be null if a required option is missing. Map<String, List<String>> argMap = parseArguments(args); if (argMap == null) { return -1; } Path inputPath = getInputPath(); assertNotNull("getInputPath() returns non-null", inputPath); Path outputPath = getInputPath(); assertNotNull("getOutputPath() returns non-null", outputPath); return 0; } }; } }; int ret = ToolRunner.run(fact.getJob(), new String[0]); assertEquals("-1 for missing input option", -1, ret); String testInputPath = "testInputPath"; AbstractJob job = fact.getJob(); ret = ToolRunner.run(job, new String[] { "--input", testInputPath }); assertEquals("-1 for missing output option", -1, ret); assertEquals("input path is correct", testInputPath, job.getInputPath().toString()); job = fact.getJob(); String testOutputPath = "testOutputPath"; ret = ToolRunner.run(job, new String[] { "--output", testOutputPath }); assertEquals("-1 for missing input option", -1, ret); assertEquals("output path is correct", testOutputPath, job.getOutputPath().toString()); job = fact.getJob(); ret = ToolRunner.run(job, new String[] { "--input", testInputPath, "--output", testOutputPath }); assertEquals("0 for complete options", 0, ret); assertEquals("input path is correct", testInputPath, job.getInputPath().toString()); assertEquals("output path is correct", testOutputPath, job.getOutputPath().toString()); job = fact.getJob(); ret = ToolRunner.run(job, new String[] { "--input", testInputPath, "--output", testOutputPath }); assertEquals("0 for complete options", 0, ret); assertEquals("input path is correct", testInputPath, job.getInputPath().toString()); assertEquals("output path is correct", testOutputPath, job.getOutputPath().toString()); job = fact.getJob(); String testInputPropertyPath = "testInputPropertyPath"; String testOutputPropertyPath = "testOutputPropertyPath"; ret = ToolRunner.run(job, new String[] { "-Dmapred.input.dir=" + testInputPropertyPath, "-Dmapred.output.dir=" + testOutputPropertyPath }); assertEquals("0 for complete options", 0, ret); assertEquals("input path from property is correct", testInputPropertyPath, job.getInputPath().toString()); assertEquals("output path from property is correct", testOutputPropertyPath, job.getOutputPath().toString()); job = fact.getJob(); ret = ToolRunner.run(job, new String[] { "-Dmapred.input.dir=" + testInputPropertyPath, "-Dmapred.output.dir=" + testOutputPropertyPath, "--input", testInputPath, "--output", testOutputPath }); assertEquals("0 for complete options", 0, ret); assertEquals("input command-line option precedes property", testInputPath, job.getInputPath().toString()); assertEquals("output command-line option precedes property", testOutputPath, job.getOutputPath().toString()); }