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: AbstractXPathTest.java 505938 2007-02-11 12:41:13Z brett $
35   */
36  public abstract class AbstractXPathTest 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 = 3;
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       * Clears the test environment.
64       */
65      protected void tearDown() throws Exception
66      {
67          root = null;
68      }
69  
70      /***
71       * Builds up a hierarchy of nodes. Each node has <code>CHILD_COUNT</code>
72       * child nodes having the names <code>CHILD_NAME1</code> or
73       * <code>CHILD_NAME2</code>. Their values are named like their parent
74       * node with an additional index. Each node has an attribute with a counter
75       * value.
76       *
77       * @param levels the number of levels in the hierarchy
78       * @return the root node of the hierarchy
79       */
80      protected ConfigurationNode constructHierarchy(int levels)
81      {
82          ConfigurationNode result = new DefaultConfigurationNode();
83          createLevel(result, levels);
84          return result;
85      }
86  
87      /***
88       * Determines the number of elements contained in the given iterator.
89       *
90       * @param iterator the iterator
91       * @return the number of elements in this iteration
92       */
93      protected int iteratorSize(NodeIterator iterator)
94      {
95          int cnt = 0;
96          boolean ok;
97  
98          do
99          {
100             ok = iterator.setPosition(cnt + 1);
101             if (ok)
102             {
103                 cnt++;
104             }
105         } while (ok);
106 
107         return cnt;
108     }
109 
110     /***
111      * Returns a list with all configuration nodes contained in the specified
112      * iteration. It is assumed that the iteration contains only elements of
113      * this type.
114      *
115      * @param iterator the iterator
116      * @return a list with configuration nodes obtained from the iterator
117      */
118     protected List iterationElements(NodeIterator iterator)
119     {
120         List result = new ArrayList();
121         for (int pos = 1; iterator.setPosition(pos); pos++)
122         {
123             result.add(iterator.getNodePointer().getNode());
124         }
125         return result;
126     }
127 
128     /***
129      * Recursive helper method for creating a level of the node hierarchy.
130      *
131      * @param parent the parent node
132      * @param level the level counter
133      */
134     private void createLevel(ConfigurationNode parent, int level)
135     {
136         if (level >= 0)
137         {
138             String prefix = (parent.getValue() == null) ? "" : parent
139                     .getValue()
140                     + ".";
141             for (int i = 1; i <= CHILD_COUNT; i++)
142             {
143                 ConfigurationNode child = new DefaultConfigurationNode(
144                         (i % 2 == 0) ? CHILD_NAME1 : CHILD_NAME2, prefix + i);
145                 parent.addChild(child);
146                 child.addAttribute(new DefaultConfigurationNode(ATTR_NAME,
147                         String.valueOf(i)));
148 
149                 createLevel(child, level - 1);
150             }
151         }
152     }
153 }