001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.configuration.tree.xpath; 018 019 import java.util.ArrayList; 020 import java.util.List; 021 022 import org.apache.commons.configuration.tree.ConfigurationNode; 023 import org.apache.commons.configuration.tree.DefaultConfigurationNode; 024 import org.apache.commons.jxpath.ri.model.NodeIterator; 025 import org.junit.After; 026 import org.junit.Before; 027 028 /** 029 * A base class for testing classes of the XPath package. This base class 030 * creates a hierarchy of nodes in its setUp() method that can be used for test 031 * cases. 032 * 033 * @author <a 034 * href="http://commons.apache.org/configuration/team-list.html">Commons 035 * Configuration team</a> 036 * @version $Id: AbstractXPathTest.java 1226104 2011-12-31 15:37:16Z oheger $ 037 */ 038 public abstract class AbstractXPathTest 039 { 040 /** Constant for the name of the counter attribute. */ 041 protected static final String ATTR_NAME = "counter"; 042 043 /** Constant for the name of the first child. */ 044 protected static final String CHILD_NAME1 = "subNode"; 045 046 /** Constant for the name of the second child. */ 047 protected static final String CHILD_NAME2 = "childNode"; 048 049 /** Constant for the number of sub nodes. */ 050 protected static final int CHILD_COUNT = 5; 051 052 /** Constant for the number of levels in the hierarchy. */ 053 protected static final int LEVEL_COUNT = 3; 054 055 /** Stores the root node of the hierarchy. */ 056 protected ConfigurationNode root; 057 058 @Before 059 public void setUp() throws Exception 060 { 061 root = constructHierarchy(LEVEL_COUNT); 062 } 063 064 /** 065 * Clears the test environment. 066 */ 067 @After 068 public void tearDown() throws Exception 069 { 070 root = null; 071 } 072 073 /** 074 * Builds up a hierarchy of nodes. Each node has {@code CHILD_COUNT} 075 * child nodes having the names {@code CHILD_NAME1} or 076 * {@code CHILD_NAME2}. Their values are named like their parent 077 * node with an additional index. Each node has an attribute with a counter 078 * value. 079 * 080 * @param levels the number of levels in the hierarchy 081 * @return the root node of the hierarchy 082 */ 083 protected ConfigurationNode constructHierarchy(int levels) 084 { 085 ConfigurationNode result = new DefaultConfigurationNode(); 086 createLevel(result, levels); 087 return result; 088 } 089 090 /** 091 * Determines the number of elements contained in the given iterator. 092 * 093 * @param iterator the iterator 094 * @return the number of elements in this iteration 095 */ 096 protected int iteratorSize(NodeIterator iterator) 097 { 098 int cnt = 0; 099 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 }