Чистый код. Создание, анализ и рефакторинг — страница 49 из 94

      assertEquals(ArgsException.ErrorCode.INVALID_DOUBLE, e.getErrorCode());

      assertEquals('x', e.getErrorArgumentId());

      assertEquals("Forty two", e.getErrorParameter());

    }

  }


  public void testMissingDouble() throws Exception {

    try {

      new Args("x##", new String[]{"-x"});

      fail();

    } catch (ArgsException e) {

      assertEquals(ArgsException.ErrorCode.MISSING_DOUBLE, e.getErrorCode());

      assertEquals('x', e.getErrorArgumentId());

    }

  }

}


Листинг 14.14. ArgsExceptionTest.java

public class ArgsExceptionTest extends TestCase {

  public void testUnexpectedMessage() throws Exception {

    ArgsException e =

      new ArgsException(ArgsException.ErrorCode.UNEXPECTED_ARGUMENT,

                        'x', null);

    assertEquals("Argument -x unexpected.", e.errorMessage());

  }


  public void testMissingStringMessage() throws Exception {

    ArgsException e = new ArgsException(ArgsException.ErrorCode.MISSING_STRING,

                                        'x', null);

    assertEquals("Could not find string parameter for -x.", e.errorMessage());

  }


  public void testInvalidIntegerMessage() throws Exception {

    ArgsException e =

      new ArgsException(ArgsException.ErrorCode.INVALID_INTEGER,

                        'x', "Forty two");

    assertEquals("Argument -x expects an integer but was 'Forty two'.",

                 e.errorMessage());

  }


  public void testMissingIntegerMessage() throws Exception {

    ArgsException e =

      new ArgsException(ArgsException.ErrorCode.MISSING_INTEGER, 'x', null);

    assertEquals("Could not find integer parameter for -x.", e.errorMessage());

  }


  public void testInvalidDoubleMessage() throws Exception {

    ArgsException e = new ArgsException(ArgsException.ErrorCode.INVALID_DOUBLE,

                                        'x', "Forty two");

    assertEquals("Argument -x expects a double but was 'Forty two'.",

                 e.errorMessage());

  }


  public void testMissingDoubleMessage() throws Exception {

    ArgsException e = new ArgsException(ArgsException.ErrorCode.MISSING_DOUBLE,

                                        'x', null);

    assertEquals("Could not find double parameter for -x.", e.errorMessage());

  }

}


Листинг 14.15. ArgsException.java

public class ArgsException extends Exception {

  private char errorArgumentId = '\0';

  private String errorParameter = "TILT";

  private ErrorCode errorCode = ErrorCode.OK;


  public ArgsException() {}


  public ArgsException(String message) {super(message);}


  public ArgsException(ErrorCode errorCode) {

    this.errorCode = errorCode;

  }


  public ArgsException(ErrorCode errorCode, String errorParameter) {

    this.errorCode = errorCode;

    this.errorParameter = errorParameter;

  }


  public ArgsException(ErrorCode errorCode, char errorArgumentId,

                       String errorParameter) {


Листинг 14.15 (продолжение)

    this.errorCode = errorCode;

    this.errorParameter = errorParameter;

    this.errorArgumentId = errorArgumentId;

  }


  public char getErrorArgumentId() {

    return errorArgumentId;

  }


  public void setErrorArgumentId(char errorArgumentId) {

    this.errorArgumentId = errorArgumentId;

  }


  public String getErrorParameter() {

    return errorParameter;

  }


  public void setErrorParameter(String errorParameter) {

    this.errorParameter = errorParameter;

  }


  public ErrorCode getErrorCode() {

    return errorCode;

  }


  public void setErrorCode(ErrorCode errorCode) {

    this.errorCode = errorCode;

  }


  public String errorMessage() throws Exception {

    switch (errorCode) {

      case OK:

        throw new Exception("TILT: Should not get here.");

      case UNEXPECTED_ARGUMENT:

        return String.format("Argument -%c unexpected.", errorArgumentId);

      case MISSING_STRING:

        return String.format("Could not find string parameter for -%c.",

                             errorArgumentId);

      case INVALID_INTEGER:

        return String.format("Argument -%c expects an integer but was '%s'.",

                             errorArgumentId, errorParameter);

      case MISSING_INTEGER:

        return String.format("Could not find integer parameter for -%c.",

                             errorArgumentId);

      case INVALID_DOUBLE:

        return String.format("Argument -%c expects a double but was '%s'.",

                             errorArgumentId, errorParameter);

      case MISSING_DOUBLE:

        return String.format("Could not find double parameter for -%c.",

                             errorArgumentId);

    }

    return "";

  }


  public enum ErrorCode {

    OK, INVALID_FORMAT, UNEXPECTED_ARGUMENT, INVALID_ARGUMENT_NAME,

    MISSING_STRING,

    MISSING_INTEGER, INVALID_INTEGER,

    MISSING_DOUBLE, INVALID_DOUBLE}

}


Листинг 14.16. Args.java

public class Args {

  private String schema;

  private Map