首页

基于异常Exception的简单实现Validator校验器实现及测试Tester类源码分享

标签:validator,校验器,自定义验证器     发布时间:2018-01-22   

一、前言

分享开源的dbdeploy包中关于超简单的校验器validator的实现类net.sf.dbdeploy.Validator,其中net.sf.dbdeploy.ValidatorTest针对常见的数据部署中用例场景对校验器validator做完整的验证测试。

二、源码说明

1.Validator实现类

package net.sf.dbdeploy;@b@@b@import org.apache.tools.ant.BuildException;@b@@b@public class Validator@b@{@b@  private String usage;@b@  private static String ERROR_MESSAGE_HEADER = "Dbdeploy parameter validation error\n\n";@b@  private static String ANT_USAGE = "\n\nDbdeploy Ant Task Usage\n=======================\n\n\t<dbdeploy\n\t\tuserid=\"[DATABASE USER ID]\" *\n\t\tpassword=\"[DATABASE USER ID PASSWORD]\"\n\t\tdriver=\"[DATABASE DRIVER]\" *\n\t\turl=\"[DATABASE URL]\" *\n\t\tdbms=\"[YOUR DBMS]\" *\n\t\tdir=\"[YOUR SCRIPT FOLDER]\" *\n\t\toutputfile=\"[OUTPUT SCRIPT PATH + NAME]\" *\n\t\tmaxNumberToApply=\"[NUMBER OF THE LAST SCRIPT TO APPLY]\"\n\t\tdeltaSet=\"[NAME OF DELTA SET TO BE APPLIED]\"\n\t\tundoOutputfile=\"[UNDO SCRIPT PATH + NAME]\"\n\t/>\n\n* - Indicates mandatory parameter";@b@  private static String COMMAND_LINE_USAGE = "\n\n\nDbdeploy Command Line Usage\n===========================\n\n\tTODO - SPECIFY USAGE HERE";@b@@b@  public void validate(String paramString1, String paramString2, String paramString3, String paramString4, String paramString5, String paramString6)@b@  {@b@    if ((paramString1 == null) || (paramString1 == ""))@b@      throw new BuildException(constructErrorMessage("Userid expected"));@b@    if ((paramString2 == null) || (paramString2 == ""))@b@      throw new BuildException(constructErrorMessage("Driver expected"));@b@    if ((paramString3 == null) || (paramString3 == ""))@b@      throw new BuildException(constructErrorMessage("Url expected"));@b@    if ((!(paramString4.equals("ora"))) && (!(paramString4.equals("ora-sqlplus"))) && (!(paramString4.equals("hsql"))) && (!(paramString4.equals("syb-ase"))) && (!(paramString4.equals("mssql"))) && (!(paramString4.equals("mysql"))))@b@      throw new BuildException(constructErrorMessage("Unknown DBMS: " + paramString4 + "\n\nAllowed values:\nora - Oracle\nhsql - Hypersonic SQL\nsyb-ase - Sybase ASE\nmssql - Microsoft SQL Server\nmysql - MySQL database"));@b@    if ((paramString5 == null) || (paramString5 == ""))@b@      throw new BuildException(constructErrorMessage("Dir expected"));@b@    if ((paramString6 == null) || (paramString6 == ""))@b@      throw new BuildException(constructErrorMessage("Output file expected"));@b@  }@b@@b@  private String constructErrorMessage(String paramString)@b@  {@b@    if (this.usage == "ant")@b@      return ERROR_MESSAGE_HEADER + paramString + ANT_USAGE;@b@    if (this.usage == "commandline")@b@      return ERROR_MESSAGE_HEADER + paramString + COMMAND_LINE_USAGE;@b@    throw new BuildException("Unexpected usage!");@b@  }@b@@b@  public void setUsage(String paramString)@b@  {@b@    this.usage = paramString;@b@  }@b@}

2.ValidatorTest测试类

package net.sf.dbdeploy;@b@@b@import junit.framework.TestCase;@b@import org.apache.tools.ant.BuildException;@b@@b@public class ValidatorTest extends TestCase@b@{@b@  private static final String USER_ID = "userId";@b@  private static final String DRIVER = "driver";@b@  private static final String URL = "url";@b@  private static final String DBMS = "ora";@b@  private static final String DIR = "dir";@b@  private static final String OUTPUT_FILE = "output file";@b@@b@  public void testShouldFailWhenPassedNullUserid()@b@  {@b@    Validator localValidator = new Validator();@b@    localValidator.setUsage("ant");@b@    try@b@    {@b@      localValidator.validate(null, "driver", "url", "ora", "dir", "output file");@b@      fail("BuildException expected");@b@    }@b@    catch (BuildException localBuildException)@b@    {@b@      assertEquals("Userid expected", localBuildException.getMessage().substring(37, 52));@b@    }@b@  }@b@@b@  public void testShouldFailWhenPassedEmptyUserid()@b@  {@b@    Validator localValidator = new Validator();@b@    localValidator.setUsage("ant");@b@    try@b@    {@b@      localValidator.validate("", "driver", "url", "ora", "dir", "output file");@b@      fail("BuildException expected");@b@    }@b@    catch (BuildException localBuildException)@b@    {@b@      assertEquals("Userid expected", localBuildException.getMessage().substring(37, 52));@b@    }@b@  }@b@@b@  public void testShouldFailWhenPassedNullDriver()@b@  {@b@    Validator localValidator = new Validator();@b@    localValidator.setUsage("ant");@b@    try@b@    {@b@      localValidator.validate("userId", null, "url", "ora", "dir", "output file");@b@      fail("BuildException expected");@b@    }@b@    catch (BuildException localBuildException)@b@    {@b@      assertEquals("Driver expected", localBuildException.getMessage().substring(37, 52));@b@    }@b@  }@b@@b@  public void testShouldFailWhenPassedEmptyDriver()@b@  {@b@    Validator localValidator = new Validator();@b@    localValidator.setUsage("ant");@b@    try@b@    {@b@      localValidator.validate("userId", "", "url", "ora", "dir", "output file");@b@      fail("BuildException expected");@b@    }@b@    catch (BuildException localBuildException)@b@    {@b@      assertEquals("Driver expected", localBuildException.getMessage().substring(37, 52));@b@    }@b@  }@b@@b@  public void testShouldFailWhenPassedNullUrl()@b@  {@b@    Validator localValidator = new Validator();@b@    localValidator.setUsage("ant");@b@    try@b@    {@b@      localValidator.validate("userId", "driver", null, "ora", "dir", "output file");@b@      fail("BuildException expected");@b@    }@b@    catch (BuildException localBuildException)@b@    {@b@      assertEquals("Url expected", localBuildException.getMessage().substring(37, 49));@b@    }@b@  }@b@@b@  public void testShouldFailWhenPassedEmptyUrl()@b@  {@b@    Validator localValidator = new Validator();@b@    localValidator.setUsage("ant");@b@    try@b@    {@b@      localValidator.validate("userId", "driver", "", "ora", "dir", "output file");@b@      fail("BuildException expected");@b@    }@b@    catch (BuildException localBuildException)@b@    {@b@      assertEquals("Url expected", localBuildException.getMessage().substring(37, 49));@b@    }@b@  }@b@@b@  public void testShouldFailWhenPassedNonOraDbms()@b@  {@b@    Validator localValidator = new Validator();@b@    localValidator.setUsage("ant");@b@    try@b@    {@b@      localValidator.validate("userId", "driver", "url", "nothing", "dir", "output file");@b@      fail("BuildException expected");@b@    }@b@    catch (BuildException localBuildException)@b@    {@b@      assertEquals("Unknown DBMS", localBuildException.getMessage().substring(37, 49));@b@    }@b@  }@b@@b@  public void testShouldFailWhenPassedNullDir()@b@  {@b@    Validator localValidator = new Validator();@b@    localValidator.setUsage("ant");@b@    try@b@    {@b@      localValidator.validate("userId", "driver", "url", "ora", null, "output file");@b@      fail("BuildException expected");@b@    }@b@    catch (BuildException localBuildException)@b@    {@b@      assertEquals("Dir expected", localBuildException.getMessage().substring(37, 49));@b@    }@b@  }@b@@b@  public void testShouldFailWhenPassedEmptyDir()@b@  {@b@    Validator localValidator = new Validator();@b@    localValidator.setUsage("ant");@b@    try@b@    {@b@      localValidator.validate("userId", "driver", "url", "ora", "", "output file");@b@      fail("BuildException expected");@b@    }@b@    catch (BuildException localBuildException)@b@    {@b@      assertEquals("Dir expected", localBuildException.getMessage().substring(37, 49));@b@    }@b@  }@b@@b@  public void testShouldFailWhenPassedNullOutputFile()@b@  {@b@    Validator localValidator = new Validator();@b@    localValidator.setUsage("ant");@b@    try@b@    {@b@      localValidator.validate("userId", "driver", "url", "ora", "dir", null);@b@      fail("BuildException expected");@b@    }@b@    catch (BuildException localBuildException)@b@    {@b@      assertEquals("Output file expected", localBuildException.getMessage().substring(37, 57));@b@    }@b@  }@b@@b@  public void testShouldFailWhenPassedEmptyOutputFile()@b@  {@b@    Validator localValidator = new Validator();@b@    localValidator.setUsage("ant");@b@    try@b@    {@b@      localValidator.validate("userId", "driver", "url", "ora", "dir", "");@b@      fail("BuildException expected");@b@    }@b@    catch (BuildException localBuildException)@b@    {@b@      assertEquals("Output file expected", localBuildException.getMessage().substring(37, 57));@b@    }@b@  }@b@}
<<热门下载>>