DefParser { public InnerCNode getTree() throws CodegenRuntimeException { try { if (root == null) parse(); } catch (DefParserException | IOException e) { throw new CodegenRuntimeException("Error parsing or reading config definition." + e.getMessage(), e); } return root; } DefParser(String name, Reader defReader); InnerCNode getTree(); NormalizedDefinition getNormalizedDefinition(); static void dumpTree(CNode root, String indent); static final String DEFAULT_PACKAGE_PREFIX; static final Pattern versionPattern; static final Pattern namespacePattern; static final Pattern packagePattern; }
@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(34)); 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)); CNode myArray = root.getChild("myArray"); assertThat(myArray.getChildren().length, is(5)); LeafCNode myArrayInt = (LeafCNode) myArray.getChild("intVal"); assertThat(myArrayInt.getDefaultValue().getValue(), is("14")); LeafCNode myArrayEnum = (LeafCNode) myArray.getChild("enumVal"); assertThat(myArrayEnum.getDefaultValue().getValue(), is("TYPE")); 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 + "baz.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("f901bdc5c96e7005130399c63f247823")); }
@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 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")); }
@Ignore @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"); }