1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  */
   4 /*
   5  * Licensed to the Apache Software Foundation (ASF) under one or more
   6  * contributor license agreements.  See the NOTICE file distributed with
   7  * this work for additional information regarding copyright ownership.
   8  * The ASF licenses this file to You under the Apache License, Version 2.0
   9  * (the "License"); you may not use this file except in compliance with
  10  * the License.  You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 package org.apache.qetest.dtm;
  21 
  22 import com.sun.org.apache.xml.internal.dtm.Axis;
  23 import com.sun.org.apache.xml.internal.dtm.DTM;
  24 import com.sun.org.apache.xml.internal.dtm.DTMAxisIterator;
  25 import java.io.FilePermission;
  26 import java.util.ArrayList;
  27 import java.util.List;
  28 import jaxp.library.JAXPBaseTest;
  29 import static org.apache.qetest.dtm.QeDtmConst.XML_DIR;
  30 import static org.apache.qetest.dtm.QeDtmConst.createDTM;
  31 import static org.testng.Assert.assertEquals;
  32 import org.testng.annotations.DataProvider;
  33 import org.testng.annotations.Test;
  34 
  35 /**
  36  * Basic API test for DTMAxisIterator. Iterator on different Axis type.
  37  * 
  38  */
  39 public class TestDTMIterDeep extends JAXPBaseTest {
  40     /**
  41      * Iterator on different Axis type.
  42      * 
  43      * @param axisType axis type.
  44      * @param expectedLastNode expected last node we will validate.
  45      * @param expectedTotal expected total node number we will validate. 
  46      */
  47     @Test(dataProvider = "axisProvider")
  48     public void test(int axisType, int expectedLastNode, int expectedTotal) {
  49         setPermissions(new RuntimePermission("accessClassInPackage.com.sun.org.apache.xml.internal.utils"),
  50                 new RuntimePermission("accessClassInPackage.com.sun.org.apache.xpath.internal.objects"),
  51                 new FilePermission(System.getProperty("test.src") + "/-", "read"));
  52         String deepFile = XML_DIR + "xtestdata/elem10kdeep.xml";
  53         List<String> lines = new ArrayList<>();
  54         lines.add("Axis is " + Axis.getNames(axisType).toUpperCase());
  55         DTM dtm = createDTM(0, deepFile, lines);
  56 
  57         // Get various nodes to use as context nodes.
  58         int DNode = dtm.getFirstChild(dtm.getDocument());
  59         assertEquals(dtm.getNodeName(dtm.getFirstChild(DNode)), "e1");
  60 
  61         // Get an iterator for Descendant:: axis.
  62         DTMAxisIterator iter = dtm.getAxisIterator(axisType);
  63         iter.setStartNode(DNode);
  64         int lastNode = 0;
  65         int numOfNodes = 0;
  66         for (int atNode = iter.next(); DTM.NULL != atNode; atNode = iter.next()) {
  67             lastNode = atNode;
  68             numOfNodes = numOfNodes + 1;  // Need to know that we Iterated the whole tree
  69         }
  70         assertEquals(lastNode, expectedLastNode);
  71         assertEquals(numOfNodes, expectedTotal);
  72     }
  73 
  74     /**
  75      * Axis types for testing.
  76      * @return an array contains axis type and its expected number.
  77      */
  78     @DataProvider
  79     public Object[][] axisProvider(){
  80         return new Object[][]{
  81             {Axis.DESCENDANT, 75538, 10000},
  82             {Axis.DESCENDANTORSELF, 75538, 10001},
  83             {Axis.ANCESTOR, 65536, 1},
  84             {Axis.ANCESTORORSELF, 65537, 2},
  85         };
  86     }
  87 }