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.event; 018 019 import static org.junit.Assert.assertEquals; 020 import static org.junit.Assert.assertTrue; 021 022 import java.util.ArrayList; 023 import java.util.Collection; 024 025 import org.apache.commons.configuration.AbstractConfiguration; 026 import org.apache.commons.configuration.HierarchicalConfiguration; 027 import org.apache.commons.configuration.SubnodeConfiguration; 028 import org.apache.commons.configuration.tree.ConfigurationNode; 029 import org.apache.commons.configuration.tree.DefaultConfigurationNode; 030 import org.junit.Test; 031 032 /** 033 * Test class for the events generated by hierarchical configurations. 034 * 035 * @version $Id: TestHierarchicalConfigurationEvents.java 1225648 2011-12-29 20:55:07Z oheger $ 036 */ 037 public class TestHierarchicalConfigurationEvents extends 038 AbstractTestConfigurationEvents 039 { 040 @Override 041 protected AbstractConfiguration createConfiguration() 042 { 043 return new HierarchicalConfiguration(); 044 } 045 046 /** 047 * Tests events generated by the clearTree() method. 048 */ 049 @Test 050 public void testClearTreeEvent() 051 { 052 HierarchicalConfiguration hc = (HierarchicalConfiguration) config; 053 String key = EXIST_PROPERTY.substring(0, EXIST_PROPERTY.indexOf('.')); 054 Collection<ConfigurationNode> nodes = hc.getExpressionEngine() 055 .query(hc.getRootNode(), key); 056 hc.clearTree(key); 057 l.checkEvent(HierarchicalConfiguration.EVENT_CLEAR_TREE, key, null, 058 true); 059 l.checkEvent(HierarchicalConfiguration.EVENT_CLEAR_TREE, key, nodes, 060 false); 061 l.done(); 062 } 063 064 /** 065 * Tests events generated by the addNodes() method. 066 */ 067 @Test 068 public void testAddNodesEvent() 069 { 070 HierarchicalConfiguration hc = (HierarchicalConfiguration) config; 071 Collection<ConfigurationNode> nodes = new ArrayList<ConfigurationNode>(1); 072 nodes.add(new DefaultConfigurationNode("a_key", TEST_PROPVALUE)); 073 hc.addNodes(TEST_PROPNAME, nodes); 074 l.checkEvent(HierarchicalConfiguration.EVENT_ADD_NODES, TEST_PROPNAME, 075 nodes, true); 076 l.checkEvent(HierarchicalConfiguration.EVENT_ADD_NODES, TEST_PROPNAME, 077 nodes, false); 078 l.done(); 079 } 080 081 /** 082 * Tests events generated by addNodes() when the list of nodes is empty. In 083 * this case no events should be generated. 084 */ 085 @Test 086 public void testAddNodesEmptyEvent() 087 { 088 ((HierarchicalConfiguration) config).addNodes(TEST_PROPNAME, 089 new ArrayList<ConfigurationNode>()); 090 l.done(); 091 } 092 093 /** 094 * Tests whether manipulations of a subnode configuration trigger correct 095 * events. 096 */ 097 @Test 098 public void testSubnodeChangedEvent() 099 { 100 SubnodeConfiguration sub = ((HierarchicalConfiguration) config) 101 .configurationAt(EXIST_PROPERTY); 102 sub.addProperty("newProp", "newValue"); 103 checkSubnodeEvent(l 104 .nextEvent(HierarchicalConfiguration.EVENT_SUBNODE_CHANGED), 105 true); 106 checkSubnodeEvent(l 107 .nextEvent(HierarchicalConfiguration.EVENT_SUBNODE_CHANGED), 108 false); 109 l.done(); 110 } 111 112 /** 113 * Tests whether a received event contains a correct subnode event. 114 * 115 * @param event the event object 116 * @param before the expected before flag 117 */ 118 private void checkSubnodeEvent(ConfigurationEvent event, boolean before) 119 { 120 assertEquals("Wrong before flag of nesting event", before, event 121 .isBeforeUpdate()); 122 assertTrue("No subnode event found in value", 123 event.getPropertyValue() instanceof ConfigurationEvent); 124 ConfigurationEvent evSub = (ConfigurationEvent) event 125 .getPropertyValue(); 126 assertEquals("Wrong event type", 127 HierarchicalConfiguration.EVENT_ADD_PROPERTY, evSub.getType()); 128 assertEquals("Wrong property name", "newProp", evSub.getPropertyName()); 129 assertEquals("Wrong property value", "newValue", evSub 130 .getPropertyValue()); 131 assertEquals("Wrong before flag", before, evSub.isBeforeUpdate()); 132 } 133 }