diff options
author | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
commit | 72231250ed81e10d66bfe70701e64fa5fe50f712 (patch) | |
tree | 2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /configgen/src/test/java/com/yahoo |
Publish
Diffstat (limited to 'configgen/src/test/java/com/yahoo')
4 files changed, 892 insertions, 0 deletions
diff --git a/configgen/src/test/java/com/yahoo/config/codegen/DefLineParsingTest.java b/configgen/src/test/java/com/yahoo/config/codegen/DefLineParsingTest.java new file mode 100644 index 00000000000..515f7838436 --- /dev/null +++ b/configgen/src/test/java/com/yahoo/config/codegen/DefLineParsingTest.java @@ -0,0 +1,221 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.config.codegen; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.*; + +import org.junit.Test; + +/** + * Tests parsing of a single line of a .def file + * + * @author <a href="mailto:bratseth@yahoo-inc.com">Jon S Bratseth</a> + * @author gjoranv + */ +public class DefLineParsingTest { + + @Test(expected = IllegalArgumentException.class) + public void require_that_null_default_is_not_allowed() { + new DefLine("s string default=null"); + } + + @Test + public void testParseIntArray() { + DefLine l = new DefLine("foo[] int"); + + assertEquals("foo[]", l.getName()); + assertNull(l.getDefault()); + assertEquals("int", l.getType().getName()); + } + + @Test + public void testParseIntMap() { + DefLine l = new DefLine("foo{} int"); + + assertEquals("foo{}", l.getName()); + assertNull(l.getDefault()); + assertEquals("int", l.getType().getName()); + } + + @Test + public void testParseInnerMap() { + DefLine l = new DefLine("foo{}.i int"); + + assertEquals("foo{}.i", l.getName()); + assertNull(l.getDefault()); + assertEquals("int", l.getType().getName()); + } + + @Test + public void testParseEnum() { + DefLine l = new DefLine("idtype enum { us_east, US_WEST, EMEA } default=EMEA"); + + assertEquals("idtype", l.getName()); + assertEquals("EMEA", l.getDefault().getValue()); + assertEquals("EMEA", l.getDefault().getStringRepresentation()); + assertEquals("enum", l.getType().getName()); + assertEquals("us_east", l.getType().getEnumArray()[0]); + assertEquals("US_WEST", l.getType().getEnumArray()[1]); + assertEquals("EMEA", l.getType().getEnumArray()[2]); + } + + @Test + public void testParseDefaultReference() { + DefLine l = new DefLine("foo.bar reference default=\"value\""); + + assertEquals("foo.bar", l.getName()); + assertEquals("value", l.getDefault().getValue()); + assertEquals("\"value\"", l.getDefault().getStringRepresentation()); + assertEquals("reference", l.getType().getName()); + } + + @Test + public void testParseNoDefaultReference() { + DefLine l = new DefLine("foo.bar reference"); + + assertEquals("foo.bar", l.getName()); + assertNull(l.getDefault()); + assertEquals("reference", l.getType().getName()); + } + + /** + * 'file' parameters with default value is currently (2010-01-05) not allowed, but that might change in + * the future, so the test is included to verify that value and name can be retrieved. + */ + @Test + public void testParseDefaultFile() { + DefLine l = new DefLine("fileWithDef file default=\"value\""); + + assertEquals("fileWithDef", l.getName()); + assertEquals("value", l.getDefault().getValue()); + assertEquals("\"value\"", l.getDefault().getStringRepresentation()); + assertEquals("file", l.getType().getName()); + } + + @Test + public void testParseNoDefaultFile() { + DefLine l = new DefLine("fileVal file"); + + assertEquals("fileVal", l.getName()); + assertNull(l.getDefault()); + assertEquals("file", l.getType().getName()); + } + + @Test + public void testParseDefaultInt() { + DefLine l = new DefLine("foo int default=1000"); + + assertEquals("foo", l.getName()); + assertEquals("1000", l.getDefault().getValue()); + assertEquals("1000", l.getDefault().getStringRepresentation()); + assertEquals("int", l.getType().getName()); + } + + @Test + public void testParseDefaultLong() { + DefLine l = new DefLine("foo long default=9223372036854775807"); + + assertEquals("foo", l.getName()); + assertEquals("9223372036854775807", l.getDefault().getValue()); + assertEquals("9223372036854775807", l.getDefault().getStringRepresentation()); + assertEquals("long", l.getType().getName()); + } + + @Test + public void testParseDefaultDouble() { + DefLine l = new DefLine("foo double default=5.37"); + + assertEquals("foo", l.getName()); + assertEquals("5.37", l.getDefault().getValue()); + assertEquals("5.37", l.getDefault().getStringRepresentation()); + assertEquals("double", l.getType().getName()); + } + + @Test + public void testParseDefaultFalseBoolean() { + DefLine l = new DefLine("foo bool default=false"); + + assertEquals("foo", l.getName()); + assertEquals("false", l.getDefault().getValue()); + assertEquals("false", l.getDefault().getStringRepresentation()); + assertEquals("bool", l.getType().getName()); + } + + @Test + public void testParseDefaultTrueBoolean() { + DefLine l = new DefLine("foo bool default=true"); + + assertEquals("foo", l.getName()); + assertEquals("true", l.getDefault().getValue()); + assertEquals("true", l.getDefault().getStringRepresentation()); + assertEquals("bool", l.getType().getName()); + } + + @Test + public void testParseNoDefaultString() { + DefLine l = new DefLine("foo.bar string"); + + assertEquals("foo.bar", l.getName()); + assertNull(l.getDefault()); + assertEquals("string", l.getType().getName()); + } + + @Test + public void testParseDefaultString() { + DefLine l = new DefLine("foo.bar string default=\"value\""); + + assertEquals("foo.bar", l.getName()); + assertEquals("value", l.getDefault().getValue()); + assertEquals("\"value\"", l.getDefault().getStringRepresentation()); + assertEquals("string", l.getType().getName()); + } + + @Test + public void testParseDefaultEmptyString() { + DefLine l = new DefLine("foo.bar string default=\"\""); + + assertEquals("foo.bar", l.getName()); + assertEquals("", l.getDefault().getValue()); + assertEquals("\"\"", l.getDefault().getStringRepresentation()); + assertEquals("string", l.getType().getName()); + } + + @Test + public void testParseDefaultStringUnquoted() { + DefLine l = new DefLine("foo.bar string default=value"); + + assertEquals("foo.bar", l.getName()); + assertEquals("value", l.getDefault().getValue()); + assertEquals("\"value\"", l.getDefault().getStringRepresentation()); + assertEquals("string", l.getType().getName()); + } + + @Test + public void testParseStringnullDefaultString() { + DefLine l = new DefLine("foo.bar string default=\"null\""); + + assertEquals("foo.bar", l.getName()); + assertEquals("null", l.getDefault().getValue()); + assertEquals("\"null\"", l.getDefault().getStringRepresentation()); + assertEquals("string", l.getType().getName()); + } + + @Test + public void testRanges() { + DefLine i = new DefLine("i int range=[0, 100]"); + DefLine l = new DefLine("l long range=[-1e10, 0]"); + DefLine d = new DefLine("d double range=[0, 1.0]"); + assertThat(i.getRange(), is("[0, 100]")); + assertThat(l.getRange(), is("[-1e10, 0]")); + assertThat(d.getRange(), is("[0, 1.0]")); + } + + @Test + public void testRestartSpecification() { + DefLine r0 = new DefLine("i int"); + DefLine r1 = new DefLine("i int restart"); + assertFalse(r0.getRestart()); + assertTrue(r1.getRestart()); + } + +} diff --git a/configgen/src/test/java/com/yahoo/config/codegen/DefParserTest.java b/configgen/src/test/java/com/yahoo/config/codegen/DefParserTest.java new file mode 100644 index 00000000000..567d7419778 --- /dev/null +++ b/configgen/src/test/java/com/yahoo/config/codegen/DefParserTest.java @@ -0,0 +1,514 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.config.codegen; + +import static org.hamcrest.CoreMatchers.not; +import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.junit.Ignore; + +import static org.hamcrest.CoreMatchers.is; + +import java.io.*; + +/** + * Unit tests for DefParser. + * + * @author <a href="musum@yahoo-inc.com">Harald Musum</a> + * @author <a href="gv@yahoo-inc.com">G. Voldengen</a> + */ +public class DefParserTest { + + private static final String TEST_DIR = "target/test-classes/"; + private static final String DEF_NAME = TEST_DIR + "allfeatures.def"; + + @Test + public void testTraverseTree() throws IOException { + File defFile = new File(DEF_NAME); + CNode root = new DefParser("test", new FileReader(defFile)).getTree(); + assertNotNull(root); + CNode[] children = root.getChildren(); + assertThat(children.length, is(31)); + + int numGrandChildren = 0; + int numGreatGrandChildren = 0; + for (CNode child : children) { + CNode[] childsChildren = child.getChildren(); + numGrandChildren += childsChildren.length; + for (CNode grandChild : childsChildren) { + numGreatGrandChildren += grandChild.getChildren().length; + } + } + assertThat(numGrandChildren, is(14)); + assertThat(numGreatGrandChildren, is(6)); + + // Verify that each array creates a sub-tree, and that defaults for leafs are handled correctly. + CNode myArray = root.getChild("myArray"); + assertThat(myArray.getChildren().length, is(5)); + // int within array + LeafCNode myArrayInt = (LeafCNode) myArray.getChild("intVal"); + assertThat(myArrayInt.getDefaultValue().getValue(), is("14")); + // enum within array + LeafCNode myArrayEnum = (LeafCNode) myArray.getChild("enumVal"); + assertThat(myArrayEnum.getDefaultValue().getValue(), is("TYPE")); + + // Verify array within array and a default value for a leaf in the inner array. + CNode anotherArray = myArray.getChild("anotherArray"); + assertThat(anotherArray.getChildren().length, is(1)); + LeafCNode foo = (LeafCNode) anotherArray.getChild("foo"); + assertThat(foo.getDefaultValue().getValue(), is("-4")); + } + + @Test + public void testFileWithNamespaceInFilename() throws IOException { + File defFile = new File(TEST_DIR + "bar.foo.def"); + CNode root = new DefParser("test", new FileReader(defFile)).getTree(); + assertThat(root.defMd5, is("31a0f9bda0e5ff929762a29569575a7e")); + } + + @Test + public void testMd5Sum() throws IOException { + File defFile = new File(DEF_NAME); + CNode root = new DefParser("test", new FileReader(defFile)).getTree(); + assertThat(root.defMd5, is("eb2d24dbbcf054b21be729e2cfaafd93")); + } + + @Test + public void testMd5Sum2() { + String def = "version=1\na string\n"; + CNode root = new DefParser("testMd5Sum2", new StringReader(def)).getTree(); + assertThat(root.defMd5, is("a5e5fdbb2b27e56ba7d5e60e335c598b")); + } + + @Test + public void testExplicitNamespace() { + DefParser parser = createParser("version=1\nnamespace=myproject.config\na string\n"); + CNode root = parser.getTree(); + assertThat(root.getNamespace(), is("myproject.config")); + + // with spaces + parser = createParser("version=1\nnamespace = myproject.config\na string\n"); + root = parser.getTree(); + assertThat(root.getNamespace(), is("myproject.config")); + + // invalid + parser = createParser("version=1\nnamespace \na string\n"); + try { + parser.getTree(); + fail(); + } catch (Exception e) { + //e.printStackTrace(); + assertExceptionAndMessage(e, CodegenRuntimeException.class, + "Error parsing or reading config definition.Error when parsing line 2: namespace \n" + + "namespace"); + } + + // invalid + parser = createParser("version=1\nnamespace=a..b\na string\n"); + try { + parser.getTree(); + fail(); + } catch (Exception e) { + //e.printStackTrace(); + assertExceptionAndMessage(e, CodegenRuntimeException.class, + "Error parsing or reading config definition.Error when parsing line 2: namespace=a..b\n" + + "namespace=a..b"); + } + } + + @Test + public void verifyThatExplicitNamespaceAltersDefMd5() { + DefParser parser = createParser("version=1\na string\n"); + CNode root = parser.getTree(); + + parser = createParser("version=1\nnamespace=myproject.config\na string\n"); + CNode namespaceRoot = parser.getTree(); + + assertThat(root.defMd5, not(namespaceRoot.defMd5)); + } + + + @Test(expected = CodegenRuntimeException.class) + public void verify_fail_on_illegal_char_in_namespace() { + createParser("version=1\nnamespace=Foo\na string\n").getTree(); + } + + @Test(expected = CodegenRuntimeException.class) + public void verify_fail_on_com_yahoo_in_explicit_namespace() { + createParser("version=1\n" + + "namespace=com.yahoo.myproject.config\n" + + "a string\n").getTree(); + } + + @Test + public void testInvalidType() { + Class<?> exceptionClass = DefParser.DefParserException.class; + try { + createParser("version=1\n" + + "# comment\n" + + "a sting").getTree(); + fail("Didn't find expected exception of type " + exceptionClass); + } catch (Exception e) { + assertExceptionAndMessage((Exception) e.getCause(), exceptionClass, + "Error when parsing line 3: a sting", false); + } + } + + @Test + public void testValidVersions() { + try { + testExpectedVersion("version=8", "8"); + testExpectedVersion("version=8-1", "8-1"); + testExpectedVersion("version =8", "8"); + testExpectedVersion("version = 8", "8"); + testExpectedVersion("version = 8 ", "8"); + testExpectedVersion("version =\t8", "8"); + } catch (Exception e) { + e.printStackTrace(); + fail(); + } + } + + private void testExpectedVersion(String versionLine, String expectedVersion) { + InnerCNode root = createParser(versionLine).getTree(); + assertThat(root.defVersion, is(expectedVersion)); + } + + @Test + public void testMissingVersion() { + try { + createParser("a string\n").parse(); + } catch (Exception e) { + fail("Should not get an exception here"); + } + } + + private DefParser createParser(String def) { + return new DefParser("test", new StringReader(def)); + } + + @Test + public void testInvalidVersion() { + Class<?> exceptionClass = DefParser.DefParserException.class; + testInvalidVersion("version=a\n", exceptionClass, + "Error when parsing line 1: version=a\nversion=a"); + testInvalidVersion("version = a\n", exceptionClass, + "Error when parsing line 1: version = a\n a"); + } + + private void testInvalidVersion(String versionLine, Class<?> exceptionClass, String exceptionMessage) { + try { + createParser(versionLine).parse(); + fail("Didn't find expected exception of type " + exceptionClass); + } catch (Exception e) { + assertExceptionAndMessage(e, exceptionClass, exceptionMessage); + } + } + + @Test + public void verify_fail_on_default_for_file() { + Class<?> exceptionClass = DefParser.DefParserException.class; + DefParser parser = createParser("version=1\nf file default=\"file1.txt\"\n"); + try { + parser.getTree(); + fail("Didn't find expected exception of type " + exceptionClass); + } catch (Exception e) { + assertExceptionAndMessage((Exception) e.getCause(), exceptionClass, + "Error when parsing line 2: f file default=\"file1.txt\"\n" + + "Invalid default value", false); + } + } + + // Helper method for checking correct exception class and message + void assertExceptionAndMessage(Exception e, Class<?> exceptionClass, String message) { + assertExceptionAndMessage(e, exceptionClass, message, true); + } + + // Helper method for checking correct exception class and message + void assertExceptionAndMessage(Exception e, Class<?> exceptionClass, String message, boolean exact) { + if (exact) { + assertEquals(message, e.getMessage()); + } else { + assertTrue(e.getMessage().startsWith(message)); + } + assertEquals(exceptionClass.getName(), e.getClass().getName()); + } + + @Test(expected = CodegenRuntimeException.class) + @Ignore("Not implemented yet") + public void testInvalidEnum() throws DefParser.DefParserException { + DefParser parser = createParser("version=1\nanEnum enum {A, B, A}\n"); + //parser.validateDef(def); + } + + @Test + public void testEnum() { + StringBuilder sb = createDefTemplate(); + sb.append("enum1 enum {A,B} default=A\n"); + sb.append("enum2 enum {A, B} default=A\n"); + sb.append("enum3 enum { A, B} default=A\n"); + sb.append("enum4 enum { A, B } default=A\n"); + sb.append("enum5 enum { A , B } default=A\n"); + sb.append("enum6 enum {A , B } default=A\n"); + sb.append("enumVal enum { FOO, BAR, FOOBAR }\n"); + + DefParser parser = createParser(sb.toString()); + try { + parser.getTree(); + } catch (Exception e) { + assertNotNull(null); + } + CNode root = parser.getTree(); + LeafCNode node = (LeafCNode) root.getChild("enum1"); + assertNotNull(node); + assertThat(node.getDefaultValue().getStringRepresentation(), is("A")); + } + + @Test(expected = DefParser.DefParserException.class) + public void testInvalidCommaInEnum() throws DefParser.DefParserException, IOException { + String invalidEnum = "anEnum enum {A, B, } default=A\n"; + String validEnum = "anotherEnum enum {A, B} default=A\n"; + StringBuilder sb = createDefTemplate(); + sb.append(invalidEnum); + sb.append(validEnum); + DefParser parser = createParser(sb.toString()); + parser.parse(); + } + + @Ignore //TODO: finish this! The numeric leaf nodes must contain their range. + @Test + public void testRanges() { + StringBuilder sb = new StringBuilder("version=1\n"); + sb.append("i int range=[0,10]"); + sb.append("l long range=[-1e20,0]"); + sb.append("d double range=[0,1]"); + + DefParser parser = createParser(sb.toString()); + CNode root = parser.getTree(); + LeafCNode.IntegerLeaf intNode = (LeafCNode.IntegerLeaf) root.getChild("i"); + } + + @Test + public void testInvalidLine() { + Class<?> exceptionClass = DefParser.DefParserException.class; + StringBuilder sb = createDefTemplate(); + String invalidLine = "a inta\n"; + sb.append(invalidLine); + try { + createParser(sb.toString()).parse(); + fail("Didn't find expected exception of type " + exceptionClass); + } catch (Exception e) { + assertExceptionAndMessage(e, exceptionClass, + "Error when parsing line 3: " + invalidLine + "Could not create inta a"); + } + } + + @Test + public void testDuplicateDefinition() { + Class<?> exceptionClass = DefParser.DefParserException.class; + StringBuilder sb = createDefTemplate(); + String invalidLine = "b int\n"; + sb.append(invalidLine); + // Add a duplicate line, which should be illegal + sb.append(invalidLine); + try { + createParser(sb.toString()).parse(); + fail("Didn't find expected exception of type " + exceptionClass); + } catch (Exception e) { + assertExceptionAndMessage(e, exceptionClass, + "Error when parsing line 4: " + invalidLine + "b is already defined"); + } + } + + @Test + public void testIllegalCharacterInName() { + Class<?> exceptionClass = DefParser.DefParserException.class; + StringBuilder sb = createDefTemplate(); + String invalidLine = "a-b int\n"; + sb.append(invalidLine); + try { + createParser(sb.toString()).parse(); + fail("Didn't find expected exception of type " + exceptionClass); + } catch (Exception e) { + assertExceptionAndMessage(e, exceptionClass, + "Error when parsing line 3: " + invalidLine + "a-b contains unexpected character"); + } + } + + @Test + public void require_that_parameter_name_starting_with_digit_is_illegal() { + Class<?> exceptionClass = DefParser.DefParserException.class; + StringBuilder sb = createDefTemplate(); + String invalidLine = "1a int\n"; + sb.append(invalidLine); + try { + createParser(sb.toString()).parse(); + fail("Didn't find expected exception of type " + exceptionClass); + } catch (Exception e) { + assertExceptionAndMessage(e, exceptionClass, + "Error when parsing line 3: " + invalidLine + "1a must start with a non-digit character"); + } + } + + @Test + public void require_that_parameter_name_starting_with_uppercase_is_illegal() { + Class<?> exceptionClass = DefParser.DefParserException.class; + StringBuilder sb = createDefTemplate(); + String invalidLine = "SomeInt int\n"; + sb.append(invalidLine); + try { + createParser(sb.toString()).parse(); + fail("Didn't find expected exception of type " + exceptionClass); + } catch (Exception e) { + assertExceptionAndMessage(e, exceptionClass, + "Error when parsing line 3: " + invalidLine + "'SomeInt' cannot start with an uppercase letter"); + } + } + + @Test + public void require_that_parameter_name_starting_with_the_internal_prefix_is_illegal() { + String internalPrefix = ReservedWords.INTERNAL_PREFIX; + Class<?> exceptionClass = DefParser.DefParserException.class; + StringBuilder sb = createDefTemplate(); + String invalidLine = internalPrefix + "i int\n"; + sb.append(invalidLine); + try { + createParser(sb.toString()).parse(); + fail("Didn't find expected exception of type " + exceptionClass); + } catch (Exception e) { + assertExceptionAndMessage(e, exceptionClass, + "Error when parsing line 3: " + invalidLine + + "'" + internalPrefix + "i' cannot start with '" + internalPrefix + "'"); + } + } + + @Test + public void testIllegalArray() { + Class<?> exceptionClass = DefParser.DefParserException.class; + StringBuilder sb = createDefTemplate(); + String invalidLine = "intArr[ int\n"; + sb.append(invalidLine); + try { + createParser(sb.toString()).parse(); + fail("Didn't find expected exception of type " + exceptionClass); + } catch (Exception e) { + assertExceptionAndMessage(e, exceptionClass, + "Error when parsing line 3: " + invalidLine + "intArr[ Expected ] to terminate array definition"); + } + } + + @Test + public void testIllegalDefault() { + Class<?> exceptionClass = DefParser.DefParserException.class; + StringBuilder sb = createDefTemplate(); + String invalidLine = "a int deflt 10\n"; + sb.append(invalidLine); + try { + createParser(sb.toString()).parse(); + fail("Didn't find expected exception of type " + exceptionClass); + } catch (Exception e) { + assertExceptionAndMessage(e, exceptionClass, + "Error when parsing line 3: " + invalidLine + " deflt 10"); + } + } + + @Test + public void testReservedWordInC() { + Class<?> exceptionClass = DefParser.DefParserException.class; + StringBuilder sb = createDefTemplate(); + String invalidLine = "auto int\n"; + sb.append(invalidLine); + try { + createParser(sb.toString()).parse(); + fail("Didn't find expected exception of type " + exceptionClass); + } catch (Exception e) { + assertExceptionAndMessage(e, exceptionClass, + "Error when parsing line 3: " + invalidLine + "auto is a reserved word in C"); + } + } + + @Test + public void testReservedWordInJava() { + Class<?> exceptionClass = DefParser.DefParserException.class; + StringBuilder sb = createDefTemplate(); + String invalidLine = "abstract int\n"; + sb.append(invalidLine); + try { + createParser(sb.toString()).parse(); + fail("Didn't find expected exception of type " + exceptionClass); + } catch (Exception e) { + assertExceptionAndMessage(e, exceptionClass, + "Error when parsing line 3: " + invalidLine + "abstract is a reserved word in Java"); + } + } + + @Test + public void testReservedWordInCAndJava() { + Class<?> exceptionClass = DefParser.DefParserException.class; + StringBuilder sb = createDefTemplate(); + String invalidLine = "continue int\n"; + sb.append(invalidLine); + try { + createParser(sb.toString()).parse(); + fail("Didn't find expected exception of type " + exceptionClass); + } catch (Exception e) { + assertExceptionAndMessage(e, exceptionClass, + "Error when parsing line 3: " + invalidLine + "continue is a reserved word in C and Java"); + } + } + + @Test + public void testNumberInNamespace() throws IOException, DefParser.DefParserException { + StringBuilder sb = createDefTemplate(); + String line = "namespace=a.b.c2\nfoo int\n"; + sb.append(line); + createParser(sb.toString()).parse(); + + sb = createDefTemplate(); + line = "namespace=2.a.b\n"; + sb.append(line); + Class<?> exceptionClass = DefParser.DefParserException.class; + try { + createParser(sb.toString()).parse(); + fail("Didn't find expected exception of type " + exceptionClass); + } catch (Exception e) { + assertExceptionAndMessage(e, exceptionClass, + "Error when parsing line 3: " + line + "namespace=2.a.b"); + } + + sb = createDefTemplate(); + line = "namespace=a.b.2c\n"; + sb.append(line); + exceptionClass = DefParser.DefParserException.class; + try { + createParser(sb.toString()).parse(); + fail("Didn't find expected exception of type " + exceptionClass); + } catch (Exception e) { + assertExceptionAndMessage(e, exceptionClass, + "Error when parsing line 3: " + line + "namespace=a.b.2c"); + } + } + + @Test + public void testUnderscoreInNamespace() throws IOException, DefParser.DefParserException { + StringBuilder sb = createDefTemplate(); + String line = "namespace=a_b.c\nfoo int\n"; + sb.append(line); + createParser(sb.toString()).parse(); + + sb = createDefTemplate(); + line = "namespace=a_b.c_d\nfoo int\n"; + sb.append(line); + createParser(sb.toString()).parse(); + } + + private StringBuilder createDefTemplate() { + StringBuilder sb = new StringBuilder(); + sb.append("version=8\n"); + // Add a comment line to check that we get correct line number with comments + sb.append("# comment\n"); + + return sb; + } +} diff --git a/configgen/src/test/java/com/yahoo/config/codegen/MakeConfigTest.java b/configgen/src/test/java/com/yahoo/config/codegen/MakeConfigTest.java new file mode 100644 index 00000000000..a9f29fd23dd --- /dev/null +++ b/configgen/src/test/java/com/yahoo/config/codegen/MakeConfigTest.java @@ -0,0 +1,80 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.config.codegen; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.IOException; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class MakeConfigTest { + + File dest; + + @Before + public void setUp() { + dest = new File("/tmp/"+System.currentTimeMillis()+File.separator); + dest.mkdir(); + } + + @After + public void tearDown() { + recursiveDeleteDir(dest); + } + + private boolean recursiveDeleteDir(File dir) { + if (dir.isDirectory()) { + String[] children = dir.list(); + + for (int i = 0; i < children.length; i++) { + boolean success = recursiveDeleteDir(new File(dir, children[i])); + + if (!success) return false; + } + } + + // The directory is now empty so delete it + return dir.delete(); + } + + @Test + public void testProps() throws PropertyException { + long ts = System.currentTimeMillis(); + System.setProperty("config.dumpTree", "true"); + System.setProperty("config.useFramework", "true"); + System.setProperty("config.requireNamespace", "true"); + System.setProperty("config.dest", dest.getAbsolutePath()); + System.setProperty("config.spec", "src/test/resources/allfeatures.def"); + MakeConfigProperties p = new MakeConfigProperties(); + assertEquals(p.destDir.getAbsolutePath(), dest.getAbsolutePath()); + assertTrue(p.dumpTree); + assertTrue(p.generateFrameworkCode); + assertEquals(p.specFiles.length, 1); + assertEquals(p.specFiles[0].getAbsolutePath(), new File("src/test/resources/allfeatures.def").getAbsolutePath()); + + System.setProperty("config.dumpTree", "false"); + System.setProperty("config.useFramework", "false"); + System.setProperty("config.requireNamespace", "false"); + System.setProperty("config.dest", dest.getAbsolutePath()); + System.setProperty("config.spec", "src/test/resources/allfeatures.def,src/test/resources/bar.foo.def"); + p = new MakeConfigProperties(); + assertEquals(p.destDir.getAbsolutePath(), dest.getAbsolutePath()); + assertFalse(p.dumpTree); + assertFalse(p.generateFrameworkCode); + assertEquals(p.specFiles.length, 2); + } + + @Test + public void testMake() throws IOException, InterruptedException { + System.setProperty("config.dumpTree", "true"); + System.setProperty("config.useFramework", "true"); + System.setProperty("config.requireNamespace", "true"); + System.setProperty("config.dest", dest.getAbsolutePath()); + System.setProperty("config.spec", "src/test/resources/allfeatures.def"); + MakeConfig.main(new String[]{}); + } + +} diff --git a/configgen/src/test/java/com/yahoo/config/codegen/NormalizedDefinitionTest.java b/configgen/src/test/java/com/yahoo/config/codegen/NormalizedDefinitionTest.java new file mode 100644 index 00000000000..f0713a090ec --- /dev/null +++ b/configgen/src/test/java/com/yahoo/config/codegen/NormalizedDefinitionTest.java @@ -0,0 +1,77 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.config.codegen; + +import static org.junit.Assert.*; + +import java.io.*; +import java.util.List; + +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; + + +/** + * @author <a href="musum@yahoo-inc.com">Harald Musum</a> + */ +public class NormalizedDefinitionTest { + + @Test + public void testNormalizingFromReader() { + String def = + "version=1\n" + + "aString string \n" + + "anInt int #comment \n" + + "aStringCommentCharacterAfter string default=\"ab\" #foo\n" + + "aStringWithCommentCharacter string default=\"a#b\"\n" + + "aStringWithEscapedQuote string default=\"a\"b\"\n"; + + StringReader reader = new StringReader(def); + + NormalizedDefinition nd = new NormalizedDefinition(); + List<String> out = null; + try { + nd.normalize(new BufferedReader(reader)); + out = nd.getNormalizedContent(); + } catch (IOException e) { + e.printStackTrace(); + } + + assertNotNull(out); + assertThat(out.size(), is(6)); + assertThat(out.get(0), is ("version=1\n")); + assertThat(out.get(1), is ("aString string\n")); + assertThat(out.get(2), is ("anInt int\n")); + assertThat(out.get(3), is ("aStringCommentCharacterAfter string default=\"ab\"\n")); + assertThat(out.get(4), is ("aStringWithCommentCharacter string default=\"a#b\"\n")); + assertThat(out.get(5), is ("aStringWithEscapedQuote string default=\"a\"b\"\n")); + + reader.close(); + } + + @Test + public void testNormalizingFromFile() throws IOException { + FileReader fileReader = null; + try { + fileReader = new FileReader("src/test/resources/allfeatures.def"); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + + NormalizedDefinition nd = new NormalizedDefinition(); + List<String> out = null; + try { + nd.normalize(new BufferedReader(fileReader)); + out = nd.getNormalizedContent(); + } catch (IOException e) { + e.printStackTrace(); + } + + assertNotNull(out); + assertThat(out.size(), is(70)); + assertThat(out.get(19), is ("version=11\n")); + + assertNotNull(fileReader); + fileReader.close(); + } +} |