@Test public void givenCloseTag_WhenParse_ThenNameOfTheTagEqualsNameOfTheInputTag() { String inputString = "<name></name>"; handler = new Handler() { @Override public void handle(Element element) { myElement = element; } }; builder.onCloseTag(handler); parser = builder.build(); parser.parse(inputString); assertEquals("name", myElement.getName()); }
@Test public void givenTagWithSingleTagElement_WhenParseWholeTag_ThenOnSingleTagInvokedAndCheckName() { String inputString = "<? xml ?><tagname param = \"param 'value'\"/>"; handler = new Handler() { @Override public void handle(Element element) { myElement = element; } }; builder.onSingleTag(handler); parser = builder.build(); parser.parse(inputString); assertEquals("tagname", myElement.getName()); }
@Test public void givenErrorSingleTag_WhenParse_ThenOnErrorTagInvoked() { String inputString = "<? xml ?><tagname param = \"param 'value'\"/6>"; handler = new Handler() { @Override public void handle(Element element) { myElement = element; } }; builder.onError(handler); parser = builder.build(); parser.parse(inputString); String actual = myElement.getValue(); assertEquals("XML Syntax Error", actual); }
@Test public void givenOpenTag_WhenParse_ThenTypeOfTheTagEqualsOPEN() { String inputString = "<name>"; handler = new Handler() { @Override public void handle(Element element) { myElement = element; } }; builder.onOpenTag(handler); parser = builder.build(); parser.parse(inputString); TagElementType actual = myElement.getType(); TagElementType expected = TagElementType.OPEN; assertEquals(expected, actual); }
@Test public void givenCommentInsideTextValue() { String inputString = "<? xml ?><tagname>Text<!--comment_text-->Value</tagname>"; handler = new Handler() { @Override public void handle(Element element) { myElement = element; } }; builder.onTextValue(handler); parser = builder.build(); parser.parse(inputString); String expected = "TextValue"; String actual = myElement.getValue(); assertEquals(expected, actual); }
@Test public void givenValidTagWithTextValue_WhenParseWholeTag_ThenOnTextValueInvoked() { String inputString = "<? xml ?><tagname>TextValue</tagname>"; handler = new Handler() { @Override public void handle(Element element) { myElement = element; } }; builder.onTextValue(handler); parser = builder.build(); parser.parse(inputString); String expected = "TextValue"; String actual = myElement.getValue(); assertEquals(expected, actual); }
@Test public void givenHeader_WhenParse_ThenTypeOfTheTagEqualsHEADER() { String inputString = "<? xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>"; handler = new Handler() { @Override public void handle(Element element) { myElement = element; } }; builder.onStart(handler); parser = builder.build(); parser.parse(inputString); TagElementType actual = myElement.getType(); TagElementType expected = TagElementType.HEADER; assertEquals(expected, actual); }
@Test public void givenCloseTag_WhenHandleSetsTypeOfTag_ThenTypeOfTheTagEqualsCLOSE() { String inputString = "<name></name>"; handler = new Handler() { @Override public void handle(Element element) { myElement = element; } }; builder.onCloseTag(handler); parser = builder.build(); parser.parse(inputString); TagElementType actual = myElement.getType(); TagElementType expected = TagElementType.CLOSE; assertEquals(expected, actual); }
@Test public void givenTagWithAttributes_WhenParseWholeTag_ThenOnOpenTagInvokedAndCheckAttributes() { String inputString = "<? xml ?><tagname param = \"param value\">TextValue</tagname>"; handler = new Handler() { @Override public void handle(Element element) { myElement = element; } }; builder.onOpenTag(handler); parser = builder.build(); parser.parse(inputString); String expected = "param value"; Map<String, String> params = myElement.getParams(); String actual = params.get("param"); assertEquals(expected, actual); }