/* * 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.FilePermission; import java.util.ArrayList; import java.util.List; import jaxp.library.JAXPBaseTest; import static org.apache.qetest.dtm.QeDtmConst.XML_DIR; import static org.apache.qetest.dtm.QeDtmConst.createDTM; import static org.testng.Assert.assertEquals; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; /** * Basic API test for DTMAxisTraverser. Traverser on different Axis type. * */ public class TestDTMTravDeep extends JAXPBaseTest { /** * Traverser on different Axis type. * @param axisType axis type. * @param expectedLastNode expected last node we will validate. * @param expectedTotal expected total node number we will validate. */ @Test(dataProvider = "axisProvider") public void test(int axisType, int expectedLastNode, int expectedTotal) { setPermissions(new RuntimePermission("accessClassInPackage.com.sun.org.apache.xml.internal.utils"), new RuntimePermission("accessClassInPackage.com.sun.org.apache.xpath.internal.objects"), new FilePermission(System.getProperty("test.src") + "/-", "read")); String deepFile = XML_DIR + "xtestdata/elem10kdeep.xml"; List lines = new ArrayList<>(); lines.add("Axis is " + Axis.getNames(axisType).toUpperCase()); DTM dtm = createDTM(0, deepFile, lines); // Get various nodes to use as context nodes. int dtmRoot = dtm.getDocument(); // #document int DNode = dtm.getFirstChild(dtmRoot); // // returns Traversal time, last node, number of nodes traversed assertEquals(dtm.getNodeName(dtm.getFirstChild(DNode)), "e1"); // Get a iterator for Descendant:: axis. DTMAxisTraverser trav = dtm.getAxisTraverser(Axis.DESCENDANT); int lastNode = 0; int numOfNodes = 0; for (int atNode = trav.first(axisType); DTM.NULL != atNode; atNode = trav.next(DNode, axisType)) { lastNode = atNode; numOfNodes = numOfNodes + 1; } assertEquals(lastNode, expectedLastNode); assertEquals(numOfNodes, expectedTotal); } /** * Axis types for testing. * @return an array contains axis type and its expected number. */ @DataProvider public Object[][] axisProvider(){ return new Object[][]{ { Axis.DESCENDANT, 65536, 1}, { Axis.DESCENDANTORSELF, 65536, 1}, { Axis.ANCESTOR, 65536, 1}, { Axis.ANCESTORORSELF, 65536, 1}, }; } }