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 import org.junit.After; 26 import org.junit.Before; 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 <a 34 * href="http://commons.apache.org/configuration/team-list.html">Commons 35 * Configuration team</a> 36 * @version $Id: AbstractXPathTest.java 1226104 2011-12-31 15:37:16Z oheger $ 37 */ 38 public abstract class AbstractXPathTest 39 { 40 /** Constant for the name of the counter attribute. */ 41 protected static final String ATTR_NAME = "counter"; 42 43 /** Constant for the name of the first child. */ 44 protected static final String CHILD_NAME1 = "subNode"; 45 46 /** Constant for the name of the second child. */ 47 protected static final String CHILD_NAME2 = "childNode"; 48 49 /** Constant for the number of sub nodes. */ 50 protected static final int CHILD_COUNT = 5; 51 52 /** Constant for the number of levels in the hierarchy. */ 53 protected static final int LEVEL_COUNT = 3; 54 55 /** Stores the root node of the hierarchy. */ 56 protected ConfigurationNode root; 57 58 @Before 59 public void setUp() throws Exception 60 { 61 root = constructHierarchy(LEVEL_COUNT); 62 } 63 64 /** 65 * Clears the test environment. 66 */ 67 @After 68 public void tearDown() throws Exception 69 { 70 root = null; 71 } 72 73 /** 74 * Builds up a hierarchy of nodes. Each node has {@code CHILD_COUNT} 75 * child nodes having the names {@code CHILD_NAME1} or 76 * {@code CHILD_NAME2}. Their values are named like their parent 77 * node with an additional index. Each node has an attribute with a counter 78 * value. 79 * 80 * @param levels the number of levels in the hierarchy 81 * @return the root node of the hierarchy 82 */ 83 protected ConfigurationNode constructHierarchy(int levels) 84 { 85 ConfigurationNode result = new DefaultConfigurationNode(); 86 createLevel(result, levels); 87 return result; 88 } 89 90 /** 91 * Determines the number of elements contained in the given iterator. 92 * 93 * @param iterator the iterator 94 * @return the number of elements in this iteration 95 */ 96 protected int iteratorSize(NodeIterator iterator) 97 { 98 int cnt = 0; 99 boolean ok; 100 101 do 102 { 103 ok = iterator.setPosition(cnt + 1); 104 if (ok) 105 { 106 cnt++; 107 } 108 } while (ok); 109 110 return cnt; 111 } 112 113 /** 114 * Returns a list with all configuration nodes contained in the specified 115 * iteration. It is assumed that the iteration contains only elements of 116 * this type. 117 * 118 * @param iterator the iterator 119 * @return a list with configuration nodes obtained from the iterator 120 */ 121 protected List<ConfigurationNode> iterationElements(NodeIterator iterator) 122 { 123 List<ConfigurationNode> result = new ArrayList<ConfigurationNode>(); 124 for (int pos = 1; iterator.setPosition(pos); pos++) 125 { 126 result.add((ConfigurationNode) iterator.getNodePointer().getNode()); 127 } 128 return result; 129 } 130 131 /** 132 * Recursive helper method for creating a level of the node hierarchy. 133 * 134 * @param parent the parent node 135 * @param level the level counter 136 */ 137 private void createLevel(ConfigurationNode parent, int level) 138 { 139 if (level >= 0) 140 { 141 String prefix = (parent.getValue() == null) ? "" : parent 142 .getValue() 143 + "."; 144 for (int i = 1; i <= CHILD_COUNT; i++) 145 { 146 ConfigurationNode child = new DefaultConfigurationNode( 147 (i % 2 == 0) ? CHILD_NAME1 : CHILD_NAME2, prefix + i); 148 parent.addChild(child); 149 child.addAttribute(new DefaultConfigurationNode(ATTR_NAME, 150 String.valueOf(i))); 151 152 createLevel(child, level - 1); 153 } 154 } 155 } 156 }