// This is free software released into the public domain (CC0 license).

package it.svario.xpathapi.jaxp.test;

import java.util.HashMap;
import java.util.Map;
import it.svario.xpathapi.jaxp.XPathAPI;
import org.testng.annotations.Test;
import org.w3c.dom.*;
import static org.testng.Assert.*;

@SuppressWarnings("javadoc")
public class SingleNodeTest extends TestBase {
	public SingleNodeTest() throws Exception {
		super();
	}

	@Test
	public void findsFirstNode() throws Exception {
		Node node = XPathAPI.selectSingleNode(doc, "//b");

		assertNotNull(node);
		assertEquals(node.getTextContent(), "aabb11");
	}

	@Test
	public void findsCorrectNode() throws Exception {
		Node node = XPathAPI.selectSingleNode(doc, "//b[2]");

		assertNotNull(node);
		assertEquals(node.getTextContent(), "aabb33");
	}

	@Test
	public void selectNodeWithNS() throws Exception {
		Node aNode = doc.getFirstChild().getFirstChild();
		Node bNode = aNode.getLastChild();
		Node node = XPathAPI.selectSingleNode(doc, "//k:b", bNode);

		assertNotNull(node);
	}

	@Test
	public void selectNodeWithNSMap() throws Exception {
		Map<String, String> namespaces = new HashMap<String, String>();
		namespaces.put("k", "abc");

		Node node = XPathAPI.selectSingleNode(doc, "//k:b", namespaces);

		assertNotNull(node);
	}

	@Test
	public void doNotIgnoreNS() throws Exception {
		Node node = XPathAPI.selectSingleNode(doc, "//a");

		assertNull(node);
	}

	@Test
	public void selectNodeWithNSDeclaredInIt() throws Exception {
		Node aNode = doc.getFirstChild().getFirstChild();
		Node node = XPathAPI.selectSingleNode(doc, "//k:a", aNode);

		assertNotNull(node);
	}

	@Test
	public void returnsNullIfNotFound() throws Exception {
		Node node = XPathAPI.selectSingleNode(doc, "//d");

		assertNull(node);
	}

	@Test
	public void extractNSFromRoot() throws Exception {
		String docStr = "<a xmlns='abc' xmlns:k='def'><k:b/></a>";
		Document doc = documentFromString(docStr);

		Node node = XPathAPI.selectSingleNode(doc, "//k:b");

		assertNotNull(node);
	}
}

@@ -2023,11 +2023,360 @@
 ode);%0A%09%7D
+%0A%0A%09@Test%0A%09public void doesNotIgnoreDefaultNSInRoot() throws Exception %7B%0A%09%09String docStr = %22%3Ca xmlns='abc'%3E%3Cb/%3E%3C/a%3E%22;%0A%09%09Document doc = documentFromString(docStr);%0A%0A%09%09Map%3CString, String%3E nsMap = new HashMap%3CString, String%3E();%0A%09%09nsMap.put(%22k%22, %22abc%22);%0A%0A%09%09Node aWithoutNS = XPathAPI.selectSingleNode(doc, %22/k:a%22, nsMap);%0A%0A%09%09assertNotNull(aWithoutNS);%0A%09%7D
 %0A%7D%0A

Test whether the default NS is treated normally