Allon Mureinik has uploaded a new change for review. Change subject: core: Add minimal tests to XmlUtils ......................................................................
core: Add minimal tests to XmlUtils Change-Id: I81c895335dae1ca370320e4dd437dc5da8ba36cd Singed-off-by: Idan Shaby <ish...@redhat.com> Signed-off-by: Allon Mureinik <amure...@redhat.com> --- A backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/XmlUtilsTest.java 1 file changed, 66 insertions(+), 0 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/83/29083/1 diff --git a/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/XmlUtilsTest.java b/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/XmlUtilsTest.java new file mode 100644 index 0000000..74468ac --- /dev/null +++ b/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/XmlUtilsTest.java @@ -0,0 +1,66 @@ +package org.ovirt.engine.core.utils; + +import static org.junit.Assert.assertEquals; +import static org.junit.runners.Parameterized.Parameters; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.w3c.dom.Element; +import org.xml.sax.SAXException; + +@RunWith(Parameterized.class) +public class XmlUtilsTest { + @Rule + public RandomUtilsSeedingRule rusr = new RandomUtilsSeedingRule(); + + private int level; + private String value; + + public XmlUtilsTest(int level, String value) { + this.level = level; + this.value = value; + } + + @Parameters + public static List<Object[]> data() { + List<Object[]> params = new ArrayList<>(); + for (int i = 2; i < 10; ++i) { + params.add(new Object[]{i, String.valueOf(RandomUtils.instance().nextInt())}); + } + return params; + } + + @Test + public void testGetIntValueNesting() throws Exception { + int actual = XmlUtils.getIntValue(stringToElement(buildNestedXml(level, value)), "Level1"); + assertEquals(Integer.valueOf(value).intValue(), actual); + } + + @Test + public void testGetTextValue() throws Exception { + String actual = XmlUtils.getTextValue(stringToElement(buildNestedXml(level, value)), "Level1"); + assertEquals(value, actual); + } + + private static Element stringToElement(String s) throws ParserConfigurationException, IOException, SAXException { + return DocumentBuilderFactory.newInstance() + .newDocumentBuilder() + .parse(new ByteArrayInputStream(s.getBytes("UTF-8"))) + .getDocumentElement(); + } + + private static String buildNestedXml (int count, String s) { + if (count == 0) { + return s; + } + return "<Level" + count + '>' + buildNestedXml(count - 1, s) + "</Level" + count + '>'; + } +} -- To view, visit http://gerrit.ovirt.org/29083 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I81c895335dae1ca370320e4dd437dc5da8ba36cd Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Allon Mureinik <amure...@redhat.com> _______________________________________________ Engine-patches mailing list Engine-patches@ovirt.org http://lists.ovirt.org/mailman/listinfo/engine-patches