/* * Copyright (c) 2014, 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.DTMAxisTraverser; 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 Traverser walking. */ public class TestDTMTrav extends JAXPBaseTest { /** * DTM instance. */ private final DTM dtm; /* * Intiate by creating DTM instance and generate initial context. * */ public TestDTMTrav() { dtm = generateDTM(); } /** * A provider that provides validation parameters and expected lines. * * @return an two dimensional array that provide axis type and expected * lines. */ @DataProvider public Object[][] travProvider(){ 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[][] chkNamespaceDecls = new Object[][]{ {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[][]{ { 65557, "ELEMENT", "Ac1", 4, null}, { 65556, "ELEMENT", "Ac", 3, null}, { 65555, "ELEMENT", "Ab", 3, null}, { 65554, "ELEMENT", "Aa", 3, null}, { 65550, "ELEMENT", "F", 8, null}, { 65549, "ELEMENT", "E", 7, null}, { 65548, "ELEMENT", "D", 6, null}, { 65547, "ELEMENT", "C", 5, null}, { 65546, "ELEMENT", "B", 4, null}, { 65545, "COMMENT", "#comment", 4, " A Subtree "}, { 65544, "ELEMENT", "A", 3, null}, { 65543, "PROCESSING_INSTRUCTION", "api", 3, "a1=\"yes\" a2=\"no\""}, { 65542, "COMMENT", "#comment", 3, " Default test document "}, }; final Object[][] chkPrecedingsibling = new Object[][]{ { 65556, "ELEMENT", "Ac", 3, null}, { 65555, "ELEMENT", "Ab", 3, null}, { 65554, "ELEMENT", "Aa", 3, null}, { 65544, "ELEMENT", "A", 3, null}, { 65543, "PROCESSING_INSTRUCTION", "api", 3, "a1=\"yes\" a2=\"no\""}, { 65542, "COMMENT", "#comment", 3, " Default test document "} }; 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[][]{ { 65549, "ELEMENT", "E", 7, null}, { 65548, "ELEMENT", "D", 6, null}, { 65547, "ELEMENT", "C", 5, null}, { 65546, "ELEMENT", "B", 4, null}, { 65544, "ELEMENT", "A", 3, null}, { 65537, "ELEMENT", "Document", 2, null}, { 65536, "DOCUMENT", "#document", 1, null}, }; final Object[][] checkAncestorSelf = new Object[][]{ { 65550, "ELEMENT", "F", 8, null}, { 65549, "ELEMENT", "E", 7, null}, { 65548, "ELEMENT", "D", 6, null}, { 65547, "ELEMENT", "C", 5, null}, { 65546, "ELEMENT", "B", 4, null}, { 65544, "ELEMENT", "A", 3, null}, { 65537, "ELEMENT", "Document", 2, null}, { 65536, "DOCUMENT", "#document", 1, null} }; final Object[][] checkAllFromNode = new Object[][]{ { 65550, "ELEMENT", "F", 8, null}, { 65551, "NAMESPACE", "xmlns:f", 9, "www.f.com"}, { 65552, "ATTRIBUTE", "a1", 9, "down"}, { 65553, "ATTRIBUTE", "a2", 9, "up"} }; final Object[][] checkAttribute = new Object[][]{ { 65540, "ATTRIBUTE", "a1", 3, "hello"}, { 65541, "ATTRIBUTE", "a2", 3, "goodbye"} }; final Object[][] checkAll = new Object[][]{ { 65536, "DOCUMENT", "#document", 1, null}, { 65537, "ELEMENT", "Document", 2, null}, { 65538, "NAMESPACE", "xmlns:xml", 3, "http://www.w3.org/XML/1998/namespace"}, { 65539, "NAMESPACE", "xmlns:d", 3, "www.d.com"}, { 65540, "ATTRIBUTE", "a1", 3, "hello"}, { 65541, "ATTRIBUTE", "a2", 3, "goodbye"}, { 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}, { 65551, "NAMESPACE", "xmlns:f", 9, "www.f.com"}, { 65552, "ATTRIBUTE", "a1", 9, "down"}, { 65553, "ATTRIBUTE", "a2", 9, "up"}, { 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}, { 65559, "NAMESPACE", "xmlns:Ad", 4, "www.Ad.com"}, { 65560, "NAMESPACE", "xmlns:y", 4, "www.y.com"}, { 65561, "NAMESPACE", "xmlns:z", 4, "www.z.com"}, { 65562, "ELEMENT", "Ad1", 4, null} }; final Object[][] checkDescentsFromRoot = new Object[][]{ { 65537, "ELEMENT", "Document", 2, null}, { 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}, { 65558, "ELEMENT", "Ad", 3, null}, { 65562, "ELEMENT", "Ad1", 4, null} }; final Object[][] checkDescentsOrSelfFromRoot = new Object[][]{ { 65536, "DOCUMENT", "#document", 1, null}, { 65537, "ELEMENT", "Document", 2, null}, { 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}, { 65558, "ELEMENT", "Ad", 3, null}, { 65562, "ELEMENT", "Ad1", 4, null} }; final Object[][] checkANodeAll = 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}, { 65551, "NAMESPACE", "xmlns:f", 9, "www.f.com"}, { 65552, "ATTRIBUTE", "a1", 9, "down"}, { 65553, "ATTRIBUTE", "a2", 9, "up"} }; final Object[][] checkCNodeAll = new Object[][]{ { 65542, "COMMENT", "#comment", 3, " Default test document "} }; return new Object[][]{ {65537, Axis.CHILD, "Document", chkChild}, {65558, Axis.PARENT, "Ad", chkParent}, {65558, Axis.SELF, "Ad", chkSelf}, {65558, Axis.NAMESPACE, "Ad", chkNamespace}, {65558, Axis.NAMESPACEDECLS, "Ad", chkNamespaceDecls}, {65558, Axis.PRECEDING, "Ad", chkPreceding}, {65558, Axis.PRECEDINGSIBLING, "Ad", chkPrecedingsibling}, {65558, Axis.FOLLOWING, "Ad", chkFollowing}, {65558, Axis.FOLLOWINGSIBLING, "Ad", checkFollowingSibling}, {65544, Axis.DESCENDANT, "A", checkDescendant}, {65544, Axis.DESCENDANTORSELF, "A", checkDescendantSelf}, {65550, Axis.ANCESTOR, "F", checkAncestor}, {65550, Axis.ANCESTORORSELF, "F", checkAncestorSelf}, {65550, Axis.ALLFROMNODE, "F", checkAllFromNode}, {65537, Axis.ATTRIBUTE, "Document", checkAttribute}, {65550, Axis.ALL, "F", checkAll}, {65550, Axis.DESCENDANTSFROMROOT, "F", checkDescentsFromRoot}, {65550, Axis.DESCENDANTSORSELFFROMROOT, "F", checkDescentsOrSelfFromRoot}, {65544, Axis.ALLFROMNODE, "A", checkANodeAll}, {65542, Axis.ALLFROMNODE, "#comment", checkCNodeAll} }; } /** * Walk all kind of AxisTraversers and checking API as expected.. * * @param startNode node id to get AxisTraverser. * @param axisType Axis type. * @param expectedName expected node name. * @param expectedLines an two dimensional array that will be validate * traversal of AxisTraverser * @throws IOException if any I/O operation error. */ @Test(dataProvider = "travProvider") public void test(int startNode, int axisType, String expectedName, 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 Traverser by given axis type. assertEquals(dtm.getNodeName(startNode), expectedName); DTMAxisTraverser at = dtm.getAxisTraverser(axisType); int line = 0; // Traverse the axis and validate API output. for (int travNode = at.first(startNode); DTM.NULL != travNode; travNode = at.next(startNode, travNode), line++) { assertEquals(travNode, expectedLines[line][0]); assertEquals(getNodeName(dtm, travNode), expectedLines[line][1]); assertEquals(dtm.getNodeName(travNode), expectedLines[line][2]); Integer expectedLevel = (Integer)expectedLines[line][3]; assertEquals(dtm.getLevel(travNode), expectedLevel.shortValue()); assertEquals(dtm.getNodeValue(travNode), expectedLines[line][4]); } } }