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 java.io.IOException;
020: import java.net.URL;
021:
022: import org.apache.commons.configuration.AbstractFileConfiguration;
023: import org.apache.commons.configuration.ConfigurationException;
024: import org.apache.commons.configuration.FileConfiguration;
025: import org.apache.commons.configuration.reloading.ReloadingStrategy;
026:
027: /**
028: * A base test class that can be used for testing file-based configurations.
029: * This class tests reload events, too.
030: *
031: * @version $Id: AbstractTestFileConfigurationEvents.java 439648 2006-09-02 20:42:10Z oheger $
032: */
033: public abstract class AbstractTestFileConfigurationEvents extends
034: AbstractTestConfigurationEvents {
035: /**
036: * Initializes the file configuration for the tests.
037: *
038: * @throws ConfigurationException if an error occurs
039: */
040: protected void setUpFileConfiguration()
041: throws ConfigurationException, IOException {
042: FileConfiguration fc = (FileConfiguration) config;
043: fc.setReloadingStrategy(new AlwaysReloadingStrategy());
044: fc.setURL(getSourceURL());
045:
046: // deregister event listener before load because load will cause
047: // other events being generated
048: config.removeConfigurationListener(l);
049: fc.load();
050: config.addConfigurationListener(l);
051: }
052:
053: /**
054: * Returns the URL of the file to be loaded. Must be implemented in concrete
055: * test classes.
056: *
057: * @return the URL of the file-based configuration
058: * @throws IOException if an error occurs
059: */
060: protected abstract URL getSourceURL() throws IOException;
061:
062: /**
063: * Tests events generated by the reload() method.
064: */
065: public void testReloadEvent() throws ConfigurationException,
066: IOException {
067: setUpFileConfiguration();
068: config.isEmpty(); // This should cause a reload
069: l.checkEvent(AbstractFileConfiguration.EVENT_RELOAD, null,
070: getSourceURL(), true);
071: l.checkEvent(AbstractFileConfiguration.EVENT_RELOAD, null,
072: getSourceURL(), false);
073: l.done();
074: }
075:
076: /**
077: * Tests events generated by the reload() method when detail events are
078: * enabled.
079: */
080: public void testReloadEventWithDetails()
081: throws ConfigurationException, IOException {
082: setUpFileConfiguration();
083: config.setDetailEvents(true);
084: config.isEmpty(); // This should cause a reload
085: l.checkEventCount(2);
086: l.checkEvent(AbstractFileConfiguration.EVENT_RELOAD, null,
087: getSourceURL(), true);
088: l.skipToLast(AbstractFileConfiguration.EVENT_RELOAD);
089: l.checkEvent(AbstractFileConfiguration.EVENT_RELOAD, null,
090: getSourceURL(), false);
091: l.done();
092: }
093:
094: /**
095: * Tests accessing a property during a reload event to ensure that no
096: * infinite loops are possible.
097: */
098: public void testAccessPropertiesOnReload()
099: throws ConfigurationException, IOException {
100: setUpFileConfiguration();
101: config.addConfigurationListener(new ConfigurationListener() {
102: public void configurationChanged(ConfigurationEvent event) {
103: config.getString("test");
104: }
105: });
106: config.isEmpty();
107: l.checkEvent(AbstractFileConfiguration.EVENT_RELOAD, null,
108: getSourceURL(), true);
109: l.checkEvent(AbstractFileConfiguration.EVENT_RELOAD, null,
110: getSourceURL(), false);
111: l.done();
112: }
113:
114: /**
115: * A dummy implementation of the ReloadingStrategy interface. This
116: * implementation will always indicate that a reload should be performed. So
117: * it can be used for testing reloading events.
118: */
119: static class AlwaysReloadingStrategy implements ReloadingStrategy {
120: public void setConfiguration(FileConfiguration configuration) {
121: }
122:
123: public void init() {
124: }
125:
126: public boolean reloadingRequired() {
127: return true;
128: }
129:
130: public void reloadingPerformed() {
131: }
132: }
133: }
|