1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.configuration.tree.xpath;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.apache.commons.configuration.tree.ConfigurationNode;
23  import org.apache.commons.configuration.tree.DefaultConfigurationNode;
24  import org.apache.commons.jxpath.ri.model.NodeIterator;
25  
26  import junit.framework.TestCase;
27  
28  /***
29   * A base class for testing classes of the XPath package. This base class
30   * creates a hierarchy of nodes in its setUp() method that can be used for test
31   * cases.
32   *
33   * @author Oliver Heger
34   * @version $Id: XPathTest.java 439648 2006-09-02 20:42:10Z oheger $
35   */
36  public class XPathTest extends TestCase
37  {
38      /*** Constant for the name of the counter attribute. */
39      protected static final String ATTR_NAME = "counter";
40  
41      /*** Constant for the name of the first child. */
42      protected static final String CHILD_NAME1 = "subNode";
43  
44      /*** Constant for the name of the second child. */
45      protected static final String CHILD_NAME2 = "childNode";
46  
47      /*** Constant for the number of sub nodes. */
48      protected static final int CHILD_COUNT = 5;
49  
50      /*** Constant for the number of levels in the hierarchy. */
51      protected static final int LEVEL_COUNT = 4;
52  
53      /*** Stores the root node of the hierarchy. */
54      protected ConfigurationNode root;
55  
56      protected void setUp() throws Exception
57      {
58          super.setUp();
59          root = constructHierarchy(LEVEL_COUNT);
60      }
61  
62      /***
63       * Builds up a hierarchy of nodes. Each node has <code>CHILD_COUNT</code>
64       * child nodes having the names <code>CHILD_NAME1</code> or
65       * <code>CHILD_NAME2</code>. Their values are named like their parent
66       * node with an additional index. Each node has an attribute with a counter
67       * value.
68       *
69       * @param levels the number of levels in the hierarchy
70       * @return the root node of the hierarchy
71       */
72      protected ConfigurationNode constructHierarchy(int levels)
73      {
74          ConfigurationNode result = new DefaultConfigurationNode();
75          createLevel(result, levels);
76          return result;
77      }
78  
79      /***
80       * Determines the number of elements contained in the given iterator.
81       *
82       * @param iterator the iterator
83       * @return the number of elements in this iteration
84       */
85      protected int iteratorSize(NodeIterator iterator)
86      {
87          int cnt = 0;
88          boolean ok;
89  
90          do
91          {
92              ok = iterator.setPosition(cnt + 1);
93              if (ok)
94              {
95                  cnt++;
96              }
97          } while (ok);
98  
99          return cnt;
100     }
101 
102     /***
103      * Returns a list with all configuration nodes contained in the specified
104      * iteration. It is assumed that the iteration contains only elements of
105      * this type.
106      *
107      * @param iterator the iterator
108      * @return a list with configuration nodes obtained from the iterator
109      */
110     protected List iterationElements(NodeIterator iterator)
111     {
112         List result = new ArrayList();
113         for (int pos = 1; iterator.setPosition(pos); pos++)
114         {
115             result.add(iterator.getNodePointer().getNode());
116         }
117         return result;
118     }
119 
120     /***
121      * Recursive helper method for creating a level of the node hierarchy.
122      *
123      * @param parent the parent node
124      * @param level the level counter
125      */
126     private void createLevel(ConfigurationNode parent, int level)
127     {
128         if (level >= 0)
129         {
130             String prefix = (parent.getValue() == null) ? "" : parent
131                     .getValue()
132                     + ".";
133             for (int i = 1; i <= CHILD_COUNT; i++)
134             {
135                 ConfigurationNode child = new DefaultConfigurationNode(
136                         (i % 2 == 0) ? CHILD_NAME1 : CHILD_NAME2, prefix + i);
137                 parent.addChild(child);
138                 child.addAttribute(new DefaultConfigurationNode(ATTR_NAME,
139                         String.valueOf(i)));
140 
141                 createLevel(child, level - 1);
142             }
143         }
144     }
145 }