unit-test: names, types, prefix v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Mon, 16 Dec 2013 00:29:22 +0100
branchv_0
changeset 122dcb67d90fd2
parent 11 2712ded000ef
child 13 599aad77e986
unit-test: names, types, prefix
java/sql-dk/src/info/globalcode/sql/dk/CLIParser.java
java/sql-dk/test/info/globalcode/sql/dk/CLIParserTest.java
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/CLIParser.java	Mon Dec 16 00:07:46 2013 +0100
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIParser.java	Mon Dec 16 00:29:22 2013 +0100
     1.3 @@ -44,7 +44,7 @@
     1.4  						if (sepatratorIndex == -1) {
     1.5  							numberedTypes.add(getType(oneType));
     1.6  						} else {
     1.7 -							String namePart = oneType.substring(0, sepatratorIndex);
     1.8 +							String namePart = oneType.substring(0, sepatratorIndex).trim();
     1.9  							String typePart = oneType.substring(sepatratorIndex + TYPE_NAME_SEPARATOR.length(), oneType.length());
    1.10  							namedTypes.put(namePart, getType(typePart));
    1.11  						}
     2.1 --- a/java/sql-dk/test/info/globalcode/sql/dk/CLIParserTest.java	Mon Dec 16 00:07:46 2013 +0100
     2.2 +++ b/java/sql-dk/test/info/globalcode/sql/dk/CLIParserTest.java	Mon Dec 16 00:29:22 2013 +0100
     2.3 @@ -1,7 +1,9 @@
     2.4  package info.globalcode.sql.dk;
     2.5  
     2.6  import info.globalcode.sql.dk.CLIParser.Tokens;
     2.7 +import static info.globalcode.sql.dk.CLIParser.TYPE_NAME_SEPARATOR;
     2.8  import java.sql.Types;
     2.9 +import java.util.Collection;
    2.10  import static org.testng.Assert.*;
    2.11  import org.testng.annotations.BeforeMethod;
    2.12  import org.testng.annotations.Test;
    2.13 @@ -17,6 +19,9 @@
    2.14  	private static final String DATA_1 = "aaa";
    2.15  	private static final String DATA_2 = "bbb";
    2.16  	private static final String DATA_3 = "ccc";
    2.17 +	private static final String NAME_1 = "param1";
    2.18 +	private static final String NAME_2 = "param2";
    2.19 +	private static final String NAME_3 = "param3";
    2.20  	private CLIParser parser;
    2.21  
    2.22  	@BeforeMethod
    2.23 @@ -25,6 +30,21 @@
    2.24  	}
    2.25  
    2.26  	@Test
    2.27 +	public void testParseOptions_QueryNow_NoParams() throws InvalidOptionsException, CLIParserException {
    2.28 +		String[] args = new String[]{
    2.29 +			Tokens.DB, DATABASE_NAME_1,
    2.30 +			Tokens.SQL, SQL_1};
    2.31 +		CLIOptions options = parser.parseOptions(args);
    2.32 +		options.validate();
    2.33 +
    2.34 +		assertEquals(options.getDatabaseName(), DATABASE_NAME_1);
    2.35 +		assertEquals(options.getSql(), SQL_1);
    2.36 +		assertEquals(options.getMode(), CLIOptions.MODE.QUERY_NOW);
    2.37 +		assertTrue(options.getNamedParameters().isEmpty(), "Named parameters should be empty.");
    2.38 +		assertTrue(options.getNumberedParameters().isEmpty(), "Numbered parameters should be empty.");
    2.39 +	}
    2.40 +
    2.41 +	@Test
    2.42  	public void testParseOptions_QueryNow_Numbered() throws InvalidOptionsException, CLIParserException {
    2.43  		String[] args = new String[]{
    2.44  			Tokens.DB, DATABASE_NAME_1,
    2.45 @@ -71,13 +91,49 @@
    2.46  	public void testParseOptions_QueryNow_Named() throws InvalidOptionsException, CLIParserException {
    2.47  		String[] args = new String[]{
    2.48  			Tokens.DB, DATABASE_NAME_1,
    2.49 -			Tokens.SQL, SQL_1};
    2.50 +			Tokens.SQL, SQL_1,
    2.51 +			Tokens.DATA, TYPE_NAME_SEPARATOR + NAME_1, DATA_1, TYPE_NAME_SEPARATOR + NAME_2, DATA_2, TYPE_NAME_SEPARATOR + NAME_3, DATA_3};
    2.52  		CLIOptions options = parser.parseOptions(args);
    2.53  		options.validate();
    2.54  
    2.55  		assertEquals(options.getDatabaseName(), DATABASE_NAME_1);
    2.56  		assertEquals(options.getSql(), SQL_1);
    2.57  		assertEquals(options.getMode(), CLIOptions.MODE.QUERY_NOW);
    2.58 +		assertEquals(options.getNamedParameters().size(), 3);
    2.59 +		assertNamedParameter(options.getNamedParameters(), NAME_1, DATA_1, Parameter.DEFAULT_TYPE);
    2.60 +		assertNamedParameter(options.getNamedParameters(), NAME_2, DATA_2, Parameter.DEFAULT_TYPE);
    2.61 +		assertNamedParameter(options.getNamedParameters(), NAME_3, DATA_3, Parameter.DEFAULT_TYPE);
    2.62 +	}
    2.63 +
    2.64 +	@Test
    2.65 +	public void testParseOptions_QueryNow_Named_withTypes() throws InvalidOptionsException, CLIParserException {
    2.66 +		String[] args = new String[]{
    2.67 +			Tokens.DB, DATABASE_NAME_1,
    2.68 +			Tokens.SQL, SQL_1,
    2.69 +			Tokens.NAME_PREFIX, "$",
    2.70 +			Tokens.TYPES, " " + NAME_1 + TYPE_NAME_SEPARATOR + "int" + "," + NAME_2 + TYPE_NAME_SEPARATOR + "string" + "," + NAME_3 + TYPE_NAME_SEPARATOR + "boolean",
    2.71 +			Tokens.DATA, "$" + NAME_1, DATA_1, "$" + NAME_2, DATA_2, "$" + NAME_3, DATA_3};
    2.72 +		CLIOptions options = parser.parseOptions(args);
    2.73 +		options.validate();
    2.74 +
    2.75 +		assertEquals(options.getDatabaseName(), DATABASE_NAME_1);
    2.76 +		assertEquals(options.getSql(), SQL_1);
    2.77 +		assertEquals(options.getMode(), CLIOptions.MODE.QUERY_NOW);
    2.78 +		assertEquals(options.getNamedParameters().size(), 3);
    2.79 +		assertNamedParameter(options.getNamedParameters(), NAME_1, DATA_1, Types.INTEGER);
    2.80 +		assertNamedParameter(options.getNamedParameters(), NAME_2, DATA_2, Types.VARCHAR);
    2.81 +		assertNamedParameter(options.getNamedParameters(), NAME_3, DATA_3, Types.BOOLEAN);
    2.82 +	}
    2.83 +
    2.84 +	private void assertNamedParameter(Collection<NamedParameter> params, String name, Object value, int type) {
    2.85 +		for (NamedParameter p : params) {
    2.86 +			if (name.equals(p.getName())) {
    2.87 +				assertEquals(p.getValue(), value, "value does not match – name: " + name);
    2.88 +				assertEquals(p.getType(), type, "value does not match – name: " + name);
    2.89 +				return;
    2.90 +			}
    2.91 +		}
    2.92 +		fail("Named parameter not found: " + name);
    2.93  	}
    2.94  
    2.95  	@Test