package rath.libxml;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.w3c.dom.NodeList;
import rath.libxml.util.Utils;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.io.File;

/**
 * User: rath
 * Date: 16/11/2013
 * Time: 09:27
 */
@RunWith(JUnit4.class)
public class XPathTest {
	@Test
	public void testCompiled() throws Exception {
		XPathExpression expr = LibXml.compileXPath("//item");

		Document doc = LibXml.parseFile(new File("sample-xmls/rss-infoq.xml"));
		XPathContext context = doc.createXPathContext();
		XPathObject result = context.evaluate(expr);

		for(Node itemNode : result.nodeset) {
//			System.out.println(itemNode);
		}

		result.dispose();
		context.dispose();
		doc.dispose();

		expr.dispose();
	}

	@Test
	public void testPlain() throws Exception {
		Document doc = LibXml.parseFile(new File("sample-xmls/rss-infoq.xml"));
		XPathContext context = doc.createXPathContext();
		XPathObject result = context.evaluate("//item");
		for(Node itemNode : result.nodeset) {

		}
	}

	@Test
	public void testJaxpNodeset() throws Exception {
		// XPathFactory impl is not allowed to use another DocumentBuilderFactory implementations
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance("rath.libxml.jaxp.DocumentBuilderFactoryImpl", null);
		XPathFactory factory = XPathFactory.newInstance(XPathFactory.DEFAULT_OBJECT_MODEL_URI, "rath.libxml.jaxp.XPathFactoryImpl", null);
//		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//		XPathFactory factory = XPathFactory.newInstance();

		XPath xpath = factory.newXPath();
		org.w3c.dom.Document doc = dbf.newDocumentBuilder().parse(new File("sample-xmls/rss-infoq.xml"));

		NodeList nl = (NodeList)xpath.evaluate("//item[1]/title", doc, XPathConstants.NODESET);
		for(int i=0; i<nl.getLength(); i++) {
			org.w3c.dom.Node node = nl.item(i);
			String title = node.getTextContent();
			Assert.assertEquals("Oracle Releases Videos and Slides from the 2013 JVM Language Summit", title);
		}
		Assert.assertNotEquals("zero length result?", 0, nl.getLength());
	}

	@Test
	public void testJaxpString() throws Exception {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance("rath.libxml.jaxp.DocumentBuilderFactoryImpl", null);
		XPathFactory factory = XPathFactory.newInstance(XPathFactory.DEFAULT_OBJECT_MODEL_URI, "rath.libxml.jaxp.XPathFactoryImpl", null);
//		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//		XPathFactory factory = XPathFactory.newInstance();

		XPath xpath = factory.newXPath();
		org.w3c.dom.Document doc = dbf.newDocumentBuilder().parse(new File("sample-xmls/rss-infoq.xml"));

		String title = (String) xpath.evaluate("//item[1]/title", doc, XPathConstants.STRING);
		Assert.assertEquals("Oracle Releases Videos and Slides from the 2013 JVM Language Summit", title);
	}

	@Test
	public void testJaxpCompiled() throws Exception {

	}
}

@@ -3010,24 +3010,596 @@
 title);%0A%09%7D%0A%0A
+%09@Test%0A%09public void testNumberResult() throws Exception %7B%0A%09%09String xml = %22%3C?xml version=%5C%221.0%5C%22?%3E%22 +%0A%09%09%09%22%3Citems%3E%22 +%0A%09%09%09%22%3Citem id=%5C%22one%5C%22%3E%3Cvalue tag=%5C%221%5C%22 /%3E%3C/item%3E%22 +%0A%09%09%09%22%3Citem id=%5C%22two%5C%22%3E%3Cvalue tag=%5C%222%5C%22 /%3E%3C/item%3E%22 +%0A%09%09%09%22%3Citem id=%5C%22three%5C%22%3E%3Cvalue tag=%5C%223%5C%22 /%3E%3C/item%3E%22 +%0A%09%09%09%22%3C/items%3E%22;%0A%0A%09%09Document doc = LibXml.parseString(xml);%0A%09%09XPathContext ctx = doc.createXPathContext();%0A%09%09XPathObject result = ctx.evaluate(%22//item%5B@id='two'%5D/value/@tag%22);%0A%09%09Assert.assertEquals(%22tag%22, 2.0D, result.castToNumber(), 0.0D);%0A%09%09Assert.assertEquals(%22tag%22, %222%22, result.castToString());%0A%09%7D%0A%0A
 %09@Test%0A%09publ

Test case for XPathObject.castToXXX()