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:
026: import junit.framework.TestCase;
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 Oliver Heger
034: * @version $Id: AbstractXPathTest.java 505938 2007-02-11 12:41:13Z brett $
035: */
036: public abstract class AbstractXPathTest extends TestCase {
037: /** Constant for the name of the counter attribute. */
038: protected static final String ATTR_NAME = "counter";
039:
040: /** Constant for the name of the first child. */
041: protected static final String CHILD_NAME1 = "subNode";
042:
043: /** Constant for the name of the second child. */
044: protected static final String CHILD_NAME2 = "childNode";
045:
046: /** Constant for the number of sub nodes. */
047: protected static final int CHILD_COUNT = 5;
048:
049: /** Constant for the number of levels in the hierarchy. */
050: protected static final int LEVEL_COUNT = 3;
051:
052: /** Stores the root node of the hierarchy. */
053: protected ConfigurationNode root;
054:
055: protected void setUp() throws Exception {
056: super .setUp();
057: root = constructHierarchy(LEVEL_COUNT);
058: }
059:
060: /**
061: * Clears the test environment.
062: */
063: protected void tearDown() throws Exception {
064: root = null;
065: }
066:
067: /**
068: * Builds up a hierarchy of nodes. Each node has <code>CHILD_COUNT</code>
069: * child nodes having the names <code>CHILD_NAME1</code> or
070: * <code>CHILD_NAME2</code>. Their values are named like their parent
071: * node with an additional index. Each node has an attribute with a counter
072: * value.
073: *
074: * @param levels the number of levels in the hierarchy
075: * @return the root node of the hierarchy
076: */
077: protected ConfigurationNode constructHierarchy(int levels) {
078: ConfigurationNode result = new DefaultConfigurationNode();
079: createLevel(result, levels);
080: return result;
081: }
082:
083: /**
084: * Determines the number of elements contained in the given iterator.
085: *
086: * @param iterator the iterator
087: * @return the number of elements in this iteration
088: */
089: protected int iteratorSize(NodeIterator iterator) {
090: int cnt = 0;
091: boolean ok;
092:
093: do {
094: ok = iterator.setPosition(cnt + 1);
095: if (ok) {
096: cnt++;
097: }
098: } while (ok);
099:
100: return cnt;
101: }
102:
103: /**
104: * Returns a list with all configuration nodes contained in the specified
105: * iteration. It is assumed that the iteration contains only elements of
106: * this type.
107: *
108: * @param iterator the iterator
109: * @return a list with configuration nodes obtained from the iterator
110: */
111: protected List iterationElements(NodeIterator iterator) {
112: List result = new ArrayList();
113: for (int pos = 1; iterator.setPosition(pos); pos++) {
114: result.add(iterator.getNodePointer().getNode());
115: }
116: return result;
117: }
118:
119: /**
120: * Recursive helper method for creating a level of the node hierarchy.
121: *
122: * @param parent the parent node
123: * @param level the level counter
124: */
125: private void createLevel(ConfigurationNode parent, int level) {
126: if (level >= 0) {
127: String prefix = (parent.getValue() == null) ? "" : parent
128: .getValue()
129: + ".";
130: for (int i = 1; i <= CHILD_COUNT; i++) {
131: ConfigurationNode child = new DefaultConfigurationNode(
132: (i % 2 == 0) ? CHILD_NAME1 : CHILD_NAME2,
133: prefix + i);
134: parent.addChild(child);
135: child.addAttribute(new DefaultConfigurationNode(
136: ATTR_NAME, String.valueOf(i)));
137:
138: createLevel(child, level - 1);
139: }
140: }
141: }
142: }
|