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.DTMAxisTraverser;
  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 DTMAxisTraverser. Traverser on different Axis type.
  37  * 
  38  */
  39 public class TestDTMTravDeep extends JAXPBaseTest {
  40     /**
  41      * Traverser on different Axis type.
  42      * @param axisType axis type.
  43      * @param expectedLastNode expected last node we will validate.
  44      * @param expectedTotal expected total node number we will validate. 
  45      */
  46     @Test(dataProvider = "axisProvider")
  47     public void test(int axisType, int expectedLastNode, int expectedTotal) {
  48         setPermissions(new RuntimePermission("accessClassInPackage.com.sun.org.apache.xml.internal.utils"),
  49                 new RuntimePermission("accessClassInPackage.com.sun.org.apache.xpath.internal.objects"),
  50                 new FilePermission(System.getProperty("test.src") + "/-", "read"));
  51         String deepFile = XML_DIR + "xtestdata/elem10kdeep.xml";
  52         
  53         List<String> lines = new ArrayList<>();
  54         lines.add("Axis is  " + Axis.getNames(axisType).toUpperCase());
  55         DTM dtm = createDTM(0, deepFile, lines);
  56         // Get various nodes to use as context nodes.
  57         int dtmRoot = dtm.getDocument();        // #document                                                  
  58         int DNode = dtm.getFirstChild(dtmRoot); // <Doc>
  59         // returns Traversal time, last node, number of nodes traversed
  60         assertEquals(dtm.getNodeName(dtm.getFirstChild(DNode)), "e1");
  61         
  62         // Get a iterator for Descendant:: axis.
  63         DTMAxisTraverser trav = dtm.getAxisTraverser(Axis.DESCENDANT);
  64         int lastNode = 0;
  65         int numOfNodes = 0;
  66         for (int atNode = trav.first(axisType); DTM.NULL != atNode;
  67                 atNode = trav.next(DNode, axisType)) {
  68             lastNode = atNode;
  69             numOfNodes = numOfNodes + 1; 
  70         }
  71         assertEquals(lastNode, expectedLastNode);
  72         assertEquals(numOfNodes, expectedTotal);
  73     }
  74     
  75     /**
  76      * Axis types for testing.
  77      * @return an array contains axis type and its expected number.
  78      */
  79     @DataProvider
  80     public Object[][] axisProvider(){
  81         return new Object[][]{
  82             { Axis.DESCENDANT, 65536, 1},
  83             { Axis.DESCENDANTORSELF, 65536, 1},
  84             { Axis.ANCESTOR, 65536, 1},
  85             { Axis.ANCESTORORSELF, 65536, 1},
  86         };
  87     }
  88 }