You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mahout.apache.org by Sebastian Schelter <ss...@googlemail.com> on 2010/06/16 21:11:29 UTC

Problem with default values for options in AbstractJob

Hi,

I was just trying to integrate the patch from MAHOUT-407 into the head
when I ran into another issue. It seems to me like the default values
given to addOption(...) are not correctly returned by
parseArguments(...). Can anybody please check that? I'm not familiar
enough with CLI to fix it myself.

-sebastian

A unit test to confirm the problem:

package org.apache.mahout.common;

import java.util.Map;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.util.ToolRunner;
import org.apache.mahout.common.AbstractJob;
import org.apache.mahout.math.MahoutTestCase;

public class AbstractJobTest extends MahoutTestCase {

  private static class TestJob extends AbstractJob {
    private String valueOfTestOption;
    @Override
    public int run(String[] args) throws Exception {
      addOption("test", "t", "an option having a default value", "default");
      Map<String,String> parsedArgs = parseArguments(args);
      valueOfTestOption = parsedArgs.get("--test");
      return 0;
    }
    public String getValueOfTestOption() {
      return valueOfTestOption;
    }
  }

  private TestJob job;
  private Configuration conf;

  @Override
  protected void setUp() throws Exception {
    super.setUp();
    job = new TestJob();
    conf = new Configuration();
    job.setConf(conf);
  }

  public void testValueForOption() throws Exception {
    ToolRunner.run(job, new String[] { "--test", "value" });
    assertEquals("value", job.getValueOfTestOption());
  }

  public void testDefaultValueForOption() throws Exception {
    job.run(new String[] {});
    //this assertion fails with current head
    assertEquals("default", job.getValueOfTestOption());
  }

}


Re: Problem with default values for options in AbstractJob

Posted by Sean Owen <sr...@gmail.com>.
You're right, I found it. Looks like it should not check "hasOption()"
from CommandLine since it won't be true for defaulted arguments. Fix
incoming to SVN.

On Wed, Jun 16, 2010 at 8:14 PM, Sebastian Schelter
<ss...@googlemail.com> wrote:
> Please use this unit test code, the former missed ToolRunner.run(...) in
> the second test.
>
> package org.apache.mahout.common;
>
> import java.util.Map;
>
> import org.apache.hadoop.conf.Configuration;
> import org.apache.hadoop.util.ToolRunner;
> import org.apache.mahout.common.AbstractJob;
> import org.apache.mahout.math.MahoutTestCase;
>
> public class AbstractJobTest extends MahoutTestCase {
>
>  private static class TestJob extends AbstractJob {
>    private String valueOfTestOption;
>    @Override
>    public int run(String[] args) throws Exception {
>      addOption("test", "t", "an option having a default value", "default");
>      Map<String,String> parsedArgs = parseArguments(args);
>      valueOfTestOption = parsedArgs.get("--test");
>      return 0;
>    }
>    public String getValueOfTestOption() {
>      return valueOfTestOption;
>    }
>  }
>
>  private TestJob job;
>  private Configuration conf;
>
>  @Override
>  protected void setUp() throws Exception {
>    super.setUp();
>    job = new TestJob();
>    conf = new Configuration();
>    job.setConf(conf);
>  }
>
>  public void testValueForOption() throws Exception {
>    ToolRunner.run(job, new String[] { "--test", "value" });
>    assertEquals("value", job.getValueOfTestOption());
>  }
>
>  public void testDefaultValueForOption() throws Exception {
>    ToolRunner.run(job, new String[] {});
>    assertEquals("default", job.getValueOfTestOption());
>  }
>
> }
>

Re: Problem with default values for options in AbstractJob

Posted by Sebastian Schelter <ss...@googlemail.com>.
Please use this unit test code, the former missed ToolRunner.run(...) in
the second test.

package org.apache.mahout.common;

import java.util.Map;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.util.ToolRunner;
import org.apache.mahout.common.AbstractJob;
import org.apache.mahout.math.MahoutTestCase;

public class AbstractJobTest extends MahoutTestCase {

  private static class TestJob extends AbstractJob {
    private String valueOfTestOption;
    @Override
    public int run(String[] args) throws Exception {
      addOption("test", "t", "an option having a default value", "default");
      Map<String,String> parsedArgs = parseArguments(args);
      valueOfTestOption = parsedArgs.get("--test");
      return 0;
    }
    public String getValueOfTestOption() {
      return valueOfTestOption;
    }
  }

  private TestJob job;
  private Configuration conf;

  @Override
  protected void setUp() throws Exception {
    super.setUp();
    job = new TestJob();
    conf = new Configuration();
    job.setConf(conf);
  }

  public void testValueForOption() throws Exception {
    ToolRunner.run(job, new String[] { "--test", "value" });
    assertEquals("value", job.getValueOfTestOption());
  }

  public void testDefaultValueForOption() throws Exception {
    ToolRunner.run(job, new String[] {});
    assertEquals("default", job.getValueOfTestOption());
  }

}