Package org.apache.xindice.xml.dom

Source Code of org.apache.xindice.xml.dom.TreeWalkerTest

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $Id: TreeWalkerTest.java 512591 2007-02-28 03:35:44Z vgritsenko $
*/

package org.apache.xindice.xml.dom;

import org.apache.xindice.xml.dom.traversal.TreeWalkerImpl;

import junit.framework.TestCase;
import org.w3c.dom.DOMException;
import org.w3c.dom.Node;
import org.w3c.dom.traversal.NodeFilter;
import org.w3c.dom.traversal.TreeWalker;

/**
* Tests TreeWalker class
*
* @version $Revision: 512591 $, $Date: 2007-02-27 22:35:44 -0500 (Tue, 27 Feb 2007) $
*/
public class TreeWalkerTest extends TestCase {

    private static final String XML =
            "<a>" +
              "<b/>" +
              "<c>" +
                "<d>" +
                  "<e/>" +
                  "<f/>" +
                "</d>" +
                "<g/>" +
                "<h/>" +
              "</c>" +
              "<i/>" +
            "</a>";

    private Node dom;

    public void setUp() throws Exception {
        dom = DOMParser.toDocument(XML).getDocumentElement();
    }

    public void testRootNode() throws Exception {
        TreeWalker tw = new TreeWalkerImpl(dom, NodeFilter.SHOW_ALL, null, false);
        assertEquals("a", tw.getRoot().getLocalName());
    }

    public void testWhatToShow() throws Exception {
        TreeWalker tw = new TreeWalkerImpl(dom, NodeFilter.SHOW_COMMENT, null, false);
        assertEquals(NodeFilter.SHOW_COMMENT, tw.getWhatToShow());
        assertEquals(null, tw.nextNode());
    }

    public void testFilter() throws Exception {
        NodeFilter nf = new NodeFilter() {
            public short acceptNode(Node node) {
                if ("b".equals(node.getNodeName()) || "f".equals(node.getNodeName())) {
                    return NodeFilter.FILTER_ACCEPT;
                }
                return NodeFilter.FILTER_SKIP;
            }
        };

        TreeWalker tw = new TreeWalkerImpl(dom, NodeFilter.SHOW_ALL, nf, false);
        assertEquals(nf, tw.getFilter());
        assertEquals("b", tw.nextNode().getNodeName());
        assertEquals("f", tw.nextNode().getNodeName());
        assertEquals(null, tw.nextNode());
    }

    public void testNextNode() throws Exception {
        TreeWalker tw = new TreeWalkerImpl(dom, NodeFilter.SHOW_ALL, null, false);
        assertEquals("a", tw.getCurrentNode().getLocalName());

        assertEquals("b", tw.nextNode().getLocalName());
        assertEquals("c", tw.nextNode().getLocalName());
        assertEquals("d", tw.nextNode().getLocalName());
        assertEquals("e", tw.nextNode().getLocalName());
        assertEquals("f", tw.nextNode().getLocalName());
        assertEquals("g", tw.nextNode().getLocalName());
        assertEquals("h", tw.nextNode().getLocalName());
        assertEquals("i", tw.nextNode().getLocalName());
        assertEquals(null, tw.nextNode());
    }

    public void testPreviousNode() throws Exception {
        TreeWalker tw = new TreeWalkerImpl(dom, NodeFilter.SHOW_ALL, null, false);
        tw.setCurrentNode(dom.getLastChild());
        assertEquals("i", tw.getCurrentNode().getLocalName());

        assertEquals("h", tw.previousNode().getLocalName());
        assertEquals("g", tw.previousNode().getLocalName());
        assertEquals("f", tw.previousNode().getLocalName());
        assertEquals("e", tw.previousNode().getLocalName());
        assertEquals("d", tw.previousNode().getLocalName());
        assertEquals("c", tw.previousNode().getLocalName());
        assertEquals("b", tw.previousNode().getLocalName());
        assertEquals("a", tw.previousNode().getLocalName());
        assertEquals(null, tw.previousNode());
    }

    public void testCurrentNode() throws Exception {
        TreeWalker tw = new TreeWalkerImpl(dom, NodeFilter.SHOW_ALL, null, false);

        assertEquals("a", tw.getCurrentNode().getLocalName());
        assertEquals(null, tw.previousNode());
        assertEquals("a", tw.getCurrentNode().getLocalName());

        tw.setCurrentNode(dom.getLastChild());
        assertEquals("i", tw.getCurrentNode().getLocalName());
        assertEquals(null, tw.nextNode());
        assertEquals("i", tw.getCurrentNode().getLocalName());

        try {
            tw.setCurrentNode(null);
            fail("Exception expected");
        } catch (DOMException e) {
        }
        assertEquals("i", tw.getCurrentNode().getLocalName());
    }

    public void testParentNode() throws Exception {
        TreeWalker tw = new TreeWalkerImpl(dom, NodeFilter.SHOW_ALL, null, false);
        // Go to F node
        tw.nextNode();
        tw.nextNode();
        tw.nextNode();
        tw.nextNode();
        tw.nextNode();
        assertEquals("f", tw.getCurrentNode().getLocalName());

        assertEquals("d", tw.parentNode().getLocalName());
        assertEquals("c", tw.parentNode().getLocalName());
        assertEquals("a", tw.parentNode().getLocalName());
        assertEquals(null, tw.parentNode());
    }

}
TOP

Related Classes of org.apache.xindice.xml.dom.TreeWalkerTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.