-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
71 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
spy-common-ui/src/test/java/pl/baczkowicz/spy/xpath/XPathTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package pl.baczkowicz.spy.xpath; | ||
|
||
import java.io.StringReader; | ||
|
||
import javax.xml.xpath.XPath; | ||
import javax.xml.xpath.XPathConstants; | ||
import javax.xml.xpath.XPathExpression; | ||
import javax.xml.xpath.XPathExpressionException; | ||
import javax.xml.xpath.XPathFactory; | ||
|
||
import org.junit.Test; | ||
import org.xml.sax.InputSource; | ||
|
||
import junit.framework.TestCase; | ||
|
||
public class XPathTest extends TestCase | ||
{ | ||
|
||
@Test | ||
public void test() | ||
{ | ||
final String message = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + | ||
|
||
"<book category=\"COOKING\"> <year>2005</year> <price>30.00</price> </book>"; | ||
|
||
assertEquals(evaluateXPath("/book/price", message), 30.0); | ||
} | ||
|
||
protected Double evaluateXPath(final String expression, final String message) | ||
{ | ||
Double value = 0.0; | ||
final XPath xpath = XPathFactory.newInstance().newXPath(); | ||
final InputSource inputSource = new InputSource(new StringReader(message)); | ||
|
||
try | ||
{ | ||
final XPathExpression exp = xpath.compile(expression); | ||
value = (Double) exp.evaluate(inputSource, XPathConstants.NUMBER); | ||
} | ||
catch (XPathExpressionException e) | ||
{ | ||
// TODO | ||
} | ||
|
||
return value; | ||
} | ||
} |