Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b0f2ac9

Browse filesBrowse files
committed
test for selectSingleElement
1 parent 3148a9e commit b0f2ac9
Copy full SHA for b0f2ac9

2 files changed

+40Lines changed: 40 additions & 0 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎biojava-core/src/main/java/org/biojava/nbio/core/util/XMLHelper.java‎

Copy file name to clipboardExpand all lines: biojava-core/src/main/java/org/biojava/nbio/core/util/XMLHelper.java
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,17 @@ public static Element selectParentElement(Element element, String parentName) {
158158
return selectParentElement(parentElement, parentName);
159159
}
160160

161+
/**
162+
* If {@code}xpathExpression{@code} is a plain string with no '/' characterr, this is
163+
* interpreted as a child element name to search for.
164+
* <b/>
165+
* If {@code}xpathExpression{@code} is an XPath expression, this is evaluated and is assumed
166+
* to identify a single element.
167+
* @param element
168+
* @param xpathExpression
169+
* @return A single element or null if no match or the 1st match if matches more than 1
170+
* @throws XPathExpressionException
171+
*/
161172
public static Element selectSingleElement(Element element, String xpathExpression) throws XPathExpressionException {
162173
if (xpathExpression.indexOf("/") == -1) {
163174
NodeList nodeList = element.getChildNodes();
Collapse file

‎biojava-core/src/test/java/org/biojava/nbio/core/util/XMLHelperTest.java‎

Copy file name to clipboardExpand all lines: biojava-core/src/test/java/org/biojava/nbio/core/util/XMLHelperTest.java
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import javax.xml.parsers.ParserConfigurationException;
1616
import javax.xml.transform.TransformerException;
17+
import javax.xml.xpath.XPathExpressionException;
1718

1819
import org.junit.jupiter.api.DisplayName;
1920
import org.junit.jupiter.api.Test;
@@ -81,14 +82,42 @@ void documentToOutputStream() throws SAXException, IOException, ParserConfigurat
8182
@Test
8283
void selectParentElement() throws SAXException, IOException, ParserConfigurationException{
8384
Document doc = readTestDoc();
85+
// get a a grandchild element
8486
NodeList nodes = doc.getElementsByTagName("a");
87+
// can get root node
8588
Element el = (Element)nodes.item(0);
8689
Element root = XMLHelper.selectParentElement(el, "root");
8790
assertNotNull(root);
91+
// non-existing element or if is root node returns null
8892
assertNull(XMLHelper.selectParentElement(el, "notexisting"));
8993
assertNull(XMLHelper.selectParentElement(root, "notexisting"));
9094

9195
}
96+
97+
@Test
98+
void selectSingleElement() throws SAXException, IOException, ParserConfigurationException, XPathExpressionException{
99+
Document doc = readTestDoc();
100+
Element root = (Element)doc.getElementsByTagName("root").item(0);
101+
102+
// not direct child
103+
assertNull(XMLHelper.selectSingleElement(root, "a"));
104+
105+
// direct child
106+
assertNotNull(XMLHelper.selectSingleElement(root, "list"));
107+
108+
// xpath match
109+
Element found = XMLHelper.selectSingleElement(root, "/root/list/a[@id = \"2\"]");
110+
assertNotNull(found);
111+
assertEquals("2", found.getAttribute("id"));
112+
113+
// xpath no match
114+
Element Notfound = XMLHelper.selectSingleElement(root, "/root/list/a[@id = \"45\"]");
115+
assertNull(Notfound);
116+
117+
// xpath returning multiple elements returns 1st element
118+
Element mult = XMLHelper.selectSingleElement(root, "/root/list/a");
119+
assertNotNull(mult);
120+
}
92121
void assertParsedDocument (Document doc){
93122
assertNotNull(doc);
94123
assertEquals(2, doc.getElementsByTagName("a").getLength());

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.