--- /dev/null 2014-09-08 10:45:56.830930409 -0700 +++ new/test/javax/xml/jaxp/functional/org/apache/qetest/dtm/TestDTMIter.java 2015-01-09 15:41:35.738115473 -0800 @@ -0,0 +1,221 @@ +/* + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + */ +/* + * 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. + */ +package org.apache.qetest.dtm; + +import com.sun.org.apache.xml.internal.dtm.Axis; +import com.sun.org.apache.xml.internal.dtm.DTM; +import com.sun.org.apache.xml.internal.dtm.DTMAxisIterator; +import java.io.IOException; +import jaxp.library.JAXPBaseTest; +import static org.apache.qetest.dtm.QeDtmConst.generateDTM; +import static org.apache.qetest.dtm.QeDtmConst.getNodeName; +import static org.testng.Assert.assertEquals; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +/** + * + * Test for Axis Iterator walking. + */ +public class TestDTMIter extends JAXPBaseTest { + /** + * DTM instance. + */ + private final DTM dtm; + + /* + * Intiate by creating DTM instance and generate initial context. + * + */ + public TestDTMIter() { + dtm = generateDTM(); + } + + /** + * A data provider that provide validation parameters and expected lines. + * + * @return an two dimensional array that provide axis type and expected + * lines. + */ + @DataProvider + public Object[][] iterProvider() { + final Object[][] chkChild = new Object[][] { + { 65542, "COMMENT", "#comment", 3, " Default test document " }, + { 65543, "PROCESSING_INSTRUCTION", "api", 3, "a1=\"yes\" a2=\"no\"" }, + { 65544, "ELEMENT", "A", 3, null }, + { 65554, "ELEMENT", "Aa", 3, null }, + { 65555, "ELEMENT", "Ab", 3, null }, + { 65556, "ELEMENT", "Ac", 3, null }, + { 65558, "ELEMENT", "Ad", 3, null } + }; + + final Object[][] chkParent = new Object[][]{ + {65537, "ELEMENT", "Document", 2, null} + }; + + Object[][] chkSelf = new Object[][]{ + {65558, "ELEMENT", "Ad", 3, null} + }; + + final Object[][] chkNamespace = new Object[][]{ + {65538, "NAMESPACE", "xmlns:xml", 3, "http://www.w3.org/XML/1998/namespace"}, + {65539, "NAMESPACE", "xmlns:d", 3, "www.d.com"}, + {65559, "NAMESPACE", "xmlns:Ad", 4, "www.Ad.com"}, + {65560, "NAMESPACE", "xmlns:y", 4, "www.y.com"}, + {65561, "NAMESPACE", "xmlns:z", 4, "www.z.com"} + }; + + final Object[][] chkPreceding = new Object[][]{ + { 65542, "COMMENT", "#comment", 3, " Default test document "}, + { 65543, "PROCESSING_INSTRUCTION", "api", 3, "a1=\"yes\" a2=\"no\""}, + { 65544, "ELEMENT", "A", 3, null}, + { 65545, "COMMENT", "#comment", 4, " A Subtree "}, + { 65546, "ELEMENT", "B", 4, null}, + { 65547, "ELEMENT", "C", 5, null}, + { 65548, "ELEMENT", "D", 6, null}, + { 65549, "ELEMENT", "E", 7, null}, + { 65550, "ELEMENT", "F", 8, null}, + { 65554, "ELEMENT", "Aa", 3, null}, + { 65555, "ELEMENT", "Ab", 3, null}, + { 65556, "ELEMENT", "Ac", 3, null}, + { 65557, "ELEMENT", "Ac1", 4, null} + }; + + final Object[][] chkPrecedingsibling = new Object[][]{ + {65542, "COMMENT", "#comment", 3, " Default test document "}, + {65543, "PROCESSING_INSTRUCTION", "api", 3, "a1=\"yes\" a2=\"no\""}, + {65544, "ELEMENT", "A", 3, null}, + {65554, "ELEMENT", "Aa", 3, null}, + {65555, "ELEMENT", "Ab", 3, null}, + {65556, "ELEMENT", "Ac", 3, null} + }; + + final Object[][] chkFollowing = new Object[][]{ + { 65554, "ELEMENT", "Aa", 3, null}, + { 65555, "ELEMENT", "Ab", 3, null}, + { 65556, "ELEMENT", "Ac", 3, null}, + { 65557, "ELEMENT", "Ac1", 4, null}, + { 65558, "ELEMENT", "Ad", 3, null}, + { 65562, "ELEMENT", "Ad1", 4, null} + }; + + final Object[][] checkFollowingSibling = new Object[][]{ + { 65554, "ELEMENT", "Aa", 3, null}, + { 65555, "ELEMENT", "Ab", 3, null}, + { 65556, "ELEMENT", "Ac", 3, null}, + { 65558, "ELEMENT", "Ad", 3, null}, + }; + + final Object[][] checkDescendant = new Object[][]{ + { 65545, "COMMENT", "#comment", 4, " A Subtree "}, + { 65546, "ELEMENT", "B", 4, null}, + { 65547, "ELEMENT", "C", 5, null}, + { 65548, "ELEMENT", "D", 6, null}, + { 65549, "ELEMENT", "E", 7, null}, + { 65550, "ELEMENT", "F", 8, null} + }; + + final Object[][] checkDescendantSelf = new Object[][]{ + { 65544, "ELEMENT", "A", 3, null}, + { 65545, "COMMENT", "#comment", 4, " A Subtree "}, + { 65546, "ELEMENT", "B", 4, null}, + { 65547, "ELEMENT", "C", 5, null}, + { 65548, "ELEMENT", "D", 6, null}, + { 65549, "ELEMENT", "E", 7, null}, + { 65550, "ELEMENT", "F", 8, null} + }; + + final Object[][] checkAncestor = new Object[][]{ + { 65536, "DOCUMENT", "#document", 1, null}, + { 65537, "ELEMENT", "Document", 2, null}, + { 65544, "ELEMENT", "A", 3, null}, + { 65546, "ELEMENT", "B", 4, null}, + { 65547, "ELEMENT", "C", 5, null}, + { 65548, "ELEMENT", "D", 6, null}, + { 65549, "ELEMENT", "E", 7, null} + }; + + final Object[][] checkAncestorSelf = new Object[][]{ + { 65536, "DOCUMENT", "#document", 1, null}, + { 65537, "ELEMENT", "Document", 2, null}, + { 65544, "ELEMENT", "A", 3, null}, + { 65546, "ELEMENT", "B", 4, null}, + { 65547, "ELEMENT", "C", 5, null}, + { 65548, "ELEMENT", "D", 6, null}, + { 65549, "ELEMENT", "E", 7, null}, + { 65550, "ELEMENT", "F", 8, null} + }; + return new Object[][]{ + {65537, Axis.CHILD, "Document", false, chkChild}, + {65558, Axis.PARENT, "Ad", false, chkParent}, + {65558, Axis.SELF, "Ad", false, chkSelf}, + {65558, Axis.NAMESPACE, "Ad", false, chkNamespace}, + {65558, Axis.PRECEDING, "Ad", true, chkPreceding}, + {65558, Axis.PRECEDINGSIBLING, "Ad", true, chkPrecedingsibling}, + {65558, Axis.FOLLOWING, "Ad", false, chkFollowing}, + {65558, Axis.FOLLOWINGSIBLING, "Ad", false, checkFollowingSibling}, + {65544, Axis.DESCENDANT, "A", false, checkDescendant}, + {65544, Axis.DESCENDANTORSELF, "A", false, checkDescendantSelf} , + {65550, Axis.ANCESTOR, "F", true, checkAncestor}, + {65550, Axis.ANCESTORORSELF, "F", true, checkAncestorSelf} + }; + } + + /** + * Walk all kind of AxisIterator and checking API as expected. + * + * @param startNode node id to get AxisIterator. + * @param axisType Axis type. + * @param expectedName expected node name. + * @param reversable expected boolean value for checking if the AxisIterator + * is revertible. + * @param expectedLines an two dimensional array that will be validate + * traversal of AxisIterator + * @throws IOException if any I/O operation error. + */ + @Test(dataProvider = "iterProvider") + public void test(int startNode, int axisType, String expectedName, + boolean reversable, Object[][] expectedLines) throws IOException { + // This permission is special for the testing. + setPermissions( + new RuntimePermission("accessClassInPackage.com.sun.org.apache.xml.internal.utils"), + new RuntimePermission("accessClassInPackage.com.sun.org.apache.xpath.internal.objects")); + + // Get a Iterator by given Axis type. + DTMAxisIterator iter = dtm.getAxisIterator(axisType); + iter.setStartNode(startNode); + + // Print out info about the axis + assertEquals(dtm.getNodeName(startNode), expectedName); + assertEquals(iter.isReverse(), reversable); + + // Iterate the axis and write node info to output file + int line = 0; + for (int itNode = iter.next(); DTM.NULL != itNode; + itNode = iter.next(), line++) { + assertEquals(itNode, expectedLines[line][0]); + assertEquals(getNodeName(dtm, itNode), expectedLines[line][1]); + assertEquals(dtm.getNodeName(itNode), expectedLines[line][2]); + Integer expectedLevel = (Integer)expectedLines[line][3]; + assertEquals(dtm.getLevel(itNode), expectedLevel.shortValue()); + assertEquals(dtm.getNodeValue(itNode), expectedLines[line][4]); + } + } +}