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 static org.junit.Assert.assertEquals; 020 import static org.junit.Assert.assertFalse; 021 import static org.junit.Assert.assertTrue; 022 023 import java.util.Locale; 024 025 import org.apache.commons.configuration.tree.ConfigurationNode; 026 import org.apache.commons.configuration.tree.DefaultConfigurationNode; 027 import org.apache.commons.jxpath.ri.QName; 028 import org.apache.commons.jxpath.ri.model.NodeIterator; 029 import org.apache.commons.jxpath.ri.model.NodePointer; 030 import org.junit.Before; 031 import org.junit.Test; 032 033 /** 034 * Test class for ConfigurationNodePointer. 035 * 036 * @author <a 037 * href="http://commons.apache.org/configuration/team-list.html">Commons 038 * Configuration team</a> 039 * @version $Id: TestConfigurationNodePointer.java 1226104 2011-12-31 15:37:16Z oheger $ 040 */ 041 public class TestConfigurationNodePointer extends AbstractXPathTest 042 { 043 /** Stores the node pointer to be tested. */ 044 NodePointer pointer; 045 046 @Override 047 @Before 048 public void setUp() throws Exception 049 { 050 super.setUp(); 051 pointer = new ConfigurationNodePointer(root, Locale.getDefault()); 052 } 053 054 /** 055 * Tests comparing child node pointers for child nodes. 056 */ 057 @Test 058 public void testCompareChildNodePointersChildren() 059 { 060 NodePointer p1 = new ConfigurationNodePointer(pointer, root.getChild(1)); 061 NodePointer p2 = new ConfigurationNodePointer(pointer, root.getChild(3)); 062 assertEquals("Incorrect order", -1, pointer.compareChildNodePointers( 063 p1, p2)); 064 assertEquals("Incorrect symmetric order", 1, pointer 065 .compareChildNodePointers(p2, p1)); 066 } 067 068 /** 069 * Tests comparing child node pointers for attribute nodes. 070 */ 071 @Test 072 public void testCompareChildNodePointersAttributes() 073 { 074 root.addAttribute(new DefaultConfigurationNode("attr1", "test1")); 075 root.addAttribute(new DefaultConfigurationNode("attr2", "test2")); 076 NodePointer p1 = new ConfigurationNodePointer(pointer, root 077 .getAttribute(0)); 078 NodePointer p2 = new ConfigurationNodePointer(pointer, root 079 .getAttribute(1)); 080 assertEquals("Incorrect order", -1, pointer.compareChildNodePointers( 081 p1, p2)); 082 assertEquals("Incorrect symmetric order", 1, pointer 083 .compareChildNodePointers(p2, p1)); 084 } 085 086 /** 087 * tests comparing child node pointers for both child and attribute nodes. 088 */ 089 @Test 090 public void testCompareChildNodePointersChildAndAttribute() 091 { 092 root.addAttribute(new DefaultConfigurationNode("attr1", "test1")); 093 NodePointer p1 = new ConfigurationNodePointer(pointer, root.getChild(2)); 094 NodePointer p2 = new ConfigurationNodePointer(pointer, root 095 .getAttribute(0)); 096 assertEquals("Incorrect order for attributes", 1, pointer 097 .compareChildNodePointers(p1, p2)); 098 assertEquals("Incorrect symmetric order for attributes", -1, pointer 099 .compareChildNodePointers(p2, p1)); 100 } 101 102 /** 103 * Tests comparing child node pointers for child nodes that do not belong to 104 * the parent node. 105 */ 106 @Test 107 public void testCompareChildNodePointersInvalidChildren() 108 { 109 ConfigurationNode node = root.getChild(1); 110 NodePointer p1 = new ConfigurationNodePointer(pointer, node.getChild(1)); 111 NodePointer p2 = new ConfigurationNodePointer(pointer, node.getChild(3)); 112 assertEquals("Non child nodes could be sorted", 0, pointer 113 .compareChildNodePointers(p1, p2)); 114 assertEquals("Non child nodes could be sorted symmetrically", 0, 115 pointer.compareChildNodePointers(p2, p1)); 116 } 117 118 /** 119 * Tests the attribute flag. 120 */ 121 @Test 122 public void testIsAttribute() 123 { 124 ConfigurationNode node = new DefaultConfigurationNode("test", "testval"); 125 NodePointer p = new ConfigurationNodePointer(pointer, node); 126 assertFalse("Node is an attribute", p.isAttribute()); 127 node.setAttribute(true); 128 assertTrue("Node is no attribute", p.isAttribute()); 129 } 130 131 /** 132 * Tests if leaves in the tree are correctly detected. 133 */ 134 @Test 135 public void testIsLeave() 136 { 137 assertFalse("Root node is leaf", pointer.isLeaf()); 138 139 NodePointer p = pointer; 140 while (!p.isLeaf()) 141 { 142 ConfigurationNode node = (ConfigurationNode) p.getNode(); 143 assertTrue("Node has no children", node.getChildrenCount() > 0); 144 p = new ConfigurationNodePointer(p, node.getChild(0)); 145 } 146 assertTrue("Node has children", ((ConfigurationNode) p.getNode()) 147 .getChildrenCount() == 0); 148 } 149 150 /** 151 * Tests the iterators returned by the node pointer. 152 */ 153 @Test 154 public void testIterators() 155 { 156 checkIterators(pointer); 157 } 158 159 /** 160 * Recursive helper method for testing the returned iterators. 161 * 162 * @param p the node pointer to test 163 */ 164 private void checkIterators(NodePointer p) 165 { 166 ConfigurationNode node = (ConfigurationNode) p.getNode(); 167 NodeIterator it = p.childIterator(null, false, null); 168 assertEquals("Iterator count differs from children count", node 169 .getChildrenCount(), iteratorSize(it)); 170 171 for (int index = 1; it.setPosition(index); index++) 172 { 173 NodePointer pchild = it.getNodePointer(); 174 assertEquals("Wrong child", node.getChild(index - 1), pchild 175 .getNode()); 176 checkIterators(pchild); 177 } 178 179 it = p.attributeIterator(new QName(null, "*")); 180 assertEquals("Iterator count differs from attribute count", node 181 .getAttributeCount(), iteratorSize(it)); 182 for (int index = 1; it.setPosition(index); index++) 183 { 184 NodePointer pattr = it.getNodePointer(); 185 assertTrue("Node pointer is no attribute", pattr.isAttribute()); 186 assertEquals("Wrong attribute", node.getAttribute(index - 1), pattr 187 .getNode()); 188 checkIterators(pattr); 189 } 190 } 191 }