package dk.statsbiblioteket.util.xml;

import junit.framework.TestCase;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import static dk.statsbiblioteket.util.xml.DOM.*;

/**
 * Test cases for the {@code DOM.select*} methods
 */
public class DOMSelectTest extends TestCase {

    static final String SIMPLE_XML =
        DOM.XML_HEADER +
        "<body>"+
        "  <double>1.1234</double>"+
        "  <boolean>true</boolean>"+
        "  <string>foobar</string>"+
        "  <integer>27</integer>"+
        "</body>";

    Document dom;

    public void setUp() {
        clearXPathCache();
        dom = stringToDOM(SIMPLE_XML);
        assertNotNull(dom);
    }

     public void testSelectInteger() {
        Integer i = selectInteger(dom, "asdfg");
        assertEquals(null, i);

        i = selectInteger(dom, "asdfg", 1);
        assertEquals(1, i.intValue());

        i = selectInteger(dom, "/body/integer");
        assertEquals(27, i.intValue());
    }

    public void testSelectDouble() {
        Double d = selectDouble(dom, "asdfg");
        assertEquals(null, d);
        
        d = selectDouble(dom, "asdfg", 1.1);
        assertEquals(1.1, d);

        d = selectDouble(dom, "/body/double");
        assertEquals(1.1234, d);
    }

    public void testSelectBoolean() {
        Boolean b = selectBoolean(dom, "asdfg");
        assertEquals(Boolean.FALSE, b);

        b = selectBoolean(dom, "asdfg", false);
        assertEquals(Boolean.FALSE, b);

        b = selectBoolean(dom, "asdfg", null);
        assertEquals(null, b);

        b = selectBoolean(dom, "/body/boolean");
        assertEquals(Boolean.TRUE, b);
    }

    public void testSelectString() {
        String s = selectString(dom, "asdfg");
        assertEquals("", s);

        s = selectString(dom, "asdfg", "sbutil");
        assertEquals("sbutil", s);

        s = selectString(dom, "asdfg", null);
        assertEquals(null, s);

        s = selectString(dom, "/body/string");
        assertEquals("foobar", s);

        s = selectString(dom, "/body/string", "baz");
        assertEquals("foobar", s);
    }

    public void testSelectNode() {
        Node n = selectNode(dom, "asdfg");
        assertEquals(null, n);

        n = selectNode(dom, "/body");
        assertSame(dom.getFirstChild(), n);
    }

    public void testSelectNodeList() {
        NodeList l = selectNodeList(dom, "asdfg");
        assertEquals(0, l.getLength());

        // We use /body/node() because /body/* doesn't select the text nodes
        l = selectNodeList(dom, "/body/node()");
        NodeList expected = dom.getFirstChild().getChildNodes(); 
        assertSame(expected.getLength(), l.getLength());
        assertEquals(8, l.getLength());
    }
}

@@ -149,16 +149,42 @@
 eList;%0A%0A
+import java.util.Random;%0A%0A
 import s
@@ -2782,11 +2782,1400 @@
 );%0A    %7D
+%0A%0A    public void threadTest(final boolean blowCache) throws Exception %7B%0A        Thread%5B%5D threads = new Thread%5B20%5D;%0A        final Random random = new Random();%0A%0A        for (int i = 0; i %3C threads.length; i++) %7B%0A            threads%5Bi%5D = new Thread(new Runnable() %7B%0A                public void run() %7B%0A                    for (int j = 0; j %3C 50; j++) %7B%0A                        testSelectBoolean();%0A                        testSelectDouble();%0A                        testSelectInteger();%0A                        testSelectNode();%0A                        testSelectNodeList();%0A                        testSelectString();%0A%0A                        if (blowCache) %7B%0A                            for (int k = 0; k %3C 50; k++) %7B%0A                                String bleh = selectString(%0A                                        dom, %22/body/a%22 + random.nextInt(),%0A                                        %22bleh, no such node%22);%0A                                assertEquals(%22bleh, no such node%22, bleh);%0A                            %7D%0A                        %7D%0A                    %7D%0A                %7D%0A            %7D);%0A        %7D%0A%0A        for (Thread t : threads) t.start();%0A        for (Thread t : threads) t.join();%0A    %7D%0A%0A    public void testThreadsBlowCache() throws Exception %7B%0A        threadTest(true);%0A    %7D%0A%0A    public void testThreadsWithCache() throws Exception %7B%0A        threadTest(false);%0A    %7D
 %0A%7D%0A

Add two unit tests that heavily excercises the DOM.select* API from 20 threads. They both pass just fine (although I had expeccted that they wouldn't)